Skip to content

Commit 8d5eaf4

Browse files
committed
Create FilesystemUtils with filesystem-related convenience helpers
1 parent 65e23ff commit 8d5eaf4

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ set(sources
88
EVMHost.h
99
ExecutionFramework.cpp
1010
ExecutionFramework.h
11+
FilesystemUtils.cpp
12+
FilesystemUtils.h
1113
InteractiveTests.h
1214
Metadata.cpp
1315
Metadata.h

test/FilesystemUtils.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
This file is part of solidity.
3+
4+
solidity is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
solidity is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with solidity. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
// SPDX-License-Identifier: GPL-3.0
18+
19+
#include <test/FilesystemUtils.h>
20+
21+
#include <test/libsolidity/util/SoltestErrors.h>
22+
23+
using namespace std;
24+
using namespace solidity;
25+
using namespace solidity::test;
26+
27+
void solidity::test::createFileWithContent(boost::filesystem::path const& _path, string const& content)
28+
{
29+
if (boost::filesystem::is_regular_file(_path))
30+
BOOST_THROW_EXCEPTION(runtime_error("File already exists: \"" + _path.string() + "\".")); \
31+
32+
// Use binary mode to avoid line ending conversion on Windows.
33+
ofstream newFile(_path.string(), std::ofstream::binary);
34+
if (newFile.fail() || !boost::filesystem::is_regular_file(_path))
35+
BOOST_THROW_EXCEPTION(runtime_error("Failed to create a file: \"" + _path.string() + "\".")); \
36+
37+
newFile << content;
38+
}
39+
40+
bool solidity::test::createSymlinkIfSupportedByFilesystem(
41+
boost::filesystem::path const& _targetPath,
42+
boost::filesystem::path const& _linkName
43+
)
44+
{
45+
boost::system::error_code symlinkCreationError;
46+
boost::filesystem::create_symlink(_targetPath, _linkName, symlinkCreationError);
47+
48+
if (!symlinkCreationError)
49+
return true;
50+
else if (
51+
symlinkCreationError == boost::system::errc::not_supported ||
52+
symlinkCreationError == boost::system::errc::operation_not_supported
53+
)
54+
return false;
55+
else
56+
BOOST_THROW_EXCEPTION(runtime_error(
57+
"Failed to create a symbolic link: \"" + _linkName.string() + "\""
58+
" -> " + _targetPath.string() + "\"."
59+
));
60+
}

test/FilesystemUtils.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
This file is part of solidity.
3+
4+
solidity is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
solidity is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with solidity. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
// SPDX-License-Identifier: GPL-3.0
18+
/**
19+
* Helpers for common filesystem operations used in multiple tests.
20+
*/
21+
22+
#pragma once
23+
24+
#include <boost/filesystem.hpp>
25+
26+
#include <string>
27+
28+
namespace solidity::test
29+
{
30+
31+
/// Creates a file with the exact content specified in the second argument.
32+
/// Throws an exception if the file already exists or if the parent directory of the file does not.
33+
void createFileWithContent(boost::filesystem::path const& _path, std::string const& content);
34+
35+
/// Creates a symlink between two paths.
36+
/// The target does not have to exist.
37+
/// @returns true if the symlink has been successfully created, false if the filesystem does not
38+
/// support symlinks.
39+
/// Throws an exception of the operation fails for a different reason.
40+
bool createSymlinkIfSupportedByFilesystem(
41+
boost::filesystem::path const& _targetPath,
42+
boost::filesystem::path const& _linkName
43+
);
44+
45+
}

0 commit comments

Comments
 (0)