mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Implement TemporaryWorkingDirectory test helper
This commit is contained in:
parent
d07c85db67
commit
828b15b34f
@ -56,3 +56,14 @@ TemporaryDirectory::~TemporaryDirectory()
|
||||
cerr << "Reason: " << errorCode.message() << endl;
|
||||
}
|
||||
}
|
||||
|
||||
TemporaryWorkingDirectory::TemporaryWorkingDirectory(fs::path const& _newDirectory):
|
||||
m_originalWorkingDirectory(fs::current_path())
|
||||
{
|
||||
fs::current_path(_newDirectory);
|
||||
}
|
||||
|
||||
TemporaryWorkingDirectory::~TemporaryWorkingDirectory()
|
||||
{
|
||||
fs::current_path(m_originalWorkingDirectory);
|
||||
}
|
||||
|
@ -16,7 +16,8 @@
|
||||
*/
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
/**
|
||||
* Utility for creating temporary directories for use in tests.
|
||||
* Utilities for creating temporary directories and temporarily changing the working directory
|
||||
* for use in tests.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
@ -48,4 +49,19 @@ private:
|
||||
boost::filesystem::path m_path;
|
||||
};
|
||||
|
||||
/**
|
||||
* An object that changes current working directory and restores it upon destruction.
|
||||
*/
|
||||
class TemporaryWorkingDirectory
|
||||
{
|
||||
public:
|
||||
TemporaryWorkingDirectory(boost::filesystem::path const& _newDirectory);
|
||||
~TemporaryWorkingDirectory();
|
||||
|
||||
boost::filesystem::path const& originalWorkingDirectory() const { return m_originalWorkingDirectory; }
|
||||
|
||||
private:
|
||||
boost::filesystem::path m_originalWorkingDirectory;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -66,6 +66,31 @@ BOOST_AUTO_TEST_CASE(TemporaryDirectory_should_delete_its_directory_even_if_not_
|
||||
BOOST_TEST(!fs::exists(dirPath / "test-file.txt"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(TemporaryWorkingDirectory_should_change_and_restore_working_directory)
|
||||
{
|
||||
fs::path originalWorkingDirectory = fs::current_path();
|
||||
|
||||
try
|
||||
{
|
||||
{
|
||||
TemporaryDirectory tempDir("temporary-directory-test-");
|
||||
assert(fs::equivalent(fs::current_path(), originalWorkingDirectory));
|
||||
assert(!fs::equivalent(tempDir.path(), originalWorkingDirectory));
|
||||
|
||||
TemporaryWorkingDirectory tempWorkDir(tempDir.path());
|
||||
|
||||
BOOST_TEST(fs::equivalent(fs::current_path(), tempDir.path()));
|
||||
}
|
||||
BOOST_TEST(fs::equivalent(fs::current_path(), originalWorkingDirectory));
|
||||
|
||||
fs::current_path(originalWorkingDirectory);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
fs::current_path(originalWorkingDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user