TemporaryDirectory: Expose boost::filesystem::path in class interface

This commit is contained in:
Kamil Śliwak 2021-04-16 20:45:19 +02:00
parent 5619702d31
commit cdebbb0dbb
6 changed files with 20 additions and 52 deletions

View File

@ -31,7 +31,7 @@ using namespace solidity::test;
namespace fs = boost::filesystem;
TemporaryDirectory::TemporaryDirectory(std::string const& _prefix):
m_path((fs::temp_directory_path() / fs::unique_path(_prefix + "%%%%-%%%%-%%%%-%%%%")).string())
m_path(fs::temp_directory_path() / fs::unique_path(_prefix + "%%%%-%%%%-%%%%-%%%%"))
{
// Prefix should just be a file name and not contain anything that would make us step out of /tmp.
assert(fs::path(_prefix) == fs::path(_prefix).stem());
@ -42,10 +42,10 @@ TemporaryDirectory::TemporaryDirectory(std::string const& _prefix):
TemporaryDirectory::~TemporaryDirectory()
{
// A few paranoid sanity checks just to be extra sure we're not deleting someone's homework.
assert(m_path.find(fs::temp_directory_path().string()) == 0);
assert(fs::path(m_path) != fs::temp_directory_path());
assert(fs::path(m_path) != fs::path(m_path).root_path());
assert(!fs::path(m_path).empty());
assert(m_path.string().find(fs::temp_directory_path().string()) == 0);
assert(m_path != fs::temp_directory_path());
assert(m_path != m_path.root_path());
assert(!m_path.empty());
boost::system::error_code errorCode;
uintmax_t numRemoved = fs::remove_all(m_path, errorCode);
@ -56,10 +56,3 @@ TemporaryDirectory::~TemporaryDirectory()
cerr << "Reason: " << errorCode.message() << endl;
}
}
string TemporaryDirectory::memberPath(string const& _relativePath) const
{
assert(fs::path(_relativePath).is_relative());
return (fs::path(m_path) / _relativePath).string();
}

View File

@ -21,6 +21,8 @@
#pragma once
#include <boost/filesystem.hpp>
#include <string>
namespace solidity::test
@ -40,13 +42,10 @@ public:
TemporaryDirectory(std::string const& _prefix = "solidity-test-");
~TemporaryDirectory();
std::string const& path() const { return m_path; }
/// Converts a path relative to the directory held by the object into an absolute one.
std::string memberPath(std::string const& _relativePath) const;
boost::filesystem::path const& path() const { return m_path; }
private:
std::string m_path;
boost::filesystem::path m_path;
};
}

View File

@ -66,30 +66,6 @@ BOOST_AUTO_TEST_CASE(TemporaryDirectory_should_delete_its_directory_even_if_not_
BOOST_TEST(!fs::exists(dirPath / "test-file.txt"));
}
BOOST_AUTO_TEST_CASE(TemporaryDirectory_memberPath_should_construct_paths_relative_to_the_temporary_directory)
{
TemporaryDirectory tempDir("temporary-directory-test-");
BOOST_TEST(fs::equivalent(tempDir.memberPath(""), tempDir.path()));
BOOST_TEST(fs::equivalent(tempDir.memberPath("."), tempDir.path() / fs::path(".")));
BOOST_TEST(fs::equivalent(tempDir.memberPath(".."), tempDir.path() / fs::path("..")));
// NOTE: fs::equivalent() only works with paths that actually exist
{
ofstream file;
file.open(tempDir.memberPath("file.txt"), ios::out);
}
BOOST_TEST(fs::equivalent(tempDir.memberPath("file.txt"), tempDir.path() / fs::path("file.txt")));
{
fs::create_directories(tempDir.memberPath("a/b/"));
ofstream file;
file.open(tempDir.memberPath("a/b/file.txt"), ios::out);
}
BOOST_TEST(fs::equivalent(tempDir.memberPath("a/b/file.txt"), tempDir.path() / fs::path("a") / fs::path("b") / fs::path("file.txt")));
}
BOOST_AUTO_TEST_SUITE_END()
}

View File

@ -120,7 +120,7 @@ public:
protected:
TemporaryDirectory m_tempDir;
string const m_autosavePath = m_tempDir.memberPath("population-autosave.txt");
string const m_autosavePath = (m_tempDir.path() / "population-autosave.txt").string();
RandomisingAlgorithm m_algorithm;
};

View File

@ -75,11 +75,11 @@ BOOST_AUTO_TEST_SUITE(CommonTest)
BOOST_FIXTURE_TEST_CASE(readLinesFromFile_should_return_all_lines_from_a_text_file_as_strings_without_newlines, ReadLinesFromFileFixture)
{
{
ofstream tmpFile(m_tempDir.memberPath("test-file.txt"));
ofstream tmpFile((m_tempDir.path() / "test-file.txt").string());
tmpFile << endl << "Line 1" << endl << endl << endl << "Line 2" << endl << "#" << endl << endl;
}
vector<string> lines = readLinesFromFile(m_tempDir.memberPath("test-file.txt"));
vector<string> lines = readLinesFromFile((m_tempDir.path() / "test-file.txt").string());
BOOST_TEST((lines == vector<string>{"", "Line 1", "", "", "Line 2", "#", ""}));
}

View File

@ -328,11 +328,11 @@ BOOST_FIXTURE_TEST_CASE(build_should_respect_population_from_file_option, Poulat
TemporaryDirectory tempDir;
for (auto const& [fileName, chromosomes]: fileContent)
{
ofstream tmpFile(tempDir.memberPath(fileName));
ofstream tmpFile((tempDir.path() / fileName).string());
for (auto const& chromosome: chromosomes)
tmpFile << chromosome << endl;
m_options.populationFromFile.push_back(tempDir.memberPath(fileName));
m_options.populationFromFile.push_back((tempDir.path() / fileName).string());
}
BOOST_TEST(
@ -361,13 +361,13 @@ BOOST_FIXTURE_TEST_CASE(build_should_combine_populations_from_all_sources, Poula
{
TemporaryDirectory tempDir;
{
ofstream tmpFile(tempDir.memberPath("population.txt"));
ofstream tmpFile((tempDir.path() / "population.txt").string());
tmpFile << "axc" << endl << "fcL" << endl;
}
m_options.population = {"axc", "fcL"};
m_options.randomPopulation = {2};
m_options.populationFromFile = {tempDir.memberPath("population.txt")};
m_options.populationFromFile = {(tempDir.path() / "population.txt").string()};
m_options.minChromosomeLength = 3;
m_options.maxChromosomeLength = 3;
@ -419,9 +419,9 @@ BOOST_AUTO_TEST_CASE(build_should_load_programs_from_files)
vector<string> sources{"{}", "{{}}", "{{{}}}"};
ProgramFactory::Options options{
/* inputFiles = */ {
tempDir.memberPath("program1.yul"),
tempDir.memberPath("program2.yul"),
tempDir.memberPath("program3.yul"),
(tempDir.path() / "program1.yul").string(),
(tempDir.path() / "program2.yul").string(),
(tempDir.path() / "program3.yul").string(),
},
/* prefix = */ "",
};
@ -446,7 +446,7 @@ BOOST_AUTO_TEST_CASE(build_should_apply_prefix)
{
TemporaryDirectory tempDir;
ProgramFactory::Options options{
/* inputFiles = */ {tempDir.memberPath("program1.yul")},
/* inputFiles = */ {(tempDir.path() / "program1.yul").string()},
/* prefix = */ "f",
};