1 //===- llvm/unittest/Support/ReplaceFileTest.cpp - unit tests -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/Support/Process.h"
16 #include "gtest/gtest.h"
19 using namespace llvm::sys;
21 #define ASSERT_NO_ERROR(x) \
23 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
24 errs() << #x ": did not return errc::success.\n" \
25 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
26 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
31 std::error_code CreateFileWithContent(const SmallString<128> &FilePath,
32 const StringRef &content) {
34 if (std::error_code ec = fs::openFileForWrite(FilePath, FD, fs::F_None))
37 const bool ShouldClose = true;
38 raw_fd_ostream OS(FD, ShouldClose);
41 return std::error_code();
47 ScopedFD(const ScopedFD &) = delete;
48 ScopedFD &operator=(const ScopedFD &) = delete;
51 explicit ScopedFD(int Descriptor) : FD(Descriptor) {}
52 ~ScopedFD() { Process::SafelyCloseFileDescriptor(FD); }
55 TEST(rename, FileOpenedForReadingCanBeReplaced) {
56 // Create unique temporary directory for this test.
57 SmallString<128> TestDirectory;
58 ASSERT_NO_ERROR(fs::createUniqueDirectory(
59 "FileOpenedForReadingCanBeReplaced-test", TestDirectory));
61 // Add a couple of files to the test directory.
62 SmallString<128> SourceFileName(TestDirectory);
63 path::append(SourceFileName, "source");
65 SmallString<128> TargetFileName(TestDirectory);
66 path::append(TargetFileName, "target");
68 ASSERT_NO_ERROR(CreateFileWithContent(SourceFileName, "!!source!!"));
69 ASSERT_NO_ERROR(CreateFileWithContent(TargetFileName, "!!target!!"));
72 // Open the target file for reading.
74 ASSERT_NO_ERROR(fs::openFileForRead(TargetFileName, ReadFD));
75 ScopedFD EventuallyCloseIt(ReadFD);
77 // Confirm we can replace the file while it is open.
78 EXPECT_TRUE(!fs::rename(SourceFileName, TargetFileName));
80 // We should still be able to read the old data through the existing
82 auto Buffer = MemoryBuffer::getOpenFile(ReadFD, TargetFileName, -1);
83 ASSERT_TRUE(static_cast<bool>(Buffer));
84 EXPECT_EQ(Buffer.get()->getBuffer(), "!!target!!");
86 // The source file should no longer exist
87 EXPECT_FALSE(fs::exists(SourceFileName));
91 // If we obtain a new descriptor for the target file, we should find that it
92 // contains the content that was in the source file.
94 ASSERT_NO_ERROR(fs::openFileForRead(TargetFileName, ReadFD));
95 ScopedFD EventuallyCloseIt(ReadFD);
96 auto Buffer = MemoryBuffer::getOpenFile(ReadFD, TargetFileName, -1);
97 ASSERT_TRUE(static_cast<bool>(Buffer));
99 EXPECT_EQ(Buffer.get()->getBuffer(), "!!source!!");
102 // Rename the target file back to the source file name to confirm that rename
103 // still works if the destination does not already exist.
104 EXPECT_TRUE(!fs::rename(TargetFileName, SourceFileName));
105 EXPECT_FALSE(fs::exists(TargetFileName));
106 ASSERT_TRUE(fs::exists(SourceFileName));
109 ASSERT_NO_ERROR(fs::remove(SourceFileName));
110 ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
113 } // anonymous namespace