VirtualFileSystemTest.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. //===- unittests/Basic/VirtualFileSystem.cpp ---------------- VFS 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. #include "clang/Basic/VirtualFileSystem.h"
  10. #include "llvm/Support/Errc.h"
  11. #include "llvm/Support/MemoryBuffer.h"
  12. #include "llvm/Support/Path.h"
  13. #include "llvm/Support/SourceMgr.h"
  14. #include "gtest/gtest.h"
  15. #include <map>
  16. using namespace clang;
  17. using namespace llvm;
  18. using llvm::sys::fs::UniqueID;
  19. namespace {
  20. struct DummyFile : public vfs::File {
  21. vfs::Status S;
  22. explicit DummyFile(vfs::Status S) : S(S) {}
  23. llvm::ErrorOr<vfs::Status> status() override { return S; }
  24. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
  25. getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
  26. bool IsVolatile) override {
  27. llvm_unreachable("unimplemented");
  28. }
  29. std::error_code close() override { return std::error_code(); }
  30. };
  31. class DummyFileSystem : public vfs::FileSystem {
  32. int FSID; // used to produce UniqueIDs
  33. int FileID; // used to produce UniqueIDs
  34. std::map<std::string, vfs::Status> FilesAndDirs;
  35. static int getNextFSID() {
  36. static int Count = 0;
  37. return Count++;
  38. }
  39. public:
  40. DummyFileSystem() : FSID(getNextFSID()), FileID(0) {}
  41. ErrorOr<vfs::Status> status(const Twine &Path) override {
  42. std::map<std::string, vfs::Status>::iterator I =
  43. FilesAndDirs.find(Path.str());
  44. if (I == FilesAndDirs.end())
  45. return make_error_code(llvm::errc::no_such_file_or_directory);
  46. return I->second;
  47. }
  48. ErrorOr<std::unique_ptr<vfs::File>>
  49. openFileForRead(const Twine &Path) override {
  50. auto S = status(Path);
  51. if (S)
  52. return std::unique_ptr<vfs::File>(new DummyFile{*S});
  53. return S.getError();
  54. }
  55. llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
  56. return std::string();
  57. }
  58. std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
  59. return std::error_code();
  60. }
  61. struct DirIterImpl : public clang::vfs::detail::DirIterImpl {
  62. std::map<std::string, vfs::Status> &FilesAndDirs;
  63. std::map<std::string, vfs::Status>::iterator I;
  64. std::string Path;
  65. bool isInPath(StringRef S) {
  66. if (Path.size() < S.size() && S.find(Path) == 0) {
  67. auto LastSep = S.find_last_of('/');
  68. if (LastSep == Path.size() || LastSep == Path.size()-1)
  69. return true;
  70. }
  71. return false;
  72. }
  73. DirIterImpl(std::map<std::string, vfs::Status> &FilesAndDirs,
  74. const Twine &_Path)
  75. : FilesAndDirs(FilesAndDirs), I(FilesAndDirs.begin()),
  76. Path(_Path.str()) {
  77. for ( ; I != FilesAndDirs.end(); ++I) {
  78. if (isInPath(I->first)) {
  79. CurrentEntry = I->second;
  80. break;
  81. }
  82. }
  83. }
  84. std::error_code increment() override {
  85. ++I;
  86. for ( ; I != FilesAndDirs.end(); ++I) {
  87. if (isInPath(I->first)) {
  88. CurrentEntry = I->second;
  89. break;
  90. }
  91. }
  92. if (I == FilesAndDirs.end())
  93. CurrentEntry = vfs::Status();
  94. return std::error_code();
  95. }
  96. };
  97. vfs::directory_iterator dir_begin(const Twine &Dir,
  98. std::error_code &EC) override {
  99. return vfs::directory_iterator(
  100. std::make_shared<DirIterImpl>(FilesAndDirs, Dir));
  101. }
  102. void addEntry(StringRef Path, const vfs::Status &Status) {
  103. FilesAndDirs[Path] = Status;
  104. }
  105. void addRegularFile(StringRef Path, sys::fs::perms Perms = sys::fs::all_all) {
  106. vfs::Status S(Path, UniqueID(FSID, FileID++), sys::TimeValue::now(), 0, 0,
  107. 1024, sys::fs::file_type::regular_file, Perms);
  108. addEntry(Path, S);
  109. }
  110. void addDirectory(StringRef Path, sys::fs::perms Perms = sys::fs::all_all) {
  111. vfs::Status S(Path, UniqueID(FSID, FileID++), sys::TimeValue::now(), 0, 0,
  112. 0, sys::fs::file_type::directory_file, Perms);
  113. addEntry(Path, S);
  114. }
  115. void addSymlink(StringRef Path) {
  116. vfs::Status S(Path, UniqueID(FSID, FileID++), sys::TimeValue::now(), 0, 0,
  117. 0, sys::fs::file_type::symlink_file, sys::fs::all_all);
  118. addEntry(Path, S);
  119. }
  120. };
  121. } // end anonymous namespace
  122. TEST(VirtualFileSystemTest, StatusQueries) {
  123. IntrusiveRefCntPtr<DummyFileSystem> D(new DummyFileSystem());
  124. ErrorOr<vfs::Status> Status((std::error_code()));
  125. D->addRegularFile("/foo");
  126. Status = D->status("/foo");
  127. ASSERT_FALSE(Status.getError());
  128. EXPECT_TRUE(Status->isStatusKnown());
  129. EXPECT_FALSE(Status->isDirectory());
  130. EXPECT_TRUE(Status->isRegularFile());
  131. EXPECT_FALSE(Status->isSymlink());
  132. EXPECT_FALSE(Status->isOther());
  133. EXPECT_TRUE(Status->exists());
  134. D->addDirectory("/bar");
  135. Status = D->status("/bar");
  136. ASSERT_FALSE(Status.getError());
  137. EXPECT_TRUE(Status->isStatusKnown());
  138. EXPECT_TRUE(Status->isDirectory());
  139. EXPECT_FALSE(Status->isRegularFile());
  140. EXPECT_FALSE(Status->isSymlink());
  141. EXPECT_FALSE(Status->isOther());
  142. EXPECT_TRUE(Status->exists());
  143. D->addSymlink("/baz");
  144. Status = D->status("/baz");
  145. ASSERT_FALSE(Status.getError());
  146. EXPECT_TRUE(Status->isStatusKnown());
  147. EXPECT_FALSE(Status->isDirectory());
  148. EXPECT_FALSE(Status->isRegularFile());
  149. EXPECT_TRUE(Status->isSymlink());
  150. EXPECT_FALSE(Status->isOther());
  151. EXPECT_TRUE(Status->exists());
  152. EXPECT_TRUE(Status->equivalent(*Status));
  153. ErrorOr<vfs::Status> Status2 = D->status("/foo");
  154. ASSERT_FALSE(Status2.getError());
  155. EXPECT_FALSE(Status->equivalent(*Status2));
  156. }
  157. TEST(VirtualFileSystemTest, BaseOnlyOverlay) {
  158. IntrusiveRefCntPtr<DummyFileSystem> D(new DummyFileSystem());
  159. ErrorOr<vfs::Status> Status((std::error_code()));
  160. EXPECT_FALSE(Status = D->status("/foo"));
  161. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(new vfs::OverlayFileSystem(D));
  162. EXPECT_FALSE(Status = O->status("/foo"));
  163. D->addRegularFile("/foo");
  164. Status = D->status("/foo");
  165. EXPECT_FALSE(Status.getError());
  166. ErrorOr<vfs::Status> Status2((std::error_code()));
  167. Status2 = O->status("/foo");
  168. EXPECT_FALSE(Status2.getError());
  169. EXPECT_TRUE(Status->equivalent(*Status2));
  170. }
  171. TEST(VirtualFileSystemTest, OverlayFiles) {
  172. IntrusiveRefCntPtr<DummyFileSystem> Base(new DummyFileSystem());
  173. IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem());
  174. IntrusiveRefCntPtr<DummyFileSystem> Top(new DummyFileSystem());
  175. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  176. new vfs::OverlayFileSystem(Base));
  177. O->pushOverlay(Middle);
  178. O->pushOverlay(Top);
  179. ErrorOr<vfs::Status> Status1((std::error_code())),
  180. Status2((std::error_code())), Status3((std::error_code())),
  181. StatusB((std::error_code())), StatusM((std::error_code())),
  182. StatusT((std::error_code()));
  183. Base->addRegularFile("/foo");
  184. StatusB = Base->status("/foo");
  185. ASSERT_FALSE(StatusB.getError());
  186. Status1 = O->status("/foo");
  187. ASSERT_FALSE(Status1.getError());
  188. Middle->addRegularFile("/foo");
  189. StatusM = Middle->status("/foo");
  190. ASSERT_FALSE(StatusM.getError());
  191. Status2 = O->status("/foo");
  192. ASSERT_FALSE(Status2.getError());
  193. Top->addRegularFile("/foo");
  194. StatusT = Top->status("/foo");
  195. ASSERT_FALSE(StatusT.getError());
  196. Status3 = O->status("/foo");
  197. ASSERT_FALSE(Status3.getError());
  198. EXPECT_TRUE(Status1->equivalent(*StatusB));
  199. EXPECT_TRUE(Status2->equivalent(*StatusM));
  200. EXPECT_TRUE(Status3->equivalent(*StatusT));
  201. EXPECT_FALSE(Status1->equivalent(*Status2));
  202. EXPECT_FALSE(Status2->equivalent(*Status3));
  203. EXPECT_FALSE(Status1->equivalent(*Status3));
  204. }
  205. TEST(VirtualFileSystemTest, OverlayDirsNonMerged) {
  206. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  207. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  208. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  209. new vfs::OverlayFileSystem(Lower));
  210. O->pushOverlay(Upper);
  211. Lower->addDirectory("/lower-only");
  212. Upper->addDirectory("/upper-only");
  213. // non-merged paths should be the same
  214. ErrorOr<vfs::Status> Status1 = Lower->status("/lower-only");
  215. ASSERT_FALSE(Status1.getError());
  216. ErrorOr<vfs::Status> Status2 = O->status("/lower-only");
  217. ASSERT_FALSE(Status2.getError());
  218. EXPECT_TRUE(Status1->equivalent(*Status2));
  219. Status1 = Upper->status("/upper-only");
  220. ASSERT_FALSE(Status1.getError());
  221. Status2 = O->status("/upper-only");
  222. ASSERT_FALSE(Status2.getError());
  223. EXPECT_TRUE(Status1->equivalent(*Status2));
  224. }
  225. TEST(VirtualFileSystemTest, MergedDirPermissions) {
  226. // merged directories get the permissions of the upper dir
  227. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  228. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  229. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  230. new vfs::OverlayFileSystem(Lower));
  231. O->pushOverlay(Upper);
  232. ErrorOr<vfs::Status> Status((std::error_code()));
  233. Lower->addDirectory("/both", sys::fs::owner_read);
  234. Upper->addDirectory("/both", sys::fs::owner_all | sys::fs::group_read);
  235. Status = O->status("/both");
  236. ASSERT_FALSE(Status.getError());
  237. EXPECT_EQ(0740, Status->getPermissions());
  238. // permissions (as usual) are not recursively applied
  239. Lower->addRegularFile("/both/foo", sys::fs::owner_read);
  240. Upper->addRegularFile("/both/bar", sys::fs::owner_write);
  241. Status = O->status("/both/foo");
  242. ASSERT_FALSE( Status.getError());
  243. EXPECT_EQ(0400, Status->getPermissions());
  244. Status = O->status("/both/bar");
  245. ASSERT_FALSE(Status.getError());
  246. EXPECT_EQ(0200, Status->getPermissions());
  247. }
  248. namespace {
  249. struct ScopedDir {
  250. SmallString<128> Path;
  251. ScopedDir(const Twine &Name, bool Unique=false) {
  252. std::error_code EC;
  253. if (Unique) {
  254. EC = llvm::sys::fs::createUniqueDirectory(Name, Path);
  255. } else {
  256. Path = Name.str();
  257. EC = llvm::sys::fs::create_directory(Twine(Path));
  258. }
  259. if (EC)
  260. Path = "";
  261. EXPECT_FALSE(EC);
  262. }
  263. ~ScopedDir() {
  264. if (Path != "")
  265. EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
  266. }
  267. operator StringRef() { return Path.str(); }
  268. };
  269. } // end anonymous namespace
  270. TEST(VirtualFileSystemTest, BasicRealFSIteration) {
  271. ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/true);
  272. IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
  273. std::error_code EC;
  274. vfs::directory_iterator I = FS->dir_begin(Twine(TestDirectory), EC);
  275. ASSERT_FALSE(EC);
  276. EXPECT_EQ(vfs::directory_iterator(), I); // empty directory is empty
  277. ScopedDir _a(TestDirectory+"/a");
  278. ScopedDir _ab(TestDirectory+"/a/b");
  279. ScopedDir _c(TestDirectory+"/c");
  280. ScopedDir _cd(TestDirectory+"/c/d");
  281. I = FS->dir_begin(Twine(TestDirectory), EC);
  282. ASSERT_FALSE(EC);
  283. ASSERT_NE(vfs::directory_iterator(), I);
  284. // Check either a or c, since we can't rely on the iteration order.
  285. EXPECT_TRUE(I->getName().endswith("a") || I->getName().endswith("c"));
  286. I.increment(EC);
  287. ASSERT_FALSE(EC);
  288. ASSERT_NE(vfs::directory_iterator(), I);
  289. EXPECT_TRUE(I->getName().endswith("a") || I->getName().endswith("c"));
  290. I.increment(EC);
  291. EXPECT_EQ(vfs::directory_iterator(), I);
  292. }
  293. TEST(VirtualFileSystemTest, BasicRealFSRecursiveIteration) {
  294. ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/true);
  295. IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
  296. std::error_code EC;
  297. auto I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC);
  298. ASSERT_FALSE(EC);
  299. EXPECT_EQ(vfs::recursive_directory_iterator(), I); // empty directory is empty
  300. ScopedDir _a(TestDirectory+"/a");
  301. ScopedDir _ab(TestDirectory+"/a/b");
  302. ScopedDir _c(TestDirectory+"/c");
  303. ScopedDir _cd(TestDirectory+"/c/d");
  304. I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC);
  305. ASSERT_FALSE(EC);
  306. ASSERT_NE(vfs::recursive_directory_iterator(), I);
  307. std::vector<std::string> Contents;
  308. for (auto E = vfs::recursive_directory_iterator(); !EC && I != E;
  309. I.increment(EC)) {
  310. Contents.push_back(I->getName());
  311. }
  312. // Check contents, which may be in any order
  313. EXPECT_EQ(4U, Contents.size());
  314. int Counts[4] = { 0, 0, 0, 0 };
  315. for (const std::string &Name : Contents) {
  316. ASSERT_FALSE(Name.empty());
  317. int Index = Name[Name.size()-1] - 'a';
  318. ASSERT_TRUE(Index >= 0 && Index < 4);
  319. Counts[Index]++;
  320. }
  321. EXPECT_EQ(1, Counts[0]); // a
  322. EXPECT_EQ(1, Counts[1]); // b
  323. EXPECT_EQ(1, Counts[2]); // c
  324. EXPECT_EQ(1, Counts[3]); // d
  325. }
  326. template <typename DirIter>
  327. static void checkContents(DirIter I, ArrayRef<StringRef> ExpectedOut) {
  328. std::error_code EC;
  329. SmallVector<StringRef, 4> Expected(ExpectedOut.begin(), ExpectedOut.end());
  330. SmallVector<std::string, 4> InputToCheck;
  331. // Do not rely on iteration order to check for contents, sort both
  332. // content vectors before comparison.
  333. for (DirIter E; !EC && I != E; I.increment(EC))
  334. InputToCheck.push_back(I->getName());
  335. std::sort(InputToCheck.begin(), InputToCheck.end());
  336. std::sort(Expected.begin(), Expected.end());
  337. EXPECT_EQ(InputToCheck.size(), Expected.size());
  338. unsigned LastElt = std::min(InputToCheck.size(), Expected.size());
  339. for (unsigned Idx = 0; Idx != LastElt; ++Idx)
  340. EXPECT_EQ(Expected[Idx], StringRef(InputToCheck[Idx]));
  341. }
  342. TEST(VirtualFileSystemTest, OverlayIteration) {
  343. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  344. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  345. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  346. new vfs::OverlayFileSystem(Lower));
  347. O->pushOverlay(Upper);
  348. std::error_code EC;
  349. checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>());
  350. Lower->addRegularFile("/file1");
  351. checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>("/file1"));
  352. Upper->addRegularFile("/file2");
  353. checkContents(O->dir_begin("/", EC), {"/file2", "/file1"});
  354. Lower->addDirectory("/dir1");
  355. Lower->addRegularFile("/dir1/foo");
  356. Upper->addDirectory("/dir2");
  357. Upper->addRegularFile("/dir2/foo");
  358. checkContents(O->dir_begin("/dir2", EC), ArrayRef<StringRef>("/dir2/foo"));
  359. checkContents(O->dir_begin("/", EC), {"/dir2", "/file2", "/dir1", "/file1"});
  360. }
  361. TEST(VirtualFileSystemTest, OverlayRecursiveIteration) {
  362. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  363. IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem());
  364. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  365. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  366. new vfs::OverlayFileSystem(Lower));
  367. O->pushOverlay(Middle);
  368. O->pushOverlay(Upper);
  369. std::error_code EC;
  370. checkContents(vfs::recursive_directory_iterator(*O, "/", EC),
  371. ArrayRef<StringRef>());
  372. Lower->addRegularFile("/file1");
  373. checkContents(vfs::recursive_directory_iterator(*O, "/", EC),
  374. ArrayRef<StringRef>("/file1"));
  375. Upper->addDirectory("/dir");
  376. Upper->addRegularFile("/dir/file2");
  377. checkContents(vfs::recursive_directory_iterator(*O, "/", EC),
  378. {"/dir", "/dir/file2", "/file1"});
  379. Lower->addDirectory("/dir1");
  380. Lower->addRegularFile("/dir1/foo");
  381. Lower->addDirectory("/dir1/a");
  382. Lower->addRegularFile("/dir1/a/b");
  383. Middle->addDirectory("/a");
  384. Middle->addDirectory("/a/b");
  385. Middle->addDirectory("/a/b/c");
  386. Middle->addRegularFile("/a/b/c/d");
  387. Middle->addRegularFile("/hiddenByUp");
  388. Upper->addDirectory("/dir2");
  389. Upper->addRegularFile("/dir2/foo");
  390. Upper->addRegularFile("/hiddenByUp");
  391. checkContents(vfs::recursive_directory_iterator(*O, "/dir2", EC),
  392. ArrayRef<StringRef>("/dir2/foo"));
  393. checkContents(vfs::recursive_directory_iterator(*O, "/", EC),
  394. {"/dir", "/dir/file2", "/dir2", "/dir2/foo", "/hiddenByUp",
  395. "/a", "/a/b", "/a/b/c", "/a/b/c/d", "/dir1", "/dir1/a",
  396. "/dir1/a/b", "/dir1/foo", "/file1"});
  397. }
  398. TEST(VirtualFileSystemTest, ThreeLevelIteration) {
  399. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  400. IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem());
  401. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  402. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  403. new vfs::OverlayFileSystem(Lower));
  404. O->pushOverlay(Middle);
  405. O->pushOverlay(Upper);
  406. std::error_code EC;
  407. checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>());
  408. Middle->addRegularFile("/file2");
  409. checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>("/file2"));
  410. Lower->addRegularFile("/file1");
  411. Upper->addRegularFile("/file3");
  412. checkContents(O->dir_begin("/", EC), {"/file3", "/file2", "/file1"});
  413. }
  414. TEST(VirtualFileSystemTest, HiddenInIteration) {
  415. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  416. IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem());
  417. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  418. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  419. new vfs::OverlayFileSystem(Lower));
  420. O->pushOverlay(Middle);
  421. O->pushOverlay(Upper);
  422. std::error_code EC;
  423. Lower->addRegularFile("/onlyInLow", sys::fs::owner_read);
  424. Lower->addRegularFile("/hiddenByMid", sys::fs::owner_read);
  425. Lower->addRegularFile("/hiddenByUp", sys::fs::owner_read);
  426. Middle->addRegularFile("/onlyInMid", sys::fs::owner_write);
  427. Middle->addRegularFile("/hiddenByMid", sys::fs::owner_write);
  428. Middle->addRegularFile("/hiddenByUp", sys::fs::owner_write);
  429. Upper->addRegularFile("/onlyInUp", sys::fs::owner_all);
  430. Upper->addRegularFile("/hiddenByUp", sys::fs::owner_all);
  431. checkContents(
  432. O->dir_begin("/", EC),
  433. {"/hiddenByUp", "/onlyInUp", "/hiddenByMid", "/onlyInMid", "/onlyInLow"});
  434. // Make sure we get the top-most entry
  435. {
  436. std::error_code EC;
  437. vfs::directory_iterator I = O->dir_begin("/", EC), E;
  438. for ( ; !EC && I != E; I.increment(EC))
  439. if (I->getName() == "/hiddenByUp")
  440. break;
  441. ASSERT_NE(E, I);
  442. EXPECT_EQ(sys::fs::owner_all, I->getPermissions());
  443. }
  444. {
  445. std::error_code EC;
  446. vfs::directory_iterator I = O->dir_begin("/", EC), E;
  447. for ( ; !EC && I != E; I.increment(EC))
  448. if (I->getName() == "/hiddenByMid")
  449. break;
  450. ASSERT_NE(E, I);
  451. EXPECT_EQ(sys::fs::owner_write, I->getPermissions());
  452. }
  453. }
  454. class InMemoryFileSystemTest : public ::testing::Test {
  455. protected:
  456. clang::vfs::InMemoryFileSystem FS;
  457. clang::vfs::InMemoryFileSystem NormalizedFS;
  458. InMemoryFileSystemTest()
  459. : FS(/*UseNormalizedPaths=*/false),
  460. NormalizedFS(/*UseNormalizedPaths=*/true) {}
  461. };
  462. TEST_F(InMemoryFileSystemTest, IsEmpty) {
  463. auto Stat = FS.status("/a");
  464. ASSERT_EQ(Stat.getError(),errc::no_such_file_or_directory) << FS.toString();
  465. Stat = FS.status("/");
  466. ASSERT_EQ(Stat.getError(), errc::no_such_file_or_directory) << FS.toString();
  467. }
  468. TEST_F(InMemoryFileSystemTest, WindowsPath) {
  469. FS.addFile("c:/windows/system128/foo.cpp", 0, MemoryBuffer::getMemBuffer(""));
  470. auto Stat = FS.status("c:");
  471. #if !defined(_WIN32)
  472. ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
  473. #endif
  474. Stat = FS.status("c:/windows/system128/foo.cpp");
  475. ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
  476. FS.addFile("d:/windows/foo.cpp", 0, MemoryBuffer::getMemBuffer(""));
  477. Stat = FS.status("d:/windows/foo.cpp");
  478. ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
  479. }
  480. TEST_F(InMemoryFileSystemTest, OverlayFile) {
  481. FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a"));
  482. NormalizedFS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a"));
  483. auto Stat = FS.status("/");
  484. ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
  485. Stat = FS.status("/.");
  486. ASSERT_FALSE(Stat);
  487. Stat = NormalizedFS.status("/.");
  488. ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
  489. Stat = FS.status("/a");
  490. ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString();
  491. ASSERT_EQ("/a", Stat->getName());
  492. }
  493. TEST_F(InMemoryFileSystemTest, OverlayFileNoOwn) {
  494. auto Buf = MemoryBuffer::getMemBuffer("a");
  495. FS.addFileNoOwn("/a", 0, Buf.get());
  496. auto Stat = FS.status("/a");
  497. ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString();
  498. ASSERT_EQ("/a", Stat->getName());
  499. }
  500. TEST_F(InMemoryFileSystemTest, OpenFileForRead) {
  501. FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a"));
  502. FS.addFile("././c", 0, MemoryBuffer::getMemBuffer("c"));
  503. FS.addFile("./d/../d", 0, MemoryBuffer::getMemBuffer("d"));
  504. NormalizedFS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a"));
  505. NormalizedFS.addFile("././c", 0, MemoryBuffer::getMemBuffer("c"));
  506. NormalizedFS.addFile("./d/../d", 0, MemoryBuffer::getMemBuffer("d"));
  507. auto File = FS.openFileForRead("/a");
  508. ASSERT_EQ("a", (*(*File)->getBuffer("ignored"))->getBuffer());
  509. File = FS.openFileForRead("/a"); // Open again.
  510. ASSERT_EQ("a", (*(*File)->getBuffer("ignored"))->getBuffer());
  511. File = NormalizedFS.openFileForRead("/././a"); // Open again.
  512. ASSERT_EQ("a", (*(*File)->getBuffer("ignored"))->getBuffer());
  513. File = FS.openFileForRead("/");
  514. ASSERT_EQ(File.getError(), errc::invalid_argument) << FS.toString();
  515. File = FS.openFileForRead("/b");
  516. ASSERT_EQ(File.getError(), errc::no_such_file_or_directory) << FS.toString();
  517. File = FS.openFileForRead("./c");
  518. ASSERT_FALSE(File);
  519. File = FS.openFileForRead("e/../d");
  520. ASSERT_FALSE(File);
  521. File = NormalizedFS.openFileForRead("./c");
  522. ASSERT_EQ("c", (*(*File)->getBuffer("ignored"))->getBuffer());
  523. File = NormalizedFS.openFileForRead("e/../d");
  524. ASSERT_EQ("d", (*(*File)->getBuffer("ignored"))->getBuffer());
  525. }
  526. TEST_F(InMemoryFileSystemTest, DuplicatedFile) {
  527. ASSERT_TRUE(FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a")));
  528. ASSERT_FALSE(FS.addFile("/a/b", 0, MemoryBuffer::getMemBuffer("a")));
  529. ASSERT_TRUE(FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a")));
  530. ASSERT_FALSE(FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("b")));
  531. }
  532. TEST_F(InMemoryFileSystemTest, DirectoryIteration) {
  533. FS.addFile("/a", 0, MemoryBuffer::getMemBuffer(""));
  534. FS.addFile("/b/c", 0, MemoryBuffer::getMemBuffer(""));
  535. std::error_code EC;
  536. vfs::directory_iterator I = FS.dir_begin("/", EC);
  537. ASSERT_FALSE(EC);
  538. ASSERT_EQ("/a", I->getName());
  539. I.increment(EC);
  540. ASSERT_FALSE(EC);
  541. ASSERT_EQ("/b", I->getName());
  542. I.increment(EC);
  543. ASSERT_FALSE(EC);
  544. ASSERT_EQ(vfs::directory_iterator(), I);
  545. I = FS.dir_begin("/b", EC);
  546. ASSERT_FALSE(EC);
  547. ASSERT_EQ("/b/c", I->getName());
  548. I.increment(EC);
  549. ASSERT_FALSE(EC);
  550. ASSERT_EQ(vfs::directory_iterator(), I);
  551. }
  552. TEST_F(InMemoryFileSystemTest, WorkingDirectory) {
  553. FS.setCurrentWorkingDirectory("/b");
  554. FS.addFile("c", 0, MemoryBuffer::getMemBuffer(""));
  555. auto Stat = FS.status("/b/c");
  556. ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString();
  557. ASSERT_EQ("c", Stat->getName());
  558. ASSERT_EQ("/b", *FS.getCurrentWorkingDirectory());
  559. Stat = FS.status("c");
  560. ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString();
  561. auto ReplaceBackslashes = [](std::string S) {
  562. std::replace(S.begin(), S.end(), '\\', '/');
  563. return S;
  564. };
  565. NormalizedFS.setCurrentWorkingDirectory("/b/c");
  566. NormalizedFS.setCurrentWorkingDirectory(".");
  567. ASSERT_EQ("/b/c", ReplaceBackslashes(
  568. NormalizedFS.getCurrentWorkingDirectory().get()));
  569. NormalizedFS.setCurrentWorkingDirectory("..");
  570. ASSERT_EQ("/b", ReplaceBackslashes(
  571. NormalizedFS.getCurrentWorkingDirectory().get()));
  572. }
  573. // NOTE: in the tests below, we use '//root/' as our root directory, since it is
  574. // a legal *absolute* path on Windows as well as *nix.
  575. class VFSFromYAMLTest : public ::testing::Test {
  576. public:
  577. int NumDiagnostics;
  578. void SetUp() override { NumDiagnostics = 0; }
  579. static void CountingDiagHandler(const SMDiagnostic &, void *Context) {
  580. VFSFromYAMLTest *Test = static_cast<VFSFromYAMLTest *>(Context);
  581. ++Test->NumDiagnostics;
  582. }
  583. IntrusiveRefCntPtr<vfs::FileSystem>
  584. getFromYAMLRawString(StringRef Content,
  585. IntrusiveRefCntPtr<vfs::FileSystem> ExternalFS) {
  586. std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer(Content);
  587. return getVFSFromYAML(std::move(Buffer), CountingDiagHandler, "", this,
  588. ExternalFS);
  589. }
  590. IntrusiveRefCntPtr<vfs::FileSystem> getFromYAMLString(
  591. StringRef Content,
  592. IntrusiveRefCntPtr<vfs::FileSystem> ExternalFS = new DummyFileSystem()) {
  593. std::string VersionPlusContent("{\n 'version':0,\n");
  594. VersionPlusContent += Content.slice(Content.find('{') + 1, StringRef::npos);
  595. return getFromYAMLRawString(VersionPlusContent, ExternalFS);
  596. }
  597. };
  598. TEST_F(VFSFromYAMLTest, BasicVFSFromYAML) {
  599. IntrusiveRefCntPtr<vfs::FileSystem> FS;
  600. FS = getFromYAMLString("");
  601. EXPECT_EQ(nullptr, FS.get());
  602. FS = getFromYAMLString("[]");
  603. EXPECT_EQ(nullptr, FS.get());
  604. FS = getFromYAMLString("'string'");
  605. EXPECT_EQ(nullptr, FS.get());
  606. EXPECT_EQ(3, NumDiagnostics);
  607. }
  608. TEST_F(VFSFromYAMLTest, MappedFiles) {
  609. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  610. Lower->addRegularFile("//root/foo/bar/a");
  611. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  612. getFromYAMLString("{ 'roots': [\n"
  613. "{\n"
  614. " 'type': 'directory',\n"
  615. " 'name': '//root/',\n"
  616. " 'contents': [ {\n"
  617. " 'type': 'file',\n"
  618. " 'name': 'file1',\n"
  619. " 'external-contents': '//root/foo/bar/a'\n"
  620. " },\n"
  621. " {\n"
  622. " 'type': 'file',\n"
  623. " 'name': 'file2',\n"
  624. " 'external-contents': '//root/foo/b'\n"
  625. " }\n"
  626. " ]\n"
  627. "}\n"
  628. "]\n"
  629. "}",
  630. Lower);
  631. ASSERT_TRUE(FS.get() != nullptr);
  632. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  633. new vfs::OverlayFileSystem(Lower));
  634. O->pushOverlay(FS);
  635. // file
  636. ErrorOr<vfs::Status> S = O->status("//root/file1");
  637. ASSERT_FALSE(S.getError());
  638. EXPECT_EQ("//root/foo/bar/a", S->getName());
  639. EXPECT_TRUE(S->IsVFSMapped);
  640. ErrorOr<vfs::Status> SLower = O->status("//root/foo/bar/a");
  641. EXPECT_EQ("//root/foo/bar/a", SLower->getName());
  642. EXPECT_TRUE(S->equivalent(*SLower));
  643. EXPECT_FALSE(SLower->IsVFSMapped);
  644. // file after opening
  645. auto OpenedF = O->openFileForRead("//root/file1");
  646. ASSERT_FALSE(OpenedF.getError());
  647. auto OpenedS = (*OpenedF)->status();
  648. ASSERT_FALSE(OpenedS.getError());
  649. EXPECT_EQ("//root/foo/bar/a", OpenedS->getName());
  650. EXPECT_TRUE(OpenedS->IsVFSMapped);
  651. // directory
  652. S = O->status("//root/");
  653. ASSERT_FALSE(S.getError());
  654. EXPECT_TRUE(S->isDirectory());
  655. EXPECT_TRUE(S->equivalent(*O->status("//root/"))); // non-volatile UniqueID
  656. // broken mapping
  657. EXPECT_EQ(O->status("//root/file2").getError(),
  658. llvm::errc::no_such_file_or_directory);
  659. EXPECT_EQ(0, NumDiagnostics);
  660. }
  661. TEST_F(VFSFromYAMLTest, CaseInsensitive) {
  662. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  663. Lower->addRegularFile("//root/foo/bar/a");
  664. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  665. getFromYAMLString("{ 'case-sensitive': 'false',\n"
  666. " 'roots': [\n"
  667. "{\n"
  668. " 'type': 'directory',\n"
  669. " 'name': '//root/',\n"
  670. " 'contents': [ {\n"
  671. " 'type': 'file',\n"
  672. " 'name': 'XX',\n"
  673. " 'external-contents': '//root/foo/bar/a'\n"
  674. " }\n"
  675. " ]\n"
  676. "}]}",
  677. Lower);
  678. ASSERT_TRUE(FS.get() != nullptr);
  679. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  680. new vfs::OverlayFileSystem(Lower));
  681. O->pushOverlay(FS);
  682. ErrorOr<vfs::Status> S = O->status("//root/XX");
  683. ASSERT_FALSE(S.getError());
  684. ErrorOr<vfs::Status> SS = O->status("//root/xx");
  685. ASSERT_FALSE(SS.getError());
  686. EXPECT_TRUE(S->equivalent(*SS));
  687. SS = O->status("//root/xX");
  688. EXPECT_TRUE(S->equivalent(*SS));
  689. SS = O->status("//root/Xx");
  690. EXPECT_TRUE(S->equivalent(*SS));
  691. EXPECT_EQ(0, NumDiagnostics);
  692. }
  693. TEST_F(VFSFromYAMLTest, CaseSensitive) {
  694. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  695. Lower->addRegularFile("//root/foo/bar/a");
  696. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  697. getFromYAMLString("{ 'case-sensitive': 'true',\n"
  698. " 'roots': [\n"
  699. "{\n"
  700. " 'type': 'directory',\n"
  701. " 'name': '//root/',\n"
  702. " 'contents': [ {\n"
  703. " 'type': 'file',\n"
  704. " 'name': 'XX',\n"
  705. " 'external-contents': '//root/foo/bar/a'\n"
  706. " }\n"
  707. " ]\n"
  708. "}]}",
  709. Lower);
  710. ASSERT_TRUE(FS.get() != nullptr);
  711. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  712. new vfs::OverlayFileSystem(Lower));
  713. O->pushOverlay(FS);
  714. ErrorOr<vfs::Status> SS = O->status("//root/xx");
  715. EXPECT_EQ(SS.getError(), llvm::errc::no_such_file_or_directory);
  716. SS = O->status("//root/xX");
  717. EXPECT_EQ(SS.getError(), llvm::errc::no_such_file_or_directory);
  718. SS = O->status("//root/Xx");
  719. EXPECT_EQ(SS.getError(), llvm::errc::no_such_file_or_directory);
  720. EXPECT_EQ(0, NumDiagnostics);
  721. }
  722. TEST_F(VFSFromYAMLTest, IllegalVFSFile) {
  723. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  724. // invalid YAML at top-level
  725. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString("{]", Lower);
  726. EXPECT_EQ(nullptr, FS.get());
  727. // invalid YAML in roots
  728. FS = getFromYAMLString("{ 'roots':[}", Lower);
  729. // invalid YAML in directory
  730. FS = getFromYAMLString(
  731. "{ 'roots':[ { 'name': 'foo', 'type': 'directory', 'contents': [}",
  732. Lower);
  733. EXPECT_EQ(nullptr, FS.get());
  734. // invalid configuration
  735. FS = getFromYAMLString("{ 'knobular': 'true', 'roots':[] }", Lower);
  736. EXPECT_EQ(nullptr, FS.get());
  737. FS = getFromYAMLString("{ 'case-sensitive': 'maybe', 'roots':[] }", Lower);
  738. EXPECT_EQ(nullptr, FS.get());
  739. // invalid roots
  740. FS = getFromYAMLString("{ 'roots':'' }", Lower);
  741. EXPECT_EQ(nullptr, FS.get());
  742. FS = getFromYAMLString("{ 'roots':{} }", Lower);
  743. EXPECT_EQ(nullptr, FS.get());
  744. // invalid entries
  745. FS = getFromYAMLString(
  746. "{ 'roots':[ { 'type': 'other', 'name': 'me', 'contents': '' }", Lower);
  747. EXPECT_EQ(nullptr, FS.get());
  748. FS = getFromYAMLString("{ 'roots':[ { 'type': 'file', 'name': [], "
  749. "'external-contents': 'other' }",
  750. Lower);
  751. EXPECT_EQ(nullptr, FS.get());
  752. FS = getFromYAMLString(
  753. "{ 'roots':[ { 'type': 'file', 'name': 'me', 'external-contents': [] }",
  754. Lower);
  755. EXPECT_EQ(nullptr, FS.get());
  756. FS = getFromYAMLString(
  757. "{ 'roots':[ { 'type': 'file', 'name': 'me', 'external-contents': {} }",
  758. Lower);
  759. EXPECT_EQ(nullptr, FS.get());
  760. FS = getFromYAMLString(
  761. "{ 'roots':[ { 'type': 'directory', 'name': 'me', 'contents': {} }",
  762. Lower);
  763. EXPECT_EQ(nullptr, FS.get());
  764. FS = getFromYAMLString(
  765. "{ 'roots':[ { 'type': 'directory', 'name': 'me', 'contents': '' }",
  766. Lower);
  767. EXPECT_EQ(nullptr, FS.get());
  768. FS = getFromYAMLString(
  769. "{ 'roots':[ { 'thingy': 'directory', 'name': 'me', 'contents': [] }",
  770. Lower);
  771. EXPECT_EQ(nullptr, FS.get());
  772. // missing mandatory fields
  773. FS = getFromYAMLString("{ 'roots':[ { 'type': 'file', 'name': 'me' }", Lower);
  774. EXPECT_EQ(nullptr, FS.get());
  775. FS = getFromYAMLString(
  776. "{ 'roots':[ { 'type': 'file', 'external-contents': 'other' }", Lower);
  777. EXPECT_EQ(nullptr, FS.get());
  778. FS = getFromYAMLString("{ 'roots':[ { 'name': 'me', 'contents': [] }", Lower);
  779. EXPECT_EQ(nullptr, FS.get());
  780. // duplicate keys
  781. FS = getFromYAMLString("{ 'roots':[], 'roots':[] }", Lower);
  782. EXPECT_EQ(nullptr, FS.get());
  783. FS = getFromYAMLString(
  784. "{ 'case-sensitive':'true', 'case-sensitive':'true', 'roots':[] }",
  785. Lower);
  786. EXPECT_EQ(nullptr, FS.get());
  787. FS =
  788. getFromYAMLString("{ 'roots':[{'name':'me', 'name':'you', 'type':'file', "
  789. "'external-contents':'blah' } ] }",
  790. Lower);
  791. EXPECT_EQ(nullptr, FS.get());
  792. // missing version
  793. FS = getFromYAMLRawString("{ 'roots':[] }", Lower);
  794. EXPECT_EQ(nullptr, FS.get());
  795. // bad version number
  796. FS = getFromYAMLRawString("{ 'version':'foo', 'roots':[] }", Lower);
  797. EXPECT_EQ(nullptr, FS.get());
  798. FS = getFromYAMLRawString("{ 'version':-1, 'roots':[] }", Lower);
  799. EXPECT_EQ(nullptr, FS.get());
  800. FS = getFromYAMLRawString("{ 'version':100000, 'roots':[] }", Lower);
  801. EXPECT_EQ(nullptr, FS.get());
  802. EXPECT_EQ(24, NumDiagnostics);
  803. }
  804. TEST_F(VFSFromYAMLTest, UseExternalName) {
  805. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  806. Lower->addRegularFile("//root/external/file");
  807. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString(
  808. "{ 'roots': [\n"
  809. " { 'type': 'file', 'name': '//root/A',\n"
  810. " 'external-contents': '//root/external/file'\n"
  811. " },\n"
  812. " { 'type': 'file', 'name': '//root/B',\n"
  813. " 'use-external-name': true,\n"
  814. " 'external-contents': '//root/external/file'\n"
  815. " },\n"
  816. " { 'type': 'file', 'name': '//root/C',\n"
  817. " 'use-external-name': false,\n"
  818. " 'external-contents': '//root/external/file'\n"
  819. " }\n"
  820. "] }", Lower);
  821. ASSERT_TRUE(nullptr != FS.get());
  822. // default true
  823. EXPECT_EQ("//root/external/file", FS->status("//root/A")->getName());
  824. // explicit
  825. EXPECT_EQ("//root/external/file", FS->status("//root/B")->getName());
  826. EXPECT_EQ("//root/C", FS->status("//root/C")->getName());
  827. // global configuration
  828. FS = getFromYAMLString(
  829. "{ 'use-external-names': false,\n"
  830. " 'roots': [\n"
  831. " { 'type': 'file', 'name': '//root/A',\n"
  832. " 'external-contents': '//root/external/file'\n"
  833. " },\n"
  834. " { 'type': 'file', 'name': '//root/B',\n"
  835. " 'use-external-name': true,\n"
  836. " 'external-contents': '//root/external/file'\n"
  837. " },\n"
  838. " { 'type': 'file', 'name': '//root/C',\n"
  839. " 'use-external-name': false,\n"
  840. " 'external-contents': '//root/external/file'\n"
  841. " }\n"
  842. "] }", Lower);
  843. ASSERT_TRUE(nullptr != FS.get());
  844. // default
  845. EXPECT_EQ("//root/A", FS->status("//root/A")->getName());
  846. // explicit
  847. EXPECT_EQ("//root/external/file", FS->status("//root/B")->getName());
  848. EXPECT_EQ("//root/C", FS->status("//root/C")->getName());
  849. }
  850. TEST_F(VFSFromYAMLTest, MultiComponentPath) {
  851. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  852. Lower->addRegularFile("//root/other");
  853. // file in roots
  854. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString(
  855. "{ 'roots': [\n"
  856. " { 'type': 'file', 'name': '//root/path/to/file',\n"
  857. " 'external-contents': '//root/other' }]\n"
  858. "}", Lower);
  859. ASSERT_TRUE(nullptr != FS.get());
  860. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  861. EXPECT_FALSE(FS->status("//root/path/to").getError());
  862. EXPECT_FALSE(FS->status("//root/path").getError());
  863. EXPECT_FALSE(FS->status("//root/").getError());
  864. // at the start
  865. FS = getFromYAMLString(
  866. "{ 'roots': [\n"
  867. " { 'type': 'directory', 'name': '//root/path/to',\n"
  868. " 'contents': [ { 'type': 'file', 'name': 'file',\n"
  869. " 'external-contents': '//root/other' }]}]\n"
  870. "}", Lower);
  871. ASSERT_TRUE(nullptr != FS.get());
  872. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  873. EXPECT_FALSE(FS->status("//root/path/to").getError());
  874. EXPECT_FALSE(FS->status("//root/path").getError());
  875. EXPECT_FALSE(FS->status("//root/").getError());
  876. // at the end
  877. FS = getFromYAMLString(
  878. "{ 'roots': [\n"
  879. " { 'type': 'directory', 'name': '//root/',\n"
  880. " 'contents': [ { 'type': 'file', 'name': 'path/to/file',\n"
  881. " 'external-contents': '//root/other' }]}]\n"
  882. "}", Lower);
  883. ASSERT_TRUE(nullptr != FS.get());
  884. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  885. EXPECT_FALSE(FS->status("//root/path/to").getError());
  886. EXPECT_FALSE(FS->status("//root/path").getError());
  887. EXPECT_FALSE(FS->status("//root/").getError());
  888. }
  889. TEST_F(VFSFromYAMLTest, TrailingSlashes) {
  890. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  891. Lower->addRegularFile("//root/other");
  892. // file in roots
  893. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString(
  894. "{ 'roots': [\n"
  895. " { 'type': 'directory', 'name': '//root/path/to////',\n"
  896. " 'contents': [ { 'type': 'file', 'name': 'file',\n"
  897. " 'external-contents': '//root/other' }]}]\n"
  898. "}", Lower);
  899. ASSERT_TRUE(nullptr != FS.get());
  900. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  901. EXPECT_FALSE(FS->status("//root/path/to").getError());
  902. EXPECT_FALSE(FS->status("//root/path").getError());
  903. EXPECT_FALSE(FS->status("//root/").getError());
  904. }
  905. TEST_F(VFSFromYAMLTest, DirectoryIteration) {
  906. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  907. Lower->addDirectory("//root/");
  908. Lower->addDirectory("//root/foo");
  909. Lower->addDirectory("//root/foo/bar");
  910. Lower->addRegularFile("//root/foo/bar/a");
  911. Lower->addRegularFile("//root/foo/bar/b");
  912. Lower->addRegularFile("//root/file3");
  913. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  914. getFromYAMLString("{ 'use-external-names': false,\n"
  915. " 'roots': [\n"
  916. "{\n"
  917. " 'type': 'directory',\n"
  918. " 'name': '//root/',\n"
  919. " 'contents': [ {\n"
  920. " 'type': 'file',\n"
  921. " 'name': 'file1',\n"
  922. " 'external-contents': '//root/foo/bar/a'\n"
  923. " },\n"
  924. " {\n"
  925. " 'type': 'file',\n"
  926. " 'name': 'file2',\n"
  927. " 'external-contents': '//root/foo/bar/b'\n"
  928. " }\n"
  929. " ]\n"
  930. "}\n"
  931. "]\n"
  932. "}",
  933. Lower);
  934. ASSERT_TRUE(FS.get() != nullptr);
  935. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  936. new vfs::OverlayFileSystem(Lower));
  937. O->pushOverlay(FS);
  938. std::error_code EC;
  939. checkContents(O->dir_begin("//root/", EC),
  940. {"//root/file1", "//root/file2", "//root/file3", "//root/foo"});
  941. checkContents(O->dir_begin("//root/foo/bar", EC),
  942. {"//root/foo/bar/a", "//root/foo/bar/b"});
  943. }