1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
//===-- FileCollectorTest.cpp -----------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "llvm/Support/FileCollector.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Testing/Support/SupportHelpers.h"
using namespace llvm;
using llvm::unittest::TempDir;
using llvm::unittest::TempFile;
using llvm::unittest::TempLink;
namespace llvm {
namespace vfs {
inline bool operator==(const llvm::vfs::YAMLVFSEntry &LHS,
const llvm::vfs::YAMLVFSEntry &RHS) {
return LHS.VPath == RHS.VPath && LHS.RPath == RHS.RPath;
}
} // namespace vfs
} // namespace llvm
namespace {
class TestingFileCollector : public FileCollector {
public:
using FileCollector::FileCollector;
using FileCollector::Root;
using FileCollector::Seen;
using FileCollector::VFSWriter;
bool hasSeen(StringRef fs) {
return Seen.find(fs) != Seen.end();
}
};
} // end anonymous namespace
TEST(FileCollectorTest, addFile) {
TempDir root("add_file_root", /*Unique*/ true);
std::string root_fs(root.path());
TestingFileCollector FileCollector(root_fs, root_fs);
FileCollector.addFile("/path/to/a");
FileCollector.addFile("/path/to/b");
FileCollector.addFile("/path/to/c");
// Make sure the root is correct.
EXPECT_EQ(FileCollector.Root, root_fs);
// Make sure we've seen all the added files.
EXPECT_TRUE(FileCollector.hasSeen("/path/to/a"));
EXPECT_TRUE(FileCollector.hasSeen("/path/to/b"));
EXPECT_TRUE(FileCollector.hasSeen("/path/to/c"));
// Make sure we've only seen the added files.
EXPECT_FALSE(FileCollector.hasSeen("/path/to/d"));
}
TEST(FileCollectorTest, addDirectory) {
TempDir file_root("file_root", /*Unique*/ true);
llvm::SmallString<128> aaa(file_root.path());
llvm::sys::path::append(aaa, "aaa");
TempFile a(aaa.str());
llvm::SmallString<128> bbb(file_root.path());
llvm::sys::path::append(bbb, "bbb");
TempFile b(bbb.str());
llvm::SmallString<128> ccc(file_root.path());
llvm::sys::path::append(ccc, "ccc");
TempFile c(ccc.str());
std::string root_fs(file_root.path());
TestingFileCollector FileCollector(root_fs, root_fs);
FileCollector.addDirectory(file_root.path());
// Make sure the root is correct.
EXPECT_EQ(FileCollector.Root, root_fs);
// Make sure we've seen all the added files.
EXPECT_TRUE(FileCollector.hasSeen(a.path()));
EXPECT_TRUE(FileCollector.hasSeen(b.path()));
EXPECT_TRUE(FileCollector.hasSeen(c.path()));
// Make sure we've only seen the added files.
llvm::SmallString<128> ddd(file_root.path());
llvm::sys::path::append(ddd, "ddd");
TempFile d(ddd);
EXPECT_FALSE(FileCollector.hasSeen(d.path()));
}
TEST(FileCollectorTest, copyFiles) {
TempDir file_root("file_root", /*Unique*/ true);
TempFile a(file_root.path("aaa"));
TempFile b(file_root.path("bbb"));
TempFile c(file_root.path("ccc"));
// Create file collector and add files.
TempDir root("copy_files_root", /*Unique*/ true);
std::string root_fs(root.path());
TestingFileCollector FileCollector(root_fs, root_fs);
FileCollector.addFile(a.path());
FileCollector.addFile(b.path());
FileCollector.addFile(c.path());
// Make sure we can copy the files.
std::error_code ec = FileCollector.copyFiles(true);
EXPECT_FALSE(ec);
// Now add a bogus file and make sure we error out.
FileCollector.addFile("/some/bogus/file");
ec = FileCollector.copyFiles(true);
EXPECT_TRUE(ec);
// However, if stop_on_error is true the copy should still succeed.
ec = FileCollector.copyFiles(false);
EXPECT_FALSE(ec);
}
TEST(FileCollectorTest, recordAndConstructDirectory) {
TempDir file_root("dir_root", /*Unique*/ true);
TempDir subdir(file_root.path("subdir"));
TempDir subdir2(file_root.path("subdir2"));
TempFile a(subdir2.path("a"));
// Create file collector and add files.
TempDir root("copy_files_root", /*Unique*/ true);
std::string root_fs(root.path());
TestingFileCollector FileCollector(root_fs, root_fs);
FileCollector.addFile(a.path());
// The empty directory isn't seen until we add it.
EXPECT_TRUE(FileCollector.hasSeen(a.path()));
EXPECT_FALSE(FileCollector.hasSeen(subdir.path()));
FileCollector.addFile(subdir.path());
EXPECT_TRUE(FileCollector.hasSeen(subdir.path()));
// Make sure we can construct the directory.
std::error_code ec = FileCollector.copyFiles(true);
EXPECT_FALSE(ec);
bool IsDirectory = false;
llvm::SmallString<128> SubdirInRoot = root.path();
llvm::sys::path::append(SubdirInRoot,
llvm::sys::path::relative_path(subdir.path()));
ec = sys::fs::is_directory(SubdirInRoot, IsDirectory);
EXPECT_FALSE(ec);
ASSERT_TRUE(IsDirectory);
}
TEST(FileCollectorTest, recordVFSAccesses) {
TempDir file_root("dir_root", /*Unique*/ true);
TempDir subdir(file_root.path("subdir"));
TempDir subdir2(file_root.path("subdir2"));
TempFile a(subdir2.path("a"));
TempFile b(file_root.path("b"));
TempDir subdir3(file_root.path("subdir3"));
TempFile subdir3a(subdir3.path("aa"));
TempDir subdir3b(subdir3.path("subdirb"));
{ TempFile subdir3fileremoved(subdir3.path("removed")); }
// Create file collector and add files.
TempDir root("copy_files_root", /*Unique*/ true);
std::string root_fs(root.path());
auto Collector = std::make_shared<TestingFileCollector>(root_fs, root_fs);
auto VFS =
FileCollector::createCollectorVFS(vfs::getRealFileSystem(), Collector);
VFS->status(a.path());
EXPECT_TRUE(Collector->hasSeen(a.path()));
VFS->openFileForRead(b.path());
EXPECT_TRUE(Collector->hasSeen(b.path()));
VFS->status(subdir.path());
EXPECT_TRUE(Collector->hasSeen(subdir.path()));
#ifndef _WIN32
std::error_code EC;
auto It = VFS->dir_begin(subdir3.path(), EC);
EXPECT_FALSE(EC);
EXPECT_TRUE(Collector->hasSeen(subdir3.path()));
EXPECT_TRUE(Collector->hasSeen(subdir3a.path()));
EXPECT_TRUE(Collector->hasSeen(subdir3b.path()));
std::string RemovedFileName((Twine(subdir3.path("removed"))).str());
EXPECT_FALSE(Collector->hasSeen(RemovedFileName));
#endif
}
#ifndef _WIN32
TEST(FileCollectorTest, Symlinks) {
// Root where the original files live.
TempDir file_root("file_root", /*Unique*/ true);
// Create some files in the file root.
TempFile a(file_root.path("aaa"));
TempFile b(file_root.path("bbb"));
TempFile c(file_root.path("ccc"));
// Create a directory foo with file ddd.
TempDir foo(file_root.path("foo"));
TempFile d(foo.path("ddd"));
// Create a file eee in the foo's parent directory.
TempFile e(foo.path("../eee"));
// Create a symlink bar pointing to foo.
TempLink symlink(file_root.path("foo"), file_root.path("bar"));
// Root where files are copied to.
TempDir reproducer_root("reproducer_root", /*Unique*/ true);
std::string root_fs(reproducer_root.path());
TestingFileCollector FileCollector(root_fs, root_fs);
// Add all the files to the collector.
FileCollector.addFile(a.path());
FileCollector.addFile(b.path());
FileCollector.addFile(c.path());
FileCollector.addFile(d.path());
FileCollector.addFile(e.path());
FileCollector.addFile(file_root.path() + "/bar/ddd");
auto mapping = FileCollector.VFSWriter.getMappings();
{
// Make sure the common case works.
std::string vpath = (file_root.path() + "/aaa").str();
std::string rpath =
(reproducer_root.path() + file_root.path() + "/aaa").str();
printf("%s -> %s\n", vpath.c_str(), rpath.c_str());
EXPECT_THAT(mapping, testing::Contains(vfs::YAMLVFSEntry(vpath, rpath)));
}
{
// Make sure the virtual path points to the real source path.
std::string vpath = (file_root.path() + "/bar/ddd").str();
std::string rpath =
(reproducer_root.path() + file_root.path() + "/foo/ddd").str();
printf("%s -> %s\n", vpath.c_str(), rpath.c_str());
EXPECT_THAT(mapping, testing::Contains(vfs::YAMLVFSEntry(vpath, rpath)));
}
{
// Make sure that .. is removed from the source path.
std::string vpath = (file_root.path() + "/eee").str();
std::string rpath =
(reproducer_root.path() + file_root.path() + "/eee").str();
printf("%s -> %s\n", vpath.c_str(), rpath.c_str());
EXPECT_THAT(mapping, testing::Contains(vfs::YAMLVFSEntry(vpath, rpath)));
}
}
TEST(FileCollectorTest, recordVFSSymlinkAccesses) {
TempDir file_root("dir_root", /*Unique*/ true);
TempFile a(file_root.path("a"));
TempLink symlink(file_root.path("a"), file_root.path("b"));
// Create file collector and add files.
TempDir root("copy_files_root", true);
std::string root_fs(root.path());
auto Collector = std::make_shared<TestingFileCollector>(root_fs, root_fs);
auto VFS =
FileCollector::createCollectorVFS(vfs::getRealFileSystem(), Collector);
SmallString<256> Output;
VFS->getRealPath(symlink.path(), Output);
EXPECT_TRUE(Collector->hasSeen(a.path()));
EXPECT_TRUE(Collector->hasSeen(symlink.path()));
}
#endif
|