From 92446cbcab47486e518507f00e31d8b30d384ec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Thu, 22 Jul 2021 20:33:01 +0200 Subject: [PATCH] TemporaryDirectory: Add an alternative constructor that can create subdirectories --- test/TemporaryDirectory.cpp | 19 +++++++++++++++++++ test/TemporaryDirectory.h | 5 +++++ test/TemporaryDirectoryTest.cpp | 13 +++++++++++++ test/libsolidity/interface/FileReader.cpp | 9 +++------ test/solc/CommandLineInterface.cpp | 6 ++---- 5 files changed, 42 insertions(+), 10 deletions(-) diff --git a/test/TemporaryDirectory.cpp b/test/TemporaryDirectory.cpp index 76d369bbe..51c1ba9af 100644 --- a/test/TemporaryDirectory.cpp +++ b/test/TemporaryDirectory.cpp @@ -20,6 +20,7 @@ #include +#include #include #include @@ -40,6 +41,24 @@ TemporaryDirectory::TemporaryDirectory(std::string const& _prefix): fs::create_directory(m_path); } +TemporaryDirectory::TemporaryDirectory( + vector 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. diff --git a/test/TemporaryDirectory.h b/test/TemporaryDirectory.h index 823251d8d..94ecdca13 100644 --- a/test/TemporaryDirectory.h +++ b/test/TemporaryDirectory.h @@ -25,6 +25,7 @@ #include #include +#include namespace solidity::test { @@ -41,6 +42,10 @@ class TemporaryDirectory { public: TemporaryDirectory(std::string const& _prefix = "solidity-test"); + TemporaryDirectory( + std::vector const& _subdirectories, + std::string const& _prefix = "solidity-test" + ); ~TemporaryDirectory(); boost::filesystem::path const& path() const { return m_path; } diff --git a/test/TemporaryDirectoryTest.cpp b/test/TemporaryDirectoryTest.cpp index 8c25794d2..1fef44c3e 100644 --- a/test/TemporaryDirectoryTest.cpp +++ b/test/TemporaryDirectoryTest.cpp @@ -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(); diff --git a/test/libsolidity/interface/FileReader.cpp b/test/libsolidity/interface/FileReader.cpp index 1c6af8a5a..fd1eb52cc 100644 --- a/test/libsolidity/interface/FileReader.cpp +++ b/test/libsolidity/interface/FileReader.cpp @@ -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; diff --git a/test/solc/CommandLineInterface.cpp b/test/solc/CommandLineInterface.cpp index 321b33475..0023e9f57 100644 --- a/test/solc/CommandLineInterface.cpp +++ b/test/solc/CommandLineInterface.cpp @@ -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 (