VirtualFileSystemTest.cpp 44 KB

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