Skip to content

Commit 6feb4d5

Browse files
committed
TemporaryDirectory: Add an alternative constructor that can create subdirectories
1 parent 4ac6f59 commit 6feb4d5

File tree

5 files changed

+42
-10
lines changed

5 files changed

+42
-10
lines changed

test/TemporaryDirectory.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <test/libsolidity/util/SoltestErrors.h>
2222

23+
#include <boost/algorithm/string/predicate.hpp>
2324
#include <boost/filesystem.hpp>
2425

2526
#include <regex>
@@ -40,6 +41,24 @@ TemporaryDirectory::TemporaryDirectory(std::string const& _prefix):
4041
fs::create_directory(m_path);
4142
}
4243

44+
TemporaryDirectory::TemporaryDirectory(
45+
vector<boost::filesystem::path> const& _subdirectories,
46+
string const& _prefix
47+
):
48+
TemporaryDirectory(_prefix)
49+
{
50+
for (boost::filesystem::path const& subdirectory: _subdirectories)
51+
{
52+
soltestAssert(!subdirectory.is_absolute() && subdirectory.root_path() != "/", "");
53+
soltestAssert(
54+
m_path.lexically_relative(subdirectory).empty() ||
55+
*m_path.lexically_relative(subdirectory).begin() != "..",
56+
""
57+
);
58+
boost::filesystem::create_directories(m_path / subdirectory);
59+
}
60+
}
61+
4362
TemporaryDirectory::~TemporaryDirectory()
4463
{
4564
// A few paranoid sanity checks just to be extra sure we're not deleting someone's homework.

test/TemporaryDirectory.h

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <boost/filesystem.hpp>
2626

2727
#include <string>
28+
#include <vector>
2829

2930
namespace solidity::test
3031
{
@@ -41,6 +42,10 @@ class TemporaryDirectory
4142
{
4243
public:
4344
TemporaryDirectory(std::string const& _prefix = "solidity-test");
45+
TemporaryDirectory(
46+
std::vector<boost::filesystem::path> const& _subdirectories,
47+
std::string const& _prefix = "solidity-test"
48+
);
4449
~TemporaryDirectory();
4550

4651
boost::filesystem::path const& path() const { return m_path; }

test/TemporaryDirectoryTest.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ BOOST_AUTO_TEST_CASE(TemporaryDirectory_should_delete_its_directory_even_if_not_
6666
BOOST_TEST(!boost::filesystem::exists(dirPath / "test-file.txt"));
6767
}
6868

69+
BOOST_AUTO_TEST_CASE(TemporaryDirectory_should_create_subdirectories)
70+
{
71+
boost::filesystem::path dirPath;
72+
{
73+
TemporaryDirectory tempDir({"a", "a/", "a/b/c", "x.y/z"}, "temporary-directory-test");
74+
dirPath = tempDir.path();
75+
76+
BOOST_TEST(boost::filesystem::is_directory(dirPath / "a"));
77+
BOOST_TEST(boost::filesystem::is_directory(dirPath / "a/b/c"));
78+
BOOST_TEST(boost::filesystem::is_directory(dirPath / "x.y/z"));
79+
}
80+
}
81+
6982
BOOST_AUTO_TEST_CASE(TemporaryWorkingDirectory_should_change_and_restore_working_directory)
7083
{
7184
boost::filesystem::path originalWorkingDirectory = boost::filesystem::current_path();

test/libsolidity/interface/FileReader.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ BOOST_AUTO_TEST_CASE(normalizeCLIPathForVFS_absolute_path)
6565

6666
BOOST_AUTO_TEST_CASE(normalizeCLIPathForVFS_relative_path)
6767
{
68-
TemporaryDirectory tempDir(TEST_CASE_NAME);
69-
boost::filesystem::create_directories(tempDir.path() / "x/y/z");
68+
TemporaryDirectory tempDir({"x/y/z"}, TEST_CASE_NAME);
7069
TemporaryWorkingDirectory tempWorkDir(tempDir.path() / "x/y/z");
7170

7271
// 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)
253252

254253
BOOST_AUTO_TEST_CASE(normalizeCLIPathForVFS_should_not_resolve_symlinks)
255254
{
256-
TemporaryDirectory tempDir(TEST_CASE_NAME);
255+
TemporaryDirectory tempDir({"abc/"}, TEST_CASE_NAME);
257256
soltestAssert(tempDir.path().is_absolute(), "");
258-
boost::filesystem::create_directories(tempDir.path() / "abc");
259257

260258
if (!createSymlinkIfSupportedByFilesystem(tempDir.path() / "abc", tempDir.path() / "sym", true))
261259
return;
@@ -269,9 +267,8 @@ BOOST_AUTO_TEST_CASE(normalizeCLIPathForVFS_should_not_resolve_symlinks)
269267

270268
BOOST_AUTO_TEST_CASE(normalizeCLIPathForVFS_should_resolve_symlinks_in_workdir_when_path_is_relative)
271269
{
272-
TemporaryDirectory tempDir(TEST_CASE_NAME);
270+
TemporaryDirectory tempDir({"abc/"}, TEST_CASE_NAME);
273271
soltestAssert(tempDir.path().is_absolute(), "");
274-
boost::filesystem::create_directories(tempDir.path() / "abc");
275272

276273
if (!createSymlinkIfSupportedByFilesystem(tempDir.path() / "abc", tempDir.path() / "sym", true))
277274
return;

test/solc/CommandLineInterface.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -610,8 +610,7 @@ BOOST_AUTO_TEST_CASE(cli_paths_to_source_unit_names_relative_base_path)
610610

611611
BOOST_AUTO_TEST_CASE(cli_paths_to_source_unit_names_normalization_and_weird_names)
612612
{
613-
TemporaryDirectory tempDir(TEST_CASE_NAME);
614-
boost::filesystem::create_directories(tempDir.path() / "x/y/z");
613+
TemporaryDirectory tempDir({"x/y/z"}, TEST_CASE_NAME);
615614
TemporaryWorkingDirectory tempWorkDir(tempDir.path() / "x/y/z");
616615
soltestAssert(tempDir.path().is_absolute(), "");
617616

@@ -777,9 +776,8 @@ BOOST_AUTO_TEST_CASE(cli_paths_to_source_unit_names_normalization_and_weird_name
777776

778777
BOOST_AUTO_TEST_CASE(cli_paths_to_source_unit_names_symlinks)
779778
{
780-
TemporaryDirectory tempDir(TEST_CASE_NAME);
779+
TemporaryDirectory tempDir({"r/"}, TEST_CASE_NAME);
781780
createFilesWithParentDirs({tempDir.path() / "x/y/z/contract.sol"});
782-
boost::filesystem::create_directories(tempDir.path() / "r");
783781
TemporaryWorkingDirectory tempWorkDir(tempDir.path() / "r");
784782

785783
if (

0 commit comments

Comments
 (0)