1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 12:41:49 +01:00

[llvm] [unittests] Remove temporary files after they're not needed

Some LLVM unit tests forget to clean up temporary files and
directories. Introduce RAII classes for cleaning them up.

Refactor the tests to use those classes.

Differential Revision: https://reviews.llvm.org/D83228
This commit is contained in:
Sergej Jaskiewicz 2020-07-06 15:55:49 +03:00
parent e10dc0379d
commit 0975a7654a
10 changed files with 464 additions and 484 deletions

View File

@ -12,6 +12,8 @@
#include "llvm/ADT/Optional.h" #include "llvm/ADT/Optional.h"
#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallString.h"
#include "llvm/Support/Error.h" #include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_os_ostream.h" #include "llvm/Support/raw_os_ostream.h"
#include "gmock/gmock-matchers.h" #include "gmock/gmock-matchers.h"
#include "gtest/gtest-printers.h" #include "gtest/gtest-printers.h"
@ -103,7 +105,140 @@ detail::ValueIsMatcher<InnerMatcher> ValueIs(const InnerMatcher &ValueMatcher) {
return detail::ValueIsMatcher<InnerMatcher>(ValueMatcher); return detail::ValueIsMatcher<InnerMatcher>(ValueMatcher);
} }
namespace unittest { namespace unittest {
SmallString<128> getInputFileDirectory(const char *Argv0); SmallString<128> getInputFileDirectory(const char *Argv0);
/// A RAII object that creates a temporary directory upon initialization and
/// removes it upon destruction.
class TempDir {
SmallString<128> Path;
public:
/// Creates a managed temporary directory.
///
/// @param Name The name of the directory to create.
/// @param Unique If true, the directory will be created using
/// llvm::sys::fs::createUniqueDirectory.
explicit TempDir(StringRef Name, bool Unique = false) {
std::error_code EC;
if (Unique) {
EC = llvm::sys::fs::createUniqueDirectory(Name, Path);
if (!EC) {
// Resolve any symlinks in the new directory.
std::string UnresolvedPath(Path.str());
EC = llvm::sys::fs::real_path(UnresolvedPath, Path);
}
} else {
Path = Name;
EC = llvm::sys::fs::create_directory(Path);
}
if (EC)
Path.clear();
EXPECT_FALSE(EC) << EC.message();
}
~TempDir() {
if (!Path.empty()) {
EXPECT_FALSE(llvm::sys::fs::remove_directories(Path.str()));
}
}
TempDir(const TempDir &) = delete;
TempDir &operator=(const TempDir &) = delete;
TempDir(TempDir &&) = default;
TempDir &operator=(TempDir &&) = default;
/// The path to the temporary directory.
StringRef path() const { return Path; }
/// Creates a new path by appending the argument to the path of the managed
/// directory using the native path separator.
SmallString<128> path(StringRef component) const {
SmallString<128> Result(Path);
SmallString<128> ComponentToAppend(component);
llvm::sys::path::native(ComponentToAppend);
llvm::sys::path::append(Result, Twine(ComponentToAppend));
return Result;
}
};
/// A RAII object that creates a link upon initialization and
/// removes it upon destruction.
///
/// The link may be a soft or a hard link, depending on the platform.
class TempLink {
SmallString<128> Path;
public:
/// Creates a managed link at path Link pointing to Target.
TempLink(StringRef Target, StringRef Link) {
Path = Link;
std::error_code EC = sys::fs::create_link(Target, Link);
if (EC)
Path.clear();
EXPECT_FALSE(EC);
}
~TempLink() {
if (!Path.empty()) {
EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
}
}
TempLink(const TempLink &) = delete;
TempLink &operator=(const TempLink &) = delete;
TempLink(TempLink &&) = default;
TempLink &operator=(TempLink &&) = default;
/// The path to the link.
StringRef path() const { return Path; }
};
/// A RAII object that creates a file upon initialization and
/// removes it upon destruction.
class TempFile {
SmallString<128> Path;
public:
/// Creates a managed file.
///
/// @param Name The name of the file to create.
/// @param Contents The string to write to the file.
/// @param Unique If true, the file will be created using
/// llvm::sys::fs::createTemporaryFile.
TempFile(StringRef Name, StringRef Suffix = "", StringRef Contents = "",
bool Unique = false) {
std::error_code EC;
int fd;
if (Unique) {
EC = llvm::sys::fs::createTemporaryFile(Name, Suffix, fd, Path);
} else {
Path = Name;
if (!Suffix.empty()) {
Path.append(".");
Path.append(Suffix);
}
EC = llvm::sys::fs::openFileForWrite(Path, fd);
}
EXPECT_FALSE(EC);
raw_fd_ostream OS(fd, /*shouldClose*/ true);
OS << Contents;
OS.flush();
EXPECT_FALSE(OS.error());
if (EC || OS.error())
Path.clear();
}
~TempFile() {
if (!Path.empty()) {
EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
}
}
/// The path to the file.
StringRef path() const { return Path; }
};
} // namespace unittest } // namespace unittest
} // namespace llvm } // namespace llvm

View File

@ -19,6 +19,7 @@
#include "llvm/Support/FileSystem.h" #include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/Testing/Support/SupportHelpers.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <string> #include <string>
#include <vector> #include <vector>
@ -26,6 +27,8 @@
using namespace llvm; using namespace llvm;
using namespace sampleprof; using namespace sampleprof;
using llvm::unittest::TempFile;
static ::testing::AssertionResult NoError(std::error_code EC) { static ::testing::AssertionResult NoError(std::error_code EC) {
if (!EC) if (!EC)
return ::testing::AssertionSuccess(); return ::testing::AssertionSuccess();
@ -60,21 +63,14 @@ struct SampleProfTest : ::testing::Test {
Reader->collectFuncsFrom(M); Reader->collectFuncsFrom(M);
} }
void createRemapFile(SmallVectorImpl<char> &RemapPath, StringRef &RemapFile) { TempFile createRemapFile() {
std::error_code EC = return TempFile("remapfile", "", R"(
llvm::sys::fs::createTemporaryFile("remapfile", "", RemapPath);
ASSERT_TRUE(NoError(EC));
RemapFile = StringRef(RemapPath.data(), RemapPath.size());
std::unique_ptr<raw_fd_ostream> OS(
new raw_fd_ostream(RemapFile, EC, sys::fs::OF_None));
*OS << R"(
# Types 'int' and 'long' are equivalent # Types 'int' and 'long' are equivalent
type i l type i l
# Function names 'foo' and 'faux' are equivalent # Function names 'foo' and 'faux' are equivalent
name 3foo 4faux name 3foo 4faux
)"; )",
OS->close(); /*Unique*/ true);
} }
// Verify profile summary is consistent in the roundtrip to and from // Verify profile summary is consistent in the roundtrip to and from
@ -137,10 +133,8 @@ struct SampleProfTest : ::testing::Test {
} }
void testRoundTrip(SampleProfileFormat Format, bool Remap, bool UseMD5) { void testRoundTrip(SampleProfileFormat Format, bool Remap, bool UseMD5) {
SmallVector<char, 128> ProfilePath; TempFile ProfileFile("profile", "", "", /*Unique*/ true);
ASSERT_TRUE(NoError(llvm::sys::fs::createTemporaryFile("profile", "", ProfilePath))); createWriter(Format, ProfileFile.path());
StringRef Profile(ProfilePath.data(), ProfilePath.size());
createWriter(Format, Profile);
if (Format == SampleProfileFormat::SPF_Ext_Binary && UseMD5) if (Format == SampleProfileFormat::SPF_Ext_Binary && UseMD5)
static_cast<SampleProfileWriterExtBinary *>(Writer.get())->setUseMD5(); static_cast<SampleProfileWriterExtBinary *>(Writer.get())->setUseMD5();
@ -207,10 +201,8 @@ struct SampleProfTest : ::testing::Test {
FunctionType *fn_type = FunctionType *fn_type =
FunctionType::get(Type::getVoidTy(Context), {}, false); FunctionType::get(Type::getVoidTy(Context), {}, false);
SmallVector<char, 128> RemapPath; TempFile RemapFile(createRemapFile());
StringRef RemapFile;
if (Remap) { if (Remap) {
createRemapFile(RemapPath, RemapFile);
FooName = "_Z4fauxi"; FooName = "_Z4fauxi";
BarName = "_Z3barl"; BarName = "_Z3barl";
GooName = "_Z3gool"; GooName = "_Z3gool";
@ -234,7 +226,7 @@ struct SampleProfTest : ::testing::Test {
Writer->getOutputStream().flush(); Writer->getOutputStream().flush();
readProfile(M, Profile, RemapFile); readProfile(M, ProfileFile.path(), RemapFile.path());
EC = Reader->read(); EC = Reader->read();
ASSERT_TRUE(NoError(EC)); ASSERT_TRUE(NoError(EC));
@ -375,24 +367,21 @@ struct SampleProfTest : ::testing::Test {
void testSuffixElisionPolicy(SampleProfileFormat Format, StringRef Policy, void testSuffixElisionPolicy(SampleProfileFormat Format, StringRef Policy,
const StringMap<uint64_t> &Expected) { const StringMap<uint64_t> &Expected) {
SmallVector<char, 128> ProfilePath; TempFile ProfileFile("profile", "", "", /*Unique*/ true);
std::error_code EC;
EC = llvm::sys::fs::createTemporaryFile("profile", "", ProfilePath);
ASSERT_TRUE(NoError(EC));
StringRef ProfileFile(ProfilePath.data(), ProfilePath.size());
Module M("my_module", Context); Module M("my_module", Context);
setupModuleForElisionTest(&M, Policy); setupModuleForElisionTest(&M, Policy);
StringMap<FunctionSamples> ProfMap = setupFcnSamplesForElisionTest(Policy); StringMap<FunctionSamples> ProfMap = setupFcnSamplesForElisionTest(Policy);
// write profile // write profile
createWriter(Format, ProfileFile); createWriter(Format, ProfileFile.path());
std::error_code EC;
EC = Writer->write(ProfMap); EC = Writer->write(ProfMap);
ASSERT_TRUE(NoError(EC)); ASSERT_TRUE(NoError(EC));
Writer->getOutputStream().flush(); Writer->getOutputStream().flush();
// read profile // read profile
readProfile(M, ProfileFile); readProfile(M, ProfileFile.path());
EC = Reader->read(); EC = Reader->read();
ASSERT_TRUE(NoError(EC)); ASSERT_TRUE(NoError(EC));

View File

@ -22,6 +22,7 @@
#include "llvm/Support/StringSaver.h" #include "llvm/Support/StringSaver.h"
#include "llvm/Support/VirtualFileSystem.h" #include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/Testing/Support/SupportHelpers.h"
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <fstream> #include <fstream>
@ -30,6 +31,8 @@
#include <tuple> #include <tuple>
using namespace llvm; using namespace llvm;
using llvm::unittest::TempDir;
using llvm::unittest::TempFile;
namespace { namespace {
@ -754,20 +757,13 @@ TEST(CommandLineTest, ResponseFileWindows) {
StackOption<bool> TopLevelOpt("top-level", cl::init(false)); StackOption<bool> TopLevelOpt("top-level", cl::init(false));
// Create response file. // Create response file.
int FileDescriptor; TempFile ResponseFile("resp-", ".txt",
SmallString<64> TempPath; "-top-level\npath\\dir\\file1\npath/dir/file2",
std::error_code EC = /*Unique*/ true);
llvm::sys::fs::createTemporaryFile("resp-", ".txt", FileDescriptor, TempPath);
EXPECT_TRUE(!EC);
std::ofstream RspFile(TempPath.c_str());
EXPECT_TRUE(RspFile.is_open());
RspFile << "-top-level\npath\\dir\\file1\npath/dir/file2";
RspFile.close();
llvm::SmallString<128> RspOpt; llvm::SmallString<128> RspOpt;
RspOpt.append(1, '@'); RspOpt.append(1, '@');
RspOpt.append(TempPath.c_str()); RspOpt.append(ResponseFile.path());
const char *args[] = {"prog", RspOpt.c_str()}; const char *args[] = {"prog", RspOpt.c_str()};
EXPECT_FALSE(TopLevelOpt); EXPECT_FALSE(TopLevelOpt);
EXPECT_TRUE( EXPECT_TRUE(
@ -775,8 +771,6 @@ TEST(CommandLineTest, ResponseFileWindows) {
EXPECT_TRUE(TopLevelOpt); EXPECT_TRUE(TopLevelOpt);
EXPECT_TRUE(InputFilenames[0] == "path\\dir\\file1"); EXPECT_TRUE(InputFilenames[0] == "path\\dir\\file1");
EXPECT_TRUE(InputFilenames[1] == "path/dir/file2"); EXPECT_TRUE(InputFilenames[1] == "path/dir/file2");
llvm::sys::fs::remove(TempPath.c_str());
} }
TEST(CommandLineTest, ResponseFiles) { TEST(CommandLineTest, ResponseFiles) {
@ -1007,44 +1001,38 @@ TEST(CommandLineTest, SetDefautValue) {
TEST(CommandLineTest, ReadConfigFile) { TEST(CommandLineTest, ReadConfigFile) {
llvm::SmallVector<const char *, 1> Argv; llvm::SmallVector<const char *, 1> Argv;
llvm::SmallString<128> TestDir; TempDir TestDir("unittest", /*Unique*/ true);
std::error_code EC =
llvm::sys::fs::createUniqueDirectory("unittest", TestDir);
EXPECT_TRUE(!EC);
llvm::SmallString<128> TestCfg; llvm::SmallString<128> TestCfg;
llvm::sys::path::append(TestCfg, TestDir, "foo"); llvm::sys::path::append(TestCfg, TestDir.path(), "foo");
std::ofstream ConfigFile(TestCfg.c_str());
EXPECT_TRUE(ConfigFile.is_open()); TempFile ConfigFile(TestCfg, "",
ConfigFile << "# Comment\n" "# Comment\n"
"-option_1\n" "-option_1\n"
"@subconfig\n" "@subconfig\n"
"-option_3=abcd\n" "-option_3=abcd\n"
"-option_4=\\\n" "-option_4=\\\n"
"cdef\n"; "cdef\n");
ConfigFile.close();
llvm::SmallString<128> TestCfg2; llvm::SmallString<128> TestCfg2;
llvm::sys::path::append(TestCfg2, TestDir, "subconfig"); llvm::sys::path::append(TestCfg2, TestDir.path(), "subconfig");
std::ofstream ConfigFile2(TestCfg2.c_str()); TempFile ConfigFile2(TestCfg2, "",
EXPECT_TRUE(ConfigFile2.is_open()); "-option_2\n"
ConfigFile2 << "-option_2\n" "\n"
"\n" " # comment\n");
" # comment\n";
ConfigFile2.close();
// Make sure the current directory is not the directory where config files // Make sure the current directory is not the directory where config files
// resides. In this case the code that expands response files will not find // resides. In this case the code that expands response files will not find
// 'subconfig' unless it resolves nested inclusions relative to the including // 'subconfig' unless it resolves nested inclusions relative to the including
// file. // file.
llvm::SmallString<128> CurrDir; llvm::SmallString<128> CurrDir;
EC = llvm::sys::fs::current_path(CurrDir); std::error_code EC = llvm::sys::fs::current_path(CurrDir);
EXPECT_TRUE(!EC); EXPECT_TRUE(!EC);
EXPECT_TRUE(StringRef(CurrDir) != StringRef(TestDir)); EXPECT_TRUE(StringRef(CurrDir) != TestDir.path());
llvm::BumpPtrAllocator A; llvm::BumpPtrAllocator A;
llvm::StringSaver Saver(A); llvm::StringSaver Saver(A);
bool Result = llvm::cl::readConfigFile(TestCfg, Saver, Argv); bool Result = llvm::cl::readConfigFile(ConfigFile.path(), Saver, Argv);
EXPECT_TRUE(Result); EXPECT_TRUE(Result);
EXPECT_EQ(Argv.size(), 4U); EXPECT_EQ(Argv.size(), 4U);
@ -1052,10 +1040,6 @@ TEST(CommandLineTest, ReadConfigFile) {
EXPECT_STREQ(Argv[1], "-option_2"); EXPECT_STREQ(Argv[1], "-option_2");
EXPECT_STREQ(Argv[2], "-option_3=abcd"); EXPECT_STREQ(Argv[2], "-option_3=abcd");
EXPECT_STREQ(Argv[3], "-option_4=cdef"); EXPECT_STREQ(Argv[3], "-option_4=cdef");
llvm::sys::fs::remove(TestCfg2);
llvm::sys::fs::remove(TestCfg);
llvm::sys::fs::remove(TestDir);
} }
TEST(CommandLineTest, PositionalEatArgsError) { TEST(CommandLineTest, PositionalEatArgsError) {

View File

@ -11,8 +11,12 @@
#include "llvm/Support/FileCollector.h" #include "llvm/Support/FileCollector.h"
#include "llvm/Support/FileSystem.h" #include "llvm/Support/FileSystem.h"
#include "llvm/Testing/Support/SupportHelpers.h"
using namespace llvm; using namespace llvm;
using llvm::unittest::TempDir;
using llvm::unittest::TempFile;
using llvm::unittest::TempLink;
namespace llvm { namespace llvm {
namespace vfs { namespace vfs {
@ -37,71 +41,11 @@ public:
} }
}; };
struct ScopedDir {
SmallString<128> Path;
ScopedDir(const Twine &Name, bool Unique = false) {
std::error_code EC;
if (Unique) {
EC = llvm::sys::fs::createUniqueDirectory(Name, Path);
} else {
Path = Name.str();
EC = llvm::sys::fs::create_directory(Twine(Path));
}
if (EC)
Path = "";
EXPECT_FALSE(EC);
// Ensure the path is the real path so tests can use it to compare against
// realpath output.
SmallString<128> RealPath;
if (!llvm::sys::fs::real_path(Path, RealPath))
Path.swap(RealPath);
}
~ScopedDir() {
if (Path != "") {
EXPECT_FALSE(llvm::sys::fs::remove_directories(Path.str()));
}
}
operator StringRef() { return Path.str(); }
};
struct ScopedLink {
SmallString<128> Path;
ScopedLink(const Twine &To, const Twine &From) {
Path = From.str();
std::error_code EC = sys::fs::create_link(To, From);
if (EC)
Path = "";
EXPECT_FALSE(EC);
}
~ScopedLink() {
if (Path != "") {
EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
}
}
operator StringRef() { return Path.str(); }
};
struct ScopedFile {
SmallString<128> Path;
ScopedFile(const Twine &Name) {
std::error_code EC;
EC = llvm::sys::fs::createUniqueFile(Name, Path);
if (EC)
Path = "";
EXPECT_FALSE(EC);
}
~ScopedFile() {
if (Path != "") {
EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
}
}
operator StringRef() { return Path.str(); }
};
} // end anonymous namespace } // end anonymous namespace
TEST(FileCollectorTest, addFile) { TEST(FileCollectorTest, addFile) {
ScopedDir root("add_file_root", true); TempDir root("add_file_root", /*Unique*/ true);
std::string root_fs = std::string(root.Path.str()); std::string root_fs(root.path());
TestingFileCollector FileCollector(root_fs, root_fs); TestingFileCollector FileCollector(root_fs, root_fs);
FileCollector.addFile("/path/to/a"); FileCollector.addFile("/path/to/a");
@ -121,53 +65,53 @@ TEST(FileCollectorTest, addFile) {
} }
TEST(FileCollectorTest, addDirectory) { TEST(FileCollectorTest, addDirectory) {
ScopedDir file_root("file_root", true); TempDir file_root("file_root", /*Unique*/ true);
llvm::SmallString<128> aaa = file_root.Path; llvm::SmallString<128> aaa(file_root.path());
llvm::sys::path::append(aaa, "aaa"); llvm::sys::path::append(aaa, "aaa");
ScopedFile a(aaa.str()); TempFile a(aaa.str());
llvm::SmallString<128> bbb = file_root.Path; llvm::SmallString<128> bbb(file_root.path());
llvm::sys::path::append(bbb, "bbb"); llvm::sys::path::append(bbb, "bbb");
ScopedFile b(bbb.str()); TempFile b(bbb.str());
llvm::SmallString<128> ccc = file_root.Path; llvm::SmallString<128> ccc(file_root.path());
llvm::sys::path::append(ccc, "ccc"); llvm::sys::path::append(ccc, "ccc");
ScopedFile c(ccc.str()); TempFile c(ccc.str());
std::string root_fs = std::string(file_root.Path.str()); std::string root_fs(file_root.path());
TestingFileCollector FileCollector(root_fs, root_fs); TestingFileCollector FileCollector(root_fs, root_fs);
FileCollector.addDirectory(file_root.Path); FileCollector.addDirectory(file_root.path());
// Make sure the root is correct. // Make sure the root is correct.
EXPECT_EQ(FileCollector.Root, root_fs); EXPECT_EQ(FileCollector.Root, root_fs);
// Make sure we've seen all the added files. // Make sure we've seen all the added files.
EXPECT_TRUE(FileCollector.hasSeen(a.Path)); EXPECT_TRUE(FileCollector.hasSeen(a.path()));
EXPECT_TRUE(FileCollector.hasSeen(b.Path)); EXPECT_TRUE(FileCollector.hasSeen(b.path()));
EXPECT_TRUE(FileCollector.hasSeen(c.Path)); EXPECT_TRUE(FileCollector.hasSeen(c.path()));
// Make sure we've only seen the added files. // Make sure we've only seen the added files.
llvm::SmallString<128> ddd = file_root.Path; llvm::SmallString<128> ddd(file_root.path());
llvm::sys::path::append(ddd, "ddd"); llvm::sys::path::append(ddd, "ddd");
ScopedFile d(ddd.str()); TempFile d(ddd.str(), "", "", /*Unique*/ true);
EXPECT_FALSE(FileCollector.hasSeen(d.Path)); EXPECT_FALSE(FileCollector.hasSeen(d.path()));
} }
TEST(FileCollectorTest, copyFiles) { TEST(FileCollectorTest, copyFiles) {
ScopedDir file_root("file_root", true); TempDir file_root("file_root", /*Unique*/ true);
ScopedFile a(file_root + "/aaa"); TempFile a(file_root.path("aaa"));
ScopedFile b(file_root + "/bbb"); TempFile b(file_root.path("bbb"));
ScopedFile c(file_root + "/ccc"); TempFile c(file_root.path("ccc"));
// Create file collector and add files. // Create file collector and add files.
ScopedDir root("copy_files_root", true); TempDir root("copy_files_root", /*Unique*/ true);
std::string root_fs = std::string(root.Path.str()); std::string root_fs(root.path());
TestingFileCollector FileCollector(root_fs, root_fs); TestingFileCollector FileCollector(root_fs, root_fs);
FileCollector.addFile(a.Path); FileCollector.addFile(a.path());
FileCollector.addFile(b.Path); FileCollector.addFile(b.path());
FileCollector.addFile(c.Path); FileCollector.addFile(c.path());
// Make sure we can copy the files. // Make sure we can copy the files.
std::error_code ec = FileCollector.copyFiles(true); std::error_code ec = FileCollector.copyFiles(true);
@ -184,72 +128,70 @@ TEST(FileCollectorTest, copyFiles) {
} }
TEST(FileCollectorTest, recordAndConstructDirectory) { TEST(FileCollectorTest, recordAndConstructDirectory) {
ScopedDir file_root("dir_root", true); TempDir file_root("dir_root", /*Unique*/ true);
ScopedDir subdir(file_root + "/subdir"); TempDir subdir(file_root.path("subdir"));
ScopedDir subdir2(file_root + "/subdir2"); TempDir subdir2(file_root.path("subdir2"));
ScopedFile a(subdir2 + "/a"); TempFile a(subdir2.path("a"));
// Create file collector and add files. // Create file collector and add files.
ScopedDir root("copy_files_root", true); TempDir root("copy_files_root", /*Unique*/ true);
std::string root_fs = std::string(root.Path.str()); std::string root_fs(root.path());
TestingFileCollector FileCollector(root_fs, root_fs); TestingFileCollector FileCollector(root_fs, root_fs);
FileCollector.addFile(a.Path); FileCollector.addFile(a.path());
// The empty directory isn't seen until we add it. // The empty directory isn't seen until we add it.
EXPECT_TRUE(FileCollector.hasSeen(a.Path)); EXPECT_TRUE(FileCollector.hasSeen(a.path()));
EXPECT_FALSE(FileCollector.hasSeen(subdir.Path)); EXPECT_FALSE(FileCollector.hasSeen(subdir.path()));
FileCollector.addFile(subdir.Path); FileCollector.addFile(subdir.path());
EXPECT_TRUE(FileCollector.hasSeen(subdir.Path)); EXPECT_TRUE(FileCollector.hasSeen(subdir.path()));
// Make sure we can construct the directory. // Make sure we can construct the directory.
std::error_code ec = FileCollector.copyFiles(true); std::error_code ec = FileCollector.copyFiles(true);
EXPECT_FALSE(ec); EXPECT_FALSE(ec);
bool IsDirectory = false; bool IsDirectory = false;
llvm::SmallString<128> SubdirInRoot = root.Path; llvm::SmallString<128> SubdirInRoot = root.path();
llvm::sys::path::append(SubdirInRoot, llvm::sys::path::append(SubdirInRoot,
llvm::sys::path::relative_path(subdir.Path)); llvm::sys::path::relative_path(subdir.path()));
ec = sys::fs::is_directory(SubdirInRoot, IsDirectory); ec = sys::fs::is_directory(SubdirInRoot, IsDirectory);
EXPECT_FALSE(ec); EXPECT_FALSE(ec);
ASSERT_TRUE(IsDirectory); ASSERT_TRUE(IsDirectory);
} }
TEST(FileCollectorTest, recordVFSAccesses) { TEST(FileCollectorTest, recordVFSAccesses) {
ScopedDir file_root("dir_root", true); TempDir file_root("dir_root", /*Unique*/ true);
ScopedDir subdir(file_root + "/subdir"); TempDir subdir(file_root.path("subdir"));
ScopedDir subdir2(file_root + "/subdir2"); TempDir subdir2(file_root.path("subdir2"));
ScopedFile a(subdir2 + "/a"); TempFile a(subdir2.path("a"));
ScopedFile b(file_root + "/b"); TempFile b(file_root.path("b"));
ScopedDir subdir3(file_root + "/subdir3"); TempDir subdir3(file_root.path("subdir3"));
ScopedFile subdir3a(subdir3 + "/aa"); TempFile subdir3a(subdir3.path("aa"));
ScopedDir subdir3b(subdir3 + "/subdirb"); TempDir subdir3b(subdir3.path("subdirb"));
{ { TempFile subdir3fileremoved(subdir3.path("removed")); }
ScopedFile subdir3fileremoved(subdir3 + "/removed");
}
// Create file collector and add files. // Create file collector and add files.
ScopedDir root("copy_files_root", true); TempDir root("copy_files_root", /*Unique*/ true);
std::string root_fs = std::string(root.Path.str()); std::string root_fs(root.path());
auto Collector = std::make_shared<TestingFileCollector>(root_fs, root_fs); auto Collector = std::make_shared<TestingFileCollector>(root_fs, root_fs);
auto VFS = auto VFS =
FileCollector::createCollectorVFS(vfs::getRealFileSystem(), Collector); FileCollector::createCollectorVFS(vfs::getRealFileSystem(), Collector);
VFS->status(a.Path); VFS->status(a.path());
EXPECT_TRUE(Collector->hasSeen(a.Path)); EXPECT_TRUE(Collector->hasSeen(a.path()));
VFS->openFileForRead(b.Path); VFS->openFileForRead(b.path());
EXPECT_TRUE(Collector->hasSeen(b.Path)); EXPECT_TRUE(Collector->hasSeen(b.path()));
VFS->status(subdir.Path); VFS->status(subdir.path());
EXPECT_TRUE(Collector->hasSeen(subdir.Path)); EXPECT_TRUE(Collector->hasSeen(subdir.path()));
#ifndef _WIN32 #ifndef _WIN32
std::error_code EC; std::error_code EC;
auto It = VFS->dir_begin(subdir3.Path, EC); auto It = VFS->dir_begin(subdir3.path(), EC);
EXPECT_FALSE(EC); EXPECT_FALSE(EC);
EXPECT_TRUE(Collector->hasSeen(subdir3.Path)); EXPECT_TRUE(Collector->hasSeen(subdir3.path()));
EXPECT_TRUE(Collector->hasSeen(subdir3a.Path)); EXPECT_TRUE(Collector->hasSeen(subdir3a.path()));
EXPECT_TRUE(Collector->hasSeen(subdir3b.Path)); EXPECT_TRUE(Collector->hasSeen(subdir3b.path()));
std::string RemovedFileName = (Twine(subdir3.Path) + "/removed").str(); std::string RemovedFileName((Twine(subdir3.path("removed"))).str());
EXPECT_FALSE(Collector->hasSeen(RemovedFileName)); EXPECT_FALSE(Collector->hasSeen(RemovedFileName));
#endif #endif
} }
@ -257,78 +199,80 @@ TEST(FileCollectorTest, recordVFSAccesses) {
#ifndef _WIN32 #ifndef _WIN32
TEST(FileCollectorTest, Symlinks) { TEST(FileCollectorTest, Symlinks) {
// Root where the original files live. // Root where the original files live.
ScopedDir file_root("file_root", true); TempDir file_root("file_root", /*Unique*/ true);
// Create some files in the file root. // Create some files in the file root.
ScopedFile a(file_root + "/aaa"); TempFile a(file_root.path("aaa"));
ScopedFile b(file_root + "/bbb"); TempFile b(file_root.path("bbb"));
ScopedFile c(file_root + "/ccc"); TempFile c(file_root.path("ccc"));
// Create a directory foo with file ddd. // Create a directory foo with file ddd.
ScopedDir foo(file_root + "/foo"); TempDir foo(file_root.path("foo"));
ScopedFile d(foo + "/ddd"); TempFile d(foo.path("ddd"));
// Create a file eee in the foo's parent directory. // Create a file eee in the foo's parent directory.
ScopedFile e(foo + "/../eee"); TempFile e(foo.path("../eee"));
// Create a symlink bar pointing to foo. // Create a symlink bar pointing to foo.
ScopedLink symlink(file_root + "/foo", file_root + "/bar"); TempLink symlink(file_root.path("foo"), file_root.path("bar"));
// Root where files are copied to. // Root where files are copied to.
ScopedDir reproducer_root("reproducer_root", true); TempDir reproducer_root("reproducer_root", /*Unique*/ true);
std::string root_fs = std::string(reproducer_root.Path.str()); std::string root_fs(reproducer_root.path());
TestingFileCollector FileCollector(root_fs, root_fs); TestingFileCollector FileCollector(root_fs, root_fs);
// Add all the files to the collector. // Add all the files to the collector.
FileCollector.addFile(a.Path); FileCollector.addFile(a.path());
FileCollector.addFile(b.Path); FileCollector.addFile(b.path());
FileCollector.addFile(c.Path); FileCollector.addFile(c.path());
FileCollector.addFile(d.Path); FileCollector.addFile(d.path());
FileCollector.addFile(e.Path); FileCollector.addFile(e.path());
FileCollector.addFile(file_root + "/bar/ddd"); FileCollector.addFile(file_root.path() + "/bar/ddd");
auto mapping = FileCollector.VFSWriter.getMappings(); auto mapping = FileCollector.VFSWriter.getMappings();
{ {
// Make sure the common case works. // Make sure the common case works.
std::string vpath = (file_root + "/aaa").str(); std::string vpath = (file_root.path() + "/aaa").str();
std::string rpath = (reproducer_root.Path + 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()); printf("%s -> %s\n", vpath.c_str(), rpath.c_str());
EXPECT_THAT(mapping, testing::Contains(vfs::YAMLVFSEntry(vpath, rpath))); EXPECT_THAT(mapping, testing::Contains(vfs::YAMLVFSEntry(vpath, rpath)));
} }
{ {
// Make sure the virtual path points to the real source path. // Make sure the virtual path points to the real source path.
std::string vpath = (file_root + "/bar/ddd").str(); std::string vpath = (file_root.path() + "/bar/ddd").str();
std::string rpath = std::string rpath =
(reproducer_root.Path + file_root.Path + "/foo/ddd").str(); (reproducer_root.path() + file_root.path() + "/foo/ddd").str();
printf("%s -> %s\n", vpath.c_str(), rpath.c_str()); printf("%s -> %s\n", vpath.c_str(), rpath.c_str());
EXPECT_THAT(mapping, testing::Contains(vfs::YAMLVFSEntry(vpath, rpath))); EXPECT_THAT(mapping, testing::Contains(vfs::YAMLVFSEntry(vpath, rpath)));
} }
{ {
// Make sure that .. is removed from the source path. // Make sure that .. is removed from the source path.
std::string vpath = (file_root + "/eee").str(); std::string vpath = (file_root.path() + "/eee").str();
std::string rpath = (reproducer_root.Path + 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()); printf("%s -> %s\n", vpath.c_str(), rpath.c_str());
EXPECT_THAT(mapping, testing::Contains(vfs::YAMLVFSEntry(vpath, rpath))); EXPECT_THAT(mapping, testing::Contains(vfs::YAMLVFSEntry(vpath, rpath)));
} }
} }
TEST(FileCollectorTest, recordVFSSymlinkAccesses) { TEST(FileCollectorTest, recordVFSSymlinkAccesses) {
ScopedDir file_root("dir_root", true); TempDir file_root("dir_root", /*Unique*/ true);
ScopedFile a(file_root + "/a"); TempFile a(file_root.path("a"));
ScopedLink symlink(file_root + "/a", file_root + "/b"); TempLink symlink(file_root.path("a"), file_root.path("b"));
// Create file collector and add files. // Create file collector and add files.
ScopedDir root("copy_files_root", true); TempDir root("copy_files_root", true);
std::string root_fs = std::string(root.Path.str()); std::string root_fs(root.path());
auto Collector = std::make_shared<TestingFileCollector>(root_fs, root_fs); auto Collector = std::make_shared<TestingFileCollector>(root_fs, root_fs);
auto VFS = auto VFS =
FileCollector::createCollectorVFS(vfs::getRealFileSystem(), Collector); FileCollector::createCollectorVFS(vfs::getRealFileSystem(), Collector);
SmallString<256> Output; SmallString<256> Output;
VFS->getRealPath(symlink.Path, Output); VFS->getRealPath(symlink.path(), Output);
EXPECT_TRUE(Collector->hasSeen(a.Path)); EXPECT_TRUE(Collector->hasSeen(a.path()));
EXPECT_TRUE(Collector->hasSeen(symlink.Path)); EXPECT_TRUE(Collector->hasSeen(symlink.path()));
} }
#endif #endif

View File

@ -12,12 +12,15 @@
#include "llvm/Support/FileSystem.h" #include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h" #include "llvm/Support/Path.h"
#include "llvm/Testing/Support/SupportHelpers.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <fstream> #include <fstream>
using namespace llvm; using namespace llvm;
using namespace llvm::sys; using namespace llvm::sys;
using llvm::unittest::TempDir;
#define ASSERT_NO_ERROR(x) \ #define ASSERT_NO_ERROR(x) \
if (std::error_code ASSERT_NO_ERROR_ec = x) { \ if (std::error_code ASSERT_NO_ERROR_ec = x) { \
SmallString<128> MessageStorage; \ SmallString<128> MessageStorage; \
@ -32,11 +35,9 @@ using namespace llvm::sys;
namespace { namespace {
TEST(writeFileAtomicallyTest, Test) { TEST(writeFileAtomicallyTest, Test) {
// Create unique temporary directory for these tests // Create unique temporary directory for these tests
SmallString<128> RootTestDirectory; TempDir RootTestDirectory("writeFileAtomicallyTest", /*Unique*/ true);
ASSERT_NO_ERROR(
fs::createUniqueDirectory("writeFileAtomicallyTest", RootTestDirectory));
SmallString<128> FinalTestfilePath(RootTestDirectory); SmallString<128> FinalTestfilePath(RootTestDirectory.path());
sys::path::append(FinalTestfilePath, "foo.txt"); sys::path::append(FinalTestfilePath, "foo.txt");
const std::string TempUniqTestFileModel = const std::string TempUniqTestFileModel =
std::string(FinalTestfilePath) + "-%%%%%%%%"; std::string(FinalTestfilePath) + "-%%%%%%%%";

View File

@ -9,20 +9,19 @@
#include "llvm/Support/LockFileManager.h" #include "llvm/Support/LockFileManager.h"
#include "llvm/Support/FileSystem.h" #include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h" #include "llvm/Support/Path.h"
#include "llvm/Testing/Support/SupportHelpers.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <memory> #include <memory>
using namespace llvm; using namespace llvm;
using llvm::unittest::TempDir;
namespace { namespace {
TEST(LockFileManagerTest, Basic) { TEST(LockFileManagerTest, Basic) {
SmallString<64> TmpDir; TempDir TmpDir("LockFileManagerTestDir", /*Unique*/ true);
std::error_code EC;
EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
ASSERT_FALSE(EC);
SmallString<64> LockedFile(TmpDir); SmallString<64> LockedFile(TmpDir.path());
sys::path::append(LockedFile, "file.lock"); sys::path::append(LockedFile, "file.lock");
{ {
@ -38,28 +37,22 @@ TEST(LockFileManagerTest, Basic) {
// Now that the lock is out of scope, the file should be gone. // Now that the lock is out of scope, the file should be gone.
EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile))); EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
EC = sys::fs::remove(StringRef(TmpDir));
ASSERT_FALSE(EC);
} }
TEST(LockFileManagerTest, LinkLockExists) { TEST(LockFileManagerTest, LinkLockExists) {
SmallString<64> TmpDir; TempDir LockFileManagerTestDir("LockFileManagerTestDir", /*Unique*/ true);
std::error_code EC;
EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
ASSERT_FALSE(EC);
SmallString<64> LockedFile(TmpDir); SmallString<64> LockedFile(LockFileManagerTestDir.path());
sys::path::append(LockedFile, "file"); sys::path::append(LockedFile, "file");
SmallString<64> FileLocK(TmpDir); SmallString<64> FileLocK(LockFileManagerTestDir.path());
sys::path::append(FileLocK, "file.lock"); sys::path::append(FileLocK, "file.lock");
SmallString<64> TmpFileLock(TmpDir); SmallString<64> TmpFileLock(LockFileManagerTestDir.path());
sys::path::append(TmpFileLock, "file.lock-000"); sys::path::append(TmpFileLock, "file.lock-000");
int FD; int FD;
EC = sys::fs::openFileForWrite(StringRef(TmpFileLock), FD); std::error_code EC = sys::fs::openFileForWrite(StringRef(TmpFileLock), FD);
ASSERT_FALSE(EC); ASSERT_FALSE(EC);
int Ret = close(FD); int Ret = close(FD);
@ -80,24 +73,18 @@ TEST(LockFileManagerTest, LinkLockExists) {
// Now that the lock is out of scope, the file should be gone. // Now that the lock is out of scope, the file should be gone.
EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile))); EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
EC = sys::fs::remove(StringRef(TmpDir));
ASSERT_FALSE(EC);
} }
TEST(LockFileManagerTest, RelativePath) { TEST(LockFileManagerTest, RelativePath) {
SmallString<64> TmpDir; TempDir LockFileManagerTestDir("LockFileManagerTestDir", /*Unique*/ true);
std::error_code EC;
EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
ASSERT_FALSE(EC);
char PathBuf[1024]; char PathBuf[1024];
const char *OrigPath = getcwd(PathBuf, 1024); const char *OrigPath = getcwd(PathBuf, 1024);
ASSERT_FALSE(chdir(TmpDir.c_str())); ASSERT_FALSE(chdir(LockFileManagerTestDir.path().data()));
sys::fs::create_directory("inner"); TempDir inner("inner");
SmallString<64> LockedFile("inner"); SmallString<64> LockedFile(inner.path());
sys::path::append(LockedFile, "file"); sys::path::append(LockedFile, "file");
SmallString<64> FileLock(LockedFile); SmallString<64> FileLock(LockedFile);
@ -114,13 +101,7 @@ TEST(LockFileManagerTest, RelativePath) {
EXPECT_FALSE(sys::fs::exists(LockedFile.str())); EXPECT_FALSE(sys::fs::exists(LockedFile.str()));
EXPECT_FALSE(sys::fs::exists(FileLock.str())); EXPECT_FALSE(sys::fs::exists(FileLock.str()));
EC = sys::fs::remove("inner");
ASSERT_FALSE(EC);
ASSERT_FALSE(chdir(OrigPath)); ASSERT_FALSE(chdir(OrigPath));
EC = sys::fs::remove(StringRef(TmpDir));
ASSERT_FALSE(EC);
} }
} // end anonymous namespace } // end anonymous namespace

View File

@ -9,10 +9,13 @@
#include "llvm/Support/TarWriter.h" #include "llvm/Support/TarWriter.h"
#include "llvm/Support/FileSystem.h" #include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include "llvm/Testing/Support/SupportHelpers.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <vector> #include <vector>
using namespace llvm; using namespace llvm;
using llvm::unittest::TempFile;
namespace { namespace {
struct UstarHeader { struct UstarHeader {
@ -38,21 +41,19 @@ struct UstarHeader {
class TarWriterTest : public ::testing::Test {}; class TarWriterTest : public ::testing::Test {};
static std::vector<uint8_t> createTar(StringRef Base, StringRef Filename) { static std::vector<uint8_t> createTar(StringRef Base, StringRef Filename) {
// Create a temporary file. TempFile TarWriterTest("TarWriterTest", "tar", "", /*Unique*/ true);
SmallString<128> Path;
std::error_code EC =
sys::fs::createTemporaryFile("TarWriterTest", "tar", Path);
EXPECT_FALSE((bool)EC);
// Create a tar file. // Create a tar file.
Expected<std::unique_ptr<TarWriter>> TarOrErr = TarWriter::create(Path, Base); Expected<std::unique_ptr<TarWriter>> TarOrErr =
TarWriter::create(TarWriterTest.path(), Base);
EXPECT_TRUE((bool)TarOrErr); EXPECT_TRUE((bool)TarOrErr);
std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr); std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
Tar->append(Filename, "contents"); Tar->append(Filename, "contents");
Tar.reset(); Tar.reset();
// Read the tar file. // Read the tar file.
ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path); ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
MemoryBuffer::getFile(TarWriterTest.path());
EXPECT_TRUE((bool)MBOrErr); EXPECT_TRUE((bool)MBOrErr);
std::unique_ptr<MemoryBuffer> MB = std::move(*MBOrErr); std::unique_ptr<MemoryBuffer> MB = std::move(*MBOrErr);
std::vector<uint8_t> Buf((const uint8_t *)MB->getBufferStart(), std::vector<uint8_t> Buf((const uint8_t *)MB->getBufferStart(),
@ -61,7 +62,6 @@ static std::vector<uint8_t> createTar(StringRef Base, StringRef Filename) {
// Windows does not allow us to remove a mmap'ed files, so // Windows does not allow us to remove a mmap'ed files, so
// unmap first and then remove the temporary file. // unmap first and then remove the temporary file.
MB = nullptr; MB = nullptr;
sys::fs::remove(Path);
return Buf; return Buf;
} }
@ -123,30 +123,26 @@ TEST_F(TarWriterTest, Pax) {
} }
TEST_F(TarWriterTest, SingleFile) { TEST_F(TarWriterTest, SingleFile) {
SmallString<128> Path; TempFile TarWriterTest("TarWriterTest", "tar", "", /*Unique*/ true);
std::error_code EC =
sys::fs::createTemporaryFile("TarWriterTest", "tar", Path);
EXPECT_FALSE((bool)EC);
Expected<std::unique_ptr<TarWriter>> TarOrErr = TarWriter::create(Path, ""); Expected<std::unique_ptr<TarWriter>> TarOrErr =
TarWriter::create(TarWriterTest.path(), "");
EXPECT_TRUE((bool)TarOrErr); EXPECT_TRUE((bool)TarOrErr);
std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr); std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
Tar->append("FooPath", "foo"); Tar->append("FooPath", "foo");
Tar.reset(); Tar.reset();
uint64_t TarSize; uint64_t TarSize;
EC = sys::fs::file_size(Path, TarSize); std::error_code EC = sys::fs::file_size(TarWriterTest.path(), TarSize);
EXPECT_FALSE((bool)EC); EXPECT_FALSE((bool)EC);
EXPECT_EQ(TarSize, 2048ULL); EXPECT_EQ(TarSize, 2048ULL);
} }
TEST_F(TarWriterTest, NoDuplicate) { TEST_F(TarWriterTest, NoDuplicate) {
SmallString<128> Path; TempFile TarWriterTest("TarWriterTest", "tar", "", /*Unique*/ true);
std::error_code EC =
sys::fs::createTemporaryFile("TarWriterTest", "tar", Path);
EXPECT_FALSE((bool)EC);
Expected<std::unique_ptr<TarWriter>> TarOrErr = TarWriter::create(Path, ""); Expected<std::unique_ptr<TarWriter>> TarOrErr =
TarWriter::create(TarWriterTest.path(), "");
EXPECT_TRUE((bool)TarOrErr); EXPECT_TRUE((bool)TarOrErr);
std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr); std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
Tar->append("FooPath", "foo"); Tar->append("FooPath", "foo");
@ -154,18 +150,16 @@ TEST_F(TarWriterTest, NoDuplicate) {
Tar.reset(); Tar.reset();
uint64_t TarSize; uint64_t TarSize;
EC = sys::fs::file_size(Path, TarSize); std::error_code EC = sys::fs::file_size(TarWriterTest.path(), TarSize);
EXPECT_FALSE((bool)EC); EXPECT_FALSE((bool)EC);
EXPECT_EQ(TarSize, 3072ULL); EXPECT_EQ(TarSize, 3072ULL);
} }
TEST_F(TarWriterTest, Duplicate) { TEST_F(TarWriterTest, Duplicate) {
SmallString<128> Path; TempFile TarWriterTest("TarWriterTest", "tar", "", /*Unique*/ true);
std::error_code EC =
sys::fs::createTemporaryFile("TarWriterTest", "tar", Path);
EXPECT_FALSE((bool)EC);
Expected<std::unique_ptr<TarWriter>> TarOrErr = TarWriter::create(Path, ""); Expected<std::unique_ptr<TarWriter>> TarOrErr =
TarWriter::create(TarWriterTest.path(), "");
EXPECT_TRUE((bool)TarOrErr); EXPECT_TRUE((bool)TarOrErr);
std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr); std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
Tar->append("FooPath", "foo"); Tar->append("FooPath", "foo");
@ -173,7 +167,7 @@ TEST_F(TarWriterTest, Duplicate) {
Tar.reset(); Tar.reset();
uint64_t TarSize; uint64_t TarSize;
EC = sys::fs::file_size(Path, TarSize); std::error_code EC = sys::fs::file_size(TarWriterTest.path(), TarSize);
EXPECT_FALSE((bool)EC); EXPECT_FALSE((bool)EC);
EXPECT_EQ(TarSize, 2048ULL); EXPECT_EQ(TarSize, 2048ULL);
} }

View File

@ -14,6 +14,7 @@
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h" #include "llvm/Support/Path.h"
#include "llvm/Support/SourceMgr.h" #include "llvm/Support/SourceMgr.h"
#include "llvm/Testing/Support/SupportHelpers.h"
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <map> #include <map>
@ -21,6 +22,9 @@
using namespace llvm; using namespace llvm;
using llvm::sys::fs::UniqueID; using llvm::sys::fs::UniqueID;
using llvm::unittest::TempDir;
using llvm::unittest::TempFile;
using llvm::unittest::TempLink;
using testing::ElementsAre; using testing::ElementsAre;
using testing::Pair; using testing::Pair;
using testing::UnorderedElementsAre; using testing::UnorderedElementsAre;
@ -410,87 +414,21 @@ TEST(VirtualFileSystemTest, OverlayIterator) {
} }
} }
namespace {
struct ScopedDir {
SmallString<128> Path;
ScopedDir(const Twine &Name, bool Unique = false) {
std::error_code EC;
if (Unique) {
EC = llvm::sys::fs::createUniqueDirectory(Name, Path);
if (!EC) {
// Resolve any symlinks in the new directory.
std::string UnresolvedPath = std::string(Path.str());
EC = llvm::sys::fs::real_path(UnresolvedPath, Path);
}
} else {
Path = Name.str();
EC = llvm::sys::fs::create_directory(Twine(Path));
}
if (EC)
Path = "";
EXPECT_FALSE(EC) << EC.message();
}
~ScopedDir() {
if (Path != "") {
EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
}
}
operator StringRef() { return Path.str(); }
};
struct ScopedLink {
SmallString<128> Path;
ScopedLink(const Twine &To, const Twine &From) {
Path = From.str();
std::error_code EC = sys::fs::create_link(To, From);
if (EC)
Path = "";
EXPECT_FALSE(EC);
}
~ScopedLink() {
if (Path != "") {
EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
}
}
operator StringRef() { return Path.str(); }
};
struct ScopedFile {
SmallString<128> Path;
ScopedFile(const Twine &Path, StringRef Contents) {
Path.toVector(this->Path);
std::error_code EC;
raw_fd_ostream OS(this->Path, EC);
EXPECT_FALSE(EC);
OS << Contents;
OS.flush();
EXPECT_FALSE(OS.error());
if (EC || OS.error())
this->Path = "";
}
~ScopedFile() {
if (Path != "") {
EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
}
}
};
} // end anonymous namespace
TEST(VirtualFileSystemTest, BasicRealFSIteration) { TEST(VirtualFileSystemTest, BasicRealFSIteration) {
ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true); TempDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem(); IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
std::error_code EC; std::error_code EC;
vfs::directory_iterator I = FS->dir_begin(Twine(TestDirectory), EC); vfs::directory_iterator I = FS->dir_begin(Twine(TestDirectory.path()), EC);
ASSERT_FALSE(EC); ASSERT_FALSE(EC);
EXPECT_EQ(vfs::directory_iterator(), I); // empty directory is empty EXPECT_EQ(vfs::directory_iterator(), I); // empty directory is empty
ScopedDir _a(TestDirectory + "/a"); TempDir _a(TestDirectory.path("a"));
ScopedDir _ab(TestDirectory + "/a/b"); TempDir _ab(TestDirectory.path("a/b"));
ScopedDir _c(TestDirectory + "/c"); TempDir _c(TestDirectory.path("c"));
ScopedDir _cd(TestDirectory + "/c/d"); TempDir _cd(TestDirectory.path("c/d"));
I = FS->dir_begin(Twine(TestDirectory), EC); I = FS->dir_begin(Twine(TestDirectory.path()), EC);
ASSERT_FALSE(EC); ASSERT_FALSE(EC);
ASSERT_NE(vfs::directory_iterator(), I); ASSERT_NE(vfs::directory_iterator(), I);
// Check either a or c, since we can't rely on the iteration order. // Check either a or c, since we can't rely on the iteration order.
@ -508,16 +446,19 @@ TEST(VirtualFileSystemTest, MultipleWorkingDirs) {
// Our root contains a/aa, b/bb, c, where c is a link to a/. // Our root contains a/aa, b/bb, c, where c is a link to a/.
// Run tests both in root/b/ and root/c/ (to test "normal" and symlink dirs). // Run tests both in root/b/ and root/c/ (to test "normal" and symlink dirs).
// Interleave operations to show the working directories are independent. // Interleave operations to show the working directories are independent.
ScopedDir Root("r", true), ADir(Root.Path + "/a"), BDir(Root.Path + "/b"); TempDir Root("r", /*Unique*/ true);
ScopedLink C(ADir.Path, Root.Path + "/c"); TempDir ADir(Root.path("a"));
ScopedFile AA(ADir.Path + "/aa", "aaaa"), BB(BDir.Path + "/bb", "bbbb"); TempDir BDir(Root.path("b"));
TempLink C(ADir.path(), Root.path("c"));
TempFile AA(ADir.path("aa"), "", "aaaa");
TempFile BB(BDir.path("bb"), "", "bbbb");
std::unique_ptr<vfs::FileSystem> BFS = vfs::createPhysicalFileSystem(), std::unique_ptr<vfs::FileSystem> BFS = vfs::createPhysicalFileSystem(),
CFS = vfs::createPhysicalFileSystem(); CFS = vfs::createPhysicalFileSystem();
ASSERT_FALSE(BFS->setCurrentWorkingDirectory(BDir.Path)); ASSERT_FALSE(BFS->setCurrentWorkingDirectory(BDir.path()));
ASSERT_FALSE(CFS->setCurrentWorkingDirectory(C.Path)); ASSERT_FALSE(CFS->setCurrentWorkingDirectory(C.path()));
EXPECT_EQ(BDir.Path, *BFS->getCurrentWorkingDirectory()); EXPECT_EQ(BDir.path(), *BFS->getCurrentWorkingDirectory());
EXPECT_EQ(C.Path, *CFS->getCurrentWorkingDirectory()); EXPECT_EQ(C.path(), *CFS->getCurrentWorkingDirectory());
// openFileForRead(), indirectly. // openFileForRead(), indirectly.
auto BBuf = BFS->getBufferForFile("bb"); auto BBuf = BFS->getBufferForFile("bb");
@ -540,18 +481,18 @@ TEST(VirtualFileSystemTest, MultipleWorkingDirs) {
// getRealPath() // getRealPath()
SmallString<128> BPath; SmallString<128> BPath;
ASSERT_FALSE(BFS->getRealPath("bb", BPath)); ASSERT_FALSE(BFS->getRealPath("bb", BPath));
EXPECT_EQ(BB.Path, BPath); EXPECT_EQ(BB.path(), BPath);
SmallString<128> APath; SmallString<128> APath;
ASSERT_FALSE(CFS->getRealPath("aa", APath)); ASSERT_FALSE(CFS->getRealPath("aa", APath));
EXPECT_EQ(AA.Path, APath); // Reports resolved name. EXPECT_EQ(AA.path(), APath); // Reports resolved name.
// dir_begin // dir_begin
std::error_code EC; std::error_code EC;
auto BIt = BFS->dir_begin(".", EC); auto BIt = BFS->dir_begin(".", EC);
ASSERT_FALSE(EC); ASSERT_FALSE(EC);
ASSERT_NE(BIt, vfs::directory_iterator()); ASSERT_NE(BIt, vfs::directory_iterator());
EXPECT_EQ((BDir.Path + "/./bb").str(), BIt->path()); EXPECT_EQ((BDir.path() + "/./bb").str(), BIt->path());
BIt.increment(EC); BIt.increment(EC);
ASSERT_FALSE(EC); ASSERT_FALSE(EC);
ASSERT_EQ(BIt, vfs::directory_iterator()); ASSERT_EQ(BIt, vfs::directory_iterator());
@ -559,24 +500,27 @@ TEST(VirtualFileSystemTest, MultipleWorkingDirs) {
auto CIt = CFS->dir_begin(".", EC); auto CIt = CFS->dir_begin(".", EC);
ASSERT_FALSE(EC); ASSERT_FALSE(EC);
ASSERT_NE(CIt, vfs::directory_iterator()); ASSERT_NE(CIt, vfs::directory_iterator());
EXPECT_EQ((ADir.Path + "/./aa").str(), CIt->path()); // Partly resolved name! EXPECT_EQ((ADir.path() + "/./aa").str(),
CIt->path()); // Partly resolved name!
CIt.increment(EC); // Because likely to read through this path. CIt.increment(EC); // Because likely to read through this path.
ASSERT_FALSE(EC); ASSERT_FALSE(EC);
ASSERT_EQ(CIt, vfs::directory_iterator()); ASSERT_EQ(CIt, vfs::directory_iterator());
} }
TEST(VirtualFileSystemTest, BrokenSymlinkRealFSIteration) { TEST(VirtualFileSystemTest, BrokenSymlinkRealFSIteration) {
ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true); TempDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem(); IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
ScopedLink _a("no_such_file", TestDirectory + "/a"); TempLink _a("no_such_file", TestDirectory.path("a"));
ScopedDir _b(TestDirectory + "/b"); TempDir _b(TestDirectory.path("b"));
ScopedLink _c("no_such_file", TestDirectory + "/c"); TempLink _c("no_such_file", TestDirectory.path("c"));
// Should get no iteration error, but a stat error for the broken symlinks. // Should get no iteration error, but a stat error for the broken symlinks.
std::map<std::string, std::error_code> StatResults; std::map<std::string, std::error_code> StatResults;
std::error_code EC; std::error_code EC;
for (vfs::directory_iterator I = FS->dir_begin(Twine(TestDirectory), EC), E; for (vfs::directory_iterator
I = FS->dir_begin(Twine(TestDirectory.path()), EC),
E;
I != E; I.increment(EC)) { I != E; I.increment(EC)) {
EXPECT_FALSE(EC); EXPECT_FALSE(EC);
StatResults[std::string(sys::path::filename(I->path()))] = StatResults[std::string(sys::path::filename(I->path()))] =
@ -593,20 +537,21 @@ TEST(VirtualFileSystemTest, BrokenSymlinkRealFSIteration) {
#endif #endif
TEST(VirtualFileSystemTest, BasicRealFSRecursiveIteration) { TEST(VirtualFileSystemTest, BasicRealFSRecursiveIteration) {
ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true); TempDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem(); IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
std::error_code EC; std::error_code EC;
auto I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC); auto I =
vfs::recursive_directory_iterator(*FS, Twine(TestDirectory.path()), EC);
ASSERT_FALSE(EC); ASSERT_FALSE(EC);
EXPECT_EQ(vfs::recursive_directory_iterator(), I); // empty directory is empty EXPECT_EQ(vfs::recursive_directory_iterator(), I); // empty directory is empty
ScopedDir _a(TestDirectory + "/a"); TempDir _a(TestDirectory.path("a"));
ScopedDir _ab(TestDirectory + "/a/b"); TempDir _ab(TestDirectory.path("a/b"));
ScopedDir _c(TestDirectory + "/c"); TempDir _c(TestDirectory.path("c"));
ScopedDir _cd(TestDirectory + "/c/d"); TempDir _cd(TestDirectory.path("c/d"));
I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC); I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory.path()), EC);
ASSERT_FALSE(EC); ASSERT_FALSE(EC);
ASSERT_NE(vfs::recursive_directory_iterator(), I); ASSERT_NE(vfs::recursive_directory_iterator(), I);
@ -632,22 +577,23 @@ TEST(VirtualFileSystemTest, BasicRealFSRecursiveIteration) {
} }
TEST(VirtualFileSystemTest, BasicRealFSRecursiveIterationNoPush) { TEST(VirtualFileSystemTest, BasicRealFSRecursiveIterationNoPush) {
ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true); TempDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
ScopedDir _a(TestDirectory + "/a"); TempDir _a(TestDirectory.path("a"));
ScopedDir _ab(TestDirectory + "/a/b"); TempDir _ab(TestDirectory.path("a/b"));
ScopedDir _c(TestDirectory + "/c"); TempDir _c(TestDirectory.path("c"));
ScopedDir _cd(TestDirectory + "/c/d"); TempDir _cd(TestDirectory.path("c/d"));
ScopedDir _e(TestDirectory + "/e"); TempDir _e(TestDirectory.path("e"));
ScopedDir _ef(TestDirectory + "/e/f"); TempDir _ef(TestDirectory.path("e/f"));
ScopedDir _g(TestDirectory + "/g"); TempDir _g(TestDirectory.path("g"));
IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem(); IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
// Test that calling no_push on entries without subdirectories has no effect. // Test that calling no_push on entries without subdirectories has no effect.
{ {
std::error_code EC; std::error_code EC;
auto I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC); auto I =
vfs::recursive_directory_iterator(*FS, Twine(TestDirectory.path()), EC);
ASSERT_FALSE(EC); ASSERT_FALSE(EC);
std::vector<std::string> Contents; std::vector<std::string> Contents;
@ -672,7 +618,8 @@ TEST(VirtualFileSystemTest, BasicRealFSRecursiveIterationNoPush) {
// Test that calling no_push skips subdirectories. // Test that calling no_push skips subdirectories.
{ {
std::error_code EC; std::error_code EC;
auto I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC); auto I =
vfs::recursive_directory_iterator(*FS, Twine(TestDirectory.path()), EC);
ASSERT_FALSE(EC); ASSERT_FALSE(EC);
std::vector<std::string> Contents; std::vector<std::string> Contents;
@ -712,24 +659,26 @@ TEST(VirtualFileSystemTest, BasicRealFSRecursiveIterationNoPush) {
#ifdef LLVM_ON_UNIX #ifdef LLVM_ON_UNIX
TEST(VirtualFileSystemTest, BrokenSymlinkRealFSRecursiveIteration) { TEST(VirtualFileSystemTest, BrokenSymlinkRealFSRecursiveIteration) {
ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true); TempDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem(); IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
ScopedLink _a("no_such_file", TestDirectory + "/a"); TempLink _a("no_such_file", TestDirectory.path("a"));
ScopedDir _b(TestDirectory + "/b"); TempDir _b(TestDirectory.path("b"));
ScopedLink _ba("no_such_file", TestDirectory + "/b/a"); TempLink _ba("no_such_file", TestDirectory.path("b/a"));
ScopedDir _bb(TestDirectory + "/b/b"); TempDir _bb(TestDirectory.path("b/b"));
ScopedLink _bc("no_such_file", TestDirectory + "/b/c"); TempLink _bc("no_such_file", TestDirectory.path("b/c"));
ScopedLink _c("no_such_file", TestDirectory + "/c"); TempLink _c("no_such_file", TestDirectory.path("c"));
ScopedDir _d(TestDirectory + "/d"); TempDir _d(TestDirectory.path("d"));
ScopedDir _dd(TestDirectory + "/d/d"); TempDir _dd(TestDirectory.path("d/d"));
ScopedDir _ddd(TestDirectory + "/d/d/d"); TempDir _ddd(TestDirectory.path("d/d/d"));
ScopedLink _e("no_such_file", TestDirectory + "/e"); TempLink _e("no_such_file", TestDirectory.path("e"));
std::vector<std::string> VisitedBrokenSymlinks; std::vector<std::string> VisitedBrokenSymlinks;
std::vector<std::string> VisitedNonBrokenSymlinks; std::vector<std::string> VisitedNonBrokenSymlinks;
std::error_code EC; std::error_code EC;
for (vfs::recursive_directory_iterator I(*FS, Twine(TestDirectory), EC), E; for (vfs::recursive_directory_iterator
I(*FS, Twine(TestDirectory.path()), EC),
E;
I != E; I.increment(EC)) { I != E; I.increment(EC)) {
EXPECT_FALSE(EC); EXPECT_FALSE(EC);
(FS->status(I->path()) ? VisitedNonBrokenSymlinks : VisitedBrokenSymlinks) (FS->status(I->path()) ? VisitedNonBrokenSymlinks : VisitedBrokenSymlinks)
@ -738,13 +687,13 @@ TEST(VirtualFileSystemTest, BrokenSymlinkRealFSRecursiveIteration) {
// Check visited file names. // Check visited file names.
EXPECT_THAT(VisitedBrokenSymlinks, EXPECT_THAT(VisitedBrokenSymlinks,
UnorderedElementsAre(StringRef(_a).str(), StringRef(_ba).str(), UnorderedElementsAre(_a.path().str(), _ba.path().str(),
StringRef(_bc).str(), StringRef(_c).str(), _bc.path().str(), _c.path().str(),
StringRef(_e).str())); _e.path().str()));
EXPECT_THAT(VisitedNonBrokenSymlinks, EXPECT_THAT(VisitedNonBrokenSymlinks,
UnorderedElementsAre(StringRef(_b).str(), StringRef(_bb).str(), UnorderedElementsAre(_b.path().str(), _bb.path().str(),
StringRef(_d).str(), StringRef(_dd).str(), _d.path().str(), _dd.path().str(),
StringRef(_ddd).str())); _ddd.path().str()));
} }
#endif #endif
@ -2190,24 +2139,24 @@ TEST_F(VFSFromYAMLTest, WorkingDirectoryFallthroughInvalid) {
} }
TEST_F(VFSFromYAMLTest, YAMLVFSWriterTest) { TEST_F(VFSFromYAMLTest, YAMLVFSWriterTest) {
ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true); TempDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
ScopedDir _a(TestDirectory + "/a"); TempDir _a(TestDirectory.path("a"));
ScopedFile _ab(TestDirectory + "/a/b", ""); TempFile _ab(TestDirectory.path("a, b"));
ScopedDir _c(TestDirectory + "/c"); TempDir _c(TestDirectory.path("c"));
ScopedFile _cd(TestDirectory + "/c/d", ""); TempFile _cd(TestDirectory.path("c/d"));
ScopedDir _e(TestDirectory + "/e"); TempDir _e(TestDirectory.path("e"));
ScopedDir _ef(TestDirectory + "/e/f"); TempDir _ef(TestDirectory.path("e/f"));
ScopedFile _g(TestDirectory + "/g", ""); TempFile _g(TestDirectory.path("g"));
ScopedDir _h(TestDirectory + "/h"); TempDir _h(TestDirectory.path("h"));
vfs::YAMLVFSWriter VFSWriter; vfs::YAMLVFSWriter VFSWriter;
VFSWriter.addDirectoryMapping(_a.Path, "//root/a"); VFSWriter.addDirectoryMapping(_a.path(), "//root/a");
VFSWriter.addFileMapping(_ab.Path, "//root/a/b"); VFSWriter.addFileMapping(_ab.path(), "//root/a/b");
VFSWriter.addFileMapping(_cd.Path, "//root/c/d"); VFSWriter.addFileMapping(_cd.path(), "//root/c/d");
VFSWriter.addDirectoryMapping(_e.Path, "//root/e"); VFSWriter.addDirectoryMapping(_e.path(), "//root/e");
VFSWriter.addDirectoryMapping(_ef.Path, "//root/e/f"); VFSWriter.addDirectoryMapping(_ef.path(), "//root/e/f");
VFSWriter.addFileMapping(_g.Path, "//root/g"); VFSWriter.addFileMapping(_g.path(), "//root/g");
VFSWriter.addDirectoryMapping(_h.Path, "//root/h"); VFSWriter.addDirectoryMapping(_h.path(), "//root/h");
std::string Buffer; std::string Buffer;
raw_string_ostream OS(Buffer); raw_string_ostream OS(Buffer);
@ -2229,36 +2178,36 @@ TEST_F(VFSFromYAMLTest, YAMLVFSWriterTest) {
IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLRawString(Buffer, Lower); IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLRawString(Buffer, Lower);
ASSERT_TRUE(FS.get() != nullptr); ASSERT_TRUE(FS.get() != nullptr);
EXPECT_TRUE(FS->exists(_a.Path)); EXPECT_TRUE(FS->exists(_a.path()));
EXPECT_TRUE(FS->exists(_ab.Path)); EXPECT_TRUE(FS->exists(_ab.path()));
EXPECT_TRUE(FS->exists(_c.Path)); EXPECT_TRUE(FS->exists(_c.path()));
EXPECT_TRUE(FS->exists(_cd.Path)); EXPECT_TRUE(FS->exists(_cd.path()));
EXPECT_TRUE(FS->exists(_e.Path)); EXPECT_TRUE(FS->exists(_e.path()));
EXPECT_TRUE(FS->exists(_ef.Path)); EXPECT_TRUE(FS->exists(_ef.path()));
EXPECT_TRUE(FS->exists(_g.Path)); EXPECT_TRUE(FS->exists(_g.path()));
EXPECT_TRUE(FS->exists(_h.Path)); EXPECT_TRUE(FS->exists(_h.path()));
} }
TEST_F(VFSFromYAMLTest, YAMLVFSWriterTest2) { TEST_F(VFSFromYAMLTest, YAMLVFSWriterTest2) {
ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true); TempDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
ScopedDir _a(TestDirectory + "/a"); TempDir _a(TestDirectory.path("a"));
ScopedFile _ab(TestDirectory + "/a/b", ""); TempFile _ab(TestDirectory.path("a/b"));
ScopedDir _ac(TestDirectory + "/a/c"); TempDir _ac(TestDirectory.path("a/c"));
ScopedFile _acd(TestDirectory + "/a/c/d", ""); TempFile _acd(TestDirectory.path("a/c/d"));
ScopedFile _ace(TestDirectory + "/a/c/e", ""); TempFile _ace(TestDirectory.path("a/c/e"));
ScopedFile _acf(TestDirectory + "/a/c/f", ""); TempFile _acf(TestDirectory.path("a/c/f"));
ScopedDir _ag(TestDirectory + "/a/g"); TempDir _ag(TestDirectory.path("a/g"));
ScopedFile _agh(TestDirectory + "/a/g/h", ""); TempFile _agh(TestDirectory.path("a/g/h"));
vfs::YAMLVFSWriter VFSWriter; vfs::YAMLVFSWriter VFSWriter;
VFSWriter.addDirectoryMapping(_a.Path, "//root/a"); VFSWriter.addDirectoryMapping(_a.path(), "//root/a");
VFSWriter.addFileMapping(_ab.Path, "//root/a/b"); VFSWriter.addFileMapping(_ab.path(), "//root/a/b");
VFSWriter.addDirectoryMapping(_ac.Path, "//root/a/c"); VFSWriter.addDirectoryMapping(_ac.path(), "//root/a/c");
VFSWriter.addFileMapping(_acd.Path, "//root/a/c/d"); VFSWriter.addFileMapping(_acd.path(), "//root/a/c/d");
VFSWriter.addFileMapping(_ace.Path, "//root/a/c/e"); VFSWriter.addFileMapping(_ace.path(), "//root/a/c/e");
VFSWriter.addFileMapping(_acf.Path, "//root/a/c/f"); VFSWriter.addFileMapping(_acf.path(), "//root/a/c/f");
VFSWriter.addDirectoryMapping(_ag.Path, "//root/a/g"); VFSWriter.addDirectoryMapping(_ag.path(), "//root/a/g");
VFSWriter.addFileMapping(_agh.Path, "//root/a/g/h"); VFSWriter.addFileMapping(_agh.path(), "//root/a/g/h");
std::string Buffer; std::string Buffer;
raw_string_ostream OS(Buffer); raw_string_ostream OS(Buffer);
@ -2271,27 +2220,27 @@ TEST_F(VFSFromYAMLTest, YAMLVFSWriterTest2) {
} }
TEST_F(VFSFromYAMLTest, YAMLVFSWriterTest3) { TEST_F(VFSFromYAMLTest, YAMLVFSWriterTest3) {
ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true); TempDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
ScopedDir _a(TestDirectory + "/a"); TempDir _a(TestDirectory.path("a"));
ScopedFile _ab(TestDirectory + "/a/b", ""); TempFile _ab(TestDirectory.path("a/b"));
ScopedDir _ac(TestDirectory + "/a/c"); TempDir _ac(TestDirectory.path("a/c"));
ScopedDir _acd(TestDirectory + "/a/c/d"); TempDir _acd(TestDirectory.path("a/c/d"));
ScopedDir _acde(TestDirectory + "/a/c/d/e"); TempDir _acde(TestDirectory.path("a/c/d/e"));
ScopedFile _acdef(TestDirectory + "/a/c/d/e/f", ""); TempFile _acdef(TestDirectory.path("a/c/d/e/f"));
ScopedFile _acdeg(TestDirectory + "/a/c/d/e/g", ""); TempFile _acdeg(TestDirectory.path("a/c/d/e/g"));
ScopedDir _ah(TestDirectory + "/a/h"); TempDir _ah(TestDirectory.path("a/h"));
ScopedFile _ahi(TestDirectory + "/a/h/i", ""); TempFile _ahi(TestDirectory.path("a/h/i"));
vfs::YAMLVFSWriter VFSWriter; vfs::YAMLVFSWriter VFSWriter;
VFSWriter.addDirectoryMapping(_a.Path, "//root/a"); VFSWriter.addDirectoryMapping(_a.path(), "//root/a");
VFSWriter.addFileMapping(_ab.Path, "//root/a/b"); VFSWriter.addFileMapping(_ab.path(), "//root/a/b");
VFSWriter.addDirectoryMapping(_ac.Path, "//root/a/c"); VFSWriter.addDirectoryMapping(_ac.path(), "//root/a/c");
VFSWriter.addDirectoryMapping(_acd.Path, "//root/a/c/d"); VFSWriter.addDirectoryMapping(_acd.path(), "//root/a/c/d");
VFSWriter.addDirectoryMapping(_acde.Path, "//root/a/c/d/e"); VFSWriter.addDirectoryMapping(_acde.path(), "//root/a/c/d/e");
VFSWriter.addFileMapping(_acdef.Path, "//root/a/c/d/e/f"); VFSWriter.addFileMapping(_acdef.path(), "//root/a/c/d/e/f");
VFSWriter.addFileMapping(_acdeg.Path, "//root/a/c/d/e/g"); VFSWriter.addFileMapping(_acdeg.path(), "//root/a/c/d/e/g");
VFSWriter.addDirectoryMapping(_ahi.Path, "//root/a/h"); VFSWriter.addDirectoryMapping(_ahi.path(), "//root/a/h");
VFSWriter.addFileMapping(_ahi.Path, "//root/a/h/i"); VFSWriter.addFileMapping(_ahi.path(), "//root/a/h/i");
std::string Buffer; std::string Buffer;
raw_string_ostream OS(Buffer); raw_string_ostream OS(Buffer);
@ -2304,15 +2253,15 @@ TEST_F(VFSFromYAMLTest, YAMLVFSWriterTest3) {
} }
TEST_F(VFSFromYAMLTest, YAMLVFSWriterTestHandleDirs) { TEST_F(VFSFromYAMLTest, YAMLVFSWriterTestHandleDirs) {
ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true); TempDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
ScopedDir _a(TestDirectory + "/a"); TempDir _a(TestDirectory.path("a"));
ScopedDir _b(TestDirectory + "/b"); TempDir _b(TestDirectory.path("b"));
ScopedDir _c(TestDirectory + "/c"); TempDir _c(TestDirectory.path("c"));
vfs::YAMLVFSWriter VFSWriter; vfs::YAMLVFSWriter VFSWriter;
VFSWriter.addDirectoryMapping(_a.Path, "//root/a"); VFSWriter.addDirectoryMapping(_a.path(), "//root/a");
VFSWriter.addDirectoryMapping(_b.Path, "//root/b"); VFSWriter.addDirectoryMapping(_b.path(), "//root/b");
VFSWriter.addDirectoryMapping(_c.Path, "//root/c"); VFSWriter.addDirectoryMapping(_c.path(), "//root/c");
std::string Buffer; std::string Buffer;
raw_string_ostream OS(Buffer); raw_string_ostream OS(Buffer);
@ -2334,7 +2283,7 @@ TEST_F(VFSFromYAMLTest, YAMLVFSWriterTestHandleDirs) {
IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLRawString(Buffer, Lower); IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLRawString(Buffer, Lower);
ASSERT_TRUE(FS.get() != nullptr); ASSERT_TRUE(FS.get() != nullptr);
EXPECT_FALSE(FS->exists(_a.Path + "/a")); EXPECT_FALSE(FS->exists(_a.path("a")));
EXPECT_FALSE(FS->exists(_b.Path + "/b")); EXPECT_FALSE(FS->exists(_b.path("b")));
EXPECT_FALSE(FS->exists(_c.Path + "/c")); EXPECT_FALSE(FS->exists(_c.path("c")));
} }

View File

@ -16,6 +16,7 @@
#include "llvm/Support/TargetSelect.h" #include "llvm/Support/TargetSelect.h"
#include "llvm/Support/YAMLTraits.h" #include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/Testing/Support/SupportHelpers.h"
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
@ -25,6 +26,8 @@ using ::testing::get;
using ::testing::Pointwise; using ::testing::Pointwise;
using ::testing::Property; using ::testing::Property;
using llvm::unittest::TempDir;
namespace llvm { namespace llvm {
namespace exegesis { namespace exegesis {
@ -76,10 +79,8 @@ TEST_F(BenchmarkResultTest, WriteToAndReadFromDisk) {
ToDisk.Error = "error"; ToDisk.Error = "error";
ToDisk.Info = "info"; ToDisk.Info = "info";
SmallString<64> Filename; TempDir TestDirectory("BenchmarkResultTestDir", /*Unique*/ true);
std::error_code EC; SmallString<64> Filename(TestDirectory.path());
EC = sys::fs::createUniqueDirectory("BenchmarkResultTestDir", Filename);
ASSERT_FALSE(EC);
sys::path::append(Filename, "data.yaml"); sys::path::append(Filename, "data.yaml");
errs() << Filename << "-------\n"; errs() << Filename << "-------\n";
ExitOnErr(ToDisk.writeYaml(State, Filename)); ExitOnErr(ToDisk.writeYaml(State, Filename));

View File

@ -17,6 +17,7 @@
#include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h" #include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/Testing/Support/SupportHelpers.h"
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
@ -34,16 +35,17 @@ using testing::Field;
using testing::Property; using testing::Property;
using testing::SizeIs; using testing::SizeIs;
using llvm::unittest::TempDir;
class X86SnippetFileTest : public X86TestBase { class X86SnippetFileTest : public X86TestBase {
protected: protected:
Expected<std::vector<BenchmarkCode>> TestCommon(StringRef Contents) { Expected<std::vector<BenchmarkCode>> TestCommon(StringRef Contents) {
SmallString<64> Filename; TempDir TestDirectory("SnippetFileTestDir", /*Unique*/ true);
std::error_code EC; SmallString<64> Filename(TestDirectory.path());
EC = sys::fs::createUniqueDirectory("SnippetFileTestDir", Filename);
EXPECT_FALSE(EC);
sys::path::append(Filename, "snippet.s"); sys::path::append(Filename, "snippet.s");
errs() << Filename << "-------\n"; errs() << Filename << "-------\n";
{ {
std::error_code EC;
raw_fd_ostream FOS(Filename, EC); raw_fd_ostream FOS(Filename, EC);
FOS << Contents; FOS << Contents;
EXPECT_FALSE(EC); EXPECT_FALSE(EC);