2021-06-25 13:18:37 +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
|
|
|
|
|
|
|
|
#include <test/FilesystemUtils.h>
|
|
|
|
|
|
|
|
#include <test/libsolidity/util/SoltestErrors.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::test;
|
|
|
|
|
2021-06-13 13:53:16 +00:00
|
|
|
void solidity::test::createFilesWithParentDirs(set<boost::filesystem::path> const& _paths, string const& _content)
|
|
|
|
{
|
|
|
|
for (boost::filesystem::path const& path: _paths)
|
|
|
|
{
|
|
|
|
if (!path.parent_path().empty())
|
|
|
|
boost::filesystem::create_directories(path.parent_path());
|
|
|
|
|
2021-08-30 18:17:19 +00:00
|
|
|
// Use binary mode to avoid line ending conversion on Windows.
|
|
|
|
ofstream newFile(path.string(), std::ofstream::binary);
|
2021-06-13 13:53:16 +00:00
|
|
|
newFile << _content;
|
|
|
|
|
|
|
|
if (newFile.fail() || !boost::filesystem::exists(path))
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Failed to create an empty file: \"" + path.string() + "\"."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 22:02:58 +00:00
|
|
|
void solidity::test::createFileWithContent(boost::filesystem::path const& _path, string const& _content)
|
2021-06-25 13:18:37 +00:00
|
|
|
{
|
|
|
|
if (boost::filesystem::is_regular_file(_path))
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("File already exists: \"" + _path.string() + "\".")); \
|
|
|
|
|
|
|
|
// Use binary mode to avoid line ending conversion on Windows.
|
|
|
|
ofstream newFile(_path.string(), std::ofstream::binary);
|
|
|
|
if (newFile.fail() || !boost::filesystem::is_regular_file(_path))
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Failed to create a file: \"" + _path.string() + "\".")); \
|
|
|
|
|
2021-07-02 22:02:58 +00:00
|
|
|
newFile << _content;
|
2021-06-25 13:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool solidity::test::createSymlinkIfSupportedByFilesystem(
|
2021-07-28 00:36:30 +00:00
|
|
|
boost::filesystem::path _targetPath,
|
2021-07-02 22:02:58 +00:00
|
|
|
boost::filesystem::path const& _linkName,
|
|
|
|
bool _directorySymlink
|
2021-06-25 13:18:37 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
boost::system::error_code symlinkCreationError;
|
2021-07-02 22:02:58 +00:00
|
|
|
|
2021-07-28 00:36:30 +00:00
|
|
|
// NOTE: On Windows / works as a separator in a symlink target only if the target is absolute.
|
|
|
|
// Convert path separators to native ones to avoid this problem.
|
|
|
|
_targetPath.make_preferred();
|
|
|
|
|
2021-07-02 22:02:58 +00:00
|
|
|
if (_directorySymlink)
|
|
|
|
boost::filesystem::create_directory_symlink(_targetPath, _linkName, symlinkCreationError);
|
|
|
|
else
|
|
|
|
boost::filesystem::create_symlink(_targetPath, _linkName, symlinkCreationError);
|
2021-06-25 13:18:37 +00:00
|
|
|
|
|
|
|
if (!symlinkCreationError)
|
|
|
|
return true;
|
|
|
|
else if (
|
|
|
|
symlinkCreationError == boost::system::errc::not_supported ||
|
|
|
|
symlinkCreationError == boost::system::errc::operation_not_supported
|
|
|
|
)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error(
|
|
|
|
"Failed to create a symbolic link: \"" + _linkName.string() + "\""
|
|
|
|
" -> " + _targetPath.string() + "\"."
|
|
|
|
));
|
|
|
|
}
|