VirtualFileSystemTest.cpp 51 KB

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