Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1 | //===- unittests/Support/VirtualFileSystem.cpp -------------- VFS tests ---===// |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 9 | #include "llvm/Support/VirtualFileSystem.h" |
Bruno Cardoso Lopes | f6a0a72 | 2016-05-12 19:13:07 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/Triple.h" |
Nico Weber | d637c05 | 2018-04-30 13:52:15 +0000 | [diff] [blame] | 11 | #include "llvm/Config/llvm-config.h" |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 12 | #include "llvm/Support/Errc.h" |
Bruno Cardoso Lopes | f6a0a72 | 2016-05-12 19:13:07 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Host.h" |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 14 | #include "llvm/Support/MemoryBuffer.h" |
Simon Marchi | ddbabc6 | 2018-08-06 21:48:20 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Path.h" |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 16 | #include "llvm/Support/SourceMgr.h" |
Ilya Biryukov | d5554c51 | 2018-09-04 14:15:53 +0000 | [diff] [blame] | 17 | #include "gmock/gmock.h" |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 18 | #include "gtest/gtest.h" |
| 19 | #include <map> |
Ilya Biryukov | d5554c51 | 2018-09-04 14:15:53 +0000 | [diff] [blame] | 20 | #include <string> |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 21 | |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | using llvm::sys::fs::UniqueID; |
Sam McCall | 3922377 | 2018-10-01 16:07:03 +0000 | [diff] [blame] | 24 | using testing::ElementsAre; |
| 25 | using testing::Pair; |
| 26 | using testing::UnorderedElementsAre; |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 27 | |
| 28 | namespace { |
Ben Langmuir | f13302e | 2015-12-10 23:41:39 +0000 | [diff] [blame] | 29 | struct DummyFile : public vfs::File { |
| 30 | vfs::Status S; |
| 31 | explicit DummyFile(vfs::Status S) : S(S) {} |
| 32 | llvm::ErrorOr<vfs::Status> status() override { return S; } |
| 33 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> |
| 34 | getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator, |
| 35 | bool IsVolatile) override { |
| 36 | llvm_unreachable("unimplemented"); |
| 37 | } |
Eugene Zelenko | 1660a5d | 2016-01-26 19:01:06 +0000 | [diff] [blame] | 38 | std::error_code close() override { return std::error_code(); } |
Ben Langmuir | f13302e | 2015-12-10 23:41:39 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 41 | class DummyFileSystem : public vfs::FileSystem { |
| 42 | int FSID; // used to produce UniqueIDs |
| 43 | int FileID; // used to produce UniqueIDs |
| 44 | std::map<std::string, vfs::Status> FilesAndDirs; |
| 45 | |
| 46 | static int getNextFSID() { |
| 47 | static int Count = 0; |
| 48 | return Count++; |
| 49 | } |
| 50 | |
| 51 | public: |
| 52 | DummyFileSystem() : FSID(getNextFSID()), FileID(0) {} |
| 53 | |
Fariborz Jahanian | 5afc869 | 2014-10-01 16:56:40 +0000 | [diff] [blame] | 54 | ErrorOr<vfs::Status> status(const Twine &Path) override { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 55 | std::map<std::string, vfs::Status>::iterator I = |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 56 | FilesAndDirs.find(Path.str()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 57 | if (I == FilesAndDirs.end()) |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 58 | return make_error_code(llvm::errc::no_such_file_or_directory); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 59 | return I->second; |
| 60 | } |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 61 | ErrorOr<std::unique_ptr<vfs::File>> |
| 62 | openFileForRead(const Twine &Path) override { |
Ben Langmuir | f13302e | 2015-12-10 23:41:39 +0000 | [diff] [blame] | 63 | auto S = status(Path); |
| 64 | if (S) |
| 65 | return std::unique_ptr<vfs::File>(new DummyFile{*S}); |
| 66 | return S.getError(); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 67 | } |
Benjamin Kramer | 7708b2a | 2015-10-05 13:55:20 +0000 | [diff] [blame] | 68 | llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override { |
| 69 | return std::string(); |
| 70 | } |
| 71 | std::error_code setCurrentWorkingDirectory(const Twine &Path) override { |
| 72 | return std::error_code(); |
| 73 | } |
Eric Liu | a840a46 | 2018-05-18 13:22:49 +0000 | [diff] [blame] | 74 | // Map any symlink to "/symlink". |
Sam McCall | 99538e8 | 2018-11-09 15:11:34 +0000 | [diff] [blame] | 75 | std::error_code getRealPath(const Twine &Path, |
| 76 | SmallVectorImpl<char> &Output) const override { |
Eric Liu | a840a46 | 2018-05-18 13:22:49 +0000 | [diff] [blame] | 77 | auto I = FilesAndDirs.find(Path.str()); |
| 78 | if (I == FilesAndDirs.end()) |
| 79 | return make_error_code(llvm::errc::no_such_file_or_directory); |
| 80 | if (I->second.isSymlink()) { |
| 81 | Output.clear(); |
| 82 | Twine("/symlink").toVector(Output); |
| 83 | return std::error_code(); |
| 84 | } |
| 85 | Output.clear(); |
| 86 | Path.toVector(Output); |
| 87 | return std::error_code(); |
| 88 | } |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 89 | |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 90 | struct DirIterImpl : public llvm::vfs::detail::DirIterImpl { |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 91 | std::map<std::string, vfs::Status> &FilesAndDirs; |
| 92 | std::map<std::string, vfs::Status>::iterator I; |
| 93 | std::string Path; |
Ben Langmuir | 7c9f6c8 | 2014-06-25 20:25:40 +0000 | [diff] [blame] | 94 | bool isInPath(StringRef S) { |
| 95 | if (Path.size() < S.size() && S.find(Path) == 0) { |
| 96 | auto LastSep = S.find_last_of('/'); |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 97 | if (LastSep == Path.size() || LastSep == Path.size() - 1) |
Ben Langmuir | 7c9f6c8 | 2014-06-25 20:25:40 +0000 | [diff] [blame] | 98 | return true; |
| 99 | } |
| 100 | return false; |
| 101 | } |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 102 | DirIterImpl(std::map<std::string, vfs::Status> &FilesAndDirs, |
| 103 | const Twine &_Path) |
| 104 | : FilesAndDirs(FilesAndDirs), I(FilesAndDirs.begin()), |
| 105 | Path(_Path.str()) { |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 106 | for (; I != FilesAndDirs.end(); ++I) { |
Ben Langmuir | 7c9f6c8 | 2014-06-25 20:25:40 +0000 | [diff] [blame] | 107 | if (isInPath(I->first)) { |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 108 | CurrentEntry = |
| 109 | vfs::directory_entry(I->second.getName(), I->second.getType()); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 110 | break; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | std::error_code increment() override { |
| 115 | ++I; |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 116 | for (; I != FilesAndDirs.end(); ++I) { |
Ben Langmuir | 7c9f6c8 | 2014-06-25 20:25:40 +0000 | [diff] [blame] | 117 | if (isInPath(I->first)) { |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 118 | CurrentEntry = |
| 119 | vfs::directory_entry(I->second.getName(), I->second.getType()); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 120 | break; |
| 121 | } |
| 122 | } |
| 123 | if (I == FilesAndDirs.end()) |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 124 | CurrentEntry = vfs::directory_entry(); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 125 | return std::error_code(); |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | vfs::directory_iterator dir_begin(const Twine &Dir, |
| 130 | std::error_code &EC) override { |
| 131 | return vfs::directory_iterator( |
| 132 | std::make_shared<DirIterImpl>(FilesAndDirs, Dir)); |
| 133 | } |
| 134 | |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 135 | void addEntry(StringRef Path, const vfs::Status &Status) { |
| 136 | FilesAndDirs[Path] = Status; |
| 137 | } |
| 138 | |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 139 | void addRegularFile(StringRef Path, sys::fs::perms Perms = sys::fs::all_all) { |
Pavel Labath | ac71c8e | 2016-11-09 10:52:22 +0000 | [diff] [blame] | 140 | vfs::Status S(Path, UniqueID(FSID, FileID++), |
| 141 | std::chrono::system_clock::now(), 0, 0, 1024, |
| 142 | sys::fs::file_type::regular_file, Perms); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 143 | addEntry(Path, S); |
| 144 | } |
| 145 | |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 146 | void addDirectory(StringRef Path, sys::fs::perms Perms = sys::fs::all_all) { |
Pavel Labath | ac71c8e | 2016-11-09 10:52:22 +0000 | [diff] [blame] | 147 | vfs::Status S(Path, UniqueID(FSID, FileID++), |
| 148 | std::chrono::system_clock::now(), 0, 0, 0, |
| 149 | sys::fs::file_type::directory_file, Perms); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 150 | addEntry(Path, S); |
| 151 | } |
| 152 | |
| 153 | void addSymlink(StringRef Path) { |
Pavel Labath | ac71c8e | 2016-11-09 10:52:22 +0000 | [diff] [blame] | 154 | vfs::Status S(Path, UniqueID(FSID, FileID++), |
| 155 | std::chrono::system_clock::now(), 0, 0, 0, |
| 156 | sys::fs::file_type::symlink_file, sys::fs::all_all); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 157 | addEntry(Path, S); |
| 158 | } |
| 159 | }; |
Simon Marchi | ddbabc6 | 2018-08-06 21:48:20 +0000 | [diff] [blame] | 160 | |
| 161 | /// Replace back-slashes by front-slashes. |
| 162 | std::string getPosixPath(std::string S) { |
| 163 | SmallString<128> Result; |
| 164 | llvm::sys::path::native(S, Result, llvm::sys::path::Style::posix); |
| 165 | return Result.str(); |
Craig Topper | 576ac05 | 2018-08-08 22:31:14 +0000 | [diff] [blame] | 166 | } |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 167 | } // end anonymous namespace |
| 168 | |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 169 | TEST(VirtualFileSystemTest, StatusQueries) { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 170 | IntrusiveRefCntPtr<DummyFileSystem> D(new DummyFileSystem()); |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame] | 171 | ErrorOr<vfs::Status> Status((std::error_code())); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 172 | |
| 173 | D->addRegularFile("/foo"); |
| 174 | Status = D->status("/foo"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 175 | ASSERT_FALSE(Status.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 176 | EXPECT_TRUE(Status->isStatusKnown()); |
| 177 | EXPECT_FALSE(Status->isDirectory()); |
| 178 | EXPECT_TRUE(Status->isRegularFile()); |
| 179 | EXPECT_FALSE(Status->isSymlink()); |
| 180 | EXPECT_FALSE(Status->isOther()); |
| 181 | EXPECT_TRUE(Status->exists()); |
| 182 | |
| 183 | D->addDirectory("/bar"); |
| 184 | Status = D->status("/bar"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 185 | ASSERT_FALSE(Status.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 186 | EXPECT_TRUE(Status->isStatusKnown()); |
| 187 | EXPECT_TRUE(Status->isDirectory()); |
| 188 | EXPECT_FALSE(Status->isRegularFile()); |
| 189 | EXPECT_FALSE(Status->isSymlink()); |
| 190 | EXPECT_FALSE(Status->isOther()); |
| 191 | EXPECT_TRUE(Status->exists()); |
| 192 | |
| 193 | D->addSymlink("/baz"); |
| 194 | Status = D->status("/baz"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 195 | ASSERT_FALSE(Status.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 196 | EXPECT_TRUE(Status->isStatusKnown()); |
| 197 | EXPECT_FALSE(Status->isDirectory()); |
| 198 | EXPECT_FALSE(Status->isRegularFile()); |
| 199 | EXPECT_TRUE(Status->isSymlink()); |
| 200 | EXPECT_FALSE(Status->isOther()); |
| 201 | EXPECT_TRUE(Status->exists()); |
| 202 | |
| 203 | EXPECT_TRUE(Status->equivalent(*Status)); |
| 204 | ErrorOr<vfs::Status> Status2 = D->status("/foo"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 205 | ASSERT_FALSE(Status2.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 206 | EXPECT_FALSE(Status->equivalent(*Status2)); |
| 207 | } |
| 208 | |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 209 | TEST(VirtualFileSystemTest, BaseOnlyOverlay) { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 210 | IntrusiveRefCntPtr<DummyFileSystem> D(new DummyFileSystem()); |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame] | 211 | ErrorOr<vfs::Status> Status((std::error_code())); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 212 | EXPECT_FALSE(Status = D->status("/foo")); |
| 213 | |
| 214 | IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(new vfs::OverlayFileSystem(D)); |
| 215 | EXPECT_FALSE(Status = O->status("/foo")); |
| 216 | |
| 217 | D->addRegularFile("/foo"); |
| 218 | Status = D->status("/foo"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 219 | EXPECT_FALSE(Status.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 220 | |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame] | 221 | ErrorOr<vfs::Status> Status2((std::error_code())); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 222 | Status2 = O->status("/foo"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 223 | EXPECT_FALSE(Status2.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 224 | EXPECT_TRUE(Status->equivalent(*Status2)); |
| 225 | } |
| 226 | |
Eric Liu | a840a46 | 2018-05-18 13:22:49 +0000 | [diff] [blame] | 227 | TEST(VirtualFileSystemTest, GetRealPathInOverlay) { |
| 228 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
| 229 | Lower->addRegularFile("/foo"); |
| 230 | Lower->addSymlink("/lower_link"); |
| 231 | IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem()); |
| 232 | |
| 233 | IntrusiveRefCntPtr<vfs::OverlayFileSystem> O( |
| 234 | new vfs::OverlayFileSystem(Lower)); |
| 235 | O->pushOverlay(Upper); |
| 236 | |
| 237 | // Regular file. |
| 238 | SmallString<16> RealPath; |
| 239 | EXPECT_FALSE(O->getRealPath("/foo", RealPath)); |
| 240 | EXPECT_EQ(RealPath.str(), "/foo"); |
| 241 | |
| 242 | // Expect no error getting real path for symlink in lower overlay. |
| 243 | EXPECT_FALSE(O->getRealPath("/lower_link", RealPath)); |
| 244 | EXPECT_EQ(RealPath.str(), "/symlink"); |
| 245 | |
| 246 | // Try a non-existing link. |
| 247 | EXPECT_EQ(O->getRealPath("/upper_link", RealPath), |
| 248 | errc::no_such_file_or_directory); |
| 249 | |
| 250 | // Add a new symlink in upper. |
| 251 | Upper->addSymlink("/upper_link"); |
| 252 | EXPECT_FALSE(O->getRealPath("/upper_link", RealPath)); |
| 253 | EXPECT_EQ(RealPath.str(), "/symlink"); |
| 254 | } |
| 255 | |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 256 | TEST(VirtualFileSystemTest, OverlayFiles) { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 257 | IntrusiveRefCntPtr<DummyFileSystem> Base(new DummyFileSystem()); |
| 258 | IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem()); |
| 259 | IntrusiveRefCntPtr<DummyFileSystem> Top(new DummyFileSystem()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 260 | IntrusiveRefCntPtr<vfs::OverlayFileSystem> O( |
| 261 | new vfs::OverlayFileSystem(Base)); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 262 | O->pushOverlay(Middle); |
| 263 | O->pushOverlay(Top); |
| 264 | |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame] | 265 | ErrorOr<vfs::Status> Status1((std::error_code())), |
| 266 | Status2((std::error_code())), Status3((std::error_code())), |
| 267 | StatusB((std::error_code())), StatusM((std::error_code())), |
| 268 | StatusT((std::error_code())); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 269 | |
| 270 | Base->addRegularFile("/foo"); |
| 271 | StatusB = Base->status("/foo"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 272 | ASSERT_FALSE(StatusB.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 273 | Status1 = O->status("/foo"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 274 | ASSERT_FALSE(Status1.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 275 | Middle->addRegularFile("/foo"); |
| 276 | StatusM = Middle->status("/foo"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 277 | ASSERT_FALSE(StatusM.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 278 | Status2 = O->status("/foo"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 279 | ASSERT_FALSE(Status2.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 280 | Top->addRegularFile("/foo"); |
| 281 | StatusT = Top->status("/foo"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 282 | ASSERT_FALSE(StatusT.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 283 | Status3 = O->status("/foo"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 284 | ASSERT_FALSE(Status3.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 285 | |
| 286 | EXPECT_TRUE(Status1->equivalent(*StatusB)); |
| 287 | EXPECT_TRUE(Status2->equivalent(*StatusM)); |
| 288 | EXPECT_TRUE(Status3->equivalent(*StatusT)); |
| 289 | |
| 290 | EXPECT_FALSE(Status1->equivalent(*Status2)); |
| 291 | EXPECT_FALSE(Status2->equivalent(*Status3)); |
| 292 | EXPECT_FALSE(Status1->equivalent(*Status3)); |
| 293 | } |
| 294 | |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 295 | TEST(VirtualFileSystemTest, OverlayDirsNonMerged) { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 296 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
| 297 | IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 298 | IntrusiveRefCntPtr<vfs::OverlayFileSystem> O( |
| 299 | new vfs::OverlayFileSystem(Lower)); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 300 | O->pushOverlay(Upper); |
| 301 | |
| 302 | Lower->addDirectory("/lower-only"); |
| 303 | Upper->addDirectory("/upper-only"); |
| 304 | |
| 305 | // non-merged paths should be the same |
| 306 | ErrorOr<vfs::Status> Status1 = Lower->status("/lower-only"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 307 | ASSERT_FALSE(Status1.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 308 | ErrorOr<vfs::Status> Status2 = O->status("/lower-only"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 309 | ASSERT_FALSE(Status2.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 310 | EXPECT_TRUE(Status1->equivalent(*Status2)); |
| 311 | |
| 312 | Status1 = Upper->status("/upper-only"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 313 | ASSERT_FALSE(Status1.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 314 | Status2 = O->status("/upper-only"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 315 | ASSERT_FALSE(Status2.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 316 | EXPECT_TRUE(Status1->equivalent(*Status2)); |
| 317 | } |
| 318 | |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 319 | TEST(VirtualFileSystemTest, MergedDirPermissions) { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 320 | // merged directories get the permissions of the upper dir |
| 321 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
| 322 | IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 323 | IntrusiveRefCntPtr<vfs::OverlayFileSystem> O( |
| 324 | new vfs::OverlayFileSystem(Lower)); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 325 | O->pushOverlay(Upper); |
| 326 | |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame] | 327 | ErrorOr<vfs::Status> Status((std::error_code())); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 328 | Lower->addDirectory("/both", sys::fs::owner_read); |
| 329 | Upper->addDirectory("/both", sys::fs::owner_all | sys::fs::group_read); |
| 330 | Status = O->status("/both"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 331 | ASSERT_FALSE(Status.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 332 | EXPECT_EQ(0740, Status->getPermissions()); |
| 333 | |
| 334 | // permissions (as usual) are not recursively applied |
| 335 | Lower->addRegularFile("/both/foo", sys::fs::owner_read); |
| 336 | Upper->addRegularFile("/both/bar", sys::fs::owner_write); |
| 337 | Status = O->status("/both/foo"); |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 338 | ASSERT_FALSE(Status.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 339 | EXPECT_EQ(0400, Status->getPermissions()); |
| 340 | Status = O->status("/both/bar"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 341 | ASSERT_FALSE(Status.getError()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 342 | EXPECT_EQ(0200, Status->getPermissions()); |
| 343 | } |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 344 | |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 345 | namespace { |
| 346 | struct ScopedDir { |
| 347 | SmallString<128> Path; |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 348 | ScopedDir(const Twine &Name, bool Unique = false) { |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 349 | std::error_code EC; |
| 350 | if (Unique) { |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 351 | EC = llvm::sys::fs::createUniqueDirectory(Name, Path); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 352 | } else { |
| 353 | Path = Name.str(); |
| 354 | EC = llvm::sys::fs::create_directory(Twine(Path)); |
| 355 | } |
| 356 | if (EC) |
| 357 | Path = ""; |
| 358 | EXPECT_FALSE(EC); |
| 359 | } |
| 360 | ~ScopedDir() { |
Galina Kistanova | 45fbb59 | 2017-06-15 21:01:24 +0000 | [diff] [blame] | 361 | if (Path != "") { |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 362 | EXPECT_FALSE(llvm::sys::fs::remove(Path.str())); |
Galina Kistanova | 45fbb59 | 2017-06-15 21:01:24 +0000 | [diff] [blame] | 363 | } |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 364 | } |
| 365 | operator StringRef() { return Path.str(); } |
| 366 | }; |
Juergen Ributzka | f978743 | 2017-03-14 00:14:40 +0000 | [diff] [blame] | 367 | |
| 368 | struct ScopedLink { |
| 369 | SmallString<128> Path; |
| 370 | ScopedLink(const Twine &To, const Twine &From) { |
| 371 | Path = From.str(); |
| 372 | std::error_code EC = sys::fs::create_link(To, From); |
| 373 | if (EC) |
| 374 | Path = ""; |
| 375 | EXPECT_FALSE(EC); |
| 376 | } |
| 377 | ~ScopedLink() { |
Galina Kistanova | 45fbb59 | 2017-06-15 21:01:24 +0000 | [diff] [blame] | 378 | if (Path != "") { |
Juergen Ributzka | f978743 | 2017-03-14 00:14:40 +0000 | [diff] [blame] | 379 | EXPECT_FALSE(llvm::sys::fs::remove(Path.str())); |
Galina Kistanova | 45fbb59 | 2017-06-15 21:01:24 +0000 | [diff] [blame] | 380 | } |
Juergen Ributzka | f978743 | 2017-03-14 00:14:40 +0000 | [diff] [blame] | 381 | } |
| 382 | operator StringRef() { return Path.str(); } |
| 383 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 384 | } // end anonymous namespace |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 385 | |
| 386 | TEST(VirtualFileSystemTest, BasicRealFSIteration) { |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 387 | ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 388 | IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem(); |
| 389 | |
| 390 | std::error_code EC; |
| 391 | vfs::directory_iterator I = FS->dir_begin(Twine(TestDirectory), EC); |
| 392 | ASSERT_FALSE(EC); |
| 393 | EXPECT_EQ(vfs::directory_iterator(), I); // empty directory is empty |
| 394 | |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 395 | ScopedDir _a(TestDirectory + "/a"); |
| 396 | ScopedDir _ab(TestDirectory + "/a/b"); |
| 397 | ScopedDir _c(TestDirectory + "/c"); |
| 398 | ScopedDir _cd(TestDirectory + "/c/d"); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 399 | |
| 400 | I = FS->dir_begin(Twine(TestDirectory), EC); |
| 401 | ASSERT_FALSE(EC); |
| 402 | ASSERT_NE(vfs::directory_iterator(), I); |
Ben Langmuir | efb8b60 | 2014-06-24 21:08:13 +0000 | [diff] [blame] | 403 | // Check either a or c, since we can't rely on the iteration order. |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 404 | EXPECT_TRUE(I->path().endswith("a") || I->path().endswith("c")); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 405 | I.increment(EC); |
| 406 | ASSERT_FALSE(EC); |
| 407 | ASSERT_NE(vfs::directory_iterator(), I); |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 408 | EXPECT_TRUE(I->path().endswith("a") || I->path().endswith("c")); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 409 | I.increment(EC); |
| 410 | EXPECT_EQ(vfs::directory_iterator(), I); |
| 411 | } |
| 412 | |
Sam McCall | 62ab1a1 | 2019-01-14 17:51:10 +0000 | [diff] [blame] | 413 | #ifdef LLVM_ON_UNIX |
Juergen Ributzka | f978743 | 2017-03-14 00:14:40 +0000 | [diff] [blame] | 414 | TEST(VirtualFileSystemTest, BrokenSymlinkRealFSIteration) { |
| 415 | ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true); |
| 416 | IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem(); |
| 417 | |
| 418 | ScopedLink _a("no_such_file", TestDirectory + "/a"); |
| 419 | ScopedDir _b(TestDirectory + "/b"); |
| 420 | ScopedLink _c("no_such_file", TestDirectory + "/c"); |
| 421 | |
Sam McCall | 3922377 | 2018-10-01 16:07:03 +0000 | [diff] [blame] | 422 | // Should get no iteration error, but a stat error for the broken symlinks. |
| 423 | std::map<std::string, std::error_code> StatResults; |
Juergen Ributzka | f978743 | 2017-03-14 00:14:40 +0000 | [diff] [blame] | 424 | std::error_code EC; |
| 425 | for (vfs::directory_iterator I = FS->dir_begin(Twine(TestDirectory), EC), E; |
| 426 | I != E; I.increment(EC)) { |
Sam McCall | 3922377 | 2018-10-01 16:07:03 +0000 | [diff] [blame] | 427 | EXPECT_FALSE(EC); |
| 428 | StatResults[sys::path::filename(I->path())] = |
| 429 | FS->status(I->path()).getError(); |
Juergen Ributzka | f978743 | 2017-03-14 00:14:40 +0000 | [diff] [blame] | 430 | } |
Sam McCall | 3922377 | 2018-10-01 16:07:03 +0000 | [diff] [blame] | 431 | EXPECT_THAT( |
| 432 | StatResults, |
| 433 | ElementsAre( |
| 434 | Pair("a", std::make_error_code(std::errc::no_such_file_or_directory)), |
| 435 | Pair("b", std::error_code()), |
| 436 | Pair("c", |
| 437 | std::make_error_code(std::errc::no_such_file_or_directory)))); |
Juergen Ributzka | f978743 | 2017-03-14 00:14:40 +0000 | [diff] [blame] | 438 | } |
| 439 | #endif |
| 440 | |
Ben Langmuir | 7c9f6c8 | 2014-06-25 20:25:40 +0000 | [diff] [blame] | 441 | TEST(VirtualFileSystemTest, BasicRealFSRecursiveIteration) { |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 442 | ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true); |
Ben Langmuir | 7c9f6c8 | 2014-06-25 20:25:40 +0000 | [diff] [blame] | 443 | IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem(); |
| 444 | |
| 445 | std::error_code EC; |
| 446 | auto I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC); |
| 447 | ASSERT_FALSE(EC); |
| 448 | EXPECT_EQ(vfs::recursive_directory_iterator(), I); // empty directory is empty |
| 449 | |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 450 | ScopedDir _a(TestDirectory + "/a"); |
| 451 | ScopedDir _ab(TestDirectory + "/a/b"); |
| 452 | ScopedDir _c(TestDirectory + "/c"); |
| 453 | ScopedDir _cd(TestDirectory + "/c/d"); |
Ben Langmuir | 7c9f6c8 | 2014-06-25 20:25:40 +0000 | [diff] [blame] | 454 | |
| 455 | I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC); |
| 456 | ASSERT_FALSE(EC); |
| 457 | ASSERT_NE(vfs::recursive_directory_iterator(), I); |
| 458 | |
Ben Langmuir | 7c9f6c8 | 2014-06-25 20:25:40 +0000 | [diff] [blame] | 459 | std::vector<std::string> Contents; |
| 460 | for (auto E = vfs::recursive_directory_iterator(); !EC && I != E; |
| 461 | I.increment(EC)) { |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 462 | Contents.push_back(I->path()); |
Ben Langmuir | 7c9f6c8 | 2014-06-25 20:25:40 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | // Check contents, which may be in any order |
| 466 | EXPECT_EQ(4U, Contents.size()); |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 467 | int Counts[4] = {0, 0, 0, 0}; |
Ben Langmuir | 7c9f6c8 | 2014-06-25 20:25:40 +0000 | [diff] [blame] | 468 | for (const std::string &Name : Contents) { |
| 469 | ASSERT_FALSE(Name.empty()); |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 470 | int Index = Name[Name.size() - 1] - 'a'; |
Ben Langmuir | 7c9f6c8 | 2014-06-25 20:25:40 +0000 | [diff] [blame] | 471 | ASSERT_TRUE(Index >= 0 && Index < 4); |
| 472 | Counts[Index]++; |
| 473 | } |
| 474 | EXPECT_EQ(1, Counts[0]); // a |
| 475 | EXPECT_EQ(1, Counts[1]); // b |
| 476 | EXPECT_EQ(1, Counts[2]); // c |
| 477 | EXPECT_EQ(1, Counts[3]); // d |
| 478 | } |
| 479 | |
Jonas Devlieghere | 41fb951 | 2018-10-31 23:36:10 +0000 | [diff] [blame] | 480 | TEST(VirtualFileSystemTest, BasicRealFSRecursiveIterationNoPush) { |
| 481 | ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true); |
| 482 | |
| 483 | ScopedDir _a(TestDirectory + "/a"); |
| 484 | ScopedDir _ab(TestDirectory + "/a/b"); |
| 485 | ScopedDir _c(TestDirectory + "/c"); |
| 486 | ScopedDir _cd(TestDirectory + "/c/d"); |
| 487 | ScopedDir _e(TestDirectory + "/e"); |
| 488 | ScopedDir _ef(TestDirectory + "/e/f"); |
| 489 | ScopedDir _g(TestDirectory + "/g"); |
| 490 | |
| 491 | IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem(); |
| 492 | |
| 493 | // Test that calling no_push on entries without subdirectories has no effect. |
| 494 | { |
| 495 | std::error_code EC; |
| 496 | auto I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC); |
| 497 | ASSERT_FALSE(EC); |
| 498 | |
| 499 | std::vector<std::string> Contents; |
| 500 | for (auto E = vfs::recursive_directory_iterator(); !EC && I != E; |
| 501 | I.increment(EC)) { |
| 502 | Contents.push_back(I->path()); |
| 503 | char last = I->path().back(); |
| 504 | switch (last) { |
| 505 | case 'b': |
| 506 | case 'd': |
| 507 | case 'f': |
| 508 | case 'g': |
| 509 | I.no_push(); |
| 510 | break; |
| 511 | default: |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | EXPECT_EQ(7U, Contents.size()); |
| 516 | } |
| 517 | |
| 518 | // Test that calling no_push skips subdirectories. |
| 519 | { |
| 520 | std::error_code EC; |
| 521 | auto I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC); |
| 522 | ASSERT_FALSE(EC); |
| 523 | |
| 524 | std::vector<std::string> Contents; |
| 525 | for (auto E = vfs::recursive_directory_iterator(); !EC && I != E; |
| 526 | I.increment(EC)) { |
| 527 | Contents.push_back(I->path()); |
| 528 | char last = I->path().back(); |
| 529 | switch (last) { |
| 530 | case 'a': |
| 531 | case 'c': |
| 532 | case 'e': |
| 533 | I.no_push(); |
| 534 | break; |
| 535 | default: |
| 536 | break; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | // Check contents, which may be in any order |
| 541 | EXPECT_EQ(4U, Contents.size()); |
| 542 | int Counts[7] = {0, 0, 0, 0, 0, 0, 0}; |
| 543 | for (const std::string &Name : Contents) { |
| 544 | ASSERT_FALSE(Name.empty()); |
| 545 | int Index = Name[Name.size() - 1] - 'a'; |
| 546 | ASSERT_TRUE(Index >= 0 && Index < 7); |
| 547 | Counts[Index]++; |
| 548 | } |
| 549 | EXPECT_EQ(1, Counts[0]); // a |
| 550 | EXPECT_EQ(0, Counts[1]); // b |
| 551 | EXPECT_EQ(1, Counts[2]); // c |
| 552 | EXPECT_EQ(0, Counts[3]); // d |
| 553 | EXPECT_EQ(1, Counts[4]); // e |
| 554 | EXPECT_EQ(0, Counts[5]); // f |
| 555 | EXPECT_EQ(1, Counts[6]); // g |
| 556 | } |
| 557 | } |
| 558 | |
Juergen Ributzka | f978743 | 2017-03-14 00:14:40 +0000 | [diff] [blame] | 559 | #ifdef LLVM_ON_UNIX |
| 560 | TEST(VirtualFileSystemTest, BrokenSymlinkRealFSRecursiveIteration) { |
| 561 | ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true); |
| 562 | IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem(); |
| 563 | |
| 564 | ScopedLink _a("no_such_file", TestDirectory + "/a"); |
| 565 | ScopedDir _b(TestDirectory + "/b"); |
| 566 | ScopedLink _ba("no_such_file", TestDirectory + "/b/a"); |
| 567 | ScopedDir _bb(TestDirectory + "/b/b"); |
| 568 | ScopedLink _bc("no_such_file", TestDirectory + "/b/c"); |
| 569 | ScopedLink _c("no_such_file", TestDirectory + "/c"); |
| 570 | ScopedDir _d(TestDirectory + "/d"); |
| 571 | ScopedDir _dd(TestDirectory + "/d/d"); |
| 572 | ScopedDir _ddd(TestDirectory + "/d/d/d"); |
| 573 | ScopedLink _e("no_such_file", TestDirectory + "/e"); |
Juergen Ributzka | f978743 | 2017-03-14 00:14:40 +0000 | [diff] [blame] | 574 | |
Max Moroz | e097567 | 2018-04-04 19:47:25 +0000 | [diff] [blame] | 575 | std::vector<std::string> VisitedBrokenSymlinks; |
| 576 | std::vector<std::string> VisitedNonBrokenSymlinks; |
Juergen Ributzka | f978743 | 2017-03-14 00:14:40 +0000 | [diff] [blame] | 577 | std::error_code EC; |
| 578 | for (vfs::recursive_directory_iterator I(*FS, Twine(TestDirectory), EC), E; |
| 579 | I != E; I.increment(EC)) { |
Sam McCall | 3922377 | 2018-10-01 16:07:03 +0000 | [diff] [blame] | 580 | EXPECT_FALSE(EC); |
| 581 | (FS->status(I->path()) ? VisitedNonBrokenSymlinks : VisitedBrokenSymlinks) |
| 582 | .push_back(I->path()); |
Juergen Ributzka | f978743 | 2017-03-14 00:14:40 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Max Moroz | e097567 | 2018-04-04 19:47:25 +0000 | [diff] [blame] | 585 | // Check visited file names. |
Sam McCall | 3922377 | 2018-10-01 16:07:03 +0000 | [diff] [blame] | 586 | EXPECT_THAT(VisitedBrokenSymlinks, |
| 587 | UnorderedElementsAre(StringRef(_a), StringRef(_ba), |
| 588 | StringRef(_bc), StringRef(_c), |
| 589 | StringRef(_e))); |
| 590 | EXPECT_THAT(VisitedNonBrokenSymlinks, |
| 591 | UnorderedElementsAre(StringRef(_b), StringRef(_bb), StringRef(_d), |
| 592 | StringRef(_dd), StringRef(_ddd))); |
Juergen Ributzka | f978743 | 2017-03-14 00:14:40 +0000 | [diff] [blame] | 593 | } |
| 594 | #endif |
| 595 | |
Ben Langmuir | 7c9f6c8 | 2014-06-25 20:25:40 +0000 | [diff] [blame] | 596 | template <typename DirIter> |
Bruno Cardoso Lopes | 29ebd49 | 2016-05-11 20:58:47 +0000 | [diff] [blame] | 597 | static void checkContents(DirIter I, ArrayRef<StringRef> ExpectedOut) { |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 598 | std::error_code EC; |
Bruno Cardoso Lopes | 29ebd49 | 2016-05-11 20:58:47 +0000 | [diff] [blame] | 599 | SmallVector<StringRef, 4> Expected(ExpectedOut.begin(), ExpectedOut.end()); |
| 600 | SmallVector<std::string, 4> InputToCheck; |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 601 | |
Bruno Cardoso Lopes | 29ebd49 | 2016-05-11 20:58:47 +0000 | [diff] [blame] | 602 | // Do not rely on iteration order to check for contents, sort both |
| 603 | // content vectors before comparison. |
| 604 | for (DirIter E; !EC && I != E; I.increment(EC)) |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 605 | InputToCheck.push_back(I->path()); |
Bruno Cardoso Lopes | 29ebd49 | 2016-05-11 20:58:47 +0000 | [diff] [blame] | 606 | |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 607 | llvm::sort(InputToCheck); |
| 608 | llvm::sort(Expected); |
Bruno Cardoso Lopes | 29ebd49 | 2016-05-11 20:58:47 +0000 | [diff] [blame] | 609 | EXPECT_EQ(InputToCheck.size(), Expected.size()); |
| 610 | |
| 611 | unsigned LastElt = std::min(InputToCheck.size(), Expected.size()); |
| 612 | for (unsigned Idx = 0; Idx != LastElt; ++Idx) |
Bruno Cardoso Lopes | 2835c5c | 2016-05-12 19:13:04 +0000 | [diff] [blame] | 613 | EXPECT_EQ(StringRef(InputToCheck[Idx]), Expected[Idx]); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | TEST(VirtualFileSystemTest, OverlayIteration) { |
| 617 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
| 618 | IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem()); |
| 619 | IntrusiveRefCntPtr<vfs::OverlayFileSystem> O( |
| 620 | new vfs::OverlayFileSystem(Lower)); |
| 621 | O->pushOverlay(Upper); |
| 622 | |
| 623 | std::error_code EC; |
NAKAMURA Takumi | 4669bd4 | 2014-06-25 04:34:10 +0000 | [diff] [blame] | 624 | checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>()); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 625 | |
| 626 | Lower->addRegularFile("/file1"); |
NAKAMURA Takumi | 4669bd4 | 2014-06-25 04:34:10 +0000 | [diff] [blame] | 627 | checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>("/file1")); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 628 | |
| 629 | Upper->addRegularFile("/file2"); |
Benjamin Kramer | 2a23e55 | 2016-01-10 10:45:19 +0000 | [diff] [blame] | 630 | checkContents(O->dir_begin("/", EC), {"/file2", "/file1"}); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 631 | |
| 632 | Lower->addDirectory("/dir1"); |
| 633 | Lower->addRegularFile("/dir1/foo"); |
| 634 | Upper->addDirectory("/dir2"); |
| 635 | Upper->addRegularFile("/dir2/foo"); |
NAKAMURA Takumi | 4669bd4 | 2014-06-25 04:34:10 +0000 | [diff] [blame] | 636 | checkContents(O->dir_begin("/dir2", EC), ArrayRef<StringRef>("/dir2/foo")); |
Benjamin Kramer | 2a23e55 | 2016-01-10 10:45:19 +0000 | [diff] [blame] | 637 | checkContents(O->dir_begin("/", EC), {"/dir2", "/file2", "/dir1", "/file1"}); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Ben Langmuir | 7c9f6c8 | 2014-06-25 20:25:40 +0000 | [diff] [blame] | 640 | TEST(VirtualFileSystemTest, OverlayRecursiveIteration) { |
| 641 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
| 642 | IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem()); |
| 643 | IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem()); |
| 644 | IntrusiveRefCntPtr<vfs::OverlayFileSystem> O( |
| 645 | new vfs::OverlayFileSystem(Lower)); |
| 646 | O->pushOverlay(Middle); |
| 647 | O->pushOverlay(Upper); |
| 648 | |
| 649 | std::error_code EC; |
| 650 | checkContents(vfs::recursive_directory_iterator(*O, "/", EC), |
| 651 | ArrayRef<StringRef>()); |
| 652 | |
| 653 | Lower->addRegularFile("/file1"); |
| 654 | checkContents(vfs::recursive_directory_iterator(*O, "/", EC), |
| 655 | ArrayRef<StringRef>("/file1")); |
| 656 | |
| 657 | Upper->addDirectory("/dir"); |
| 658 | Upper->addRegularFile("/dir/file2"); |
Benjamin Kramer | 2a23e55 | 2016-01-10 10:45:19 +0000 | [diff] [blame] | 659 | checkContents(vfs::recursive_directory_iterator(*O, "/", EC), |
| 660 | {"/dir", "/dir/file2", "/file1"}); |
Ben Langmuir | 7c9f6c8 | 2014-06-25 20:25:40 +0000 | [diff] [blame] | 661 | |
| 662 | Lower->addDirectory("/dir1"); |
| 663 | Lower->addRegularFile("/dir1/foo"); |
| 664 | Lower->addDirectory("/dir1/a"); |
| 665 | Lower->addRegularFile("/dir1/a/b"); |
| 666 | Middle->addDirectory("/a"); |
| 667 | Middle->addDirectory("/a/b"); |
| 668 | Middle->addDirectory("/a/b/c"); |
| 669 | Middle->addRegularFile("/a/b/c/d"); |
| 670 | Middle->addRegularFile("/hiddenByUp"); |
| 671 | Upper->addDirectory("/dir2"); |
| 672 | Upper->addRegularFile("/dir2/foo"); |
| 673 | Upper->addRegularFile("/hiddenByUp"); |
| 674 | checkContents(vfs::recursive_directory_iterator(*O, "/dir2", EC), |
| 675 | ArrayRef<StringRef>("/dir2/foo")); |
Benjamin Kramer | 2a23e55 | 2016-01-10 10:45:19 +0000 | [diff] [blame] | 676 | checkContents(vfs::recursive_directory_iterator(*O, "/", EC), |
| 677 | {"/dir", "/dir/file2", "/dir2", "/dir2/foo", "/hiddenByUp", |
| 678 | "/a", "/a/b", "/a/b/c", "/a/b/c/d", "/dir1", "/dir1/a", |
| 679 | "/dir1/a/b", "/dir1/foo", "/file1"}); |
Ben Langmuir | 7c9f6c8 | 2014-06-25 20:25:40 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 682 | TEST(VirtualFileSystemTest, ThreeLevelIteration) { |
| 683 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
| 684 | IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem()); |
| 685 | IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem()); |
| 686 | IntrusiveRefCntPtr<vfs::OverlayFileSystem> O( |
| 687 | new vfs::OverlayFileSystem(Lower)); |
| 688 | O->pushOverlay(Middle); |
| 689 | O->pushOverlay(Upper); |
| 690 | |
| 691 | std::error_code EC; |
NAKAMURA Takumi | 4669bd4 | 2014-06-25 04:34:10 +0000 | [diff] [blame] | 692 | checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>()); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 693 | |
| 694 | Middle->addRegularFile("/file2"); |
NAKAMURA Takumi | 4669bd4 | 2014-06-25 04:34:10 +0000 | [diff] [blame] | 695 | checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>("/file2")); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 696 | |
| 697 | Lower->addRegularFile("/file1"); |
| 698 | Upper->addRegularFile("/file3"); |
Benjamin Kramer | 2a23e55 | 2016-01-10 10:45:19 +0000 | [diff] [blame] | 699 | checkContents(O->dir_begin("/", EC), {"/file3", "/file2", "/file1"}); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | TEST(VirtualFileSystemTest, HiddenInIteration) { |
| 703 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
| 704 | IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem()); |
| 705 | IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem()); |
| 706 | IntrusiveRefCntPtr<vfs::OverlayFileSystem> O( |
| 707 | new vfs::OverlayFileSystem(Lower)); |
| 708 | O->pushOverlay(Middle); |
| 709 | O->pushOverlay(Upper); |
| 710 | |
| 711 | std::error_code EC; |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 712 | Lower->addRegularFile("/onlyInLow"); |
| 713 | Lower->addDirectory("/hiddenByMid"); |
| 714 | Lower->addDirectory("/hiddenByUp"); |
| 715 | Middle->addRegularFile("/onlyInMid"); |
| 716 | Middle->addRegularFile("/hiddenByMid"); |
| 717 | Middle->addDirectory("/hiddenByUp"); |
| 718 | Upper->addRegularFile("/onlyInUp"); |
| 719 | Upper->addRegularFile("/hiddenByUp"); |
Benjamin Kramer | 2a23e55 | 2016-01-10 10:45:19 +0000 | [diff] [blame] | 720 | checkContents( |
| 721 | O->dir_begin("/", EC), |
| 722 | {"/hiddenByUp", "/onlyInUp", "/hiddenByMid", "/onlyInMid", "/onlyInLow"}); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 723 | |
| 724 | // Make sure we get the top-most entry |
Ben Langmuir | efb8b60 | 2014-06-24 21:08:13 +0000 | [diff] [blame] | 725 | { |
| 726 | std::error_code EC; |
| 727 | vfs::directory_iterator I = O->dir_begin("/", EC), E; |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 728 | for (; !EC && I != E; I.increment(EC)) |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 729 | if (I->path() == "/hiddenByUp") |
Ben Langmuir | efb8b60 | 2014-06-24 21:08:13 +0000 | [diff] [blame] | 730 | break; |
| 731 | ASSERT_NE(E, I); |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 732 | EXPECT_EQ(sys::fs::file_type::regular_file, I->type()); |
Ben Langmuir | efb8b60 | 2014-06-24 21:08:13 +0000 | [diff] [blame] | 733 | } |
| 734 | { |
| 735 | std::error_code EC; |
| 736 | vfs::directory_iterator I = O->dir_begin("/", EC), E; |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 737 | for (; !EC && I != E; I.increment(EC)) |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 738 | if (I->path() == "/hiddenByMid") |
Ben Langmuir | efb8b60 | 2014-06-24 21:08:13 +0000 | [diff] [blame] | 739 | break; |
| 740 | ASSERT_NE(E, I); |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 741 | EXPECT_EQ(sys::fs::file_type::regular_file, I->type()); |
Ben Langmuir | efb8b60 | 2014-06-24 21:08:13 +0000 | [diff] [blame] | 742 | } |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Michael J. Spencer | 937a104 | 2018-12-17 22:30:05 +0000 | [diff] [blame] | 745 | TEST(ProxyFileSystemTest, Basic) { |
| 746 | IntrusiveRefCntPtr<vfs::InMemoryFileSystem> Base( |
| 747 | new vfs::InMemoryFileSystem()); |
| 748 | vfs::ProxyFileSystem PFS(Base); |
| 749 | |
| 750 | Base->addFile("/a", 0, MemoryBuffer::getMemBuffer("test")); |
| 751 | |
| 752 | auto Stat = PFS.status("/a"); |
| 753 | ASSERT_FALSE(Stat.getError()); |
| 754 | |
| 755 | auto File = PFS.openFileForRead("/a"); |
| 756 | ASSERT_FALSE(File.getError()); |
| 757 | EXPECT_EQ("test", (*(*File)->getBuffer("ignored"))->getBuffer()); |
| 758 | |
| 759 | std::error_code EC; |
| 760 | vfs::directory_iterator I = PFS.dir_begin("/", EC); |
| 761 | ASSERT_FALSE(EC); |
| 762 | ASSERT_EQ("/a", I->path()); |
| 763 | I.increment(EC); |
| 764 | ASSERT_FALSE(EC); |
| 765 | ASSERT_EQ(vfs::directory_iterator(), I); |
| 766 | |
| 767 | ASSERT_FALSE(PFS.setCurrentWorkingDirectory("/")); |
| 768 | |
| 769 | auto PWD = PFS.getCurrentWorkingDirectory(); |
| 770 | ASSERT_FALSE(PWD.getError()); |
| 771 | ASSERT_EQ("/", *PWD); |
| 772 | |
| 773 | SmallString<16> Path; |
| 774 | ASSERT_FALSE(PFS.getRealPath("a", Path)); |
| 775 | ASSERT_EQ("/a", Path); |
| 776 | |
| 777 | bool Local = true; |
| 778 | ASSERT_FALSE(PFS.isLocal("/a", Local)); |
| 779 | ASSERT_EQ(false, Local); |
| 780 | } |
| 781 | |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 782 | class InMemoryFileSystemTest : public ::testing::Test { |
| 783 | protected: |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 784 | llvm::vfs::InMemoryFileSystem FS; |
| 785 | llvm::vfs::InMemoryFileSystem NormalizedFS; |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 786 | |
| 787 | InMemoryFileSystemTest() |
| 788 | : FS(/*UseNormalizedPaths=*/false), |
| 789 | NormalizedFS(/*UseNormalizedPaths=*/true) {} |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 790 | }; |
| 791 | |
Ilya Biryukov | d5554c51 | 2018-09-04 14:15:53 +0000 | [diff] [blame] | 792 | MATCHER_P2(IsHardLinkTo, FS, Target, "") { |
| 793 | StringRef From = arg; |
| 794 | StringRef To = Target; |
| 795 | auto OpenedFrom = FS->openFileForRead(From); |
| 796 | auto OpenedTo = FS->openFileForRead(To); |
| 797 | return !OpenedFrom.getError() && !OpenedTo.getError() && |
| 798 | (*OpenedFrom)->status()->getUniqueID() == |
| 799 | (*OpenedTo)->status()->getUniqueID(); |
| 800 | } |
| 801 | |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 802 | TEST_F(InMemoryFileSystemTest, IsEmpty) { |
| 803 | auto Stat = FS.status("/a"); |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 804 | ASSERT_EQ(Stat.getError(), errc::no_such_file_or_directory) << FS.toString(); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 805 | Stat = FS.status("/"); |
Rafael Espindola | b7ab187 | 2015-10-05 20:20:50 +0000 | [diff] [blame] | 806 | ASSERT_EQ(Stat.getError(), errc::no_such_file_or_directory) << FS.toString(); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | TEST_F(InMemoryFileSystemTest, WindowsPath) { |
| 810 | FS.addFile("c:/windows/system128/foo.cpp", 0, MemoryBuffer::getMemBuffer("")); |
| 811 | auto Stat = FS.status("c:"); |
NAKAMURA Takumi | 4c33a1a | 2015-10-06 12:16:27 +0000 | [diff] [blame] | 812 | #if !defined(_WIN32) |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 813 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString(); |
NAKAMURA Takumi | 4c33a1a | 2015-10-06 12:16:27 +0000 | [diff] [blame] | 814 | #endif |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 815 | Stat = FS.status("c:/windows/system128/foo.cpp"); |
| 816 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString(); |
| 817 | FS.addFile("d:/windows/foo.cpp", 0, MemoryBuffer::getMemBuffer("")); |
| 818 | Stat = FS.status("d:/windows/foo.cpp"); |
| 819 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString(); |
| 820 | } |
| 821 | |
| 822 | TEST_F(InMemoryFileSystemTest, OverlayFile) { |
| 823 | FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a")); |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 824 | NormalizedFS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a")); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 825 | auto Stat = FS.status("/"); |
| 826 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString(); |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 827 | Stat = FS.status("/."); |
| 828 | ASSERT_FALSE(Stat); |
| 829 | Stat = NormalizedFS.status("/."); |
| 830 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString(); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 831 | Stat = FS.status("/a"); |
| 832 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); |
| 833 | ASSERT_EQ("/a", Stat->getName()); |
| 834 | } |
| 835 | |
Benjamin Kramer | 2e2351a | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 836 | TEST_F(InMemoryFileSystemTest, OverlayFileNoOwn) { |
| 837 | auto Buf = MemoryBuffer::getMemBuffer("a"); |
| 838 | FS.addFileNoOwn("/a", 0, Buf.get()); |
| 839 | auto Stat = FS.status("/a"); |
| 840 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); |
| 841 | ASSERT_EQ("/a", Stat->getName()); |
| 842 | } |
| 843 | |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 844 | TEST_F(InMemoryFileSystemTest, OpenFileForRead) { |
| 845 | FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a")); |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 846 | FS.addFile("././c", 0, MemoryBuffer::getMemBuffer("c")); |
| 847 | FS.addFile("./d/../d", 0, MemoryBuffer::getMemBuffer("d")); |
| 848 | NormalizedFS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a")); |
| 849 | NormalizedFS.addFile("././c", 0, MemoryBuffer::getMemBuffer("c")); |
| 850 | NormalizedFS.addFile("./d/../d", 0, MemoryBuffer::getMemBuffer("d")); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 851 | auto File = FS.openFileForRead("/a"); |
| 852 | ASSERT_EQ("a", (*(*File)->getBuffer("ignored"))->getBuffer()); |
| 853 | File = FS.openFileForRead("/a"); // Open again. |
| 854 | ASSERT_EQ("a", (*(*File)->getBuffer("ignored"))->getBuffer()); |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 855 | File = NormalizedFS.openFileForRead("/././a"); // Open again. |
| 856 | ASSERT_EQ("a", (*(*File)->getBuffer("ignored"))->getBuffer()); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 857 | File = FS.openFileForRead("/"); |
Rafael Espindola | b7ab187 | 2015-10-05 20:20:50 +0000 | [diff] [blame] | 858 | ASSERT_EQ(File.getError(), errc::invalid_argument) << FS.toString(); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 859 | File = FS.openFileForRead("/b"); |
Rafael Espindola | b7ab187 | 2015-10-05 20:20:50 +0000 | [diff] [blame] | 860 | ASSERT_EQ(File.getError(), errc::no_such_file_or_directory) << FS.toString(); |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 861 | File = FS.openFileForRead("./c"); |
| 862 | ASSERT_FALSE(File); |
| 863 | File = FS.openFileForRead("e/../d"); |
| 864 | ASSERT_FALSE(File); |
| 865 | File = NormalizedFS.openFileForRead("./c"); |
Benjamin Kramer | d5e0b58 | 2015-10-07 08:32:50 +0000 | [diff] [blame] | 866 | ASSERT_EQ("c", (*(*File)->getBuffer("ignored"))->getBuffer()); |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 867 | File = NormalizedFS.openFileForRead("e/../d"); |
| 868 | ASSERT_EQ("d", (*(*File)->getBuffer("ignored"))->getBuffer()); |
| 869 | } |
| 870 | |
| 871 | TEST_F(InMemoryFileSystemTest, DuplicatedFile) { |
| 872 | ASSERT_TRUE(FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a"))); |
| 873 | ASSERT_FALSE(FS.addFile("/a/b", 0, MemoryBuffer::getMemBuffer("a"))); |
| 874 | ASSERT_TRUE(FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a"))); |
| 875 | ASSERT_FALSE(FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("b"))); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | TEST_F(InMemoryFileSystemTest, DirectoryIteration) { |
| 879 | FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("")); |
| 880 | FS.addFile("/b/c", 0, MemoryBuffer::getMemBuffer("")); |
| 881 | |
| 882 | std::error_code EC; |
| 883 | vfs::directory_iterator I = FS.dir_begin("/", EC); |
| 884 | ASSERT_FALSE(EC); |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 885 | ASSERT_EQ("/a", I->path()); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 886 | I.increment(EC); |
| 887 | ASSERT_FALSE(EC); |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 888 | ASSERT_EQ("/b", I->path()); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 889 | I.increment(EC); |
| 890 | ASSERT_FALSE(EC); |
| 891 | ASSERT_EQ(vfs::directory_iterator(), I); |
| 892 | |
| 893 | I = FS.dir_begin("/b", EC); |
| 894 | ASSERT_FALSE(EC); |
Simon Marchi | ddbabc6 | 2018-08-06 21:48:20 +0000 | [diff] [blame] | 895 | // When on Windows, we end up with "/b\\c" as the name. Convert to Posix |
| 896 | // path for the sake of the comparison. |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 897 | ASSERT_EQ("/b/c", getPosixPath(I->path())); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 898 | I.increment(EC); |
| 899 | ASSERT_FALSE(EC); |
| 900 | ASSERT_EQ(vfs::directory_iterator(), I); |
| 901 | } |
| 902 | |
Benjamin Kramer | 1b8dbe3 | 2015-10-06 14:45:16 +0000 | [diff] [blame] | 903 | TEST_F(InMemoryFileSystemTest, WorkingDirectory) { |
| 904 | FS.setCurrentWorkingDirectory("/b"); |
| 905 | FS.addFile("c", 0, MemoryBuffer::getMemBuffer("")); |
| 906 | |
| 907 | auto Stat = FS.status("/b/c"); |
| 908 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); |
Simon Marchi | ddbabc6 | 2018-08-06 21:48:20 +0000 | [diff] [blame] | 909 | ASSERT_EQ("/b/c", Stat->getName()); |
Benjamin Kramer | 1b8dbe3 | 2015-10-06 14:45:16 +0000 | [diff] [blame] | 910 | ASSERT_EQ("/b", *FS.getCurrentWorkingDirectory()); |
| 911 | |
| 912 | Stat = FS.status("c"); |
| 913 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); |
Benjamin Kramer | e9e7607 | 2016-01-09 16:33:16 +0000 | [diff] [blame] | 914 | |
| 915 | NormalizedFS.setCurrentWorkingDirectory("/b/c"); |
| 916 | NormalizedFS.setCurrentWorkingDirectory("."); |
Simon Marchi | ddbabc6 | 2018-08-06 21:48:20 +0000 | [diff] [blame] | 917 | ASSERT_EQ("/b/c", |
| 918 | getPosixPath(NormalizedFS.getCurrentWorkingDirectory().get())); |
Benjamin Kramer | e9e7607 | 2016-01-09 16:33:16 +0000 | [diff] [blame] | 919 | NormalizedFS.setCurrentWorkingDirectory(".."); |
Simon Marchi | ddbabc6 | 2018-08-06 21:48:20 +0000 | [diff] [blame] | 920 | ASSERT_EQ("/b", |
| 921 | getPosixPath(NormalizedFS.getCurrentWorkingDirectory().get())); |
Benjamin Kramer | 1b8dbe3 | 2015-10-06 14:45:16 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Jonas Devlieghere | cbb5c86 | 2018-11-08 00:01:32 +0000 | [diff] [blame] | 924 | TEST_F(InMemoryFileSystemTest, IsLocal) { |
| 925 | FS.setCurrentWorkingDirectory("/b"); |
| 926 | FS.addFile("c", 0, MemoryBuffer::getMemBuffer("")); |
| 927 | |
| 928 | std::error_code EC; |
| 929 | bool IsLocal = true; |
| 930 | EC = FS.isLocal("c", IsLocal); |
| 931 | ASSERT_FALSE(EC); |
| 932 | ASSERT_FALSE(IsLocal); |
| 933 | } |
| 934 | |
Eric Liu | 43cb451 | 2018-05-24 13:52:48 +0000 | [diff] [blame] | 935 | #if !defined(_WIN32) |
Eric Liu | 33dd619 | 2018-05-24 11:17:00 +0000 | [diff] [blame] | 936 | TEST_F(InMemoryFileSystemTest, GetRealPath) { |
| 937 | SmallString<16> Path; |
| 938 | EXPECT_EQ(FS.getRealPath("b", Path), errc::operation_not_permitted); |
| 939 | |
| 940 | auto GetRealPath = [this](StringRef P) { |
| 941 | SmallString<16> Output; |
| 942 | auto EC = FS.getRealPath(P, Output); |
| 943 | EXPECT_FALSE(EC); |
| 944 | return Output.str().str(); |
| 945 | }; |
| 946 | |
| 947 | FS.setCurrentWorkingDirectory("a"); |
| 948 | EXPECT_EQ(GetRealPath("b"), "a/b"); |
| 949 | EXPECT_EQ(GetRealPath("../b"), "b"); |
| 950 | EXPECT_EQ(GetRealPath("b/./c"), "a/b/c"); |
| 951 | |
| 952 | FS.setCurrentWorkingDirectory("/a"); |
| 953 | EXPECT_EQ(GetRealPath("b"), "/a/b"); |
| 954 | EXPECT_EQ(GetRealPath("../b"), "/b"); |
| 955 | EXPECT_EQ(GetRealPath("b/./c"), "/a/b/c"); |
| 956 | } |
Eric Liu | 43cb451 | 2018-05-24 13:52:48 +0000 | [diff] [blame] | 957 | #endif // _WIN32 |
Eric Liu | 33dd619 | 2018-05-24 11:17:00 +0000 | [diff] [blame] | 958 | |
Ben Hamilton | e5af5bd | 2017-11-09 16:01:16 +0000 | [diff] [blame] | 959 | TEST_F(InMemoryFileSystemTest, AddFileWithUser) { |
| 960 | FS.addFile("/a/b/c", 0, MemoryBuffer::getMemBuffer("abc"), 0xFEEDFACE); |
| 961 | auto Stat = FS.status("/a"); |
| 962 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); |
| 963 | ASSERT_TRUE(Stat->isDirectory()); |
| 964 | ASSERT_EQ(0xFEEDFACE, Stat->getUser()); |
| 965 | Stat = FS.status("/a/b"); |
| 966 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); |
| 967 | ASSERT_TRUE(Stat->isDirectory()); |
| 968 | ASSERT_EQ(0xFEEDFACE, Stat->getUser()); |
| 969 | Stat = FS.status("/a/b/c"); |
| 970 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); |
| 971 | ASSERT_TRUE(Stat->isRegularFile()); |
| 972 | ASSERT_EQ(sys::fs::perms::all_all, Stat->getPermissions()); |
| 973 | ASSERT_EQ(0xFEEDFACE, Stat->getUser()); |
| 974 | } |
| 975 | |
| 976 | TEST_F(InMemoryFileSystemTest, AddFileWithGroup) { |
| 977 | FS.addFile("/a/b/c", 0, MemoryBuffer::getMemBuffer("abc"), None, 0xDABBAD00); |
| 978 | auto Stat = FS.status("/a"); |
| 979 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); |
| 980 | ASSERT_TRUE(Stat->isDirectory()); |
| 981 | ASSERT_EQ(0xDABBAD00, Stat->getGroup()); |
| 982 | Stat = FS.status("/a/b"); |
| 983 | ASSERT_TRUE(Stat->isDirectory()); |
| 984 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); |
| 985 | ASSERT_EQ(0xDABBAD00, Stat->getGroup()); |
| 986 | Stat = FS.status("/a/b/c"); |
| 987 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); |
| 988 | ASSERT_TRUE(Stat->isRegularFile()); |
| 989 | ASSERT_EQ(sys::fs::perms::all_all, Stat->getPermissions()); |
| 990 | ASSERT_EQ(0xDABBAD00, Stat->getGroup()); |
| 991 | } |
| 992 | |
| 993 | TEST_F(InMemoryFileSystemTest, AddFileWithFileType) { |
| 994 | FS.addFile("/a/b/c", 0, MemoryBuffer::getMemBuffer("abc"), None, None, |
| 995 | sys::fs::file_type::socket_file); |
| 996 | auto Stat = FS.status("/a"); |
| 997 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); |
| 998 | ASSERT_TRUE(Stat->isDirectory()); |
| 999 | Stat = FS.status("/a/b"); |
| 1000 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); |
| 1001 | ASSERT_TRUE(Stat->isDirectory()); |
| 1002 | Stat = FS.status("/a/b/c"); |
| 1003 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); |
| 1004 | ASSERT_EQ(sys::fs::file_type::socket_file, Stat->getType()); |
| 1005 | ASSERT_EQ(sys::fs::perms::all_all, Stat->getPermissions()); |
| 1006 | } |
| 1007 | |
| 1008 | TEST_F(InMemoryFileSystemTest, AddFileWithPerms) { |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1009 | FS.addFile("/a/b/c", 0, MemoryBuffer::getMemBuffer("abc"), None, None, None, |
| 1010 | sys::fs::perms::owner_read | sys::fs::perms::owner_write); |
Ben Hamilton | e5af5bd | 2017-11-09 16:01:16 +0000 | [diff] [blame] | 1011 | auto Stat = FS.status("/a"); |
| 1012 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); |
| 1013 | ASSERT_TRUE(Stat->isDirectory()); |
| 1014 | ASSERT_EQ(sys::fs::perms::owner_read | sys::fs::perms::owner_write | |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1015 | sys::fs::perms::owner_exe, |
| 1016 | Stat->getPermissions()); |
Ben Hamilton | e5af5bd | 2017-11-09 16:01:16 +0000 | [diff] [blame] | 1017 | Stat = FS.status("/a/b"); |
| 1018 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); |
| 1019 | ASSERT_TRUE(Stat->isDirectory()); |
| 1020 | ASSERT_EQ(sys::fs::perms::owner_read | sys::fs::perms::owner_write | |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1021 | sys::fs::perms::owner_exe, |
| 1022 | Stat->getPermissions()); |
Ben Hamilton | e5af5bd | 2017-11-09 16:01:16 +0000 | [diff] [blame] | 1023 | Stat = FS.status("/a/b/c"); |
| 1024 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); |
| 1025 | ASSERT_TRUE(Stat->isRegularFile()); |
| 1026 | ASSERT_EQ(sys::fs::perms::owner_read | sys::fs::perms::owner_write, |
| 1027 | Stat->getPermissions()); |
| 1028 | } |
| 1029 | |
Ben Hamilton | 7838101 | 2017-11-16 19:34:08 +0000 | [diff] [blame] | 1030 | TEST_F(InMemoryFileSystemTest, AddDirectoryThenAddChild) { |
| 1031 | FS.addFile("/a", 0, MemoryBuffer::getMemBuffer(""), /*User=*/None, |
| 1032 | /*Group=*/None, sys::fs::file_type::directory_file); |
| 1033 | FS.addFile("/a/b", 0, MemoryBuffer::getMemBuffer("abc"), /*User=*/None, |
| 1034 | /*Group=*/None, sys::fs::file_type::regular_file); |
| 1035 | auto Stat = FS.status("/a"); |
| 1036 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); |
| 1037 | ASSERT_TRUE(Stat->isDirectory()); |
| 1038 | Stat = FS.status("/a/b"); |
| 1039 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); |
| 1040 | ASSERT_TRUE(Stat->isRegularFile()); |
| 1041 | } |
| 1042 | |
Simon Marchi | ddbabc6 | 2018-08-06 21:48:20 +0000 | [diff] [blame] | 1043 | // Test that the name returned by status() is in the same form as the path that |
| 1044 | // was requested (to match the behavior of RealFileSystem). |
| 1045 | TEST_F(InMemoryFileSystemTest, StatusName) { |
| 1046 | NormalizedFS.addFile("/a/b/c", 0, MemoryBuffer::getMemBuffer("abc"), |
| 1047 | /*User=*/None, |
| 1048 | /*Group=*/None, sys::fs::file_type::regular_file); |
| 1049 | NormalizedFS.setCurrentWorkingDirectory("/a/b"); |
| 1050 | |
| 1051 | // Access using InMemoryFileSystem::status. |
| 1052 | auto Stat = NormalizedFS.status("../b/c"); |
| 1053 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" |
| 1054 | << NormalizedFS.toString(); |
| 1055 | ASSERT_TRUE(Stat->isRegularFile()); |
| 1056 | ASSERT_EQ("../b/c", Stat->getName()); |
| 1057 | |
| 1058 | // Access using InMemoryFileAdaptor::status. |
| 1059 | auto File = NormalizedFS.openFileForRead("../b/c"); |
| 1060 | ASSERT_FALSE(File.getError()) << File.getError() << "\n" |
| 1061 | << NormalizedFS.toString(); |
| 1062 | Stat = (*File)->status(); |
| 1063 | ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" |
| 1064 | << NormalizedFS.toString(); |
| 1065 | ASSERT_TRUE(Stat->isRegularFile()); |
| 1066 | ASSERT_EQ("../b/c", Stat->getName()); |
| 1067 | |
| 1068 | // Access using a directory iterator. |
| 1069 | std::error_code EC; |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1070 | llvm::vfs::directory_iterator It = NormalizedFS.dir_begin("../b", EC); |
Simon Marchi | ddbabc6 | 2018-08-06 21:48:20 +0000 | [diff] [blame] | 1071 | // When on Windows, we end up with "../b\\c" as the name. Convert to Posix |
| 1072 | // path for the sake of the comparison. |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 1073 | ASSERT_EQ("../b/c", getPosixPath(It->path())); |
Simon Marchi | ddbabc6 | 2018-08-06 21:48:20 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Ilya Biryukov | d5554c51 | 2018-09-04 14:15:53 +0000 | [diff] [blame] | 1076 | TEST_F(InMemoryFileSystemTest, AddHardLinkToFile) { |
| 1077 | StringRef FromLink = "/path/to/FROM/link"; |
| 1078 | StringRef Target = "/path/to/TO/file"; |
| 1079 | FS.addFile(Target, 0, MemoryBuffer::getMemBuffer("content of target")); |
| 1080 | EXPECT_TRUE(FS.addHardLink(FromLink, Target)); |
| 1081 | EXPECT_THAT(FromLink, IsHardLinkTo(&FS, Target)); |
| 1082 | EXPECT_TRUE(FS.status(FromLink)->getSize() == FS.status(Target)->getSize()); |
| 1083 | EXPECT_TRUE(FS.getBufferForFile(FromLink)->get()->getBuffer() == |
| 1084 | FS.getBufferForFile(Target)->get()->getBuffer()); |
| 1085 | } |
| 1086 | |
| 1087 | TEST_F(InMemoryFileSystemTest, AddHardLinkInChainPattern) { |
| 1088 | StringRef Link0 = "/path/to/0/link"; |
| 1089 | StringRef Link1 = "/path/to/1/link"; |
| 1090 | StringRef Link2 = "/path/to/2/link"; |
| 1091 | StringRef Target = "/path/to/target"; |
| 1092 | FS.addFile(Target, 0, MemoryBuffer::getMemBuffer("content of target file")); |
| 1093 | EXPECT_TRUE(FS.addHardLink(Link2, Target)); |
| 1094 | EXPECT_TRUE(FS.addHardLink(Link1, Link2)); |
| 1095 | EXPECT_TRUE(FS.addHardLink(Link0, Link1)); |
| 1096 | EXPECT_THAT(Link0, IsHardLinkTo(&FS, Target)); |
| 1097 | EXPECT_THAT(Link1, IsHardLinkTo(&FS, Target)); |
| 1098 | EXPECT_THAT(Link2, IsHardLinkTo(&FS, Target)); |
| 1099 | } |
| 1100 | |
| 1101 | TEST_F(InMemoryFileSystemTest, AddHardLinkToAFileThatWasNotAddedBefore) { |
| 1102 | EXPECT_FALSE(FS.addHardLink("/path/to/link", "/path/to/target")); |
| 1103 | } |
| 1104 | |
| 1105 | TEST_F(InMemoryFileSystemTest, AddHardLinkFromAFileThatWasAddedBefore) { |
| 1106 | StringRef Link = "/path/to/link"; |
| 1107 | StringRef Target = "/path/to/target"; |
| 1108 | FS.addFile(Target, 0, MemoryBuffer::getMemBuffer("content of target")); |
| 1109 | FS.addFile(Link, 0, MemoryBuffer::getMemBuffer("content of link")); |
| 1110 | EXPECT_FALSE(FS.addHardLink(Link, Target)); |
| 1111 | } |
| 1112 | |
| 1113 | TEST_F(InMemoryFileSystemTest, AddSameHardLinkMoreThanOnce) { |
| 1114 | StringRef Link = "/path/to/link"; |
| 1115 | StringRef Target = "/path/to/target"; |
| 1116 | FS.addFile(Target, 0, MemoryBuffer::getMemBuffer("content of target")); |
| 1117 | EXPECT_TRUE(FS.addHardLink(Link, Target)); |
| 1118 | EXPECT_FALSE(FS.addHardLink(Link, Target)); |
| 1119 | } |
| 1120 | |
| 1121 | TEST_F(InMemoryFileSystemTest, AddFileInPlaceOfAHardLinkWithSameContent) { |
| 1122 | StringRef Link = "/path/to/link"; |
| 1123 | StringRef Target = "/path/to/target"; |
| 1124 | StringRef Content = "content of target"; |
| 1125 | EXPECT_TRUE(FS.addFile(Target, 0, MemoryBuffer::getMemBuffer(Content))); |
| 1126 | EXPECT_TRUE(FS.addHardLink(Link, Target)); |
| 1127 | EXPECT_TRUE(FS.addFile(Link, 0, MemoryBuffer::getMemBuffer(Content))); |
| 1128 | } |
| 1129 | |
| 1130 | TEST_F(InMemoryFileSystemTest, AddFileInPlaceOfAHardLinkWithDifferentContent) { |
| 1131 | StringRef Link = "/path/to/link"; |
| 1132 | StringRef Target = "/path/to/target"; |
| 1133 | StringRef Content = "content of target"; |
| 1134 | StringRef LinkContent = "different content of link"; |
| 1135 | EXPECT_TRUE(FS.addFile(Target, 0, MemoryBuffer::getMemBuffer(Content))); |
| 1136 | EXPECT_TRUE(FS.addHardLink(Link, Target)); |
| 1137 | EXPECT_FALSE(FS.addFile(Link, 0, MemoryBuffer::getMemBuffer(LinkContent))); |
| 1138 | } |
| 1139 | |
| 1140 | TEST_F(InMemoryFileSystemTest, AddHardLinkToADirectory) { |
| 1141 | StringRef Dir = "path/to/dummy/dir"; |
| 1142 | StringRef Link = "/path/to/link"; |
| 1143 | StringRef File = "path/to/dummy/dir/target"; |
| 1144 | StringRef Content = "content of target"; |
| 1145 | EXPECT_TRUE(FS.addFile(File, 0, MemoryBuffer::getMemBuffer(Content))); |
| 1146 | EXPECT_FALSE(FS.addHardLink(Link, Dir)); |
| 1147 | } |
| 1148 | |
| 1149 | TEST_F(InMemoryFileSystemTest, AddHardLinkFromADirectory) { |
| 1150 | StringRef Dir = "path/to/dummy/dir"; |
| 1151 | StringRef Target = "path/to/dummy/dir/target"; |
| 1152 | StringRef Content = "content of target"; |
| 1153 | EXPECT_TRUE(FS.addFile(Target, 0, MemoryBuffer::getMemBuffer(Content))); |
| 1154 | EXPECT_FALSE(FS.addHardLink(Dir, Target)); |
| 1155 | } |
| 1156 | |
| 1157 | TEST_F(InMemoryFileSystemTest, AddHardLinkUnderAFile) { |
| 1158 | StringRef CommonContent = "content string"; |
| 1159 | FS.addFile("/a/b", 0, MemoryBuffer::getMemBuffer(CommonContent)); |
| 1160 | FS.addFile("/c/d", 0, MemoryBuffer::getMemBuffer(CommonContent)); |
| 1161 | EXPECT_FALSE(FS.addHardLink("/c/d/e", "/a/b")); |
| 1162 | } |
| 1163 | |
| 1164 | TEST_F(InMemoryFileSystemTest, RecursiveIterationWithHardLink) { |
| 1165 | std::error_code EC; |
| 1166 | FS.addFile("/a/b", 0, MemoryBuffer::getMemBuffer("content string")); |
| 1167 | EXPECT_TRUE(FS.addHardLink("/c/d", "/a/b")); |
| 1168 | auto I = vfs::recursive_directory_iterator(FS, "/", EC); |
| 1169 | ASSERT_FALSE(EC); |
| 1170 | std::vector<std::string> Nodes; |
| 1171 | for (auto E = vfs::recursive_directory_iterator(); !EC && I != E; |
| 1172 | I.increment(EC)) { |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 1173 | Nodes.push_back(getPosixPath(I->path())); |
Ilya Biryukov | d5554c51 | 2018-09-04 14:15:53 +0000 | [diff] [blame] | 1174 | } |
| 1175 | EXPECT_THAT(Nodes, testing::UnorderedElementsAre("/a", "/a/b", "/c", "/c/d")); |
| 1176 | } |
| 1177 | |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1178 | // NOTE: in the tests below, we use '//root/' as our root directory, since it is |
| 1179 | // a legal *absolute* path on Windows as well as *nix. |
Ben Langmuir | 97882e7 | 2014-02-24 20:56:37 +0000 | [diff] [blame] | 1180 | class VFSFromYAMLTest : public ::testing::Test { |
| 1181 | public: |
| 1182 | int NumDiagnostics; |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1183 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1184 | void SetUp() override { NumDiagnostics = 0; } |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1185 | |
Ben Langmuir | 97882e7 | 2014-02-24 20:56:37 +0000 | [diff] [blame] | 1186 | static void CountingDiagHandler(const SMDiagnostic &, void *Context) { |
| 1187 | VFSFromYAMLTest *Test = static_cast<VFSFromYAMLTest *>(Context); |
| 1188 | ++Test->NumDiagnostics; |
| 1189 | } |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1190 | |
Ben Langmuir | 97882e7 | 2014-02-24 20:56:37 +0000 | [diff] [blame] | 1191 | IntrusiveRefCntPtr<vfs::FileSystem> |
| 1192 | getFromYAMLRawString(StringRef Content, |
| 1193 | IntrusiveRefCntPtr<vfs::FileSystem> ExternalFS) { |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 1194 | std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer(Content); |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 1195 | return getVFSFromYAML(std::move(Buffer), CountingDiagHandler, "", this, |
Rafael Espindola | 91ac8df | 2014-08-17 23:27:13 +0000 | [diff] [blame] | 1196 | ExternalFS); |
Ben Langmuir | 97882e7 | 2014-02-24 20:56:37 +0000 | [diff] [blame] | 1197 | } |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1198 | |
Ben Langmuir | 97882e7 | 2014-02-24 20:56:37 +0000 | [diff] [blame] | 1199 | IntrusiveRefCntPtr<vfs::FileSystem> getFromYAMLString( |
| 1200 | StringRef Content, |
| 1201 | IntrusiveRefCntPtr<vfs::FileSystem> ExternalFS = new DummyFileSystem()) { |
| 1202 | std::string VersionPlusContent("{\n 'version':0,\n"); |
| 1203 | VersionPlusContent += Content.slice(Content.find('{') + 1, StringRef::npos); |
| 1204 | return getFromYAMLRawString(VersionPlusContent, ExternalFS); |
| 1205 | } |
Bruno Cardoso Lopes | f6a0a72 | 2016-05-12 19:13:07 +0000 | [diff] [blame] | 1206 | |
| 1207 | // This is intended as a "XFAIL" for windows hosts. |
| 1208 | bool supportsSameDirMultipleYAMLEntries() { |
| 1209 | Triple Host(Triple::normalize(sys::getProcessTriple())); |
| 1210 | return !Host.isOSWindows(); |
| 1211 | } |
Ben Langmuir | 97882e7 | 2014-02-24 20:56:37 +0000 | [diff] [blame] | 1212 | }; |
| 1213 | |
| 1214 | TEST_F(VFSFromYAMLTest, BasicVFSFromYAML) { |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1215 | IntrusiveRefCntPtr<vfs::FileSystem> FS; |
| 1216 | FS = getFromYAMLString(""); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1217 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1218 | FS = getFromYAMLString("[]"); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1219 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1220 | FS = getFromYAMLString("'string'"); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1221 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1222 | EXPECT_EQ(3, NumDiagnostics); |
| 1223 | } |
| 1224 | |
Ben Langmuir | 97882e7 | 2014-02-24 20:56:37 +0000 | [diff] [blame] | 1225 | TEST_F(VFSFromYAMLTest, MappedFiles) { |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1226 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1227 | Lower->addRegularFile("//root/foo/bar/a"); |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1228 | IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString( |
| 1229 | "{ 'roots': [\n" |
| 1230 | "{\n" |
| 1231 | " 'type': 'directory',\n" |
| 1232 | " 'name': '//root/',\n" |
| 1233 | " 'contents': [ {\n" |
| 1234 | " 'type': 'file',\n" |
| 1235 | " 'name': 'file1',\n" |
| 1236 | " 'external-contents': '//root/foo/bar/a'\n" |
| 1237 | " },\n" |
| 1238 | " {\n" |
| 1239 | " 'type': 'file',\n" |
| 1240 | " 'name': 'file2',\n" |
| 1241 | " 'external-contents': '//root/foo/b'\n" |
| 1242 | " }\n" |
| 1243 | " ]\n" |
| 1244 | "}\n" |
| 1245 | "]\n" |
| 1246 | "}", |
| 1247 | Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1248 | ASSERT_TRUE(FS.get() != nullptr); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1249 | |
| 1250 | IntrusiveRefCntPtr<vfs::OverlayFileSystem> O( |
| 1251 | new vfs::OverlayFileSystem(Lower)); |
| 1252 | O->pushOverlay(FS); |
| 1253 | |
| 1254 | // file |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1255 | ErrorOr<vfs::Status> S = O->status("//root/file1"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 1256 | ASSERT_FALSE(S.getError()); |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1257 | EXPECT_EQ("//root/foo/bar/a", S->getName()); |
Ben Langmuir | f13302e | 2015-12-10 23:41:39 +0000 | [diff] [blame] | 1258 | EXPECT_TRUE(S->IsVFSMapped); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1259 | |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1260 | ErrorOr<vfs::Status> SLower = O->status("//root/foo/bar/a"); |
| 1261 | EXPECT_EQ("//root/foo/bar/a", SLower->getName()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1262 | EXPECT_TRUE(S->equivalent(*SLower)); |
Ben Langmuir | f13302e | 2015-12-10 23:41:39 +0000 | [diff] [blame] | 1263 | EXPECT_FALSE(SLower->IsVFSMapped); |
| 1264 | |
| 1265 | // file after opening |
| 1266 | auto OpenedF = O->openFileForRead("//root/file1"); |
| 1267 | ASSERT_FALSE(OpenedF.getError()); |
| 1268 | auto OpenedS = (*OpenedF)->status(); |
| 1269 | ASSERT_FALSE(OpenedS.getError()); |
| 1270 | EXPECT_EQ("//root/foo/bar/a", OpenedS->getName()); |
| 1271 | EXPECT_TRUE(OpenedS->IsVFSMapped); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1272 | |
| 1273 | // directory |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1274 | S = O->status("//root/"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 1275 | ASSERT_FALSE(S.getError()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1276 | EXPECT_TRUE(S->isDirectory()); |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1277 | EXPECT_TRUE(S->equivalent(*O->status("//root/"))); // non-volatile UniqueID |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1278 | |
| 1279 | // broken mapping |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 1280 | EXPECT_EQ(O->status("//root/file2").getError(), |
| 1281 | llvm::errc::no_such_file_or_directory); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1282 | EXPECT_EQ(0, NumDiagnostics); |
| 1283 | } |
| 1284 | |
Ben Langmuir | 97882e7 | 2014-02-24 20:56:37 +0000 | [diff] [blame] | 1285 | TEST_F(VFSFromYAMLTest, CaseInsensitive) { |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1286 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1287 | Lower->addRegularFile("//root/foo/bar/a"); |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1288 | IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString( |
| 1289 | "{ 'case-sensitive': 'false',\n" |
| 1290 | " 'roots': [\n" |
| 1291 | "{\n" |
| 1292 | " 'type': 'directory',\n" |
| 1293 | " 'name': '//root/',\n" |
| 1294 | " 'contents': [ {\n" |
| 1295 | " 'type': 'file',\n" |
| 1296 | " 'name': 'XX',\n" |
| 1297 | " 'external-contents': '//root/foo/bar/a'\n" |
| 1298 | " }\n" |
| 1299 | " ]\n" |
| 1300 | "}]}", |
| 1301 | Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1302 | ASSERT_TRUE(FS.get() != nullptr); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1303 | |
| 1304 | IntrusiveRefCntPtr<vfs::OverlayFileSystem> O( |
| 1305 | new vfs::OverlayFileSystem(Lower)); |
| 1306 | O->pushOverlay(FS); |
| 1307 | |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1308 | ErrorOr<vfs::Status> S = O->status("//root/XX"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 1309 | ASSERT_FALSE(S.getError()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1310 | |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1311 | ErrorOr<vfs::Status> SS = O->status("//root/xx"); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 1312 | ASSERT_FALSE(SS.getError()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1313 | EXPECT_TRUE(S->equivalent(*SS)); |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1314 | SS = O->status("//root/xX"); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1315 | EXPECT_TRUE(S->equivalent(*SS)); |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1316 | SS = O->status("//root/Xx"); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1317 | EXPECT_TRUE(S->equivalent(*SS)); |
| 1318 | EXPECT_EQ(0, NumDiagnostics); |
| 1319 | } |
| 1320 | |
Ben Langmuir | 97882e7 | 2014-02-24 20:56:37 +0000 | [diff] [blame] | 1321 | TEST_F(VFSFromYAMLTest, CaseSensitive) { |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1322 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1323 | Lower->addRegularFile("//root/foo/bar/a"); |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1324 | IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString( |
| 1325 | "{ 'case-sensitive': 'true',\n" |
| 1326 | " 'roots': [\n" |
| 1327 | "{\n" |
| 1328 | " 'type': 'directory',\n" |
| 1329 | " 'name': '//root/',\n" |
| 1330 | " 'contents': [ {\n" |
| 1331 | " 'type': 'file',\n" |
| 1332 | " 'name': 'XX',\n" |
| 1333 | " 'external-contents': '//root/foo/bar/a'\n" |
| 1334 | " }\n" |
| 1335 | " ]\n" |
| 1336 | "}]}", |
| 1337 | Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1338 | ASSERT_TRUE(FS.get() != nullptr); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1339 | |
| 1340 | IntrusiveRefCntPtr<vfs::OverlayFileSystem> O( |
| 1341 | new vfs::OverlayFileSystem(Lower)); |
| 1342 | O->pushOverlay(FS); |
| 1343 | |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1344 | ErrorOr<vfs::Status> SS = O->status("//root/xx"); |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 1345 | EXPECT_EQ(SS.getError(), llvm::errc::no_such_file_or_directory); |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1346 | SS = O->status("//root/xX"); |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 1347 | EXPECT_EQ(SS.getError(), llvm::errc::no_such_file_or_directory); |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1348 | SS = O->status("//root/Xx"); |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 1349 | EXPECT_EQ(SS.getError(), llvm::errc::no_such_file_or_directory); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1350 | EXPECT_EQ(0, NumDiagnostics); |
| 1351 | } |
| 1352 | |
Ben Langmuir | 97882e7 | 2014-02-24 20:56:37 +0000 | [diff] [blame] | 1353 | TEST_F(VFSFromYAMLTest, IllegalVFSFile) { |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1354 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
| 1355 | |
| 1356 | // invalid YAML at top-level |
| 1357 | IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString("{]", Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1358 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1359 | // invalid YAML in roots |
| 1360 | FS = getFromYAMLString("{ 'roots':[}", Lower); |
| 1361 | // invalid YAML in directory |
| 1362 | FS = getFromYAMLString( |
| 1363 | "{ 'roots':[ { 'name': 'foo', 'type': 'directory', 'contents': [}", |
| 1364 | Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1365 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1366 | |
| 1367 | // invalid configuration |
| 1368 | FS = getFromYAMLString("{ 'knobular': 'true', 'roots':[] }", Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1369 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1370 | FS = getFromYAMLString("{ 'case-sensitive': 'maybe', 'roots':[] }", Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1371 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1372 | |
| 1373 | // invalid roots |
| 1374 | FS = getFromYAMLString("{ 'roots':'' }", Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1375 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1376 | FS = getFromYAMLString("{ 'roots':{} }", Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1377 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1378 | |
| 1379 | // invalid entries |
| 1380 | FS = getFromYAMLString( |
| 1381 | "{ 'roots':[ { 'type': 'other', 'name': 'me', 'contents': '' }", Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1382 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1383 | FS = getFromYAMLString("{ 'roots':[ { 'type': 'file', 'name': [], " |
| 1384 | "'external-contents': 'other' }", |
| 1385 | Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1386 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1387 | FS = getFromYAMLString( |
| 1388 | "{ 'roots':[ { 'type': 'file', 'name': 'me', 'external-contents': [] }", |
| 1389 | Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1390 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1391 | FS = getFromYAMLString( |
| 1392 | "{ 'roots':[ { 'type': 'file', 'name': 'me', 'external-contents': {} }", |
| 1393 | Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1394 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1395 | FS = getFromYAMLString( |
| 1396 | "{ 'roots':[ { 'type': 'directory', 'name': 'me', 'contents': {} }", |
| 1397 | Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1398 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1399 | FS = getFromYAMLString( |
| 1400 | "{ 'roots':[ { 'type': 'directory', 'name': 'me', 'contents': '' }", |
| 1401 | Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1402 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1403 | FS = getFromYAMLString( |
| 1404 | "{ 'roots':[ { 'thingy': 'directory', 'name': 'me', 'contents': [] }", |
| 1405 | Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1406 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1407 | |
| 1408 | // missing mandatory fields |
| 1409 | FS = getFromYAMLString("{ 'roots':[ { 'type': 'file', 'name': 'me' }", Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1410 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1411 | FS = getFromYAMLString( |
| 1412 | "{ 'roots':[ { 'type': 'file', 'external-contents': 'other' }", Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1413 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1414 | FS = getFromYAMLString("{ 'roots':[ { 'name': 'me', 'contents': [] }", Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1415 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1416 | |
| 1417 | // duplicate keys |
| 1418 | FS = getFromYAMLString("{ 'roots':[], 'roots':[] }", Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1419 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1420 | FS = getFromYAMLString( |
| 1421 | "{ 'case-sensitive':'true', 'case-sensitive':'true', 'roots':[] }", |
| 1422 | Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1423 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1424 | FS = |
| 1425 | getFromYAMLString("{ 'roots':[{'name':'me', 'name':'you', 'type':'file', " |
| 1426 | "'external-contents':'blah' } ] }", |
| 1427 | Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1428 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1429 | |
| 1430 | // missing version |
| 1431 | FS = getFromYAMLRawString("{ 'roots':[] }", Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1432 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1433 | |
| 1434 | // bad version number |
| 1435 | FS = getFromYAMLRawString("{ 'version':'foo', 'roots':[] }", Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1436 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1437 | FS = getFromYAMLRawString("{ 'version':-1, 'roots':[] }", Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1438 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1439 | FS = getFromYAMLRawString("{ 'version':100000, 'roots':[] }", Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1440 | EXPECT_EQ(nullptr, FS.get()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1441 | EXPECT_EQ(24, NumDiagnostics); |
| 1442 | } |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 1443 | |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 1444 | TEST_F(VFSFromYAMLTest, UseExternalName) { |
| 1445 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1446 | Lower->addRegularFile("//root/external/file"); |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 1447 | |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1448 | IntrusiveRefCntPtr<vfs::FileSystem> FS = |
| 1449 | getFromYAMLString("{ 'roots': [\n" |
| 1450 | " { 'type': 'file', 'name': '//root/A',\n" |
| 1451 | " 'external-contents': '//root/external/file'\n" |
| 1452 | " },\n" |
| 1453 | " { 'type': 'file', 'name': '//root/B',\n" |
| 1454 | " 'use-external-name': true,\n" |
| 1455 | " 'external-contents': '//root/external/file'\n" |
| 1456 | " },\n" |
| 1457 | " { 'type': 'file', 'name': '//root/C',\n" |
| 1458 | " 'use-external-name': false,\n" |
| 1459 | " 'external-contents': '//root/external/file'\n" |
| 1460 | " }\n" |
| 1461 | "] }", |
| 1462 | Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1463 | ASSERT_TRUE(nullptr != FS.get()); |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 1464 | |
| 1465 | // default true |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1466 | EXPECT_EQ("//root/external/file", FS->status("//root/A")->getName()); |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 1467 | // explicit |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1468 | EXPECT_EQ("//root/external/file", FS->status("//root/B")->getName()); |
| 1469 | EXPECT_EQ("//root/C", FS->status("//root/C")->getName()); |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 1470 | |
| 1471 | // global configuration |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1472 | FS = getFromYAMLString("{ 'use-external-names': false,\n" |
| 1473 | " 'roots': [\n" |
| 1474 | " { 'type': 'file', 'name': '//root/A',\n" |
| 1475 | " 'external-contents': '//root/external/file'\n" |
| 1476 | " },\n" |
| 1477 | " { 'type': 'file', 'name': '//root/B',\n" |
| 1478 | " 'use-external-name': true,\n" |
| 1479 | " 'external-contents': '//root/external/file'\n" |
| 1480 | " },\n" |
| 1481 | " { 'type': 'file', 'name': '//root/C',\n" |
| 1482 | " 'use-external-name': false,\n" |
| 1483 | " 'external-contents': '//root/external/file'\n" |
| 1484 | " }\n" |
| 1485 | "] }", |
| 1486 | Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1487 | ASSERT_TRUE(nullptr != FS.get()); |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 1488 | |
| 1489 | // default |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1490 | EXPECT_EQ("//root/A", FS->status("//root/A")->getName()); |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 1491 | // explicit |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1492 | EXPECT_EQ("//root/external/file", FS->status("//root/B")->getName()); |
| 1493 | EXPECT_EQ("//root/C", FS->status("//root/C")->getName()); |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 1496 | TEST_F(VFSFromYAMLTest, MultiComponentPath) { |
| 1497 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1498 | Lower->addRegularFile("//root/other"); |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 1499 | |
| 1500 | // file in roots |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1501 | IntrusiveRefCntPtr<vfs::FileSystem> FS = |
| 1502 | getFromYAMLString("{ 'roots': [\n" |
| 1503 | " { 'type': 'file', 'name': '//root/path/to/file',\n" |
| 1504 | " 'external-contents': '//root/other' }]\n" |
| 1505 | "}", |
| 1506 | Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1507 | ASSERT_TRUE(nullptr != FS.get()); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 1508 | EXPECT_FALSE(FS->status("//root/path/to/file").getError()); |
| 1509 | EXPECT_FALSE(FS->status("//root/path/to").getError()); |
| 1510 | EXPECT_FALSE(FS->status("//root/path").getError()); |
| 1511 | EXPECT_FALSE(FS->status("//root/").getError()); |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 1512 | |
| 1513 | // at the start |
| 1514 | FS = getFromYAMLString( |
| 1515 | "{ 'roots': [\n" |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1516 | " { 'type': 'directory', 'name': '//root/path/to',\n" |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 1517 | " 'contents': [ { 'type': 'file', 'name': 'file',\n" |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1518 | " 'external-contents': '//root/other' }]}]\n" |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1519 | "}", |
| 1520 | Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1521 | ASSERT_TRUE(nullptr != FS.get()); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 1522 | EXPECT_FALSE(FS->status("//root/path/to/file").getError()); |
| 1523 | EXPECT_FALSE(FS->status("//root/path/to").getError()); |
| 1524 | EXPECT_FALSE(FS->status("//root/path").getError()); |
| 1525 | EXPECT_FALSE(FS->status("//root/").getError()); |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 1526 | |
| 1527 | // at the end |
| 1528 | FS = getFromYAMLString( |
| 1529 | "{ 'roots': [\n" |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1530 | " { 'type': 'directory', 'name': '//root/',\n" |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 1531 | " 'contents': [ { 'type': 'file', 'name': 'path/to/file',\n" |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1532 | " 'external-contents': '//root/other' }]}]\n" |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1533 | "}", |
| 1534 | Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1535 | ASSERT_TRUE(nullptr != FS.get()); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 1536 | EXPECT_FALSE(FS->status("//root/path/to/file").getError()); |
| 1537 | EXPECT_FALSE(FS->status("//root/path/to").getError()); |
| 1538 | EXPECT_FALSE(FS->status("//root/path").getError()); |
| 1539 | EXPECT_FALSE(FS->status("//root/").getError()); |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | TEST_F(VFSFromYAMLTest, TrailingSlashes) { |
| 1543 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1544 | Lower->addRegularFile("//root/other"); |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 1545 | |
| 1546 | // file in roots |
| 1547 | IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString( |
| 1548 | "{ 'roots': [\n" |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1549 | " { 'type': 'directory', 'name': '//root/path/to////',\n" |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 1550 | " 'contents': [ { 'type': 'file', 'name': 'file',\n" |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1551 | " 'external-contents': '//root/other' }]}]\n" |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1552 | "}", |
| 1553 | Lower); |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1554 | ASSERT_TRUE(nullptr != FS.get()); |
Rafael Espindola | 3ae0620 | 2014-05-31 03:20:52 +0000 | [diff] [blame] | 1555 | EXPECT_FALSE(FS->status("//root/path/to/file").getError()); |
| 1556 | EXPECT_FALSE(FS->status("//root/path/to").getError()); |
| 1557 | EXPECT_FALSE(FS->status("//root/path").getError()); |
| 1558 | EXPECT_FALSE(FS->status("//root/").getError()); |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 1559 | } |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 1560 | |
| 1561 | TEST_F(VFSFromYAMLTest, DirectoryIteration) { |
| 1562 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
| 1563 | Lower->addDirectory("//root/"); |
| 1564 | Lower->addDirectory("//root/foo"); |
| 1565 | Lower->addDirectory("//root/foo/bar"); |
| 1566 | Lower->addRegularFile("//root/foo/bar/a"); |
| 1567 | Lower->addRegularFile("//root/foo/bar/b"); |
| 1568 | Lower->addRegularFile("//root/file3"); |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1569 | IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString( |
| 1570 | "{ 'use-external-names': false,\n" |
| 1571 | " 'roots': [\n" |
| 1572 | "{\n" |
| 1573 | " 'type': 'directory',\n" |
| 1574 | " 'name': '//root/',\n" |
| 1575 | " 'contents': [ {\n" |
| 1576 | " 'type': 'file',\n" |
| 1577 | " 'name': 'file1',\n" |
| 1578 | " 'external-contents': '//root/foo/bar/a'\n" |
| 1579 | " },\n" |
| 1580 | " {\n" |
| 1581 | " 'type': 'file',\n" |
| 1582 | " 'name': 'file2',\n" |
| 1583 | " 'external-contents': '//root/foo/bar/b'\n" |
| 1584 | " }\n" |
| 1585 | " ]\n" |
| 1586 | "}\n" |
| 1587 | "]\n" |
| 1588 | "}", |
| 1589 | Lower); |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1590 | ASSERT_TRUE(FS.get() != nullptr); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 1591 | |
| 1592 | IntrusiveRefCntPtr<vfs::OverlayFileSystem> O( |
| 1593 | new vfs::OverlayFileSystem(Lower)); |
| 1594 | O->pushOverlay(FS); |
| 1595 | |
| 1596 | std::error_code EC; |
Benjamin Kramer | 2a23e55 | 2016-01-10 10:45:19 +0000 | [diff] [blame] | 1597 | checkContents(O->dir_begin("//root/", EC), |
Bruno Cardoso Lopes | 26092b2 | 2016-05-12 04:43:27 +0000 | [diff] [blame] | 1598 | {"//root/file1", "//root/file2", "//root/file3", "//root/foo"}); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 1599 | |
Benjamin Kramer | 2a23e55 | 2016-01-10 10:45:19 +0000 | [diff] [blame] | 1600 | checkContents(O->dir_begin("//root/foo/bar", EC), |
| 1601 | {"//root/foo/bar/a", "//root/foo/bar/b"}); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 1602 | } |
Bruno Cardoso Lopes | f6a0a72 | 2016-05-12 19:13:07 +0000 | [diff] [blame] | 1603 | |
| 1604 | TEST_F(VFSFromYAMLTest, DirectoryIterationSameDirMultipleEntries) { |
| 1605 | // https://llvm.org/bugs/show_bug.cgi?id=27725 |
| 1606 | if (!supportsSameDirMultipleYAMLEntries()) |
| 1607 | return; |
| 1608 | |
| 1609 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
| 1610 | Lower->addDirectory("//root/zab"); |
| 1611 | Lower->addDirectory("//root/baz"); |
| 1612 | Lower->addRegularFile("//root/zab/a"); |
| 1613 | Lower->addRegularFile("//root/zab/b"); |
| 1614 | IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString( |
| 1615 | "{ 'use-external-names': false,\n" |
| 1616 | " 'roots': [\n" |
| 1617 | "{\n" |
| 1618 | " 'type': 'directory',\n" |
| 1619 | " 'name': '//root/baz/',\n" |
| 1620 | " 'contents': [ {\n" |
| 1621 | " 'type': 'file',\n" |
| 1622 | " 'name': 'x',\n" |
| 1623 | " 'external-contents': '//root/zab/a'\n" |
| 1624 | " }\n" |
| 1625 | " ]\n" |
| 1626 | "},\n" |
| 1627 | "{\n" |
| 1628 | " 'type': 'directory',\n" |
| 1629 | " 'name': '//root/baz/',\n" |
| 1630 | " 'contents': [ {\n" |
| 1631 | " 'type': 'file',\n" |
| 1632 | " 'name': 'y',\n" |
| 1633 | " 'external-contents': '//root/zab/b'\n" |
| 1634 | " }\n" |
| 1635 | " ]\n" |
| 1636 | "}\n" |
| 1637 | "]\n" |
| 1638 | "}", |
| 1639 | Lower); |
| 1640 | ASSERT_TRUE(FS.get() != nullptr); |
| 1641 | |
| 1642 | IntrusiveRefCntPtr<vfs::OverlayFileSystem> O( |
| 1643 | new vfs::OverlayFileSystem(Lower)); |
| 1644 | O->pushOverlay(FS); |
| 1645 | |
| 1646 | std::error_code EC; |
| 1647 | |
| 1648 | checkContents(O->dir_begin("//root/baz/", EC), |
| 1649 | {"//root/baz/x", "//root/baz/y"}); |
| 1650 | } |
Bruno Cardoso Lopes | 32b2897 | 2016-05-14 00:00:18 +0000 | [diff] [blame] | 1651 | |
| 1652 | TEST_F(VFSFromYAMLTest, RecursiveDirectoryIterationLevel) { |
| 1653 | |
| 1654 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
| 1655 | Lower->addDirectory("//root/a"); |
| 1656 | Lower->addDirectory("//root/a/b"); |
| 1657 | Lower->addDirectory("//root/a/b/c"); |
| 1658 | Lower->addRegularFile("//root/a/b/c/file"); |
| 1659 | IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString( |
| 1660 | "{ 'use-external-names': false,\n" |
| 1661 | " 'roots': [\n" |
| 1662 | "{\n" |
| 1663 | " 'type': 'directory',\n" |
| 1664 | " 'name': '//root/a/b/c/',\n" |
| 1665 | " 'contents': [ {\n" |
| 1666 | " 'type': 'file',\n" |
| 1667 | " 'name': 'file',\n" |
| 1668 | " 'external-contents': '//root/a/b/c/file'\n" |
| 1669 | " }\n" |
| 1670 | " ]\n" |
| 1671 | "},\n" |
| 1672 | "]\n" |
| 1673 | "}", |
| 1674 | Lower); |
| 1675 | ASSERT_TRUE(FS.get() != nullptr); |
| 1676 | |
| 1677 | IntrusiveRefCntPtr<vfs::OverlayFileSystem> O( |
| 1678 | new vfs::OverlayFileSystem(Lower)); |
| 1679 | O->pushOverlay(FS); |
| 1680 | |
| 1681 | std::error_code EC; |
| 1682 | |
| 1683 | // Test recursive_directory_iterator level() |
| 1684 | vfs::recursive_directory_iterator I = vfs::recursive_directory_iterator( |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1685 | *O, "//root", EC), |
| 1686 | E; |
Bruno Cardoso Lopes | 32b2897 | 2016-05-14 00:00:18 +0000 | [diff] [blame] | 1687 | ASSERT_FALSE(EC); |
| 1688 | for (int l = 0; I != E; I.increment(EC), ++l) { |
| 1689 | ASSERT_FALSE(EC); |
| 1690 | EXPECT_EQ(I.level(), l); |
| 1691 | } |
| 1692 | EXPECT_EQ(I, E); |
| 1693 | } |
Volodymyr Sapsai | 3b2f6a4 | 2018-08-07 19:05:41 +0000 | [diff] [blame] | 1694 | |
| 1695 | TEST_F(VFSFromYAMLTest, RelativePaths) { |
| 1696 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
| 1697 | // Filename at root level without a parent directory. |
| 1698 | IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString( |
| 1699 | "{ 'roots': [\n" |
| 1700 | " { 'type': 'file', 'name': 'file-not-in-directory.h',\n" |
| 1701 | " 'external-contents': '//root/external/file'\n" |
| 1702 | " }\n" |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1703 | "] }", |
| 1704 | Lower); |
Volodymyr Sapsai | 3b2f6a4 | 2018-08-07 19:05:41 +0000 | [diff] [blame] | 1705 | EXPECT_EQ(nullptr, FS.get()); |
| 1706 | |
| 1707 | // Relative file path. |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1708 | FS = getFromYAMLString("{ 'roots': [\n" |
| 1709 | " { 'type': 'file', 'name': 'relative/file/path.h',\n" |
| 1710 | " 'external-contents': '//root/external/file'\n" |
| 1711 | " }\n" |
| 1712 | "] }", |
| 1713 | Lower); |
Volodymyr Sapsai | 3b2f6a4 | 2018-08-07 19:05:41 +0000 | [diff] [blame] | 1714 | EXPECT_EQ(nullptr, FS.get()); |
| 1715 | |
| 1716 | // Relative directory path. |
| 1717 | FS = getFromYAMLString( |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 1718 | "{ 'roots': [\n" |
| 1719 | " { 'type': 'directory', 'name': 'relative/directory/path.h',\n" |
| 1720 | " 'contents': []\n" |
| 1721 | " }\n" |
| 1722 | "] }", |
| 1723 | Lower); |
Volodymyr Sapsai | 3b2f6a4 | 2018-08-07 19:05:41 +0000 | [diff] [blame] | 1724 | EXPECT_EQ(nullptr, FS.get()); |
| 1725 | |
| 1726 | EXPECT_EQ(3, NumDiagnostics); |
| 1727 | } |
Volodymyr Sapsai | 91e1316 | 2018-10-26 22:14:33 +0000 | [diff] [blame] | 1728 | |
| 1729 | TEST_F(VFSFromYAMLTest, NonFallthroughDirectoryIteration) { |
| 1730 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
| 1731 | Lower->addDirectory("//root/"); |
| 1732 | Lower->addRegularFile("//root/a"); |
| 1733 | Lower->addRegularFile("//root/b"); |
| 1734 | IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString( |
| 1735 | "{ 'use-external-names': false,\n" |
| 1736 | " 'fallthrough': false,\n" |
| 1737 | " 'roots': [\n" |
| 1738 | "{\n" |
| 1739 | " 'type': 'directory',\n" |
| 1740 | " 'name': '//root/',\n" |
| 1741 | " 'contents': [ {\n" |
| 1742 | " 'type': 'file',\n" |
| 1743 | " 'name': 'c',\n" |
| 1744 | " 'external-contents': '//root/a'\n" |
| 1745 | " }\n" |
| 1746 | " ]\n" |
| 1747 | "}\n" |
| 1748 | "]\n" |
| 1749 | "}", |
| 1750 | Lower); |
| 1751 | ASSERT_TRUE(FS.get() != nullptr); |
| 1752 | |
| 1753 | std::error_code EC; |
| 1754 | checkContents(FS->dir_begin("//root/", EC), |
| 1755 | {"//root/c"}); |
| 1756 | } |
| 1757 | |
| 1758 | TEST_F(VFSFromYAMLTest, DirectoryIterationWithDuplicates) { |
| 1759 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
| 1760 | Lower->addDirectory("//root/"); |
| 1761 | Lower->addRegularFile("//root/a"); |
| 1762 | Lower->addRegularFile("//root/b"); |
| 1763 | IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString( |
| 1764 | "{ 'use-external-names': false,\n" |
| 1765 | " 'roots': [\n" |
| 1766 | "{\n" |
| 1767 | " 'type': 'directory',\n" |
| 1768 | " 'name': '//root/',\n" |
| 1769 | " 'contents': [ {\n" |
| 1770 | " 'type': 'file',\n" |
| 1771 | " 'name': 'a',\n" |
| 1772 | " 'external-contents': '//root/a'\n" |
| 1773 | " }\n" |
| 1774 | " ]\n" |
| 1775 | "}\n" |
| 1776 | "]\n" |
| 1777 | "}", |
| 1778 | Lower); |
| 1779 | ASSERT_TRUE(FS.get() != nullptr); |
| 1780 | |
| 1781 | std::error_code EC; |
| 1782 | checkContents(FS->dir_begin("//root/", EC), |
| 1783 | {"//root/a", "//root/b"}); |
| 1784 | } |
| 1785 | |
| 1786 | TEST_F(VFSFromYAMLTest, DirectoryIterationErrorInVFSLayer) { |
| 1787 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
| 1788 | Lower->addDirectory("//root/"); |
| 1789 | Lower->addDirectory("//root/foo"); |
| 1790 | Lower->addRegularFile("//root/foo/a"); |
| 1791 | Lower->addRegularFile("//root/foo/b"); |
| 1792 | IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString( |
| 1793 | "{ 'use-external-names': false,\n" |
| 1794 | " 'roots': [\n" |
| 1795 | "{\n" |
| 1796 | " 'type': 'directory',\n" |
| 1797 | " 'name': '//root/',\n" |
| 1798 | " 'contents': [ {\n" |
| 1799 | " 'type': 'file',\n" |
| 1800 | " 'name': 'bar/a',\n" |
| 1801 | " 'external-contents': '//root/foo/a'\n" |
| 1802 | " }\n" |
| 1803 | " ]\n" |
| 1804 | "}\n" |
| 1805 | "]\n" |
| 1806 | "}", |
| 1807 | Lower); |
| 1808 | ASSERT_TRUE(FS.get() != nullptr); |
| 1809 | |
| 1810 | std::error_code EC; |
| 1811 | checkContents(FS->dir_begin("//root/foo", EC), |
| 1812 | {"//root/foo/a", "//root/foo/b"}); |
| 1813 | } |
Volodymyr Sapsai | 7610033 | 2018-11-16 01:15:54 +0000 | [diff] [blame] | 1814 | |
| 1815 | TEST_F(VFSFromYAMLTest, GetRealPath) { |
| 1816 | IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem()); |
Volodymyr Sapsai | ab73426 | 2018-11-16 02:20:33 +0000 | [diff] [blame] | 1817 | Lower->addDirectory("//dir/"); |
Volodymyr Sapsai | 7610033 | 2018-11-16 01:15:54 +0000 | [diff] [blame] | 1818 | Lower->addRegularFile("/foo"); |
| 1819 | Lower->addSymlink("/link"); |
| 1820 | IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString( |
| 1821 | "{ 'use-external-names': false,\n" |
| 1822 | " 'roots': [\n" |
| 1823 | "{\n" |
| 1824 | " 'type': 'directory',\n" |
Volodymyr Sapsai | ab73426 | 2018-11-16 02:20:33 +0000 | [diff] [blame] | 1825 | " 'name': '//root/',\n" |
Volodymyr Sapsai | 7610033 | 2018-11-16 01:15:54 +0000 | [diff] [blame] | 1826 | " 'contents': [ {\n" |
| 1827 | " 'type': 'file',\n" |
| 1828 | " 'name': 'bar',\n" |
| 1829 | " 'external-contents': '/link'\n" |
| 1830 | " }\n" |
| 1831 | " ]\n" |
| 1832 | "},\n" |
| 1833 | "{\n" |
| 1834 | " 'type': 'directory',\n" |
Volodymyr Sapsai | ab73426 | 2018-11-16 02:20:33 +0000 | [diff] [blame] | 1835 | " 'name': '//dir/',\n" |
Volodymyr Sapsai | 7610033 | 2018-11-16 01:15:54 +0000 | [diff] [blame] | 1836 | " 'contents': []\n" |
| 1837 | "}\n" |
| 1838 | "]\n" |
| 1839 | "}", |
| 1840 | Lower); |
| 1841 | ASSERT_TRUE(FS.get() != nullptr); |
| 1842 | |
| 1843 | // Regular file present in underlying file system. |
| 1844 | SmallString<16> RealPath; |
| 1845 | EXPECT_FALSE(FS->getRealPath("/foo", RealPath)); |
| 1846 | EXPECT_EQ(RealPath.str(), "/foo"); |
| 1847 | |
| 1848 | // File present in YAML pointing to symlink in underlying file system. |
Volodymyr Sapsai | ab73426 | 2018-11-16 02:20:33 +0000 | [diff] [blame] | 1849 | EXPECT_FALSE(FS->getRealPath("//root/bar", RealPath)); |
Volodymyr Sapsai | 7610033 | 2018-11-16 01:15:54 +0000 | [diff] [blame] | 1850 | EXPECT_EQ(RealPath.str(), "/symlink"); |
| 1851 | |
| 1852 | // Directories should fall back to the underlying file system is possible. |
Volodymyr Sapsai | ab73426 | 2018-11-16 02:20:33 +0000 | [diff] [blame] | 1853 | EXPECT_FALSE(FS->getRealPath("//dir/", RealPath)); |
| 1854 | EXPECT_EQ(RealPath.str(), "//dir/"); |
Volodymyr Sapsai | 7610033 | 2018-11-16 01:15:54 +0000 | [diff] [blame] | 1855 | |
| 1856 | // Try a non-existing file. |
| 1857 | EXPECT_EQ(FS->getRealPath("/non_existing", RealPath), |
| 1858 | errc::no_such_file_or_directory); |
| 1859 | } |