2021-04-10 22:48:16 +00:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
|
2022-05-15 15:45:43 +00:00
|
|
|
#include <libsolutil/TemporaryDirectory.h>
|
2021-04-10 22:48:16 +00:00
|
|
|
|
2022-05-15 15:45:43 +00:00
|
|
|
#include <libsolidity/util/SoltestErrors.h>
|
2021-07-22 14:03:30 +00:00
|
|
|
|
2021-04-10 22:48:16 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
using namespace boost::test_tools;
|
|
|
|
|
2022-05-15 15:45:43 +00:00
|
|
|
namespace solidity::util::test
|
2021-04-10 22:48:16 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(TemporaryDirectoryTest)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(TemporaryDirectory_should_create_and_delete_a_unique_and_empty_directory)
|
|
|
|
{
|
2021-07-22 14:03:30 +00:00
|
|
|
boost::filesystem::path dirPath;
|
2021-04-10 22:48:16 +00:00
|
|
|
{
|
2021-07-20 10:17:42 +00:00
|
|
|
TemporaryDirectory tempDir("temporary-directory-test");
|
2021-04-10 22:48:16 +00:00
|
|
|
dirPath = tempDir.path();
|
|
|
|
|
2021-07-20 10:17:42 +00:00
|
|
|
BOOST_TEST(dirPath.stem().string().find("temporary-directory-test") == 0);
|
2021-07-22 14:03:30 +00:00
|
|
|
BOOST_TEST(boost::filesystem::equivalent(dirPath.parent_path(), boost::filesystem::temp_directory_path()));
|
|
|
|
BOOST_TEST(boost::filesystem::is_directory(dirPath));
|
|
|
|
BOOST_TEST(boost::filesystem::is_empty(dirPath));
|
2021-04-10 22:48:16 +00:00
|
|
|
}
|
2021-07-22 14:03:30 +00:00
|
|
|
BOOST_TEST(!boost::filesystem::exists(dirPath));
|
2021-04-10 22:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(TemporaryDirectory_should_delete_its_directory_even_if_not_empty)
|
|
|
|
{
|
2021-07-22 14:03:30 +00:00
|
|
|
boost::filesystem::path dirPath;
|
2021-04-10 22:48:16 +00:00
|
|
|
{
|
2021-07-20 10:17:42 +00:00
|
|
|
TemporaryDirectory tempDir("temporary-directory-test");
|
2021-04-10 22:48:16 +00:00
|
|
|
dirPath = tempDir.path();
|
|
|
|
|
2021-07-22 14:03:30 +00:00
|
|
|
BOOST_TEST(boost::filesystem::is_directory(dirPath));
|
2021-04-10 22:48:16 +00:00
|
|
|
|
|
|
|
{
|
2023-08-30 00:08:06 +00:00
|
|
|
std::ofstream tmpFile((dirPath / "test-file.txt").string());
|
|
|
|
tmpFile << "Delete me!" << std::endl;
|
2021-04-10 22:48:16 +00:00
|
|
|
}
|
2021-07-22 14:03:30 +00:00
|
|
|
soltestAssert(boost::filesystem::is_regular_file(dirPath / "test-file.txt"), "");
|
2021-04-10 22:48:16 +00:00
|
|
|
}
|
2021-07-22 14:03:30 +00:00
|
|
|
BOOST_TEST(!boost::filesystem::exists(dirPath / "test-file.txt"));
|
2021-04-10 22:48:16 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 18:33:01 +00:00
|
|
|
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"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-16 18:31:13 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(TemporaryWorkingDirectory_should_change_and_restore_working_directory)
|
|
|
|
{
|
2021-07-22 14:03:30 +00:00
|
|
|
boost::filesystem::path originalWorkingDirectory = boost::filesystem::current_path();
|
2021-04-16 18:31:13 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
2021-07-20 10:17:42 +00:00
|
|
|
TemporaryDirectory tempDir("temporary-directory-test");
|
2021-07-22 14:03:30 +00:00
|
|
|
soltestAssert(boost::filesystem::equivalent(boost::filesystem::current_path(), originalWorkingDirectory), "");
|
|
|
|
soltestAssert(!boost::filesystem::equivalent(tempDir.path(), originalWorkingDirectory), "");
|
2021-04-16 18:31:13 +00:00
|
|
|
|
|
|
|
TemporaryWorkingDirectory tempWorkDir(tempDir.path());
|
|
|
|
|
2021-07-22 14:03:30 +00:00
|
|
|
BOOST_TEST(boost::filesystem::equivalent(boost::filesystem::current_path(), tempDir.path()));
|
2021-04-16 18:31:13 +00:00
|
|
|
}
|
2021-07-22 14:03:30 +00:00
|
|
|
BOOST_TEST(boost::filesystem::equivalent(boost::filesystem::current_path(), originalWorkingDirectory));
|
2021-04-16 18:31:13 +00:00
|
|
|
|
2021-07-22 14:03:30 +00:00
|
|
|
boost::filesystem::current_path(originalWorkingDirectory);
|
2021-04-16 18:31:13 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2021-07-22 14:03:30 +00:00
|
|
|
boost::filesystem::current_path(originalWorkingDirectory);
|
2021-04-16 18:31:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 22:48:16 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
}
|