Skip to content

fix unit tests to not write in cwd #2134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/dev/db_toolbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2347,7 +2347,7 @@ int main(int argc, char* argv[]) {
DataDirectory data_dir{data_dir_factory()};

if (!*cmd_initgenesis) {
if (!data_dir.chaindata().exists() || data_dir.is_pristine()) {
if (!data_dir.chaindata().exists() || data_dir.chaindata().is_empty()) {
std::cerr << "\n Directory " << data_dir.chaindata().path().string() << " does not exist or is empty\n";
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions silkworm/capi/silkworm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,10 +813,10 @@ static SilkwormRpcSettings make_rpc_settings_for_test(uint16_t api_listening_por
(void)std::snprintf(settings.eth_api_host, SILKWORM_RPC_SETTINGS_HOST_SIZE, "localhost");
(void)std::snprintf(settings.eth_api_spec, SILKWORM_RPC_SETTINGS_API_NAMESPACE_SPEC_SIZE, "eth,ots");
for (auto& domain : settings.cors_domains) {
domain[0] = 0;
domain[0] = '\0';
}
(void)std::snprintf(settings.cors_domains[0], SILKWORM_RPC_SETTINGS_CORS_DOMAIN_SIZE, "*");
(void)std::snprintf(settings.jwt_file_path, SILKWORM_PATH_SIZE, "jwt.hex");
settings.jwt_file_path[0] = '\0';
return settings;
}

Expand Down
25 changes: 18 additions & 7 deletions silkworm/db/snapshot_sync_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,27 @@ static std::unique_ptr<SnapshotBundleFactory> bundle_factory() {

TEST_CASE("SnapshotSync::SnapshotSync", "[db][snapshot][sync]") {
SetLogVerbosityGuard guard{log::Level::kNone};
SnapshotRepository repository{SnapshotSettings{}, bundle_factory()};
TemporaryDirectory tmp_dir;
SnapshotSettings settings{
.bittorrent_settings = bittorrent::BitTorrentSettings{
.repository_path = tmp_dir.path() / bittorrent::BitTorrentSettings::kDefaultTorrentRepoPath,
},
};
SnapshotRepository repository{settings, bundle_factory()};
CHECK_NOTHROW(SnapshotSync{&repository, kMainnetConfig});
}

TEST_CASE("SnapshotSync::download_and_index_snapshots", "[db][snapshot][sync]") {
SetLogVerbosityGuard guard{log::Level::kNone};
db::test_util::TempChainData context;
const auto tmp_dir{TemporaryDirectory::get_unique_temporary_path()};
TemporaryDirectory tmp_dir;
bittorrent::BitTorrentSettings bittorrent_settings{
.repository_path = tmp_dir / bittorrent::BitTorrentSettings::kDefaultTorrentRepoPath,
.repository_path = tmp_dir.path() / bittorrent::BitTorrentSettings::kDefaultTorrentRepoPath,
};

SECTION("snapshots disabled") {
SnapshotSettings settings{
.repository_dir = tmp_dir,
.repository_dir = tmp_dir.path(),
.enabled = false,
.bittorrent_settings = bittorrent_settings,
};
Expand All @@ -66,7 +72,7 @@ TEST_CASE("SnapshotSync::download_and_index_snapshots", "[db][snapshot][sync]")

SECTION("no download, just reopen") {
SnapshotSettings settings{
.repository_dir = tmp_dir,
.repository_dir = tmp_dir.path(),
.no_downloader = true,
.bittorrent_settings = bittorrent_settings,
};
Expand All @@ -77,7 +83,7 @@ TEST_CASE("SnapshotSync::download_and_index_snapshots", "[db][snapshot][sync]")

SECTION("no download, just reopen and verify") {
SnapshotSettings settings{
.repository_dir = tmp_dir,
.repository_dir = tmp_dir.path(),
.no_downloader = true,
.bittorrent_settings = bittorrent_settings,
};
Expand All @@ -101,7 +107,12 @@ struct SnapshotSync_ForTest : public SnapshotSync {
TEST_CASE("SnapshotSync::update_block_headers", "[db][snapshot][sync]") {
SetLogVerbosityGuard guard{log::Level::kNone};
TemporaryDirectory tmp_dir;
SnapshotSettings settings{tmp_dir.path()};
SnapshotSettings settings{
.repository_dir = tmp_dir.path(),
.bittorrent_settings = bittorrent::BitTorrentSettings{
.repository_path = tmp_dir.path() / bittorrent::BitTorrentSettings::kDefaultTorrentRepoPath,
},
};
SnapshotRepository repository{settings, bundle_factory()};
db::test_util::TempChainData tmp_db;

Expand Down
3 changes: 2 additions & 1 deletion silkworm/db/snapshots/bittorrent/client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ static inline std::vector<char> test_resume_data() {

TEST_CASE("BitTorrentClient::BitTorrentClient", "[silkworm][snapshot][bittorrent]") {
SECTION("default settings") {
CHECK_NOTHROW(BitTorrentClient{BitTorrentSettings{}});
TemporaryDirectory tmp_dir;
CHECK_NOTHROW(BitTorrentClient{BitTorrentSettings{.repository_path = tmp_dir.path()}});
}

TestRepository repo;
Expand Down
7 changes: 2 additions & 5 deletions silkworm/infra/common/directories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ Directory::Directory(const std::filesystem::path& directory_path, bool must_crea
}
}

bool Directory::is_pristine() const {
if (!exists()) {
return false;
}
return std::filesystem::is_empty(path_);
bool Directory::is_empty() const {
return exists() && std::filesystem::is_empty(path_);
}

const std::filesystem::path& Directory::path() const { return path_; }
Expand Down
4 changes: 2 additions & 2 deletions silkworm/infra/common/directories.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class Directory {
//! \brief Returns whether this Directory exists on filesystem
[[nodiscard]] bool exists() const;

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

//! \brief Returns the cumulative size of all contained files and subdirectories
[[nodiscard]] size_t size() const;
Expand Down
18 changes: 6 additions & 12 deletions silkworm/infra/common/directories_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace silkworm {

TEST_CASE("DataDirectory") {
TEST_CASE("DataDirectory::deploy") {
{
// Open and create a storage path
TemporaryDirectory tmp_dir0;
Expand All @@ -35,15 +35,9 @@ TEST_CASE("DataDirectory") {
std::filesystem::remove_all(data_dir.path());
REQUIRE(data_dir.exists() == false);
}
}

{
// Open datadir from current process running path
DataDirectory data_dir{std::filesystem::path(), false};
REQUIRE(data_dir.is_pristine() == false);
REQUIRE(data_dir.exists() == true);
REQUIRE_NOTHROW(data_dir.deploy());
}

TEST_CASE("DataDirectory::from_chaindata") {
TemporaryDirectory tmp_dir1;
std::filesystem::path fake_path{tmp_dir1.path() / "nonexistentpath"};
std::filesystem::path fake_path_root{fake_path.root_path()};
Expand All @@ -62,7 +56,7 @@ TEST_CASE("DataDirectory") {
{
DataDirectory data_dir{DataDirectory::from_chaindata(fake_path)};
REQUIRE_NOTHROW(data_dir.deploy());
REQUIRE(data_dir.etl().is_pristine());
REQUIRE(data_dir.etl().is_empty());

// Drop a file into etl temp
{
Expand All @@ -73,10 +67,10 @@ TEST_CASE("DataDirectory") {
}
std::filesystem::path etl_subpath{data_dir.etl().path() / "subdir"};
std::filesystem::create_directories(etl_subpath);
REQUIRE(data_dir.etl().is_pristine() == false);
REQUIRE_FALSE(data_dir.etl().is_empty());
REQUIRE(data_dir.etl().size() != 0);
data_dir.etl().clear();
REQUIRE(data_dir.etl().is_pristine() == true);
REQUIRE(data_dir.etl().is_empty());
}
}

Expand Down
Loading