Skip to content

Commit a066061

Browse files
authored
fix unit tests to not write in cwd (#2134)
1 parent de343e7 commit a066061

File tree

7 files changed

+33
-30
lines changed

7 files changed

+33
-30
lines changed

cmd/dev/db_toolbox.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2347,7 +2347,7 @@ int main(int argc, char* argv[]) {
23472347
DataDirectory data_dir{data_dir_factory()};
23482348

23492349
if (!*cmd_initgenesis) {
2350-
if (!data_dir.chaindata().exists() || data_dir.is_pristine()) {
2350+
if (!data_dir.chaindata().exists() || data_dir.chaindata().is_empty()) {
23512351
std::cerr << "\n Directory " << data_dir.chaindata().path().string() << " does not exist or is empty\n";
23522352
return -1;
23532353
}

silkworm/capi/silkworm_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,10 +813,10 @@ static SilkwormRpcSettings make_rpc_settings_for_test(uint16_t api_listening_por
813813
(void)std::snprintf(settings.eth_api_host, SILKWORM_RPC_SETTINGS_HOST_SIZE, "localhost");
814814
(void)std::snprintf(settings.eth_api_spec, SILKWORM_RPC_SETTINGS_API_NAMESPACE_SPEC_SIZE, "eth,ots");
815815
for (auto& domain : settings.cors_domains) {
816-
domain[0] = 0;
816+
domain[0] = '\0';
817817
}
818818
(void)std::snprintf(settings.cors_domains[0], SILKWORM_RPC_SETTINGS_CORS_DOMAIN_SIZE, "*");
819-
(void)std::snprintf(settings.jwt_file_path, SILKWORM_PATH_SIZE, "jwt.hex");
819+
settings.jwt_file_path[0] = '\0';
820820
return settings;
821821
}
822822

silkworm/db/snapshot_sync_test.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,27 @@ static std::unique_ptr<SnapshotBundleFactory> bundle_factory() {
4141

4242
TEST_CASE("SnapshotSync::SnapshotSync", "[db][snapshot][sync]") {
4343
SetLogVerbosityGuard guard{log::Level::kNone};
44-
SnapshotRepository repository{SnapshotSettings{}, bundle_factory()};
44+
TemporaryDirectory tmp_dir;
45+
SnapshotSettings settings{
46+
.bittorrent_settings = bittorrent::BitTorrentSettings{
47+
.repository_path = tmp_dir.path() / bittorrent::BitTorrentSettings::kDefaultTorrentRepoPath,
48+
},
49+
};
50+
SnapshotRepository repository{settings, bundle_factory()};
4551
CHECK_NOTHROW(SnapshotSync{&repository, kMainnetConfig});
4652
}
4753

4854
TEST_CASE("SnapshotSync::download_and_index_snapshots", "[db][snapshot][sync]") {
4955
SetLogVerbosityGuard guard{log::Level::kNone};
5056
db::test_util::TempChainData context;
51-
const auto tmp_dir{TemporaryDirectory::get_unique_temporary_path()};
57+
TemporaryDirectory tmp_dir;
5258
bittorrent::BitTorrentSettings bittorrent_settings{
53-
.repository_path = tmp_dir / bittorrent::BitTorrentSettings::kDefaultTorrentRepoPath,
59+
.repository_path = tmp_dir.path() / bittorrent::BitTorrentSettings::kDefaultTorrentRepoPath,
5460
};
5561

5662
SECTION("snapshots disabled") {
5763
SnapshotSettings settings{
58-
.repository_dir = tmp_dir,
64+
.repository_dir = tmp_dir.path(),
5965
.enabled = false,
6066
.bittorrent_settings = bittorrent_settings,
6167
};
@@ -66,7 +72,7 @@ TEST_CASE("SnapshotSync::download_and_index_snapshots", "[db][snapshot][sync]")
6672

6773
SECTION("no download, just reopen") {
6874
SnapshotSettings settings{
69-
.repository_dir = tmp_dir,
75+
.repository_dir = tmp_dir.path(),
7076
.no_downloader = true,
7177
.bittorrent_settings = bittorrent_settings,
7278
};
@@ -77,7 +83,7 @@ TEST_CASE("SnapshotSync::download_and_index_snapshots", "[db][snapshot][sync]")
7783

7884
SECTION("no download, just reopen and verify") {
7985
SnapshotSettings settings{
80-
.repository_dir = tmp_dir,
86+
.repository_dir = tmp_dir.path(),
8187
.no_downloader = true,
8288
.bittorrent_settings = bittorrent_settings,
8389
};
@@ -101,7 +107,12 @@ struct SnapshotSync_ForTest : public SnapshotSync {
101107
TEST_CASE("SnapshotSync::update_block_headers", "[db][snapshot][sync]") {
102108
SetLogVerbosityGuard guard{log::Level::kNone};
103109
TemporaryDirectory tmp_dir;
104-
SnapshotSettings settings{tmp_dir.path()};
110+
SnapshotSettings settings{
111+
.repository_dir = tmp_dir.path(),
112+
.bittorrent_settings = bittorrent::BitTorrentSettings{
113+
.repository_path = tmp_dir.path() / bittorrent::BitTorrentSettings::kDefaultTorrentRepoPath,
114+
},
115+
};
105116
SnapshotRepository repository{settings, bundle_factory()};
106117
db::test_util::TempChainData tmp_db;
107118

silkworm/db/snapshots/bittorrent/client_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ static inline std::vector<char> test_resume_data() {
109109

110110
TEST_CASE("BitTorrentClient::BitTorrentClient", "[silkworm][snapshot][bittorrent]") {
111111
SECTION("default settings") {
112-
CHECK_NOTHROW(BitTorrentClient{BitTorrentSettings{}});
112+
TemporaryDirectory tmp_dir;
113+
CHECK_NOTHROW(BitTorrentClient{BitTorrentSettings{.repository_path = tmp_dir.path()}});
113114
}
114115

115116
TestRepository repo;

silkworm/infra/common/directories.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,8 @@ Directory::Directory(const std::filesystem::path& directory_path, bool must_crea
5757
}
5858
}
5959

60-
bool Directory::is_pristine() const {
61-
if (!exists()) {
62-
return false;
63-
}
64-
return std::filesystem::is_empty(path_);
60+
bool Directory::is_empty() const {
61+
return exists() && std::filesystem::is_empty(path_);
6562
}
6663

6764
const std::filesystem::path& Directory::path() const { return path_; }

silkworm/infra/common/directories.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class Directory {
3737
//! \brief Returns whether this Directory exists on filesystem
3838
[[nodiscard]] bool exists() const;
3939

40-
//! \brief Returns whether this Directory is uncontaminated (i.e. brand new with no contents)
41-
[[nodiscard]] bool is_pristine() const;
40+
//! \brief Returns whether this Directory is empty
41+
[[nodiscard]] bool is_empty() const;
4242

4343
//! \brief Returns the cumulative size of all contained files and subdirectories
4444
[[nodiscard]] size_t size() const;

silkworm/infra/common/directories_test.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
namespace silkworm {
2424

25-
TEST_CASE("DataDirectory") {
25+
TEST_CASE("DataDirectory::deploy") {
2626
{
2727
// Open and create a storage path
2828
TemporaryDirectory tmp_dir0;
@@ -35,15 +35,9 @@ TEST_CASE("DataDirectory") {
3535
std::filesystem::remove_all(data_dir.path());
3636
REQUIRE(data_dir.exists() == false);
3737
}
38+
}
3839

39-
{
40-
// Open datadir from current process running path
41-
DataDirectory data_dir{std::filesystem::path(), false};
42-
REQUIRE(data_dir.is_pristine() == false);
43-
REQUIRE(data_dir.exists() == true);
44-
REQUIRE_NOTHROW(data_dir.deploy());
45-
}
46-
40+
TEST_CASE("DataDirectory::from_chaindata") {
4741
TemporaryDirectory tmp_dir1;
4842
std::filesystem::path fake_path{tmp_dir1.path() / "nonexistentpath"};
4943
std::filesystem::path fake_path_root{fake_path.root_path()};
@@ -62,7 +56,7 @@ TEST_CASE("DataDirectory") {
6256
{
6357
DataDirectory data_dir{DataDirectory::from_chaindata(fake_path)};
6458
REQUIRE_NOTHROW(data_dir.deploy());
65-
REQUIRE(data_dir.etl().is_pristine());
59+
REQUIRE(data_dir.etl().is_empty());
6660

6761
// Drop a file into etl temp
6862
{
@@ -73,10 +67,10 @@ TEST_CASE("DataDirectory") {
7367
}
7468
std::filesystem::path etl_subpath{data_dir.etl().path() / "subdir"};
7569
std::filesystem::create_directories(etl_subpath);
76-
REQUIRE(data_dir.etl().is_pristine() == false);
70+
REQUIRE_FALSE(data_dir.etl().is_empty());
7771
REQUIRE(data_dir.etl().size() != 0);
7872
data_dir.etl().clear();
79-
REQUIRE(data_dir.etl().is_pristine() == true);
73+
REQUIRE(data_dir.etl().is_empty());
8074
}
8175
}
8276

0 commit comments

Comments
 (0)