100e04b0aSJan Korous //===- llvm/unittest/Support/FileUtilitiesTest.cpp - unit tests -----------===// 200e04b0aSJan Korous // 300e04b0aSJan Korous // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 400e04b0aSJan Korous // See https://llvm.org/LICENSE.txt for license information. 500e04b0aSJan Korous // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 600e04b0aSJan Korous // 700e04b0aSJan Korous //===----------------------------------------------------------------------===// 800e04b0aSJan Korous 900e04b0aSJan Korous #include "llvm/Support/FileUtilities.h" 1000e04b0aSJan Korous #include "llvm/Support/Errc.h" 1100e04b0aSJan Korous #include "llvm/Support/ErrorHandling.h" 1200e04b0aSJan Korous #include "llvm/Support/FileSystem.h" 1300e04b0aSJan Korous #include "llvm/Support/MemoryBuffer.h" 1400e04b0aSJan Korous #include "llvm/Support/Path.h" 15*fad75598SSergej Jaskiewicz #include "llvm/Testing/Support/SupportHelpers.h" 1600e04b0aSJan Korous #include "gtest/gtest.h" 1700e04b0aSJan Korous #include <fstream> 1800e04b0aSJan Korous 1900e04b0aSJan Korous using namespace llvm; 2000e04b0aSJan Korous using namespace llvm::sys; 2100e04b0aSJan Korous 22*fad75598SSergej Jaskiewicz using llvm::unittest::TempDir; 23*fad75598SSergej Jaskiewicz 2400e04b0aSJan Korous #define ASSERT_NO_ERROR(x) \ 2500e04b0aSJan Korous if (std::error_code ASSERT_NO_ERROR_ec = x) { \ 2600e04b0aSJan Korous SmallString<128> MessageStorage; \ 2700e04b0aSJan Korous raw_svector_ostream Message(MessageStorage); \ 2800e04b0aSJan Korous Message << #x ": did not return errc::success.\n" \ 2900e04b0aSJan Korous << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ 3000e04b0aSJan Korous << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ 3100e04b0aSJan Korous GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ 3200e04b0aSJan Korous } else { \ 3300e04b0aSJan Korous } 3400e04b0aSJan Korous 3500e04b0aSJan Korous namespace { TEST(writeFileAtomicallyTest,Test)3600e04b0aSJan KorousTEST(writeFileAtomicallyTest, Test) { 3700e04b0aSJan Korous // Create unique temporary directory for these tests 38*fad75598SSergej Jaskiewicz TempDir RootTestDirectory("writeFileAtomicallyTest", /*Unique*/ true); 3900e04b0aSJan Korous 40*fad75598SSergej Jaskiewicz SmallString<128> FinalTestfilePath(RootTestDirectory.path()); 4100e04b0aSJan Korous sys::path::append(FinalTestfilePath, "foo.txt"); 42b2924d99SJonas Devlieghere const std::string TempUniqTestFileModel = 43b2924d99SJonas Devlieghere std::string(FinalTestfilePath) + "-%%%%%%%%"; 4400e04b0aSJan Korous const std::string TestfileContent = "fooFOOfoo"; 4500e04b0aSJan Korous 4600e04b0aSJan Korous llvm::Error Err = llvm::writeFileAtomically(TempUniqTestFileModel, FinalTestfilePath, TestfileContent); 4700e04b0aSJan Korous ASSERT_FALSE(static_cast<bool>(Err)); 4800e04b0aSJan Korous 49adcd0268SBenjamin Kramer std::ifstream FinalFileStream(std::string(FinalTestfilePath.str())); 5000e04b0aSJan Korous std::string FinalFileContent; 5100e04b0aSJan Korous FinalFileStream >> FinalFileContent; 5200e04b0aSJan Korous ASSERT_EQ(FinalFileContent, TestfileContent); 5300e04b0aSJan Korous } 5400e04b0aSJan Korous } // anonymous namespace 55