Skip to content

Commit

Permalink
Merge pull request #11384 from ethereum/expose-temporary-directory-he…
Browse files Browse the repository at this point in the history
…lper

Expose TemporaryDirectory helper
  • Loading branch information
Leonardo authored May 18, 2021
2 parents d61f212 + cdebbb0 commit dac2429
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 136 deletions.
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ set(sources
InteractiveTests.h
Metadata.cpp
Metadata.h
TemporaryDirectory.cpp
TemporaryDirectory.h
TemporaryDirectoryTest.cpp
TestCase.cpp
TestCase.h
TestCaseReader.cpp
Expand Down
58 changes: 58 additions & 0 deletions test/TemporaryDirectory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0

#include <test/TemporaryDirectory.h>

#include <boost/filesystem.hpp>

#include <cassert>
#include <regex>
#include <iostream>

using namespace std;
using namespace solidity;
using namespace solidity::test;

namespace fs = boost::filesystem;

TemporaryDirectory::TemporaryDirectory(std::string const& _prefix):
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());

fs::create_directory(m_path);
}

TemporaryDirectory::~TemporaryDirectory()
{
// A few paranoid sanity checks just to be extra sure we're not deleting someone's homework.
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);
if (errorCode.value() != boost::system::errc::success)
{
cerr << "Failed to completely remove temporary directory '" << m_path << "'. ";
cerr << "Only " << numRemoved << " files were actually removed." << endl;
cerr << "Reason: " << errorCode.message() << endl;
}
}
51 changes: 51 additions & 0 deletions test/TemporaryDirectory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* Utility for creating temporary directories for use in tests.
*/

#pragma once

#include <boost/filesystem.hpp>

#include <string>

namespace solidity::test
{

/**
* An object that creates a unique temporary directory and automatically deletes it and its
* content upon being destroyed.
*
* The directory is guaranteed to be newly created and empty. Directory names are generated
* randomly. If a directory with the same name already exists (very unlikely but possible) the
* object won't reuse it and will fail with an exception instead.
*/
class TemporaryDirectory
{
public:
TemporaryDirectory(std::string const& _prefix = "solidity-test-");
~TemporaryDirectory();

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

private:
boost::filesystem::path m_path;
};

}
71 changes: 71 additions & 0 deletions test/TemporaryDirectoryTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0

#include <test/TemporaryDirectory.h>

#include <boost/filesystem.hpp>
#include <boost/test/unit_test.hpp>

#include <fstream>

using namespace std;
using namespace boost::test_tools;

namespace fs = boost::filesystem;

namespace solidity::test
{

BOOST_AUTO_TEST_SUITE(TemporaryDirectoryTest)

BOOST_AUTO_TEST_CASE(TemporaryDirectory_should_create_and_delete_a_unique_and_empty_directory)
{
fs::path dirPath;
{
TemporaryDirectory tempDir("temporary-directory-test-");
dirPath = tempDir.path();

BOOST_TEST(dirPath.stem().string().find("temporary-directory-test-") == 0);
BOOST_TEST(fs::equivalent(dirPath.parent_path(), fs::temp_directory_path()));
BOOST_TEST(fs::is_directory(dirPath));
BOOST_TEST(fs::is_empty(dirPath));
}
BOOST_TEST(!fs::exists(dirPath));
}

BOOST_AUTO_TEST_CASE(TemporaryDirectory_should_delete_its_directory_even_if_not_empty)
{
fs::path dirPath;
{
TemporaryDirectory tempDir("temporary-directory-test-");
dirPath = tempDir.path();

BOOST_TEST(fs::is_directory(dirPath));

{
ofstream tmpFile((dirPath / "test-file.txt").string());
tmpFile << "Delete me!" << endl;
}
assert(fs::is_regular_file(dirPath / "test-file.txt"));
}
BOOST_TEST(!fs::exists(dirPath / "test-file.txt"));
}

BOOST_AUTO_TEST_SUITE_END()

}
4 changes: 3 additions & 1 deletion test/yulPhaser/AlgorithmRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
// SPDX-License-Identifier: GPL-3.0

#include <test/TemporaryDirectory.h>
#include <test/yulPhaser/TestHelpers.h>

#include <tools/yulPhaser/AlgorithmRunner.h>
Expand All @@ -37,6 +38,7 @@ using namespace std;
using namespace boost::unit_test::framework;
using namespace boost::test_tools;
using namespace solidity::langutil;
using namespace solidity::test;
using namespace solidity::util;
using namespace solidity::yul;

Expand Down Expand Up @@ -118,7 +120,7 @@ class AlgorithmRunnerAutosaveFixture: public AlgorithmRunnerFixture

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;
};

Expand Down
6 changes: 4 additions & 2 deletions test/yulPhaser/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
// SPDX-License-Identifier: GPL-3.0

#include <test/TemporaryDirectory.h>
#include <test/yulPhaser/TestHelpers.h>

#include <tools/yulPhaser/Common.h>
Expand All @@ -31,6 +32,7 @@

using namespace std;
using namespace boost::test_tools;
using namespace solidity::test;
using namespace solidity::util;

namespace solidity::phaser::test
Expand Down Expand Up @@ -73,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", "#", ""}));
}

Expand Down
18 changes: 10 additions & 8 deletions test/yulPhaser/Phaser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
// SPDX-License-Identifier: GPL-3.0

#include <test/TemporaryDirectory.h>
#include <test/yulPhaser/TestHelpers.h>

#include <tools/yulPhaser/Exceptions.h>
Expand All @@ -31,6 +32,7 @@
#include <algorithm>

using namespace std;
using namespace solidity::test;
using namespace solidity::util;
using namespace solidity::langutil;
using namespace solidity::yul;
Expand Down Expand Up @@ -326,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(
Expand Down Expand Up @@ -359,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;

Expand Down Expand Up @@ -417,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 = */ "",
};
Expand All @@ -444,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",
};

Expand Down
39 changes: 0 additions & 39 deletions test/yulPhaser/TestHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,14 @@

#include <libyul/optimiser/Suite.h>

#include <boost/filesystem.hpp>

#include <regex>
#include <iostream>

using namespace std;
using namespace solidity;
using namespace solidity::yul;
using namespace solidity::phaser;
using namespace solidity::phaser::test;

namespace fs = boost::filesystem;

function<Mutation> phaser::test::wholeChromosomeReplacement(Chromosome _newChromosome)
{
return [_newChromosome = move(_newChromosome)](Chromosome const&) { return _newChromosome; };
Expand Down Expand Up @@ -82,40 +77,6 @@ size_t phaser::test::countDifferences(Chromosome const& _chromosome1, Chromosome
));
}

TemporaryDirectory::TemporaryDirectory(std::string const& _prefix):
m_path((fs::temp_directory_path() / fs::unique_path(_prefix + "%%%%-%%%%-%%%%-%%%%")).string())
{
// 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());

fs::create_directory(m_path);
}

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());

boost::system::error_code errorCode;
uintmax_t numRemoved = fs::remove_all(m_path, errorCode);
if (errorCode.value() != boost::system::errc::success)
{
cerr << "Failed to completely remove temporary directory '" << m_path << "'. ";
cerr << "Only " << numRemoved << " files were actually removed." << endl;
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();
}

string phaser::test::stripWhitespace(string const& input)
{
regex whitespaceRegex("\\s+");
Expand Down
Loading

0 comments on commit dac2429

Please sign in to comment.