1 //===- llvm/unittest/Support/FileUtilitiesTest.cpp - unit tests -----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/FileUtilities.h"
10 #include "llvm/Support/Errc.h"
11 #include "llvm/Support/ErrorHandling.h"
12 #include "llvm/Support/FileSystem.h"
13 #include "llvm/Support/MemoryBuffer.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Testing/Support/SupportHelpers.h"
16 #include "gtest/gtest.h"
20 using namespace llvm::sys
;
22 using llvm::unittest::TempDir
;
24 #define ASSERT_NO_ERROR(x) \
25 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
26 SmallString<128> MessageStorage; \
27 raw_svector_ostream Message(MessageStorage); \
28 Message << #x ": did not return errc::success.\n" \
29 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
30 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
31 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
36 TEST(writeFileAtomicallyTest
, Test
) {
37 // Create unique temporary directory for these tests
38 TempDir
RootTestDirectory("writeFileAtomicallyTest", /*Unique*/ true);
40 SmallString
<128> FinalTestfilePath(RootTestDirectory
.path());
41 sys::path::append(FinalTestfilePath
, "foo.txt");
42 const std::string TempUniqTestFileModel
=
43 std::string(FinalTestfilePath
) + "-%%%%%%%%";
44 const std::string TestfileContent
= "fooFOOfoo";
46 llvm::Error Err
= llvm::writeFileAtomically(TempUniqTestFileModel
, FinalTestfilePath
, TestfileContent
);
47 ASSERT_FALSE(static_cast<bool>(Err
));
49 std::ifstream
FinalFileStream(std::string(FinalTestfilePath
.str()));
50 std::string FinalFileContent
;
51 FinalFileStream
>> FinalFileContent
;
52 ASSERT_EQ(FinalFileContent
, TestfileContent
);
54 } // anonymous namespace