VirtualFileSystemTest.cpp 36 KB

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