Home | History | Annotate | Download | only in Basic
      1 //===- unittests/Basic/FileMangerTest.cpp ------------ FileManger 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 //===----------------------------------------------------------------------===//
      9 
     10 #include "clang/Basic/FileManager.h"
     11 #include "clang/Basic/FileSystemOptions.h"
     12 #include "clang/Basic/FileSystemStatCache.h"
     13 #include "gtest/gtest.h"
     14 
     15 using namespace llvm;
     16 using namespace clang;
     17 
     18 namespace {
     19 
     20 // Used to create a fake file system for running the tests with such
     21 // that the tests are not affected by the structure/contents of the
     22 // file system on the machine running the tests.
     23 class FakeStatCache : public FileSystemStatCache {
     24 private:
     25   // Maps a file/directory path to its desired stat result.  Anything
     26   // not in this map is considered to not exist in the file system.
     27   llvm::StringMap<struct stat, llvm::BumpPtrAllocator> StatCalls;
     28 
     29   void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile) {
     30     struct stat statBuf;
     31     memset(&statBuf, 0, sizeof(statBuf));
     32     statBuf.st_dev = 1;
     33 #ifndef _WIN32  // struct stat has no st_ino field on Windows.
     34     statBuf.st_ino = INode;
     35 #endif
     36     statBuf.st_mode = IsFile ? (0777 | S_IFREG)  // a regular file
     37         : (0777 | S_IFDIR);  // a directory
     38     StatCalls[Path] = statBuf;
     39   }
     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 
     47   // Inject a directory with the given inode value to the fake file system.
     48   void InjectDirectory(const char *Path, ino_t INode) {
     49     InjectFileOrDirectory(Path, INode, /*IsFile=*/false);
     50   }
     51 
     52   // Implement FileSystemStatCache::getStat().
     53   virtual LookupResult getStat(const char *Path, struct stat &StatBuf,
     54                                bool isFile, int *FileDescriptor) {
     55     if (StatCalls.count(Path) != 0) {
     56       StatBuf = StatCalls[Path];
     57       return CacheExists;
     58     }
     59 
     60     return CacheMissing;  // This means the file/directory doesn't exist.
     61   }
     62 };
     63 
     64 // The test fixture.
     65 class FileManagerTest : public ::testing::Test {
     66  protected:
     67   FileManagerTest() : manager(options) {
     68   }
     69 
     70   FileSystemOptions options;
     71   FileManager manager;
     72 };
     73 
     74 // When a virtual file is added, its getDir() field is set correctly
     75 // (not NULL, correct name).
     76 TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) {
     77   const FileEntry *file = manager.getVirtualFile("foo.cpp", 42, 0);
     78   ASSERT_TRUE(file != NULL);
     79 
     80   const DirectoryEntry *dir = file->getDir();
     81   ASSERT_TRUE(dir != NULL);
     82   EXPECT_STREQ(".", dir->getName());
     83 
     84   file = manager.getVirtualFile("x/y/z.cpp", 42, 0);
     85   ASSERT_TRUE(file != NULL);
     86 
     87   dir = file->getDir();
     88   ASSERT_TRUE(dir != NULL);
     89   EXPECT_STREQ("x/y", dir->getName());
     90 }
     91 
     92 // Before any virtual file is added, no virtual directory exists.
     93 TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) {
     94   // An empty FakeStatCache causes all stat calls made by the
     95   // FileManager to report "file/directory doesn't exist".  This
     96   // avoids the possibility of the result of this test being affected
     97   // by what's in the real file system.
     98   manager.addStatCache(new FakeStatCache);
     99 
    100   EXPECT_EQ(NULL, manager.getDirectory("virtual/dir/foo"));
    101   EXPECT_EQ(NULL, manager.getDirectory("virtual/dir"));
    102   EXPECT_EQ(NULL, manager.getDirectory("virtual"));
    103 }
    104 
    105 // When a virtual file is added, all of its ancestors should be created.
    106 TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) {
    107   // Fake an empty real file system.
    108   manager.addStatCache(new FakeStatCache);
    109 
    110   manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
    111   EXPECT_EQ(NULL, manager.getDirectory("virtual/dir/foo"));
    112 
    113   const DirectoryEntry *dir = manager.getDirectory("virtual/dir");
    114   ASSERT_TRUE(dir != NULL);
    115   EXPECT_STREQ("virtual/dir", dir->getName());
    116 
    117   dir = manager.getDirectory("virtual");
    118   ASSERT_TRUE(dir != NULL);
    119   EXPECT_STREQ("virtual", dir->getName());
    120 }
    121 
    122 // getFile() returns non-NULL if a real file exists at the given path.
    123 TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) {
    124   // Inject fake files into the file system.
    125   FakeStatCache *statCache = new FakeStatCache;
    126   statCache->InjectDirectory("/tmp", 42);
    127   statCache->InjectFile("/tmp/test", 43);
    128   manager.addStatCache(statCache);
    129 
    130   const FileEntry *file = manager.getFile("/tmp/test");
    131   ASSERT_TRUE(file != NULL);
    132   EXPECT_STREQ("/tmp/test", file->getName());
    133 
    134   const DirectoryEntry *dir = file->getDir();
    135   ASSERT_TRUE(dir != NULL);
    136   EXPECT_STREQ("/tmp", dir->getName());
    137 }
    138 
    139 // getFile() returns non-NULL if a virtual file exists at the given path.
    140 TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) {
    141   // Fake an empty real file system.
    142   manager.addStatCache(new FakeStatCache);
    143 
    144   manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
    145   const FileEntry *file = manager.getFile("virtual/dir/bar.h");
    146   ASSERT_TRUE(file != NULL);
    147   EXPECT_STREQ("virtual/dir/bar.h", file->getName());
    148 
    149   const DirectoryEntry *dir = file->getDir();
    150   ASSERT_TRUE(dir != NULL);
    151   EXPECT_STREQ("virtual/dir", dir->getName());
    152 }
    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   FakeStatCache *statCache = new FakeStatCache;
    160   statCache->InjectDirectory(".", 41);
    161   statCache->InjectFile("foo.cpp", 42);
    162   statCache->InjectFile("bar.cpp", 43);
    163   manager.addStatCache(statCache);
    164 
    165   const FileEntry *fileFoo = manager.getFile("foo.cpp");
    166   const FileEntry *fileBar = manager.getFile("bar.cpp");
    167   ASSERT_TRUE(fileFoo != NULL);
    168   ASSERT_TRUE(fileBar != NULL);
    169   EXPECT_NE(fileFoo, fileBar);
    170 }
    171 
    172 // getFile() returns NULL if neither a real file nor a virtual file
    173 // exists at the given path.
    174 TEST_F(FileManagerTest, getFileReturnsNULLForNonexistentFile) {
    175   // Inject a fake foo.cpp into the file system.
    176   FakeStatCache *statCache = new FakeStatCache;
    177   statCache->InjectDirectory(".", 41);
    178   statCache->InjectFile("foo.cpp", 42);
    179   manager.addStatCache(statCache);
    180 
    181   // Create a virtual bar.cpp file.
    182   manager.getVirtualFile("bar.cpp", 200, 0);
    183 
    184   const FileEntry *file = manager.getFile("xyz.txt");
    185   EXPECT_EQ(NULL, file);
    186 }
    187 
    188 // The following tests apply to Unix-like system only.
    189 
    190 #ifndef _WIN32
    191 
    192 // getFile() returns the same FileEntry for real files that are aliases.
    193 TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) {
    194   // Inject two real files with the same inode.
    195   FakeStatCache *statCache = new FakeStatCache;
    196   statCache->InjectDirectory("abc", 41);
    197   statCache->InjectFile("abc/foo.cpp", 42);
    198   statCache->InjectFile("abc/bar.cpp", 42);
    199   manager.addStatCache(statCache);
    200 
    201   EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
    202 }
    203 
    204 // getFile() returns the same FileEntry for virtual files that have
    205 // corresponding real files that are aliases.
    206 TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) {
    207   // Inject two real files with the same inode.
    208   FakeStatCache *statCache = new FakeStatCache;
    209   statCache->InjectDirectory("abc", 41);
    210   statCache->InjectFile("abc/foo.cpp", 42);
    211   statCache->InjectFile("abc/bar.cpp", 42);
    212   manager.addStatCache(statCache);
    213 
    214   manager.getVirtualFile("abc/foo.cpp", 100, 0);
    215   manager.getVirtualFile("abc/bar.cpp", 200, 0);
    216 
    217   EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
    218 }
    219 
    220 #endif  // !_WIN32
    221 
    222 } // anonymous namespace
    223