BuildBot fix, compiler complains about array decay to pointer
[llvm-core.git] / unittests / Support / LockFileManagerTest.cpp
blob1775d05e44db2356b2fc540c35a226412f293167
1 //===- unittests/LockFileManagerTest.cpp - LockFileManager tests ----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/LockFileManager.h"
11 #include "llvm/Support/FileSystem.h"
12 #include "llvm/Support/Path.h"
13 #include "gtest/gtest.h"
14 #include <memory>
16 using namespace llvm;
18 namespace {
20 TEST(LockFileManagerTest, Basic) {
21 SmallString<64> TmpDir;
22 std::error_code EC;
23 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
24 ASSERT_FALSE(EC);
26 SmallString<64> LockedFile(TmpDir);
27 sys::path::append(LockedFile, "file.lock");
30 // The lock file should not exist, so we should successfully acquire it.
31 LockFileManager Locked1(LockedFile);
32 EXPECT_EQ(LockFileManager::LFS_Owned, Locked1.getState());
34 // Attempting to reacquire the lock should fail. Waiting on it would cause
35 // deadlock, so don't try that.
36 LockFileManager Locked2(LockedFile);
37 EXPECT_NE(LockFileManager::LFS_Owned, Locked2.getState());
40 // Now that the lock is out of scope, the file should be gone.
41 EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
43 EC = sys::fs::remove(StringRef(TmpDir));
44 ASSERT_FALSE(EC);
47 TEST(LockFileManagerTest, LinkLockExists) {
48 SmallString<64> TmpDir;
49 std::error_code EC;
50 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
51 ASSERT_FALSE(EC);
53 SmallString<64> LockedFile(TmpDir);
54 sys::path::append(LockedFile, "file");
56 SmallString<64> FileLocK(TmpDir);
57 sys::path::append(FileLocK, "file.lock");
59 SmallString<64> TmpFileLock(TmpDir);
60 sys::path::append(TmpFileLock, "file.lock-000");
62 int FD;
63 EC = sys::fs::openFileForWrite(StringRef(TmpFileLock), FD);
64 ASSERT_FALSE(EC);
66 int Ret = close(FD);
67 ASSERT_EQ(Ret, 0);
69 EC = sys::fs::create_link(TmpFileLock.str(), FileLocK.str());
70 ASSERT_FALSE(EC);
72 EC = sys::fs::remove(StringRef(TmpFileLock));
73 ASSERT_FALSE(EC);
76 // The lock file doesn't point to a real file, so we should successfully
77 // acquire it.
78 LockFileManager Locked(LockedFile);
79 EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
82 // Now that the lock is out of scope, the file should be gone.
83 EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
85 EC = sys::fs::remove(StringRef(TmpDir));
86 ASSERT_FALSE(EC);
90 TEST(LockFileManagerTest, RelativePath) {
91 SmallString<64> TmpDir;
92 std::error_code EC;
93 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
94 ASSERT_FALSE(EC);
96 char PathBuf[1024];
97 const char *OrigPath = getcwd(PathBuf, 1024);
98 ASSERT_FALSE(chdir(TmpDir.c_str()));
100 sys::fs::create_directory("inner");
101 SmallString<64> LockedFile("inner");
102 sys::path::append(LockedFile, "file");
104 SmallString<64> FileLock(LockedFile);
105 FileLock += ".lock";
108 // The lock file should not exist, so we should successfully acquire it.
109 LockFileManager Locked(LockedFile);
110 EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
111 EXPECT_TRUE(sys::fs::exists(FileLock.str()));
114 // Now that the lock is out of scope, the file should be gone.
115 EXPECT_FALSE(sys::fs::exists(LockedFile.str()));
116 EXPECT_FALSE(sys::fs::exists(FileLock.str()));
118 EC = sys::fs::remove("inner");
119 ASSERT_FALSE(EC);
121 ASSERT_FALSE(chdir(OrigPath));
123 EC = sys::fs::remove(StringRef(TmpDir));
124 ASSERT_FALSE(EC);
127 } // end anonymous namespace