Jonas Devlieghere | 4657517 | 2019-01-29 20:36:38 +0000 | [diff] [blame] | 1 | //===-- FileCollectorTest.cpp -----------------------------------*- C++ -*-===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "gmock/gmock.h" |
| 10 | #include "gtest/gtest.h" |
| 11 | |
Alex Lorenz | 86814bf | 2019-07-24 22:59:20 +0000 | [diff] [blame^] | 12 | #include "llvm/Support/FileCollector.h" |
Jonas Devlieghere | 4657517 | 2019-01-29 20:36:38 +0000 | [diff] [blame] | 13 | #include "llvm/Support/FileSystem.h" |
| 14 | |
| 15 | using namespace llvm; |
Jonas Devlieghere | 4657517 | 2019-01-29 20:36:38 +0000 | [diff] [blame] | 16 | |
| 17 | namespace llvm { |
| 18 | namespace vfs { |
| 19 | inline bool operator==(const llvm::vfs::YAMLVFSEntry &LHS, |
| 20 | const llvm::vfs::YAMLVFSEntry &RHS) { |
| 21 | return LHS.VPath == RHS.VPath && LHS.RPath == RHS.RPath; |
| 22 | } |
| 23 | } // namespace vfs |
| 24 | } // namespace llvm |
| 25 | |
| 26 | namespace { |
| 27 | class TestingFileCollector : public FileCollector { |
| 28 | public: |
| 29 | using FileCollector::FileCollector; |
| 30 | using FileCollector::m_root; |
| 31 | using FileCollector::m_seen; |
| 32 | using FileCollector::m_symlink_map; |
| 33 | using FileCollector::m_vfs_writer; |
| 34 | |
Alex Lorenz | 86814bf | 2019-07-24 22:59:20 +0000 | [diff] [blame^] | 35 | bool HasSeen(StringRef fs) { |
| 36 | return m_seen.find(fs) != m_seen.end(); |
Jonas Devlieghere | 4657517 | 2019-01-29 20:36:38 +0000 | [diff] [blame] | 37 | } |
| 38 | }; |
| 39 | |
| 40 | struct ScopedDir { |
| 41 | SmallString<128> Path; |
| 42 | ScopedDir(const Twine &Name, bool Unique = false) { |
| 43 | std::error_code EC; |
| 44 | if (Unique) { |
| 45 | EC = llvm::sys::fs::createUniqueDirectory(Name, Path); |
| 46 | } else { |
| 47 | Path = Name.str(); |
| 48 | EC = llvm::sys::fs::create_directory(Twine(Path)); |
| 49 | } |
| 50 | if (EC) |
| 51 | Path = ""; |
| 52 | EXPECT_FALSE(EC); |
| 53 | // Ensure the path is the real path so tests can use it to compare against |
| 54 | // realpath output. |
| 55 | SmallString<128> RealPath; |
| 56 | if (!llvm::sys::fs::real_path(Path, RealPath)) |
| 57 | Path.swap(RealPath); |
| 58 | } |
| 59 | ~ScopedDir() { |
| 60 | if (Path != "") { |
| 61 | EXPECT_FALSE(llvm::sys::fs::remove_directories(Path.str())); |
| 62 | } |
| 63 | } |
| 64 | operator StringRef() { return Path.str(); } |
| 65 | }; |
| 66 | |
| 67 | struct ScopedLink { |
| 68 | SmallString<128> Path; |
| 69 | ScopedLink(const Twine &To, const Twine &From) { |
| 70 | Path = From.str(); |
| 71 | std::error_code EC = sys::fs::create_link(To, From); |
| 72 | if (EC) |
| 73 | Path = ""; |
| 74 | EXPECT_FALSE(EC); |
| 75 | } |
| 76 | ~ScopedLink() { |
| 77 | if (Path != "") { |
| 78 | EXPECT_FALSE(llvm::sys::fs::remove(Path.str())); |
| 79 | } |
| 80 | } |
| 81 | operator StringRef() { return Path.str(); } |
| 82 | }; |
| 83 | |
| 84 | struct ScopedFile { |
| 85 | SmallString<128> Path; |
| 86 | ScopedFile(const Twine &Name) { |
| 87 | std::error_code EC; |
| 88 | EC = llvm::sys::fs::createUniqueFile(Name, Path); |
| 89 | if (EC) |
| 90 | Path = ""; |
| 91 | EXPECT_FALSE(EC); |
| 92 | } |
| 93 | ~ScopedFile() { |
| 94 | if (Path != "") { |
| 95 | EXPECT_FALSE(llvm::sys::fs::remove(Path.str())); |
| 96 | } |
| 97 | } |
| 98 | operator StringRef() { return Path.str(); } |
| 99 | }; |
| 100 | } // end anonymous namespace |
| 101 | |
| 102 | TEST(FileCollectorTest, AddFile) { |
| 103 | ScopedDir root("add_file_root", true); |
Alex Lorenz | 86814bf | 2019-07-24 22:59:20 +0000 | [diff] [blame^] | 104 | std::string root_fs = root.Path.str(); |
Jonas Devlieghere | c470ac5 | 2019-06-18 16:20:17 +0000 | [diff] [blame] | 105 | TestingFileCollector file_collector(root_fs, root_fs); |
Jonas Devlieghere | 4657517 | 2019-01-29 20:36:38 +0000 | [diff] [blame] | 106 | |
Alex Lorenz | 86814bf | 2019-07-24 22:59:20 +0000 | [diff] [blame^] | 107 | file_collector.AddFile("/path/to/a"); |
| 108 | file_collector.AddFile("/path/to/b"); |
| 109 | file_collector.AddFile("/path/to/c"); |
Jonas Devlieghere | 4657517 | 2019-01-29 20:36:38 +0000 | [diff] [blame] | 110 | |
| 111 | // Make sure the root is correct. |
| 112 | EXPECT_EQ(file_collector.m_root, root_fs); |
| 113 | |
| 114 | // Make sure we've seen all the added files. |
Alex Lorenz | 86814bf | 2019-07-24 22:59:20 +0000 | [diff] [blame^] | 115 | EXPECT_TRUE(file_collector.HasSeen("/path/to/a")); |
| 116 | EXPECT_TRUE(file_collector.HasSeen("/path/to/b")); |
| 117 | EXPECT_TRUE(file_collector.HasSeen("/path/to/c")); |
Jonas Devlieghere | 4657517 | 2019-01-29 20:36:38 +0000 | [diff] [blame] | 118 | |
| 119 | // Make sure we've only seen the added files. |
Alex Lorenz | 86814bf | 2019-07-24 22:59:20 +0000 | [diff] [blame^] | 120 | EXPECT_FALSE(file_collector.HasSeen("/path/to/d")); |
Jonas Devlieghere | 4657517 | 2019-01-29 20:36:38 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TEST(FileCollectorTest, CopyFiles) { |
| 124 | ScopedDir file_root("file_root", true); |
| 125 | ScopedFile a(file_root + "/aaa"); |
| 126 | ScopedFile b(file_root + "/bbb"); |
| 127 | ScopedFile c(file_root + "/ccc"); |
| 128 | |
| 129 | // Create file collector and add files. |
| 130 | ScopedDir root("copy_files_root", true); |
Alex Lorenz | 86814bf | 2019-07-24 22:59:20 +0000 | [diff] [blame^] | 131 | std::string root_fs = root.Path.str(); |
Jonas Devlieghere | c470ac5 | 2019-06-18 16:20:17 +0000 | [diff] [blame] | 132 | TestingFileCollector file_collector(root_fs, root_fs); |
Jonas Devlieghere | 4657517 | 2019-01-29 20:36:38 +0000 | [diff] [blame] | 133 | file_collector.AddFile(a.Path); |
| 134 | file_collector.AddFile(b.Path); |
| 135 | file_collector.AddFile(c.Path); |
| 136 | |
| 137 | // Make sure we can copy the files. |
| 138 | std::error_code ec = file_collector.CopyFiles(true); |
| 139 | EXPECT_FALSE(ec); |
| 140 | |
| 141 | // Now add a bogus file and make sure we error out. |
| 142 | file_collector.AddFile("/some/bogus/file"); |
| 143 | ec = file_collector.CopyFiles(true); |
| 144 | EXPECT_TRUE(ec); |
| 145 | |
| 146 | // However, if stop_on_error is true the copy should still succeed. |
| 147 | ec = file_collector.CopyFiles(false); |
| 148 | EXPECT_FALSE(ec); |
| 149 | } |
| 150 | |
Jonas Devlieghere | 7d2192c | 2019-01-29 22:53:47 +0000 | [diff] [blame] | 151 | #ifndef _WIN32 |
Jonas Devlieghere | 4657517 | 2019-01-29 20:36:38 +0000 | [diff] [blame] | 152 | TEST(FileCollectorTest, Symlinks) { |
| 153 | // Root where the original files live. |
| 154 | ScopedDir file_root("file_root", true); |
| 155 | |
| 156 | // Create some files in the file root. |
| 157 | ScopedFile a(file_root + "/aaa"); |
| 158 | ScopedFile b(file_root + "/bbb"); |
| 159 | ScopedFile c(file_root + "/ccc"); |
| 160 | |
| 161 | // Create a directory foo with file ddd. |
| 162 | ScopedDir foo(file_root + "/foo"); |
| 163 | ScopedFile d(foo + "/ddd"); |
| 164 | |
| 165 | // Create a file eee in the foo's parent directory. |
| 166 | ScopedFile e(foo + "/../eee"); |
| 167 | |
| 168 | // Create a symlink bar pointing to foo. |
| 169 | ScopedLink symlink(file_root + "/foo", file_root + "/bar"); |
| 170 | |
| 171 | // Root where files are copied to. |
| 172 | ScopedDir reproducer_root("reproducer_root", true); |
Alex Lorenz | 86814bf | 2019-07-24 22:59:20 +0000 | [diff] [blame^] | 173 | std::string root_fs = reproducer_root.Path.str(); |
Jonas Devlieghere | c470ac5 | 2019-06-18 16:20:17 +0000 | [diff] [blame] | 174 | TestingFileCollector file_collector(root_fs, root_fs); |
Jonas Devlieghere | 4657517 | 2019-01-29 20:36:38 +0000 | [diff] [blame] | 175 | |
| 176 | // Add all the files to the collector. |
| 177 | file_collector.AddFile(a.Path); |
| 178 | file_collector.AddFile(b.Path); |
| 179 | file_collector.AddFile(c.Path); |
| 180 | file_collector.AddFile(d.Path); |
| 181 | file_collector.AddFile(e.Path); |
| 182 | file_collector.AddFile(file_root + "/bar/ddd"); |
| 183 | |
| 184 | auto mapping = file_collector.m_vfs_writer.getMappings(); |
| 185 | |
| 186 | { |
| 187 | // Make sure the common case works. |
| 188 | std::string vpath = (file_root + "/aaa").str(); |
| 189 | std::string rpath = (reproducer_root.Path + file_root.Path + "/aaa").str(); |
| 190 | printf("%s -> %s\n", vpath.c_str(), rpath.c_str()); |
| 191 | EXPECT_THAT(mapping, testing::Contains(vfs::YAMLVFSEntry(vpath, rpath))); |
| 192 | } |
| 193 | |
| 194 | { |
| 195 | // Make sure the virtual path points to the real source path. |
| 196 | std::string vpath = (file_root + "/bar/ddd").str(); |
| 197 | std::string rpath = |
| 198 | (reproducer_root.Path + file_root.Path + "/foo/ddd").str(); |
| 199 | printf("%s -> %s\n", vpath.c_str(), rpath.c_str()); |
| 200 | EXPECT_THAT(mapping, testing::Contains(vfs::YAMLVFSEntry(vpath, rpath))); |
| 201 | } |
| 202 | |
| 203 | { |
| 204 | // Make sure that .. is removed from the source path. |
| 205 | std::string vpath = (file_root + "/eee").str(); |
| 206 | std::string rpath = (reproducer_root.Path + file_root.Path + "/eee").str(); |
| 207 | printf("%s -> %s\n", vpath.c_str(), rpath.c_str()); |
| 208 | EXPECT_THAT(mapping, testing::Contains(vfs::YAMLVFSEntry(vpath, rpath))); |
| 209 | } |
| 210 | } |
| 211 | #endif |