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
9#include "clang/Basic/FileManager.h"
10#include "clang/Basic/FileSystemOptions.h"
11#include "clang/Basic/FileSystemStatCache.h"
12#include "llvm/ADT/STLExtras.h"
13#include "llvm/Support/Path.h"
14#include "llvm/Support/VirtualFileSystem.h"
15#include "llvm/Testing/Support/Error.h"
16#include "gtest/gtest.h"
17
18using namespace llvm;
19using namespace clang;
20
21namespace {
22
23// Used to create a fake file system for running the tests with such
24// that the tests are not affected by the structure/contents of the
25// file system on the machine running the tests.
26class FakeStatCache : public FileSystemStatCache {
27private:
28 // Maps a file/directory path to its desired stat result. Anything
29 // not in this map is considered to not exist in the file system.
30 llvm::StringMap<llvm::vfs::Status, llvm::BumpPtrAllocator> StatCalls;
31
32 void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile,
33 const char *StatPath) {
34 SmallString<128> NormalizedPath(Path);
35 SmallString<128> NormalizedStatPath;
36 if (is_style_posix(S: llvm::sys::path::Style::native)) {
37 llvm::sys::path::native(path&: NormalizedPath);
38 Path = NormalizedPath.c_str();
39
40 if (StatPath) {
41 NormalizedStatPath = StatPath;
42 llvm::sys::path::native(path&: NormalizedStatPath);
43 StatPath = NormalizedStatPath.c_str();
44 }
45 }
46
47 auto fileType = IsFile ?
48 llvm::sys::fs::file_type::regular_file :
49 llvm::sys::fs::file_type::directory_file;
50 llvm::vfs::Status Status(StatPath ? StatPath : Path,
51 llvm::sys::fs::UniqueID(1, INode),
52 /*MTime*/{}, /*User*/0, /*Group*/0,
53 /*Size*/0, fileType,
54 llvm::sys::fs::perms::all_all);
55 if (StatPath)
56 Status.ExposesExternalVFSPath = true;
57 StatCalls[Path] = Status;
58 }
59
60public:
61 // Inject a file with the given inode value to the fake file system.
62 void InjectFile(const char *Path, ino_t INode,
63 const char *StatPath = nullptr) {
64 InjectFileOrDirectory(Path, INode, /*IsFile=*/true, StatPath);
65 }
66
67 // Inject a directory with the given inode value to the fake file system.
68 void InjectDirectory(const char *Path, ino_t INode) {
69 InjectFileOrDirectory(Path, INode, /*IsFile=*/false, StatPath: nullptr);
70 }
71
72 // Implement FileSystemStatCache::getStat().
73 std::error_code getStat(StringRef Path, llvm::vfs::Status &Status,
74 bool isFile,
75 std::unique_ptr<llvm::vfs::File> *F,
76 llvm::vfs::FileSystem &FS) override {
77 SmallString<128> NormalizedPath(Path);
78 if (is_style_posix(S: llvm::sys::path::Style::native)) {
79 llvm::sys::path::native(path&: NormalizedPath);
80 Path = NormalizedPath.c_str();
81 }
82
83 if (StatCalls.count(Key: Path) != 0) {
84 Status = StatCalls[Path];
85 return std::error_code();
86 }
87
88 return std::make_error_code(e: std::errc::no_such_file_or_directory);
89 }
90};
91
92// The test fixture.
93class FileManagerTest : public ::testing::Test {
94 protected:
95 FileManagerTest() : manager(options) {
96 }
97
98 FileSystemOptions options;
99 FileManager manager;
100};
101
102// When a virtual file is added, its getDir() field has correct name.
103TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) {
104 FileEntryRef file = manager.getVirtualFileRef(Filename: "foo.cpp", Size: 42, ModificationTime: 0);
105 EXPECT_EQ(".", file.getDir().getName());
106
107 file = manager.getVirtualFileRef(Filename: "x/y/z.cpp", Size: 42, ModificationTime: 0);
108 EXPECT_EQ("x/y", file.getDir().getName());
109}
110
111// Before any virtual file is added, no virtual directory exists.
112TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) {
113 // An empty FakeStatCache causes all stat calls made by the
114 // FileManager to report "file/directory doesn't exist". This
115 // avoids the possibility of the result of this test being affected
116 // by what's in the real file system.
117 manager.setStatCache(std::make_unique<FakeStatCache>());
118
119 ASSERT_FALSE(manager.getOptionalDirectoryRef("virtual/dir/foo"));
120 ASSERT_FALSE(manager.getOptionalDirectoryRef("virtual/dir"));
121 ASSERT_FALSE(manager.getOptionalDirectoryRef("virtual"));
122}
123
124// When a virtual file is added, all of its ancestors should be created.
125TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) {
126 // Fake an empty real file system.
127 manager.setStatCache(std::make_unique<FakeStatCache>());
128
129 manager.getVirtualFileRef(Filename: "virtual/dir/bar.h", Size: 100, ModificationTime: 0);
130
131 auto dir = manager.getDirectoryRef(DirName: "virtual/dir/foo");
132 ASSERT_THAT_EXPECTED(dir, llvm::Failed());
133
134 dir = manager.getDirectoryRef(DirName: "virtual/dir");
135 ASSERT_THAT_EXPECTED(dir, llvm::Succeeded());
136 EXPECT_EQ("virtual/dir", dir->getName());
137
138 dir = manager.getDirectoryRef(DirName: "virtual");
139 ASSERT_THAT_EXPECTED(dir, llvm::Succeeded());
140 EXPECT_EQ("virtual", dir->getName());
141}
142
143// getFileRef() succeeds if a real file exists at the given path.
144TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) {
145 // Inject fake files into the file system.
146 auto statCache = std::make_unique<FakeStatCache>();
147 statCache->InjectDirectory(Path: "/tmp", INode: 42);
148 statCache->InjectFile(Path: "/tmp/test", INode: 43);
149
150#ifdef _WIN32
151 const char *DirName = "C:.";
152 const char *FileName = "C:test";
153 statCache->InjectDirectory(DirName, 44);
154 statCache->InjectFile(FileName, 45);
155#endif
156
157 manager.setStatCache(std::move(statCache));
158
159 auto file = manager.getFileRef(Filename: "/tmp/test");
160 ASSERT_THAT_EXPECTED(file, llvm::Succeeded());
161 EXPECT_EQ("/tmp/test", file->getName());
162
163 EXPECT_EQ("/tmp", file->getDir().getName());
164
165#ifdef _WIN32
166 file = manager.getFileRef(FileName);
167 ASSERT_THAT_EXPECTED(file, llvm::Succeeded());
168 EXPECT_EQ(DirName, file->getDir().getName());
169#endif
170}
171
172// getFileRef() succeeds if a virtual file exists at the given path.
173TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) {
174 // Fake an empty real file system.
175 manager.setStatCache(std::make_unique<FakeStatCache>());
176
177 manager.getVirtualFileRef(Filename: "virtual/dir/bar.h", Size: 100, ModificationTime: 0);
178 auto file = manager.getFileRef(Filename: "virtual/dir/bar.h");
179 ASSERT_THAT_EXPECTED(file, llvm::Succeeded());
180 EXPECT_EQ("virtual/dir/bar.h", file->getName());
181 EXPECT_EQ("virtual/dir", file->getDir().getName());
182}
183
184// getFile() returns different FileEntries for different paths when
185// there's no aliasing.
186TEST_F(FileManagerTest, getFileReturnsDifferentFileEntriesForDifferentFiles) {
187 // Inject two fake files into the file system. Different inodes
188 // mean the files are not symlinked together.
189 auto statCache = std::make_unique<FakeStatCache>();
190 statCache->InjectDirectory(Path: ".", INode: 41);
191 statCache->InjectFile(Path: "foo.cpp", INode: 42);
192 statCache->InjectFile(Path: "bar.cpp", INode: 43);
193 manager.setStatCache(std::move(statCache));
194
195 auto fileFoo = manager.getOptionalFileRef(Filename: "foo.cpp");
196 auto fileBar = manager.getOptionalFileRef(Filename: "bar.cpp");
197 ASSERT_TRUE(fileFoo);
198 ASSERT_TRUE(fileBar);
199 EXPECT_NE(&fileFoo->getFileEntry(), &fileBar->getFileEntry());
200}
201
202// getFile() returns an error if neither a real file nor a virtual file
203// exists at the given path.
204TEST_F(FileManagerTest, getFileReturnsErrorForNonexistentFile) {
205 // Inject a fake foo.cpp into the file system.
206 auto statCache = std::make_unique<FakeStatCache>();
207 statCache->InjectDirectory(Path: ".", INode: 41);
208 statCache->InjectFile(Path: "foo.cpp", INode: 42);
209 statCache->InjectDirectory(Path: "MyDirectory", INode: 49);
210 manager.setStatCache(std::move(statCache));
211
212 // Create a virtual bar.cpp file.
213 manager.getVirtualFileRef(Filename: "bar.cpp", Size: 200, ModificationTime: 0);
214
215 auto file = manager.getFileRef(Filename: "xyz.txt");
216 ASSERT_FALSE(file);
217 ASSERT_EQ(llvm::errorToErrorCode(file.takeError()),
218 std::make_error_code(std::errc::no_such_file_or_directory));
219
220 auto readingDirAsFile = manager.getFileRef(Filename: "MyDirectory");
221 ASSERT_FALSE(readingDirAsFile);
222 ASSERT_EQ(llvm::errorToErrorCode(readingDirAsFile.takeError()),
223 std::make_error_code(std::errc::is_a_directory));
224
225 auto readingFileAsDir = manager.getDirectoryRef(DirName: "foo.cpp");
226 ASSERT_FALSE(readingFileAsDir);
227 ASSERT_EQ(llvm::errorToErrorCode(readingFileAsDir.takeError()),
228 std::make_error_code(std::errc::not_a_directory));
229}
230
231// The following tests apply to Unix-like system only.
232
233#ifndef _WIN32
234
235// getFile() returns the same FileEntry for real files that are aliases.
236TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) {
237 // Inject two real files with the same inode.
238 auto statCache = std::make_unique<FakeStatCache>();
239 statCache->InjectDirectory(Path: "abc", INode: 41);
240 statCache->InjectFile(Path: "abc/foo.cpp", INode: 42);
241 statCache->InjectFile(Path: "abc/bar.cpp", INode: 42);
242 manager.setStatCache(std::move(statCache));
243
244 auto f1 = manager.getOptionalFileRef(Filename: "abc/foo.cpp");
245 auto f2 = manager.getOptionalFileRef(Filename: "abc/bar.cpp");
246
247 EXPECT_EQ(f1 ? &f1->getFileEntry() : nullptr,
248 f2 ? &f2->getFileEntry() : nullptr);
249
250 // Check that getFileRef also does the right thing.
251 auto r1 = manager.getFileRef(Filename: "abc/foo.cpp");
252 auto r2 = manager.getFileRef(Filename: "abc/bar.cpp");
253 ASSERT_FALSE(!r1);
254 ASSERT_FALSE(!r2);
255
256 EXPECT_EQ("abc/foo.cpp", r1->getName());
257 EXPECT_EQ("abc/bar.cpp", r2->getName());
258 EXPECT_EQ((f1 ? &f1->getFileEntry() : nullptr), &r1->getFileEntry());
259 EXPECT_EQ((f2 ? &f2->getFileEntry() : nullptr), &r2->getFileEntry());
260}
261
262TEST_F(FileManagerTest, getFileRefReturnsCorrectNameForDifferentStatPath) {
263 // Inject files with the same inode, but where some files have a stat that
264 // gives a different name. This is adding coverage for stat behaviour
265 // triggered by the RedirectingFileSystem for 'use-external-name' that
266 // FileManager::getFileRef has special logic for.
267 auto StatCache = std::make_unique<FakeStatCache>();
268 StatCache->InjectDirectory(Path: "dir", INode: 40);
269 StatCache->InjectFile(Path: "dir/f1.cpp", INode: 41);
270 StatCache->InjectFile(Path: "dir/f1-alias.cpp", INode: 41, StatPath: "dir/f1.cpp");
271 StatCache->InjectFile(Path: "dir/f2.cpp", INode: 42);
272 StatCache->InjectFile(Path: "dir/f2-alias.cpp", INode: 42, StatPath: "dir/f2.cpp");
273
274 // This unintuitive rename-the-file-on-stat behaviour supports how the
275 // RedirectingFileSystem VFS layer responds to stats. However, even if you
276 // have two layers, you should only get a single filename back. As such the
277 // following stat cache behaviour is not supported (the correct stat entry
278 // for a double-redirection would be "dir/f1.cpp") and the getFileRef below
279 // should assert.
280 StatCache->InjectFile(Path: "dir/f1-alias-alias.cpp", INode: 41, StatPath: "dir/f1-alias.cpp");
281
282 manager.setStatCache(std::move(StatCache));
283
284 // With F1, test accessing the non-redirected name first.
285 auto F1 = manager.getFileRef(Filename: "dir/f1.cpp");
286 auto F1Alias = manager.getFileRef(Filename: "dir/f1-alias.cpp");
287 auto F1Alias2 = manager.getFileRef(Filename: "dir/f1-alias.cpp");
288 ASSERT_FALSE(!F1);
289 ASSERT_FALSE(!F1Alias);
290 ASSERT_FALSE(!F1Alias2);
291 EXPECT_EQ("dir/f1.cpp", F1->getName());
292 EXPECT_EQ("dir/f1.cpp", F1Alias->getName());
293 EXPECT_EQ("dir/f1.cpp", F1Alias2->getName());
294 EXPECT_EQ(&F1->getFileEntry(), &F1Alias->getFileEntry());
295 EXPECT_EQ(&F1->getFileEntry(), &F1Alias2->getFileEntry());
296
297#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
298 EXPECT_DEATH((void)manager.getFileRef("dir/f1-alias-alias.cpp"),
299 "filename redirected to a non-canonical filename?");
300#endif
301
302 // With F2, test accessing the redirected name first.
303 auto F2Alias = manager.getFileRef(Filename: "dir/f2-alias.cpp");
304 auto F2 = manager.getFileRef(Filename: "dir/f2.cpp");
305 auto F2Alias2 = manager.getFileRef(Filename: "dir/f2-alias.cpp");
306 ASSERT_FALSE(!F2);
307 ASSERT_FALSE(!F2Alias);
308 ASSERT_FALSE(!F2Alias2);
309 EXPECT_EQ("dir/f2.cpp", F2->getName());
310 EXPECT_EQ("dir/f2.cpp", F2Alias->getName());
311 EXPECT_EQ("dir/f2.cpp", F2Alias2->getName());
312 EXPECT_EQ(&F2->getFileEntry(), &F2Alias->getFileEntry());
313 EXPECT_EQ(&F2->getFileEntry(), &F2Alias2->getFileEntry());
314}
315
316TEST_F(FileManagerTest, getFileRefReturnsCorrectDirNameForDifferentStatPath) {
317 // Inject files with the same inode into distinct directories (name & inode).
318 auto StatCache = std::make_unique<FakeStatCache>();
319 StatCache->InjectDirectory(Path: "dir1", INode: 40);
320 StatCache->InjectDirectory(Path: "dir2", INode: 41);
321 StatCache->InjectFile(Path: "dir1/f.cpp", INode: 42);
322 StatCache->InjectFile(Path: "dir2/f.cpp", INode: 42, StatPath: "dir1/f.cpp");
323
324 manager.setStatCache(std::move(StatCache));
325 auto Dir1F = manager.getFileRef(Filename: "dir1/f.cpp");
326 auto Dir2F = manager.getFileRef(Filename: "dir2/f.cpp");
327
328 ASSERT_FALSE(!Dir1F);
329 ASSERT_FALSE(!Dir2F);
330 EXPECT_EQ("dir1", Dir1F->getDir().getName());
331 EXPECT_EQ("dir2", Dir2F->getDir().getName());
332 EXPECT_EQ("dir1/f.cpp", Dir1F->getNameAsRequested());
333 EXPECT_EQ("dir2/f.cpp", Dir2F->getNameAsRequested());
334}
335
336// getFile() returns the same FileEntry for virtual files that have
337// corresponding real files that are aliases.
338TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) {
339 // Inject two real files with the same inode.
340 auto statCache = std::make_unique<FakeStatCache>();
341 statCache->InjectDirectory(Path: "abc", INode: 41);
342 statCache->InjectFile(Path: "abc/foo.cpp", INode: 42);
343 statCache->InjectFile(Path: "abc/bar.cpp", INode: 42);
344 manager.setStatCache(std::move(statCache));
345
346 auto f1 = manager.getOptionalFileRef(Filename: "abc/foo.cpp");
347 auto f2 = manager.getOptionalFileRef(Filename: "abc/bar.cpp");
348
349 EXPECT_EQ(f1 ? &f1->getFileEntry() : nullptr,
350 f2 ? &f2->getFileEntry() : nullptr);
351}
352
353TEST_F(FileManagerTest, getFileRefEquality) {
354 auto StatCache = std::make_unique<FakeStatCache>();
355 StatCache->InjectDirectory(Path: "dir", INode: 40);
356 StatCache->InjectFile(Path: "dir/f1.cpp", INode: 41);
357 StatCache->InjectFile(Path: "dir/f1-also.cpp", INode: 41);
358 StatCache->InjectFile(Path: "dir/f1-redirect.cpp", INode: 41, StatPath: "dir/f1.cpp");
359 StatCache->InjectFile(Path: "dir/f2.cpp", INode: 42);
360 manager.setStatCache(std::move(StatCache));
361
362 auto F1 = manager.getFileRef(Filename: "dir/f1.cpp");
363 auto F1Again = manager.getFileRef(Filename: "dir/f1.cpp");
364 auto F1Also = manager.getFileRef(Filename: "dir/f1-also.cpp");
365 auto F1Redirect = manager.getFileRef(Filename: "dir/f1-redirect.cpp");
366 auto F1RedirectAgain = manager.getFileRef(Filename: "dir/f1-redirect.cpp");
367 auto F2 = manager.getFileRef(Filename: "dir/f2.cpp");
368
369 // Check Expected<FileEntryRef> for error.
370 ASSERT_FALSE(!F1);
371 ASSERT_FALSE(!F1Also);
372 ASSERT_FALSE(!F1Again);
373 ASSERT_FALSE(!F1Redirect);
374 ASSERT_FALSE(!F1RedirectAgain);
375 ASSERT_FALSE(!F2);
376
377 // Check names.
378 EXPECT_EQ("dir/f1.cpp", F1->getName());
379 EXPECT_EQ("dir/f1.cpp", F1Again->getName());
380 EXPECT_EQ("dir/f1-also.cpp", F1Also->getName());
381 EXPECT_EQ("dir/f1.cpp", F1Redirect->getName());
382 EXPECT_EQ("dir/f1.cpp", F1RedirectAgain->getName());
383 EXPECT_EQ("dir/f2.cpp", F2->getName());
384
385 EXPECT_EQ("dir/f1.cpp", F1->getNameAsRequested());
386 EXPECT_EQ("dir/f1-redirect.cpp", F1Redirect->getNameAsRequested());
387
388 // Compare against FileEntry*.
389 EXPECT_EQ(&F1->getFileEntry(), *F1);
390 EXPECT_EQ(*F1, &F1->getFileEntry());
391 EXPECT_EQ(&F1->getFileEntry(), &F1Redirect->getFileEntry());
392 EXPECT_EQ(&F1->getFileEntry(), &F1RedirectAgain->getFileEntry());
393 EXPECT_NE(&F2->getFileEntry(), *F1);
394 EXPECT_NE(*F1, &F2->getFileEntry());
395
396 // Compare using ==.
397 EXPECT_EQ(*F1, *F1Also);
398 EXPECT_EQ(*F1, *F1Again);
399 EXPECT_EQ(*F1, *F1Redirect);
400 EXPECT_EQ(*F1Also, *F1Redirect);
401 EXPECT_EQ(*F1, *F1RedirectAgain);
402 EXPECT_NE(*F2, *F1);
403 EXPECT_NE(*F2, *F1Also);
404 EXPECT_NE(*F2, *F1Again);
405 EXPECT_NE(*F2, *F1Redirect);
406
407 // Compare using isSameRef.
408 EXPECT_TRUE(F1->isSameRef(*F1Again));
409 EXPECT_FALSE(F1->isSameRef(*F1Redirect));
410 EXPECT_FALSE(F1->isSameRef(*F1Also));
411 EXPECT_FALSE(F1->isSameRef(*F2));
412 EXPECT_TRUE(F1Redirect->isSameRef(*F1RedirectAgain));
413}
414
415// getFile() Should return the same entry as getVirtualFile if the file actually
416// is a virtual file, even if the name is not exactly the same (but is after
417// normalisation done by the file system, like on Windows). This can be checked
418// here by checking the size.
419TEST_F(FileManagerTest, getVirtualFileWithDifferentName) {
420 // Inject fake files into the file system.
421 auto statCache = std::make_unique<FakeStatCache>();
422 statCache->InjectDirectory(Path: "c:\\tmp", INode: 42);
423 statCache->InjectFile(Path: "c:\\tmp\\test", INode: 43);
424
425 manager.setStatCache(std::move(statCache));
426
427 // Inject the virtual file:
428 FileEntryRef file1 = manager.getVirtualFileRef(Filename: "c:\\tmp\\test", Size: 123, ModificationTime: 1);
429 EXPECT_EQ(43U, file1.getUniqueID().getFile());
430 EXPECT_EQ(123, file1.getSize());
431
432 // Lookup the virtual file with a different name:
433 auto file2 = manager.getOptionalFileRef(Filename: "c:/tmp/test", OpenFile: 100, CacheFailure: 1);
434 ASSERT_TRUE(file2);
435 // Check that it's the same UFE:
436 EXPECT_EQ(file1, *file2);
437 EXPECT_EQ(43U, file2->getUniqueID().getFile());
438 // Check that the contents of the UFE are not overwritten by the entry in the
439 // filesystem:
440 EXPECT_EQ(123, file2->getSize());
441}
442
443#endif // !_WIN32
444
445static StringRef getSystemRoot() {
446 return is_style_windows(S: llvm::sys::path::Style::native) ? "C:\\" : "/";
447}
448
449TEST_F(FileManagerTest, makeAbsoluteUsesVFS) {
450 // FIXME: Should this be using a root path / call getSystemRoot()? For now,
451 // avoiding that and leaving the test as-is.
452 SmallString<64> CustomWorkingDir =
453 is_style_windows(S: llvm::sys::path::Style::native) ? StringRef("C:")
454 : StringRef("/");
455 llvm::sys::path::append(path&: CustomWorkingDir, a: "some", b: "weird", c: "path");
456
457 auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
458 new llvm::vfs::InMemoryFileSystem);
459 // setCurrentworkingdirectory must finish without error.
460 ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir));
461
462 FileSystemOptions Opts;
463 FileManager Manager(Opts, FS);
464
465 SmallString<64> Path("a/foo.cpp");
466
467 SmallString<64> ExpectedResult(CustomWorkingDir);
468 llvm::sys::path::append(path&: ExpectedResult, a: Path);
469
470 ASSERT_TRUE(Manager.makeAbsolutePath(Path));
471 EXPECT_EQ(Path, ExpectedResult);
472}
473
474// getVirtualFile should always fill the real path.
475TEST_F(FileManagerTest, getVirtualFileFillsRealPathName) {
476 SmallString<64> CustomWorkingDir = getSystemRoot();
477
478 auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
479 new llvm::vfs::InMemoryFileSystem);
480 // setCurrentworkingdirectory must finish without error.
481 ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir));
482
483 FileSystemOptions Opts;
484 FileManager Manager(Opts, FS);
485
486 // Inject fake files into the file system.
487 auto statCache = std::make_unique<FakeStatCache>();
488 statCache->InjectDirectory(Path: "/tmp", INode: 42);
489 statCache->InjectFile(Path: "/tmp/test", INode: 43);
490
491 Manager.setStatCache(std::move(statCache));
492
493 // Check for real path.
494 FileEntryRef file = Manager.getVirtualFileRef(Filename: "/tmp/test", Size: 123, ModificationTime: 1);
495 SmallString<64> ExpectedResult = CustomWorkingDir;
496
497 llvm::sys::path::append(path&: ExpectedResult, a: "tmp", b: "test");
498 EXPECT_EQ(file.getFileEntry().tryGetRealPathName(), ExpectedResult);
499}
500
501TEST_F(FileManagerTest, getFileDontOpenRealPath) {
502 SmallString<64> CustomWorkingDir = getSystemRoot();
503
504 auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
505 new llvm::vfs::InMemoryFileSystem);
506 // setCurrentworkingdirectory must finish without error.
507 ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir));
508
509 FileSystemOptions Opts;
510 FileManager Manager(Opts, FS);
511
512 // Inject fake files into the file system.
513 auto statCache = std::make_unique<FakeStatCache>();
514 statCache->InjectDirectory(Path: "/tmp", INode: 42);
515 statCache->InjectFile(Path: "/tmp/test", INode: 43);
516
517 Manager.setStatCache(std::move(statCache));
518
519 // Check for real path.
520 auto file = Manager.getOptionalFileRef(Filename: "/tmp/test", /*OpenFile=*/false);
521 ASSERT_TRUE(file);
522 SmallString<64> ExpectedResult = CustomWorkingDir;
523
524 llvm::sys::path::append(path&: ExpectedResult, a: "tmp", b: "test");
525 EXPECT_EQ(file->getFileEntry().tryGetRealPathName(), ExpectedResult);
526}
527
528TEST_F(FileManagerTest, getBypassFile) {
529 SmallString<64> CustomWorkingDir;
530#ifdef _WIN32
531 CustomWorkingDir = "C:/";
532#else
533 CustomWorkingDir = "/";
534#endif
535
536 auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
537 new llvm::vfs::InMemoryFileSystem);
538 // setCurrentworkingdirectory must finish without error.
539 ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir));
540
541 FileSystemOptions Opts;
542 FileManager Manager(Opts, FS);
543
544 // Inject fake files into the file system.
545 auto Cache = std::make_unique<FakeStatCache>();
546 Cache->InjectDirectory(Path: "/tmp", INode: 42);
547 Cache->InjectFile(Path: "/tmp/test", INode: 43);
548 Manager.setStatCache(std::move(Cache));
549
550 // Set up a virtual file with a different size than FakeStatCache uses.
551 FileEntryRef File = Manager.getVirtualFileRef(Filename: "/tmp/test", /*Size=*/10, ModificationTime: 0);
552 ASSERT_TRUE(File);
553 const FileEntry &FE = *File;
554 EXPECT_EQ(FE.getSize(), 10);
555
556 // Calling a second time should not affect the UID or size.
557 unsigned VirtualUID = FE.getUID();
558 OptionalFileEntryRef SearchRef;
559 ASSERT_THAT_ERROR(Manager.getFileRef("/tmp/test").moveInto(SearchRef),
560 Succeeded());
561 EXPECT_EQ(&FE, &SearchRef->getFileEntry());
562 EXPECT_EQ(FE.getUID(), VirtualUID);
563 EXPECT_EQ(FE.getSize(), 10);
564
565 // Bypass the file.
566 OptionalFileEntryRef BypassRef = Manager.getBypassFile(VFE: File);
567 ASSERT_TRUE(BypassRef);
568 EXPECT_EQ("/tmp/test", BypassRef->getName());
569
570 // Check that it's different in the right ways.
571 EXPECT_NE(&BypassRef->getFileEntry(), File);
572 EXPECT_NE(BypassRef->getUID(), VirtualUID);
573 EXPECT_NE(BypassRef->getSize(), FE.getSize());
574
575 // The virtual file should still be returned when searching.
576 ASSERT_THAT_ERROR(Manager.getFileRef("/tmp/test").moveInto(SearchRef),
577 Succeeded());
578 EXPECT_EQ(&FE, &SearchRef->getFileEntry());
579}
580
581} // anonymous namespace
582

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of clang/unittests/Basic/FileManagerTest.cpp