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