VirtualFileSystemTest.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  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. }
  272. operator StringRef() { return Path.str(); }
  273. };
  274. struct ScopedLink {
  275. SmallString<128> Path;
  276. ScopedLink(const Twine &To, const Twine &From) {
  277. Path = From.str();
  278. std::error_code EC = sys::fs::create_link(To, From);
  279. if (EC)
  280. Path = "";
  281. EXPECT_FALSE(EC);
  282. }
  283. ~ScopedLink() {
  284. if (Path != "") {
  285. EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
  286. }
  287. }
  288. operator StringRef() { return Path.str(); }
  289. };
  290. } // end anonymous namespace
  291. TEST(VirtualFileSystemTest, BasicRealFSIteration) {
  292. ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/true);
  293. IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
  294. std::error_code EC;
  295. vfs::directory_iterator I = FS->dir_begin(Twine(TestDirectory), EC);
  296. ASSERT_FALSE(EC);
  297. EXPECT_EQ(vfs::directory_iterator(), I); // empty directory is empty
  298. ScopedDir _a(TestDirectory+"/a");
  299. ScopedDir _ab(TestDirectory+"/a/b");
  300. ScopedDir _c(TestDirectory+"/c");
  301. ScopedDir _cd(TestDirectory+"/c/d");
  302. I = FS->dir_begin(Twine(TestDirectory), EC);
  303. ASSERT_FALSE(EC);
  304. ASSERT_NE(vfs::directory_iterator(), I);
  305. // Check either a or c, since we can't rely on the iteration order.
  306. EXPECT_TRUE(I->getName().endswith("a") || I->getName().endswith("c"));
  307. I.increment(EC);
  308. ASSERT_FALSE(EC);
  309. ASSERT_NE(vfs::directory_iterator(), I);
  310. EXPECT_TRUE(I->getName().endswith("a") || I->getName().endswith("c"));
  311. I.increment(EC);
  312. EXPECT_EQ(vfs::directory_iterator(), I);
  313. }
  314. #ifdef LLVM_ON_UNIX
  315. TEST(VirtualFileSystemTest, BrokenSymlinkRealFSIteration) {
  316. ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
  317. IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
  318. ScopedLink _a("no_such_file", TestDirectory + "/a");
  319. ScopedDir _b(TestDirectory + "/b");
  320. ScopedLink _c("no_such_file", TestDirectory + "/c");
  321. std::error_code EC;
  322. for (vfs::directory_iterator I = FS->dir_begin(Twine(TestDirectory), EC), E;
  323. I != E; I.increment(EC)) {
  324. // Skip broken symlinks.
  325. auto EC2 = std::make_error_code(std::errc::no_such_file_or_directory);
  326. if (EC == EC2) {
  327. EC.clear();
  328. continue;
  329. }
  330. // For bot debugging.
  331. if (EC) {
  332. outs() << "Error code found:\n"
  333. << "EC value: " << EC.value() << "\n"
  334. << "EC category: " << EC.category().name()
  335. << "EC message: " << EC.message() << "\n";
  336. outs() << "Error code tested for:\n"
  337. << "EC value: " << EC2.value() << "\n"
  338. << "EC category: " << EC2.category().name()
  339. << "EC message: " << EC2.message() << "\n";
  340. }
  341. ASSERT_FALSE(EC);
  342. EXPECT_TRUE(I->getName() == _b);
  343. }
  344. }
  345. #endif
  346. TEST(VirtualFileSystemTest, BasicRealFSRecursiveIteration) {
  347. ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/true);
  348. IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
  349. std::error_code EC;
  350. auto I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC);
  351. ASSERT_FALSE(EC);
  352. EXPECT_EQ(vfs::recursive_directory_iterator(), I); // empty directory is empty
  353. ScopedDir _a(TestDirectory+"/a");
  354. ScopedDir _ab(TestDirectory+"/a/b");
  355. ScopedDir _c(TestDirectory+"/c");
  356. ScopedDir _cd(TestDirectory+"/c/d");
  357. I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC);
  358. ASSERT_FALSE(EC);
  359. ASSERT_NE(vfs::recursive_directory_iterator(), I);
  360. std::vector<std::string> Contents;
  361. for (auto E = vfs::recursive_directory_iterator(); !EC && I != E;
  362. I.increment(EC)) {
  363. Contents.push_back(I->getName());
  364. }
  365. // Check contents, which may be in any order
  366. EXPECT_EQ(4U, Contents.size());
  367. int Counts[4] = { 0, 0, 0, 0 };
  368. for (const std::string &Name : Contents) {
  369. ASSERT_FALSE(Name.empty());
  370. int Index = Name[Name.size()-1] - 'a';
  371. ASSERT_TRUE(Index >= 0 && Index < 4);
  372. Counts[Index]++;
  373. }
  374. EXPECT_EQ(1, Counts[0]); // a
  375. EXPECT_EQ(1, Counts[1]); // b
  376. EXPECT_EQ(1, Counts[2]); // c
  377. EXPECT_EQ(1, Counts[3]); // d
  378. }
  379. #ifdef LLVM_ON_UNIX
  380. TEST(VirtualFileSystemTest, BrokenSymlinkRealFSRecursiveIteration) {
  381. ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
  382. IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
  383. ScopedLink _a("no_such_file", TestDirectory + "/a");
  384. ScopedDir _b(TestDirectory + "/b");
  385. ScopedLink _ba("no_such_file", TestDirectory + "/b/a");
  386. ScopedDir _bb(TestDirectory + "/b/b");
  387. ScopedLink _bc("no_such_file", TestDirectory + "/b/c");
  388. ScopedLink _c("no_such_file", TestDirectory + "/c");
  389. ScopedDir _d(TestDirectory + "/d");
  390. ScopedDir _dd(TestDirectory + "/d/d");
  391. ScopedDir _ddd(TestDirectory + "/d/d/d");
  392. ScopedLink _e("no_such_file", TestDirectory + "/e");
  393. std::vector<StringRef> Expected = {_b, _bb, _d, _dd, _ddd};
  394. std::vector<std::string> Contents;
  395. std::error_code EC;
  396. for (vfs::recursive_directory_iterator I(*FS, Twine(TestDirectory), EC), E;
  397. I != E; I.increment(EC)) {
  398. // Skip broken symlinks.
  399. auto EC2 = std::make_error_code(std::errc::no_such_file_or_directory);
  400. if (EC == EC2) {
  401. EC.clear();
  402. continue;
  403. }
  404. // For bot debugging.
  405. if (EC) {
  406. outs() << "Error code found:\n"
  407. << "EC value: " << EC.value() << "\n"
  408. << "EC category: " << EC.category().name()
  409. << "EC message: " << EC.message() << "\n";
  410. outs() << "Error code tested for:\n"
  411. << "EC value: " << EC2.value() << "\n"
  412. << "EC category: " << EC2.category().name()
  413. << "EC message: " << EC2.message() << "\n";
  414. }
  415. ASSERT_FALSE(EC);
  416. Contents.push_back(I->getName());
  417. }
  418. // Check sorted contents.
  419. std::sort(Contents.begin(), Contents.end());
  420. EXPECT_EQ(Expected.size(), Contents.size());
  421. EXPECT_TRUE(std::equal(Contents.begin(), Contents.end(), Expected.begin()));
  422. }
  423. #endif
  424. template <typename DirIter>
  425. static void checkContents(DirIter I, ArrayRef<StringRef> ExpectedOut) {
  426. std::error_code EC;
  427. SmallVector<StringRef, 4> Expected(ExpectedOut.begin(), ExpectedOut.end());
  428. SmallVector<std::string, 4> InputToCheck;
  429. // Do not rely on iteration order to check for contents, sort both
  430. // content vectors before comparison.
  431. for (DirIter E; !EC && I != E; I.increment(EC))
  432. InputToCheck.push_back(I->getName());
  433. std::sort(InputToCheck.begin(), InputToCheck.end());
  434. std::sort(Expected.begin(), Expected.end());
  435. EXPECT_EQ(InputToCheck.size(), Expected.size());
  436. unsigned LastElt = std::min(InputToCheck.size(), Expected.size());
  437. for (unsigned Idx = 0; Idx != LastElt; ++Idx)
  438. EXPECT_EQ(StringRef(InputToCheck[Idx]), Expected[Idx]);
  439. }
  440. TEST(VirtualFileSystemTest, OverlayIteration) {
  441. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  442. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  443. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  444. new vfs::OverlayFileSystem(Lower));
  445. O->pushOverlay(Upper);
  446. std::error_code EC;
  447. checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>());
  448. Lower->addRegularFile("/file1");
  449. checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>("/file1"));
  450. Upper->addRegularFile("/file2");
  451. checkContents(O->dir_begin("/", EC), {"/file2", "/file1"});
  452. Lower->addDirectory("/dir1");
  453. Lower->addRegularFile("/dir1/foo");
  454. Upper->addDirectory("/dir2");
  455. Upper->addRegularFile("/dir2/foo");
  456. checkContents(O->dir_begin("/dir2", EC), ArrayRef<StringRef>("/dir2/foo"));
  457. checkContents(O->dir_begin("/", EC), {"/dir2", "/file2", "/dir1", "/file1"});
  458. }
  459. TEST(VirtualFileSystemTest, OverlayRecursiveIteration) {
  460. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  461. IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem());
  462. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  463. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  464. new vfs::OverlayFileSystem(Lower));
  465. O->pushOverlay(Middle);
  466. O->pushOverlay(Upper);
  467. std::error_code EC;
  468. checkContents(vfs::recursive_directory_iterator(*O, "/", EC),
  469. ArrayRef<StringRef>());
  470. Lower->addRegularFile("/file1");
  471. checkContents(vfs::recursive_directory_iterator(*O, "/", EC),
  472. ArrayRef<StringRef>("/file1"));
  473. Upper->addDirectory("/dir");
  474. Upper->addRegularFile("/dir/file2");
  475. checkContents(vfs::recursive_directory_iterator(*O, "/", EC),
  476. {"/dir", "/dir/file2", "/file1"});
  477. Lower->addDirectory("/dir1");
  478. Lower->addRegularFile("/dir1/foo");
  479. Lower->addDirectory("/dir1/a");
  480. Lower->addRegularFile("/dir1/a/b");
  481. Middle->addDirectory("/a");
  482. Middle->addDirectory("/a/b");
  483. Middle->addDirectory("/a/b/c");
  484. Middle->addRegularFile("/a/b/c/d");
  485. Middle->addRegularFile("/hiddenByUp");
  486. Upper->addDirectory("/dir2");
  487. Upper->addRegularFile("/dir2/foo");
  488. Upper->addRegularFile("/hiddenByUp");
  489. checkContents(vfs::recursive_directory_iterator(*O, "/dir2", EC),
  490. ArrayRef<StringRef>("/dir2/foo"));
  491. checkContents(vfs::recursive_directory_iterator(*O, "/", EC),
  492. {"/dir", "/dir/file2", "/dir2", "/dir2/foo", "/hiddenByUp",
  493. "/a", "/a/b", "/a/b/c", "/a/b/c/d", "/dir1", "/dir1/a",
  494. "/dir1/a/b", "/dir1/foo", "/file1"});
  495. }
  496. TEST(VirtualFileSystemTest, ThreeLevelIteration) {
  497. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  498. IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem());
  499. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  500. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  501. new vfs::OverlayFileSystem(Lower));
  502. O->pushOverlay(Middle);
  503. O->pushOverlay(Upper);
  504. std::error_code EC;
  505. checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>());
  506. Middle->addRegularFile("/file2");
  507. checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>("/file2"));
  508. Lower->addRegularFile("/file1");
  509. Upper->addRegularFile("/file3");
  510. checkContents(O->dir_begin("/", EC), {"/file3", "/file2", "/file1"});
  511. }
  512. TEST(VirtualFileSystemTest, HiddenInIteration) {
  513. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  514. IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem());
  515. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  516. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  517. new vfs::OverlayFileSystem(Lower));
  518. O->pushOverlay(Middle);
  519. O->pushOverlay(Upper);
  520. std::error_code EC;
  521. Lower->addRegularFile("/onlyInLow", sys::fs::owner_read);
  522. Lower->addRegularFile("/hiddenByMid", sys::fs::owner_read);
  523. Lower->addRegularFile("/hiddenByUp", sys::fs::owner_read);
  524. Middle->addRegularFile("/onlyInMid", sys::fs::owner_write);
  525. Middle->addRegularFile("/hiddenByMid", sys::fs::owner_write);
  526. Middle->addRegularFile("/hiddenByUp", sys::fs::owner_write);
  527. Upper->addRegularFile("/onlyInUp", sys::fs::owner_all);
  528. Upper->addRegularFile("/hiddenByUp", sys::fs::owner_all);
  529. checkContents(
  530. O->dir_begin("/", EC),
  531. {"/hiddenByUp", "/onlyInUp", "/hiddenByMid", "/onlyInMid", "/onlyInLow"});
  532. // Make sure we get the top-most entry
  533. {
  534. std::error_code EC;
  535. vfs::directory_iterator I = O->dir_begin("/", EC), E;
  536. for ( ; !EC && I != E; I.increment(EC))
  537. if (I->getName() == "/hiddenByUp")
  538. break;
  539. ASSERT_NE(E, I);
  540. EXPECT_EQ(sys::fs::owner_all, I->getPermissions());
  541. }
  542. {
  543. std::error_code EC;
  544. vfs::directory_iterator I = O->dir_begin("/", EC), E;
  545. for ( ; !EC && I != E; I.increment(EC))
  546. if (I->getName() == "/hiddenByMid")
  547. break;
  548. ASSERT_NE(E, I);
  549. EXPECT_EQ(sys::fs::owner_write, I->getPermissions());
  550. }
  551. }
  552. class InMemoryFileSystemTest : public ::testing::Test {
  553. protected:
  554. clang::vfs::InMemoryFileSystem FS;
  555. clang::vfs::InMemoryFileSystem NormalizedFS;
  556. InMemoryFileSystemTest()
  557. : FS(/*UseNormalizedPaths=*/false),
  558. NormalizedFS(/*UseNormalizedPaths=*/true) {}
  559. };
  560. TEST_F(InMemoryFileSystemTest, IsEmpty) {
  561. auto Stat = FS.status("/a");
  562. ASSERT_EQ(Stat.getError(),errc::no_such_file_or_directory) << FS.toString();
  563. Stat = FS.status("/");
  564. ASSERT_EQ(Stat.getError(), errc::no_such_file_or_directory) << FS.toString();
  565. }
  566. TEST_F(InMemoryFileSystemTest, WindowsPath) {
  567. FS.addFile("c:/windows/system128/foo.cpp", 0, MemoryBuffer::getMemBuffer(""));
  568. auto Stat = FS.status("c:");
  569. #if !defined(_WIN32)
  570. ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
  571. #endif
  572. Stat = FS.status("c:/windows/system128/foo.cpp");
  573. ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
  574. FS.addFile("d:/windows/foo.cpp", 0, MemoryBuffer::getMemBuffer(""));
  575. Stat = FS.status("d:/windows/foo.cpp");
  576. ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
  577. }
  578. TEST_F(InMemoryFileSystemTest, OverlayFile) {
  579. FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a"));
  580. NormalizedFS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a"));
  581. auto Stat = FS.status("/");
  582. ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
  583. Stat = FS.status("/.");
  584. ASSERT_FALSE(Stat);
  585. Stat = NormalizedFS.status("/.");
  586. ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
  587. Stat = FS.status("/a");
  588. ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString();
  589. ASSERT_EQ("/a", Stat->getName());
  590. }
  591. TEST_F(InMemoryFileSystemTest, OverlayFileNoOwn) {
  592. auto Buf = MemoryBuffer::getMemBuffer("a");
  593. FS.addFileNoOwn("/a", 0, Buf.get());
  594. auto Stat = FS.status("/a");
  595. ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString();
  596. ASSERT_EQ("/a", Stat->getName());
  597. }
  598. TEST_F(InMemoryFileSystemTest, OpenFileForRead) {
  599. FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a"));
  600. FS.addFile("././c", 0, MemoryBuffer::getMemBuffer("c"));
  601. FS.addFile("./d/../d", 0, MemoryBuffer::getMemBuffer("d"));
  602. NormalizedFS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a"));
  603. NormalizedFS.addFile("././c", 0, MemoryBuffer::getMemBuffer("c"));
  604. NormalizedFS.addFile("./d/../d", 0, MemoryBuffer::getMemBuffer("d"));
  605. auto File = FS.openFileForRead("/a");
  606. ASSERT_EQ("a", (*(*File)->getBuffer("ignored"))->getBuffer());
  607. File = FS.openFileForRead("/a"); // Open again.
  608. ASSERT_EQ("a", (*(*File)->getBuffer("ignored"))->getBuffer());
  609. File = NormalizedFS.openFileForRead("/././a"); // Open again.
  610. ASSERT_EQ("a", (*(*File)->getBuffer("ignored"))->getBuffer());
  611. File = FS.openFileForRead("/");
  612. ASSERT_EQ(File.getError(), errc::invalid_argument) << FS.toString();
  613. File = FS.openFileForRead("/b");
  614. ASSERT_EQ(File.getError(), errc::no_such_file_or_directory) << FS.toString();
  615. File = FS.openFileForRead("./c");
  616. ASSERT_FALSE(File);
  617. File = FS.openFileForRead("e/../d");
  618. ASSERT_FALSE(File);
  619. File = NormalizedFS.openFileForRead("./c");
  620. ASSERT_EQ("c", (*(*File)->getBuffer("ignored"))->getBuffer());
  621. File = NormalizedFS.openFileForRead("e/../d");
  622. ASSERT_EQ("d", (*(*File)->getBuffer("ignored"))->getBuffer());
  623. }
  624. TEST_F(InMemoryFileSystemTest, DuplicatedFile) {
  625. ASSERT_TRUE(FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a")));
  626. ASSERT_FALSE(FS.addFile("/a/b", 0, MemoryBuffer::getMemBuffer("a")));
  627. ASSERT_TRUE(FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a")));
  628. ASSERT_FALSE(FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("b")));
  629. }
  630. TEST_F(InMemoryFileSystemTest, DirectoryIteration) {
  631. FS.addFile("/a", 0, MemoryBuffer::getMemBuffer(""));
  632. FS.addFile("/b/c", 0, MemoryBuffer::getMemBuffer(""));
  633. std::error_code EC;
  634. vfs::directory_iterator I = FS.dir_begin("/", EC);
  635. ASSERT_FALSE(EC);
  636. ASSERT_EQ("/a", I->getName());
  637. I.increment(EC);
  638. ASSERT_FALSE(EC);
  639. ASSERT_EQ("/b", I->getName());
  640. I.increment(EC);
  641. ASSERT_FALSE(EC);
  642. ASSERT_EQ(vfs::directory_iterator(), I);
  643. I = FS.dir_begin("/b", EC);
  644. ASSERT_FALSE(EC);
  645. ASSERT_EQ("/b/c", I->getName());
  646. I.increment(EC);
  647. ASSERT_FALSE(EC);
  648. ASSERT_EQ(vfs::directory_iterator(), I);
  649. }
  650. TEST_F(InMemoryFileSystemTest, WorkingDirectory) {
  651. FS.setCurrentWorkingDirectory("/b");
  652. FS.addFile("c", 0, MemoryBuffer::getMemBuffer(""));
  653. auto Stat = FS.status("/b/c");
  654. ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString();
  655. ASSERT_EQ("c", Stat->getName());
  656. ASSERT_EQ("/b", *FS.getCurrentWorkingDirectory());
  657. Stat = FS.status("c");
  658. ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString();
  659. auto ReplaceBackslashes = [](std::string S) {
  660. std::replace(S.begin(), S.end(), '\\', '/');
  661. return S;
  662. };
  663. NormalizedFS.setCurrentWorkingDirectory("/b/c");
  664. NormalizedFS.setCurrentWorkingDirectory(".");
  665. ASSERT_EQ("/b/c", ReplaceBackslashes(
  666. NormalizedFS.getCurrentWorkingDirectory().get()));
  667. NormalizedFS.setCurrentWorkingDirectory("..");
  668. ASSERT_EQ("/b", ReplaceBackslashes(
  669. NormalizedFS.getCurrentWorkingDirectory().get()));
  670. }
  671. // NOTE: in the tests below, we use '//root/' as our root directory, since it is
  672. // a legal *absolute* path on Windows as well as *nix.
  673. class VFSFromYAMLTest : public ::testing::Test {
  674. public:
  675. int NumDiagnostics;
  676. void SetUp() override { NumDiagnostics = 0; }
  677. static void CountingDiagHandler(const SMDiagnostic &, void *Context) {
  678. VFSFromYAMLTest *Test = static_cast<VFSFromYAMLTest *>(Context);
  679. ++Test->NumDiagnostics;
  680. }
  681. IntrusiveRefCntPtr<vfs::FileSystem>
  682. getFromYAMLRawString(StringRef Content,
  683. IntrusiveRefCntPtr<vfs::FileSystem> ExternalFS) {
  684. std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer(Content);
  685. return getVFSFromYAML(std::move(Buffer), CountingDiagHandler, "", this,
  686. ExternalFS);
  687. }
  688. IntrusiveRefCntPtr<vfs::FileSystem> getFromYAMLString(
  689. StringRef Content,
  690. IntrusiveRefCntPtr<vfs::FileSystem> ExternalFS = new DummyFileSystem()) {
  691. std::string VersionPlusContent("{\n 'version':0,\n");
  692. VersionPlusContent += Content.slice(Content.find('{') + 1, StringRef::npos);
  693. return getFromYAMLRawString(VersionPlusContent, ExternalFS);
  694. }
  695. // This is intended as a "XFAIL" for windows hosts.
  696. bool supportsSameDirMultipleYAMLEntries() {
  697. Triple Host(Triple::normalize(sys::getProcessTriple()));
  698. return !Host.isOSWindows();
  699. }
  700. };
  701. TEST_F(VFSFromYAMLTest, BasicVFSFromYAML) {
  702. IntrusiveRefCntPtr<vfs::FileSystem> FS;
  703. FS = getFromYAMLString("");
  704. EXPECT_EQ(nullptr, FS.get());
  705. FS = getFromYAMLString("[]");
  706. EXPECT_EQ(nullptr, FS.get());
  707. FS = getFromYAMLString("'string'");
  708. EXPECT_EQ(nullptr, FS.get());
  709. EXPECT_EQ(3, NumDiagnostics);
  710. }
  711. TEST_F(VFSFromYAMLTest, MappedFiles) {
  712. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  713. Lower->addRegularFile("//root/foo/bar/a");
  714. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  715. getFromYAMLString("{ 'roots': [\n"
  716. "{\n"
  717. " 'type': 'directory',\n"
  718. " 'name': '//root/',\n"
  719. " 'contents': [ {\n"
  720. " 'type': 'file',\n"
  721. " 'name': 'file1',\n"
  722. " 'external-contents': '//root/foo/bar/a'\n"
  723. " },\n"
  724. " {\n"
  725. " 'type': 'file',\n"
  726. " 'name': 'file2',\n"
  727. " 'external-contents': '//root/foo/b'\n"
  728. " }\n"
  729. " ]\n"
  730. "}\n"
  731. "]\n"
  732. "}",
  733. Lower);
  734. ASSERT_TRUE(FS.get() != nullptr);
  735. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  736. new vfs::OverlayFileSystem(Lower));
  737. O->pushOverlay(FS);
  738. // file
  739. ErrorOr<vfs::Status> S = O->status("//root/file1");
  740. ASSERT_FALSE(S.getError());
  741. EXPECT_EQ("//root/foo/bar/a", S->getName());
  742. EXPECT_TRUE(S->IsVFSMapped);
  743. ErrorOr<vfs::Status> SLower = O->status("//root/foo/bar/a");
  744. EXPECT_EQ("//root/foo/bar/a", SLower->getName());
  745. EXPECT_TRUE(S->equivalent(*SLower));
  746. EXPECT_FALSE(SLower->IsVFSMapped);
  747. // file after opening
  748. auto OpenedF = O->openFileForRead("//root/file1");
  749. ASSERT_FALSE(OpenedF.getError());
  750. auto OpenedS = (*OpenedF)->status();
  751. ASSERT_FALSE(OpenedS.getError());
  752. EXPECT_EQ("//root/foo/bar/a", OpenedS->getName());
  753. EXPECT_TRUE(OpenedS->IsVFSMapped);
  754. // directory
  755. S = O->status("//root/");
  756. ASSERT_FALSE(S.getError());
  757. EXPECT_TRUE(S->isDirectory());
  758. EXPECT_TRUE(S->equivalent(*O->status("//root/"))); // non-volatile UniqueID
  759. // broken mapping
  760. EXPECT_EQ(O->status("//root/file2").getError(),
  761. llvm::errc::no_such_file_or_directory);
  762. EXPECT_EQ(0, NumDiagnostics);
  763. }
  764. TEST_F(VFSFromYAMLTest, CaseInsensitive) {
  765. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  766. Lower->addRegularFile("//root/foo/bar/a");
  767. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  768. getFromYAMLString("{ 'case-sensitive': 'false',\n"
  769. " 'roots': [\n"
  770. "{\n"
  771. " 'type': 'directory',\n"
  772. " 'name': '//root/',\n"
  773. " 'contents': [ {\n"
  774. " 'type': 'file',\n"
  775. " 'name': 'XX',\n"
  776. " 'external-contents': '//root/foo/bar/a'\n"
  777. " }\n"
  778. " ]\n"
  779. "}]}",
  780. Lower);
  781. ASSERT_TRUE(FS.get() != nullptr);
  782. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  783. new vfs::OverlayFileSystem(Lower));
  784. O->pushOverlay(FS);
  785. ErrorOr<vfs::Status> S = O->status("//root/XX");
  786. ASSERT_FALSE(S.getError());
  787. ErrorOr<vfs::Status> SS = O->status("//root/xx");
  788. ASSERT_FALSE(SS.getError());
  789. EXPECT_TRUE(S->equivalent(*SS));
  790. SS = O->status("//root/xX");
  791. EXPECT_TRUE(S->equivalent(*SS));
  792. SS = O->status("//root/Xx");
  793. EXPECT_TRUE(S->equivalent(*SS));
  794. EXPECT_EQ(0, NumDiagnostics);
  795. }
  796. TEST_F(VFSFromYAMLTest, CaseSensitive) {
  797. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  798. Lower->addRegularFile("//root/foo/bar/a");
  799. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  800. getFromYAMLString("{ 'case-sensitive': 'true',\n"
  801. " 'roots': [\n"
  802. "{\n"
  803. " 'type': 'directory',\n"
  804. " 'name': '//root/',\n"
  805. " 'contents': [ {\n"
  806. " 'type': 'file',\n"
  807. " 'name': 'XX',\n"
  808. " 'external-contents': '//root/foo/bar/a'\n"
  809. " }\n"
  810. " ]\n"
  811. "}]}",
  812. Lower);
  813. ASSERT_TRUE(FS.get() != nullptr);
  814. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  815. new vfs::OverlayFileSystem(Lower));
  816. O->pushOverlay(FS);
  817. ErrorOr<vfs::Status> SS = O->status("//root/xx");
  818. EXPECT_EQ(SS.getError(), llvm::errc::no_such_file_or_directory);
  819. SS = O->status("//root/xX");
  820. EXPECT_EQ(SS.getError(), llvm::errc::no_such_file_or_directory);
  821. SS = O->status("//root/Xx");
  822. EXPECT_EQ(SS.getError(), llvm::errc::no_such_file_or_directory);
  823. EXPECT_EQ(0, NumDiagnostics);
  824. }
  825. TEST_F(VFSFromYAMLTest, IllegalVFSFile) {
  826. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  827. // invalid YAML at top-level
  828. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString("{]", Lower);
  829. EXPECT_EQ(nullptr, FS.get());
  830. // invalid YAML in roots
  831. FS = getFromYAMLString("{ 'roots':[}", Lower);
  832. // invalid YAML in directory
  833. FS = getFromYAMLString(
  834. "{ 'roots':[ { 'name': 'foo', 'type': 'directory', 'contents': [}",
  835. Lower);
  836. EXPECT_EQ(nullptr, FS.get());
  837. // invalid configuration
  838. FS = getFromYAMLString("{ 'knobular': 'true', 'roots':[] }", Lower);
  839. EXPECT_EQ(nullptr, FS.get());
  840. FS = getFromYAMLString("{ 'case-sensitive': 'maybe', 'roots':[] }", Lower);
  841. EXPECT_EQ(nullptr, FS.get());
  842. // invalid roots
  843. FS = getFromYAMLString("{ 'roots':'' }", Lower);
  844. EXPECT_EQ(nullptr, FS.get());
  845. FS = getFromYAMLString("{ 'roots':{} }", Lower);
  846. EXPECT_EQ(nullptr, FS.get());
  847. // invalid entries
  848. FS = getFromYAMLString(
  849. "{ 'roots':[ { 'type': 'other', 'name': 'me', 'contents': '' }", Lower);
  850. EXPECT_EQ(nullptr, FS.get());
  851. FS = getFromYAMLString("{ 'roots':[ { 'type': 'file', 'name': [], "
  852. "'external-contents': 'other' }",
  853. Lower);
  854. EXPECT_EQ(nullptr, FS.get());
  855. FS = getFromYAMLString(
  856. "{ 'roots':[ { 'type': 'file', 'name': 'me', 'external-contents': [] }",
  857. Lower);
  858. EXPECT_EQ(nullptr, FS.get());
  859. FS = getFromYAMLString(
  860. "{ 'roots':[ { 'type': 'file', 'name': 'me', 'external-contents': {} }",
  861. Lower);
  862. EXPECT_EQ(nullptr, FS.get());
  863. FS = getFromYAMLString(
  864. "{ 'roots':[ { 'type': 'directory', 'name': 'me', 'contents': {} }",
  865. Lower);
  866. EXPECT_EQ(nullptr, FS.get());
  867. FS = getFromYAMLString(
  868. "{ 'roots':[ { 'type': 'directory', 'name': 'me', 'contents': '' }",
  869. Lower);
  870. EXPECT_EQ(nullptr, FS.get());
  871. FS = getFromYAMLString(
  872. "{ 'roots':[ { 'thingy': 'directory', 'name': 'me', 'contents': [] }",
  873. Lower);
  874. EXPECT_EQ(nullptr, FS.get());
  875. // missing mandatory fields
  876. FS = getFromYAMLString("{ 'roots':[ { 'type': 'file', 'name': 'me' }", Lower);
  877. EXPECT_EQ(nullptr, FS.get());
  878. FS = getFromYAMLString(
  879. "{ 'roots':[ { 'type': 'file', 'external-contents': 'other' }", Lower);
  880. EXPECT_EQ(nullptr, FS.get());
  881. FS = getFromYAMLString("{ 'roots':[ { 'name': 'me', 'contents': [] }", Lower);
  882. EXPECT_EQ(nullptr, FS.get());
  883. // duplicate keys
  884. FS = getFromYAMLString("{ 'roots':[], 'roots':[] }", Lower);
  885. EXPECT_EQ(nullptr, FS.get());
  886. FS = getFromYAMLString(
  887. "{ 'case-sensitive':'true', 'case-sensitive':'true', 'roots':[] }",
  888. Lower);
  889. EXPECT_EQ(nullptr, FS.get());
  890. FS =
  891. getFromYAMLString("{ 'roots':[{'name':'me', 'name':'you', 'type':'file', "
  892. "'external-contents':'blah' } ] }",
  893. Lower);
  894. EXPECT_EQ(nullptr, FS.get());
  895. // missing version
  896. FS = getFromYAMLRawString("{ 'roots':[] }", Lower);
  897. EXPECT_EQ(nullptr, FS.get());
  898. // bad version number
  899. FS = getFromYAMLRawString("{ 'version':'foo', 'roots':[] }", Lower);
  900. EXPECT_EQ(nullptr, FS.get());
  901. FS = getFromYAMLRawString("{ 'version':-1, 'roots':[] }", Lower);
  902. EXPECT_EQ(nullptr, FS.get());
  903. FS = getFromYAMLRawString("{ 'version':100000, 'roots':[] }", Lower);
  904. EXPECT_EQ(nullptr, FS.get());
  905. EXPECT_EQ(24, NumDiagnostics);
  906. }
  907. TEST_F(VFSFromYAMLTest, UseExternalName) {
  908. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  909. Lower->addRegularFile("//root/external/file");
  910. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString(
  911. "{ 'roots': [\n"
  912. " { 'type': 'file', 'name': '//root/A',\n"
  913. " 'external-contents': '//root/external/file'\n"
  914. " },\n"
  915. " { 'type': 'file', 'name': '//root/B',\n"
  916. " 'use-external-name': true,\n"
  917. " 'external-contents': '//root/external/file'\n"
  918. " },\n"
  919. " { 'type': 'file', 'name': '//root/C',\n"
  920. " 'use-external-name': false,\n"
  921. " 'external-contents': '//root/external/file'\n"
  922. " }\n"
  923. "] }", Lower);
  924. ASSERT_TRUE(nullptr != FS.get());
  925. // default true
  926. EXPECT_EQ("//root/external/file", FS->status("//root/A")->getName());
  927. // explicit
  928. EXPECT_EQ("//root/external/file", FS->status("//root/B")->getName());
  929. EXPECT_EQ("//root/C", FS->status("//root/C")->getName());
  930. // global configuration
  931. FS = getFromYAMLString(
  932. "{ 'use-external-names': false,\n"
  933. " 'roots': [\n"
  934. " { 'type': 'file', 'name': '//root/A',\n"
  935. " 'external-contents': '//root/external/file'\n"
  936. " },\n"
  937. " { 'type': 'file', 'name': '//root/B',\n"
  938. " 'use-external-name': true,\n"
  939. " 'external-contents': '//root/external/file'\n"
  940. " },\n"
  941. " { 'type': 'file', 'name': '//root/C',\n"
  942. " 'use-external-name': false,\n"
  943. " 'external-contents': '//root/external/file'\n"
  944. " }\n"
  945. "] }", Lower);
  946. ASSERT_TRUE(nullptr != FS.get());
  947. // default
  948. EXPECT_EQ("//root/A", FS->status("//root/A")->getName());
  949. // explicit
  950. EXPECT_EQ("//root/external/file", FS->status("//root/B")->getName());
  951. EXPECT_EQ("//root/C", FS->status("//root/C")->getName());
  952. }
  953. TEST_F(VFSFromYAMLTest, MultiComponentPath) {
  954. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  955. Lower->addRegularFile("//root/other");
  956. // file in roots
  957. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString(
  958. "{ 'roots': [\n"
  959. " { 'type': 'file', 'name': '//root/path/to/file',\n"
  960. " 'external-contents': '//root/other' }]\n"
  961. "}", Lower);
  962. ASSERT_TRUE(nullptr != FS.get());
  963. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  964. EXPECT_FALSE(FS->status("//root/path/to").getError());
  965. EXPECT_FALSE(FS->status("//root/path").getError());
  966. EXPECT_FALSE(FS->status("//root/").getError());
  967. // at the start
  968. FS = getFromYAMLString(
  969. "{ 'roots': [\n"
  970. " { 'type': 'directory', 'name': '//root/path/to',\n"
  971. " 'contents': [ { 'type': 'file', 'name': 'file',\n"
  972. " 'external-contents': '//root/other' }]}]\n"
  973. "}", Lower);
  974. ASSERT_TRUE(nullptr != FS.get());
  975. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  976. EXPECT_FALSE(FS->status("//root/path/to").getError());
  977. EXPECT_FALSE(FS->status("//root/path").getError());
  978. EXPECT_FALSE(FS->status("//root/").getError());
  979. // at the end
  980. FS = getFromYAMLString(
  981. "{ 'roots': [\n"
  982. " { 'type': 'directory', 'name': '//root/',\n"
  983. " 'contents': [ { 'type': 'file', 'name': 'path/to/file',\n"
  984. " 'external-contents': '//root/other' }]}]\n"
  985. "}", Lower);
  986. ASSERT_TRUE(nullptr != FS.get());
  987. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  988. EXPECT_FALSE(FS->status("//root/path/to").getError());
  989. EXPECT_FALSE(FS->status("//root/path").getError());
  990. EXPECT_FALSE(FS->status("//root/").getError());
  991. }
  992. TEST_F(VFSFromYAMLTest, TrailingSlashes) {
  993. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  994. Lower->addRegularFile("//root/other");
  995. // file in roots
  996. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString(
  997. "{ 'roots': [\n"
  998. " { 'type': 'directory', 'name': '//root/path/to////',\n"
  999. " 'contents': [ { 'type': 'file', 'name': 'file',\n"
  1000. " 'external-contents': '//root/other' }]}]\n"
  1001. "}", Lower);
  1002. ASSERT_TRUE(nullptr != FS.get());
  1003. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  1004. EXPECT_FALSE(FS->status("//root/path/to").getError());
  1005. EXPECT_FALSE(FS->status("//root/path").getError());
  1006. EXPECT_FALSE(FS->status("//root/").getError());
  1007. }
  1008. TEST_F(VFSFromYAMLTest, DirectoryIteration) {
  1009. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  1010. Lower->addDirectory("//root/");
  1011. Lower->addDirectory("//root/foo");
  1012. Lower->addDirectory("//root/foo/bar");
  1013. Lower->addRegularFile("//root/foo/bar/a");
  1014. Lower->addRegularFile("//root/foo/bar/b");
  1015. Lower->addRegularFile("//root/file3");
  1016. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  1017. getFromYAMLString("{ 'use-external-names': false,\n"
  1018. " 'roots': [\n"
  1019. "{\n"
  1020. " 'type': 'directory',\n"
  1021. " 'name': '//root/',\n"
  1022. " 'contents': [ {\n"
  1023. " 'type': 'file',\n"
  1024. " 'name': 'file1',\n"
  1025. " 'external-contents': '//root/foo/bar/a'\n"
  1026. " },\n"
  1027. " {\n"
  1028. " 'type': 'file',\n"
  1029. " 'name': 'file2',\n"
  1030. " 'external-contents': '//root/foo/bar/b'\n"
  1031. " }\n"
  1032. " ]\n"
  1033. "}\n"
  1034. "]\n"
  1035. "}",
  1036. Lower);
  1037. ASSERT_TRUE(FS.get() != nullptr);
  1038. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  1039. new vfs::OverlayFileSystem(Lower));
  1040. O->pushOverlay(FS);
  1041. std::error_code EC;
  1042. checkContents(O->dir_begin("//root/", EC),
  1043. {"//root/file1", "//root/file2", "//root/file3", "//root/foo"});
  1044. checkContents(O->dir_begin("//root/foo/bar", EC),
  1045. {"//root/foo/bar/a", "//root/foo/bar/b"});
  1046. }
  1047. TEST_F(VFSFromYAMLTest, DirectoryIterationSameDirMultipleEntries) {
  1048. // https://llvm.org/bugs/show_bug.cgi?id=27725
  1049. if (!supportsSameDirMultipleYAMLEntries())
  1050. return;
  1051. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  1052. Lower->addDirectory("//root/zab");
  1053. Lower->addDirectory("//root/baz");
  1054. Lower->addRegularFile("//root/zab/a");
  1055. Lower->addRegularFile("//root/zab/b");
  1056. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString(
  1057. "{ 'use-external-names': false,\n"
  1058. " 'roots': [\n"
  1059. "{\n"
  1060. " 'type': 'directory',\n"
  1061. " 'name': '//root/baz/',\n"
  1062. " 'contents': [ {\n"
  1063. " 'type': 'file',\n"
  1064. " 'name': 'x',\n"
  1065. " 'external-contents': '//root/zab/a'\n"
  1066. " }\n"
  1067. " ]\n"
  1068. "},\n"
  1069. "{\n"
  1070. " 'type': 'directory',\n"
  1071. " 'name': '//root/baz/',\n"
  1072. " 'contents': [ {\n"
  1073. " 'type': 'file',\n"
  1074. " 'name': 'y',\n"
  1075. " 'external-contents': '//root/zab/b'\n"
  1076. " }\n"
  1077. " ]\n"
  1078. "}\n"
  1079. "]\n"
  1080. "}",
  1081. Lower);
  1082. ASSERT_TRUE(FS.get() != nullptr);
  1083. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  1084. new vfs::OverlayFileSystem(Lower));
  1085. O->pushOverlay(FS);
  1086. std::error_code EC;
  1087. checkContents(O->dir_begin("//root/baz/", EC),
  1088. {"//root/baz/x", "//root/baz/y"});
  1089. }
  1090. TEST_F(VFSFromYAMLTest, RecursiveDirectoryIterationLevel) {
  1091. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  1092. Lower->addDirectory("//root/a");
  1093. Lower->addDirectory("//root/a/b");
  1094. Lower->addDirectory("//root/a/b/c");
  1095. Lower->addRegularFile("//root/a/b/c/file");
  1096. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString(
  1097. "{ 'use-external-names': false,\n"
  1098. " 'roots': [\n"
  1099. "{\n"
  1100. " 'type': 'directory',\n"
  1101. " 'name': '//root/a/b/c/',\n"
  1102. " 'contents': [ {\n"
  1103. " 'type': 'file',\n"
  1104. " 'name': 'file',\n"
  1105. " 'external-contents': '//root/a/b/c/file'\n"
  1106. " }\n"
  1107. " ]\n"
  1108. "},\n"
  1109. "]\n"
  1110. "}",
  1111. Lower);
  1112. ASSERT_TRUE(FS.get() != nullptr);
  1113. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  1114. new vfs::OverlayFileSystem(Lower));
  1115. O->pushOverlay(FS);
  1116. std::error_code EC;
  1117. // Test recursive_directory_iterator level()
  1118. vfs::recursive_directory_iterator I = vfs::recursive_directory_iterator(
  1119. *O, "//root", EC), E;
  1120. ASSERT_FALSE(EC);
  1121. for (int l = 0; I != E; I.increment(EC), ++l) {
  1122. ASSERT_FALSE(EC);
  1123. EXPECT_EQ(I.level(), l);
  1124. }
  1125. EXPECT_EQ(I, E);
  1126. }