TemporaryDirectory: Add an alternative constructor that can create subdirectories

This commit is contained in:
Kamil Śliwak 2021-07-22 20:33:01 +02:00
parent 9dc7360903
commit 92446cbcab
5 changed files with 42 additions and 10 deletions

View File

@ -20,6 +20,7 @@
#include <test/libsolidity/util/SoltestErrors.h>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/filesystem.hpp>
#include <regex>
@ -40,6 +41,24 @@ TemporaryDirectory::TemporaryDirectory(std::string const& _prefix):
fs::create_directory(m_path);
}
TemporaryDirectory::TemporaryDirectory(
vector<boost::filesystem::path> const& _subdirectories,
string const& _prefix
):
TemporaryDirectory(_prefix)
{
for (boost::filesystem::path const& subdirectory: _subdirectories)
{
soltestAssert(!subdirectory.is_absolute() && subdirectory.root_path() != "/", "");
soltestAssert(
m_path.lexically_relative(subdirectory).empty() ||
*m_path.lexically_relative(subdirectory).begin() != "..",
""
);
boost::filesystem::create_directories(m_path / subdirectory);
}
}
TemporaryDirectory::~TemporaryDirectory()
{
// A few paranoid sanity checks just to be extra sure we're not deleting someone's homework.

View File

@ -25,6 +25,7 @@
#include <boost/filesystem.hpp>
#include <string>
#include <vector>
namespace solidity::test
{
@ -41,6 +42,10 @@ class TemporaryDirectory
{
public:
TemporaryDirectory(std::string const& _prefix = "solidity-test");
TemporaryDirectory(
std::vector<boost::filesystem::path> const& _subdirectories,
std::string const& _prefix = "solidity-test"
);
~TemporaryDirectory();
boost::filesystem::path const& path() const { return m_path; }

View File

@ -66,6 +66,19 @@ BOOST_AUTO_TEST_CASE(TemporaryDirectory_should_delete_its_directory_even_if_not_
BOOST_TEST(!boost::filesystem::exists(dirPath / "test-file.txt"));
}
BOOST_AUTO_TEST_CASE(TemporaryDirectory_should_create_subdirectories)
{
boost::filesystem::path dirPath;
{
TemporaryDirectory tempDir({"a", "a/", "a/b/c", "x.y/z"}, "temporary-directory-test");
dirPath = tempDir.path();
BOOST_TEST(boost::filesystem::is_directory(dirPath / "a"));
BOOST_TEST(boost::filesystem::is_directory(dirPath / "a/b/c"));
BOOST_TEST(boost::filesystem::is_directory(dirPath / "x.y/z"));
}
}
BOOST_AUTO_TEST_CASE(TemporaryWorkingDirectory_should_change_and_restore_working_directory)
{
boost::filesystem::path originalWorkingDirectory = boost::filesystem::current_path();

View File

@ -65,8 +65,7 @@ BOOST_AUTO_TEST_CASE(normalizeCLIPathForVFS_absolute_path)
BOOST_AUTO_TEST_CASE(normalizeCLIPathForVFS_relative_path)
{
TemporaryDirectory tempDir(TEST_CASE_NAME);
boost::filesystem::create_directories(tempDir.path() / "x/y/z");
TemporaryDirectory tempDir({"x/y/z"}, TEST_CASE_NAME);
TemporaryWorkingDirectory tempWorkDir(tempDir.path() / "x/y/z");
// NOTE: If path to work dir contains symlinks (often the case on macOS), boost might resolve
@ -253,9 +252,8 @@ BOOST_AUTO_TEST_CASE(normalizeCLIPathForVFS_path_separators)
BOOST_AUTO_TEST_CASE(normalizeCLIPathForVFS_should_not_resolve_symlinks)
{
TemporaryDirectory tempDir(TEST_CASE_NAME);
TemporaryDirectory tempDir({"abc/"}, TEST_CASE_NAME);
soltestAssert(tempDir.path().is_absolute(), "");
boost::filesystem::create_directories(tempDir.path() / "abc");
if (!createSymlinkIfSupportedByFilesystem(tempDir.path() / "abc", tempDir.path() / "sym", true))
return;
@ -269,9 +267,8 @@ BOOST_AUTO_TEST_CASE(normalizeCLIPathForVFS_should_not_resolve_symlinks)
BOOST_AUTO_TEST_CASE(normalizeCLIPathForVFS_should_resolve_symlinks_in_workdir_when_path_is_relative)
{
TemporaryDirectory tempDir(TEST_CASE_NAME);
TemporaryDirectory tempDir({"abc/"}, TEST_CASE_NAME);
soltestAssert(tempDir.path().is_absolute(), "");
boost::filesystem::create_directories(tempDir.path() / "abc");
if (!createSymlinkIfSupportedByFilesystem(tempDir.path() / "abc", tempDir.path() / "sym", true))
return;

View File

@ -614,8 +614,7 @@ BOOST_AUTO_TEST_CASE(cli_paths_to_source_unit_names_relative_base_path)
BOOST_AUTO_TEST_CASE(cli_paths_to_source_unit_names_normalization_and_weird_names)
{
TemporaryDirectory tempDir(TEST_CASE_NAME);
boost::filesystem::create_directories(tempDir.path() / "x/y/z");
TemporaryDirectory tempDir({"x/y/z"}, TEST_CASE_NAME);
TemporaryWorkingDirectory tempWorkDir(tempDir.path() / "x/y/z");
soltestAssert(tempDir.path().is_absolute(), "");
@ -782,9 +781,8 @@ BOOST_AUTO_TEST_CASE(cli_paths_to_source_unit_names_normalization_and_weird_name
BOOST_AUTO_TEST_CASE(cli_paths_to_source_unit_names_symlinks)
{
TemporaryDirectory tempDir(TEST_CASE_NAME);
TemporaryDirectory tempDir({"r/"}, TEST_CASE_NAME);
createFilesWithParentDirs({tempDir.path() / "x/y/z/contract.sol"});
boost::filesystem::create_directories(tempDir.path() / "r");
TemporaryWorkingDirectory tempWorkDir(tempDir.path() / "r");
if (