FileManagerTest.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. //===- unittests/Basic/FileMangerTest.cpp ------------ FileManger tests ---===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "clang/Basic/FileManager.h"
  9. #include "clang/Basic/FileSystemOptions.h"
  10. #include "clang/Basic/FileSystemStatCache.h"
  11. #include "llvm/ADT/STLExtras.h"
  12. #include "llvm/Support/Path.h"
  13. #include "llvm/Support/VirtualFileSystem.h"
  14. #include "gtest/gtest.h"
  15. using namespace llvm;
  16. using namespace clang;
  17. namespace {
  18. // Used to create a fake file system for running the tests with such
  19. // that the tests are not affected by the structure/contents of the
  20. // file system on the machine running the tests.
  21. class FakeStatCache : public FileSystemStatCache {
  22. private:
  23. // Maps a file/directory path to its desired stat result. Anything
  24. // not in this map is considered to not exist in the file system.
  25. llvm::StringMap<llvm::vfs::Status, llvm::BumpPtrAllocator> StatCalls;
  26. void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile) {
  27. #ifndef _WIN32
  28. SmallString<128> NormalizedPath(Path);
  29. llvm::sys::path::native(NormalizedPath);
  30. Path = NormalizedPath.c_str();
  31. #endif
  32. auto fileType = IsFile ?
  33. llvm::sys::fs::file_type::regular_file :
  34. llvm::sys::fs::file_type::directory_file;
  35. llvm::vfs::Status Status(Path, llvm::sys::fs::UniqueID(1, INode),
  36. /*MTime*/{}, /*User*/0, /*Group*/0,
  37. /*Size*/0, fileType,
  38. llvm::sys::fs::perms::all_all);
  39. StatCalls[Path] = Status;
  40. }
  41. public:
  42. // Inject a file with the given inode value to the fake file system.
  43. void InjectFile(const char *Path, ino_t INode) {
  44. InjectFileOrDirectory(Path, INode, /*IsFile=*/true);
  45. }
  46. // Inject a directory with the given inode value to the fake file system.
  47. void InjectDirectory(const char *Path, ino_t INode) {
  48. InjectFileOrDirectory(Path, INode, /*IsFile=*/false);
  49. }
  50. // Implement FileSystemStatCache::getStat().
  51. std::error_code getStat(StringRef Path, llvm::vfs::Status &Status,
  52. bool isFile,
  53. std::unique_ptr<llvm::vfs::File> *F,
  54. llvm::vfs::FileSystem &FS) override {
  55. #ifndef _WIN32
  56. SmallString<128> NormalizedPath(Path);
  57. llvm::sys::path::native(NormalizedPath);
  58. Path = NormalizedPath.c_str();
  59. #endif
  60. if (StatCalls.count(Path) != 0) {
  61. Status = StatCalls[Path];
  62. return std::error_code();
  63. }
  64. return std::make_error_code(std::errc::no_such_file_or_directory);
  65. }
  66. };
  67. // The test fixture.
  68. class FileManagerTest : public ::testing::Test {
  69. protected:
  70. FileManagerTest() : manager(options) {
  71. }
  72. FileSystemOptions options;
  73. FileManager manager;
  74. };
  75. // When a virtual file is added, its getDir() field is set correctly
  76. // (not NULL, correct name).
  77. TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) {
  78. const FileEntry *file = manager.getVirtualFile("foo.cpp", 42, 0);
  79. ASSERT_TRUE(file != nullptr);
  80. const DirectoryEntry *dir = file->getDir();
  81. ASSERT_TRUE(dir != nullptr);
  82. EXPECT_EQ(".", dir->getName());
  83. file = manager.getVirtualFile("x/y/z.cpp", 42, 0);
  84. ASSERT_TRUE(file != nullptr);
  85. dir = file->getDir();
  86. ASSERT_TRUE(dir != nullptr);
  87. EXPECT_EQ("x/y", dir->getName());
  88. }
  89. // Before any virtual file is added, no virtual directory exists.
  90. TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) {
  91. // An empty FakeStatCache causes all stat calls made by the
  92. // FileManager to report "file/directory doesn't exist". This
  93. // avoids the possibility of the result of this test being affected
  94. // by what's in the real file system.
  95. manager.setStatCache(std::make_unique<FakeStatCache>());
  96. ASSERT_FALSE(manager.getDirectory("virtual/dir/foo"));
  97. ASSERT_FALSE(manager.getDirectory("virtual/dir"));
  98. ASSERT_FALSE(manager.getDirectory("virtual"));
  99. }
  100. // When a virtual file is added, all of its ancestors should be created.
  101. TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) {
  102. // Fake an empty real file system.
  103. manager.setStatCache(std::make_unique<FakeStatCache>());
  104. manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
  105. ASSERT_FALSE(manager.getDirectory("virtual/dir/foo"));
  106. auto dir = manager.getDirectory("virtual/dir");
  107. ASSERT_TRUE(dir);
  108. EXPECT_EQ("virtual/dir", (*dir)->getName());
  109. dir = manager.getDirectory("virtual");
  110. ASSERT_TRUE(dir);
  111. EXPECT_EQ("virtual", (*dir)->getName());
  112. }
  113. // getFile() returns non-NULL if a real file exists at the given path.
  114. TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) {
  115. // Inject fake files into the file system.
  116. auto statCache = std::make_unique<FakeStatCache>();
  117. statCache->InjectDirectory("/tmp", 42);
  118. statCache->InjectFile("/tmp/test", 43);
  119. #ifdef _WIN32
  120. const char *DirName = "C:.";
  121. const char *FileName = "C:test";
  122. statCache->InjectDirectory(DirName, 44);
  123. statCache->InjectFile(FileName, 45);
  124. #endif
  125. manager.setStatCache(std::move(statCache));
  126. auto file = manager.getFile("/tmp/test");
  127. ASSERT_TRUE(file);
  128. ASSERT_TRUE((*file)->isValid());
  129. EXPECT_EQ("/tmp/test", (*file)->getName());
  130. const DirectoryEntry *dir = (*file)->getDir();
  131. ASSERT_TRUE(dir != nullptr);
  132. EXPECT_EQ("/tmp", dir->getName());
  133. #ifdef _WIN32
  134. file = manager.getFile(FileName);
  135. ASSERT_TRUE(file);
  136. dir = (*file)->getDir();
  137. ASSERT_TRUE(dir != NULL);
  138. EXPECT_EQ(DirName, dir->getName());
  139. #endif
  140. }
  141. // getFile() returns non-NULL if a virtual file exists at the given path.
  142. TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) {
  143. // Fake an empty real file system.
  144. manager.setStatCache(std::make_unique<FakeStatCache>());
  145. manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
  146. auto file = manager.getFile("virtual/dir/bar.h");
  147. ASSERT_TRUE(file);
  148. ASSERT_TRUE((*file)->isValid());
  149. EXPECT_EQ("virtual/dir/bar.h", (*file)->getName());
  150. const DirectoryEntry *dir = (*file)->getDir();
  151. ASSERT_TRUE(dir != nullptr);
  152. EXPECT_EQ("virtual/dir", dir->getName());
  153. }
  154. // getFile() returns different FileEntries for different paths when
  155. // there's no aliasing.
  156. TEST_F(FileManagerTest, getFileReturnsDifferentFileEntriesForDifferentFiles) {
  157. // Inject two fake files into the file system. Different inodes
  158. // mean the files are not symlinked together.
  159. auto statCache = std::make_unique<FakeStatCache>();
  160. statCache->InjectDirectory(".", 41);
  161. statCache->InjectFile("foo.cpp", 42);
  162. statCache->InjectFile("bar.cpp", 43);
  163. manager.setStatCache(std::move(statCache));
  164. auto fileFoo = manager.getFile("foo.cpp");
  165. auto fileBar = manager.getFile("bar.cpp");
  166. ASSERT_TRUE(fileFoo);
  167. ASSERT_TRUE((*fileFoo)->isValid());
  168. ASSERT_TRUE(fileBar);
  169. ASSERT_TRUE((*fileBar)->isValid());
  170. EXPECT_NE(*fileFoo, *fileBar);
  171. }
  172. // getFile() returns an error if neither a real file nor a virtual file
  173. // exists at the given path.
  174. TEST_F(FileManagerTest, getFileReturnsErrorForNonexistentFile) {
  175. // Inject a fake foo.cpp into the file system.
  176. auto statCache = std::make_unique<FakeStatCache>();
  177. statCache->InjectDirectory(".", 41);
  178. statCache->InjectFile("foo.cpp", 42);
  179. statCache->InjectDirectory("MyDirectory", 49);
  180. manager.setStatCache(std::move(statCache));
  181. // Create a virtual bar.cpp file.
  182. manager.getVirtualFile("bar.cpp", 200, 0);
  183. auto file = manager.getFile("xyz.txt");
  184. ASSERT_FALSE(file);
  185. ASSERT_EQ(file.getError(), std::errc::no_such_file_or_directory);
  186. auto readingDirAsFile = manager.getFile("MyDirectory");
  187. ASSERT_FALSE(readingDirAsFile);
  188. ASSERT_EQ(readingDirAsFile.getError(), std::errc::is_a_directory);
  189. auto readingFileAsDir = manager.getDirectory("foo.cpp");
  190. ASSERT_FALSE(readingFileAsDir);
  191. ASSERT_EQ(readingFileAsDir.getError(), std::errc::not_a_directory);
  192. }
  193. // The following tests apply to Unix-like system only.
  194. #ifndef _WIN32
  195. // getFile() returns the same FileEntry for real files that are aliases.
  196. TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) {
  197. // Inject two real files with the same inode.
  198. auto statCache = std::make_unique<FakeStatCache>();
  199. statCache->InjectDirectory("abc", 41);
  200. statCache->InjectFile("abc/foo.cpp", 42);
  201. statCache->InjectFile("abc/bar.cpp", 42);
  202. manager.setStatCache(std::move(statCache));
  203. auto f1 = manager.getFile("abc/foo.cpp");
  204. auto f2 = manager.getFile("abc/bar.cpp");
  205. EXPECT_EQ(f1 ? *f1 : nullptr,
  206. f2 ? *f2 : nullptr);
  207. }
  208. // getFile() returns the same FileEntry for virtual files that have
  209. // corresponding real files that are aliases.
  210. TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) {
  211. // Inject two real files with the same inode.
  212. auto statCache = std::make_unique<FakeStatCache>();
  213. statCache->InjectDirectory("abc", 41);
  214. statCache->InjectFile("abc/foo.cpp", 42);
  215. statCache->InjectFile("abc/bar.cpp", 42);
  216. manager.setStatCache(std::move(statCache));
  217. ASSERT_TRUE(manager.getVirtualFile("abc/foo.cpp", 100, 0)->isValid());
  218. ASSERT_TRUE(manager.getVirtualFile("abc/bar.cpp", 200, 0)->isValid());
  219. auto f1 = manager.getFile("abc/foo.cpp");
  220. auto f2 = manager.getFile("abc/bar.cpp");
  221. EXPECT_EQ(f1 ? *f1 : nullptr,
  222. f2 ? *f2 : nullptr);
  223. }
  224. // getFile() Should return the same entry as getVirtualFile if the file actually
  225. // is a virtual file, even if the name is not exactly the same (but is after
  226. // normalisation done by the file system, like on Windows). This can be checked
  227. // here by checking the size.
  228. TEST_F(FileManagerTest, getVirtualFileWithDifferentName) {
  229. // Inject fake files into the file system.
  230. auto statCache = std::make_unique<FakeStatCache>();
  231. statCache->InjectDirectory("c:\\tmp", 42);
  232. statCache->InjectFile("c:\\tmp\\test", 43);
  233. manager.setStatCache(std::move(statCache));
  234. // Inject the virtual file:
  235. const FileEntry *file1 = manager.getVirtualFile("c:\\tmp\\test", 123, 1);
  236. ASSERT_TRUE(file1 != nullptr);
  237. ASSERT_TRUE(file1->isValid());
  238. EXPECT_EQ(43U, file1->getUniqueID().getFile());
  239. EXPECT_EQ(123, file1->getSize());
  240. // Lookup the virtual file with a different name:
  241. auto file2 = manager.getFile("c:/tmp/test", 100, 1);
  242. ASSERT_TRUE(file2);
  243. ASSERT_TRUE((*file2)->isValid());
  244. // Check that it's the same UFE:
  245. EXPECT_EQ(file1, *file2);
  246. EXPECT_EQ(43U, (*file2)->getUniqueID().getFile());
  247. // Check that the contents of the UFE are not overwritten by the entry in the
  248. // filesystem:
  249. EXPECT_EQ(123, (*file2)->getSize());
  250. }
  251. #endif // !_WIN32
  252. TEST_F(FileManagerTest, makeAbsoluteUsesVFS) {
  253. SmallString<64> CustomWorkingDir;
  254. #ifdef _WIN32
  255. CustomWorkingDir = "C:";
  256. #else
  257. CustomWorkingDir = "/";
  258. #endif
  259. llvm::sys::path::append(CustomWorkingDir, "some", "weird", "path");
  260. auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
  261. new llvm::vfs::InMemoryFileSystem);
  262. // setCurrentworkingdirectory must finish without error.
  263. ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir));
  264. FileSystemOptions Opts;
  265. FileManager Manager(Opts, FS);
  266. SmallString<64> Path("a/foo.cpp");
  267. SmallString<64> ExpectedResult(CustomWorkingDir);
  268. llvm::sys::path::append(ExpectedResult, Path);
  269. ASSERT_TRUE(Manager.makeAbsolutePath(Path));
  270. EXPECT_EQ(Path, ExpectedResult);
  271. }
  272. // getVirtualFile should always fill the real path.
  273. TEST_F(FileManagerTest, getVirtualFileFillsRealPathName) {
  274. SmallString<64> CustomWorkingDir;
  275. #ifdef _WIN32
  276. CustomWorkingDir = "C:/";
  277. #else
  278. CustomWorkingDir = "/";
  279. #endif
  280. auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
  281. new llvm::vfs::InMemoryFileSystem);
  282. // setCurrentworkingdirectory must finish without error.
  283. ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir));
  284. FileSystemOptions Opts;
  285. FileManager Manager(Opts, FS);
  286. // Inject fake files into the file system.
  287. auto statCache = std::make_unique<FakeStatCache>();
  288. statCache->InjectDirectory("/tmp", 42);
  289. statCache->InjectFile("/tmp/test", 43);
  290. Manager.setStatCache(std::move(statCache));
  291. // Check for real path.
  292. const FileEntry *file = Manager.getVirtualFile("/tmp/test", 123, 1);
  293. ASSERT_TRUE(file != nullptr);
  294. ASSERT_TRUE(file->isValid());
  295. SmallString<64> ExpectedResult = CustomWorkingDir;
  296. llvm::sys::path::append(ExpectedResult, "tmp", "test");
  297. EXPECT_EQ(file->tryGetRealPathName(), ExpectedResult);
  298. }
  299. TEST_F(FileManagerTest, getFileDontOpenRealPath) {
  300. SmallString<64> CustomWorkingDir;
  301. #ifdef _WIN32
  302. CustomWorkingDir = "C:/";
  303. #else
  304. CustomWorkingDir = "/";
  305. #endif
  306. auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
  307. new llvm::vfs::InMemoryFileSystem);
  308. // setCurrentworkingdirectory must finish without error.
  309. ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir));
  310. FileSystemOptions Opts;
  311. FileManager Manager(Opts, FS);
  312. // Inject fake files into the file system.
  313. auto statCache = std::make_unique<FakeStatCache>();
  314. statCache->InjectDirectory("/tmp", 42);
  315. statCache->InjectFile("/tmp/test", 43);
  316. Manager.setStatCache(std::move(statCache));
  317. // Check for real path.
  318. auto file = Manager.getFile("/tmp/test", /*OpenFile=*/false);
  319. ASSERT_TRUE(file);
  320. ASSERT_TRUE((*file)->isValid());
  321. SmallString<64> ExpectedResult = CustomWorkingDir;
  322. llvm::sys::path::append(ExpectedResult, "tmp", "test");
  323. EXPECT_EQ((*file)->tryGetRealPathName(), ExpectedResult);
  324. }
  325. TEST_F(FileManagerTest, getBypassFile) {
  326. SmallString<64> CustomWorkingDir;
  327. #ifdef _WIN32
  328. CustomWorkingDir = "C:/";
  329. #else
  330. CustomWorkingDir = "/";
  331. #endif
  332. auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
  333. new llvm::vfs::InMemoryFileSystem);
  334. // setCurrentworkingdirectory must finish without error.
  335. ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir));
  336. FileSystemOptions Opts;
  337. FileManager Manager(Opts, FS);
  338. // Inject fake files into the file system.
  339. auto Cache = std::make_unique<FakeStatCache>();
  340. Cache->InjectDirectory("/tmp", 42);
  341. Cache->InjectFile("/tmp/test", 43);
  342. Manager.setStatCache(std::move(Cache));
  343. // Set up a virtual file with a different size than FakeStatCache uses.
  344. const FileEntry *File = Manager.getVirtualFile("/tmp/test", /*Size=*/10, 0);
  345. ASSERT_TRUE(File);
  346. FileEntryRef Ref("/tmp/test", *File);
  347. EXPECT_TRUE(Ref.isValid());
  348. EXPECT_EQ(Ref.getSize(), 10);
  349. // Calling a second time should not affect the UID or size.
  350. unsigned VirtualUID = Ref.getUID();
  351. EXPECT_EQ(*expectedToOptional(Manager.getFileRef("/tmp/test")), Ref);
  352. EXPECT_EQ(Ref.getUID(), VirtualUID);
  353. EXPECT_EQ(Ref.getSize(), 10);
  354. // Bypass the file.
  355. llvm::Optional<FileEntryRef> BypassRef = Manager.getBypassFile(Ref);
  356. ASSERT_TRUE(BypassRef);
  357. EXPECT_TRUE(BypassRef->isValid());
  358. EXPECT_EQ(BypassRef->getName(), Ref.getName());
  359. // Check that it's different in the right ways.
  360. EXPECT_NE(&BypassRef->getFileEntry(), File);
  361. EXPECT_NE(BypassRef->getUID(), VirtualUID);
  362. EXPECT_NE(BypassRef->getSize(), Ref.getSize());
  363. // The virtual file should still be returned when searching.
  364. EXPECT_EQ(*expectedToOptional(Manager.getFileRef("/tmp/test")), Ref);
  365. }
  366. } // anonymous namespace