diff --git a/test/object-store/c_api/c_api.cpp b/test/object-store/c_api/c_api.cpp index c11320ed75c..2d3f98557a6 100644 --- a/test/object-store/c_api/c_api.cpp +++ b/test/object-store/c_api/c_api.cpp @@ -41,6 +41,7 @@ #if REALM_ENABLE_SYNC #include "util/sync/flx_sync_harness.hpp" #include "util/sync/sync_test_utils.hpp" +#include "util/test_path.hpp" #include "util/unit_test_transport.hpp" #include @@ -590,10 +591,7 @@ TEST_CASE("C API (non-database)", "[c_api]") { realm_app_config_set_bundle_id(app_config.get(), "some_bundle_id"); CHECK(app_config->device_info.bundle_id == "some_bundle_id"); - std::string temp_dir = util::make_temp_dir(); - auto guard = util::make_scope_exit([&temp_dir]() noexcept { - util::try_remove_dir_recursive(temp_dir); - }); + test_util::TestDirGuard temp_dir(util::make_temp_dir()); auto sync_client_config = cptr(realm_sync_client_config_new()); realm_sync_client_config_set_base_file_path(sync_client_config.get(), temp_dir.c_str()); realm_sync_client_config_set_metadata_mode(sync_client_config.get(), RLM_SYNC_CLIENT_METADATA_MODE_DISABLED); diff --git a/test/object-store/sync/metadata.cpp b/test/object-store/sync/metadata.cpp index ec2a7a43a41..65b76469992 100644 --- a/test/object-store/sync/metadata.cpp +++ b/test/object-store/sync/metadata.cpp @@ -37,15 +37,11 @@ using namespace realm::util; using File = realm::util::File; using SyncAction = SyncFileActionMetadata::Action; -static const std::string base_path = util::make_temp_dir() + "realm_objectstore_sync_metadata"; +static const std::string base_path = util::make_temp_dir() + "realm_objectstore_sync_metadata.test-dir"; static const std::string metadata_path = base_path + "/metadata.realm"; TEST_CASE("sync_metadata: user metadata", "[sync][metadata]") { - util::try_make_dir(base_path); - auto close = util::make_scope_exit([=]() noexcept { - util::try_remove_dir_recursive(base_path); - }); - + test_util::TestDirGuard test_dir(base_path); SyncMetadataManager manager(metadata_path, false); SECTION("can be properly constructed") { @@ -131,11 +127,7 @@ TEST_CASE("sync_metadata: user metadata", "[sync][metadata]") { } TEST_CASE("sync_metadata: user metadata APIs", "[sync][metadata]") { - util::try_make_dir(base_path); - auto close = util::make_scope_exit([=]() noexcept { - util::try_remove_dir_recursive(base_path); - }); - + test_util::TestDirGuard test_dir(base_path); SyncMetadataManager manager(metadata_path, false); const std::string provider_type = "https://realm.example.org"; @@ -163,11 +155,7 @@ TEST_CASE("sync_metadata: user metadata APIs", "[sync][metadata]") { } TEST_CASE("sync_metadata: file action metadata", "[sync][metadata]") { - util::try_make_dir(base_path); - auto close = util::make_scope_exit([=]() noexcept { - util::try_remove_dir_recursive(base_path); - }); - + test_util::TestDirGuard test_dir(base_path); SyncMetadataManager manager(metadata_path, false); const std::string local_uuid_1 = "asdfg"; @@ -207,10 +195,7 @@ TEST_CASE("sync_metadata: file action metadata", "[sync][metadata]") { } TEST_CASE("sync_metadata: file action metadata APIs", "[sync][metadata]") { - util::try_make_dir(base_path); - auto close = util::make_scope_exit([=]() noexcept { - util::try_remove_dir_recursive(base_path); - }); + test_util::TestDirGuard test_dir(base_path); SyncMetadataManager manager(metadata_path, false); SECTION("properly list all pending actions, reflecting their deletion") { @@ -233,11 +218,7 @@ TEST_CASE("sync_metadata: file action metadata APIs", "[sync][metadata]") { } TEST_CASE("sync_metadata: results", "[sync][metadata]") { - util::try_make_dir(base_path); - auto close = util::make_scope_exit([=]() noexcept { - util::try_remove_dir_recursive(base_path); - }); - + test_util::TestDirGuard test_dir(base_path); SyncMetadataManager manager(metadata_path, false); const auto identity1 = "testcase3a1"; const auto identity2 = "testcase3a3"; @@ -271,10 +252,7 @@ TEST_CASE("sync_metadata: results", "[sync][metadata]") { } TEST_CASE("sync_metadata: persistence across metadata manager instances", "[sync][metadata]") { - util::try_make_dir(base_path); - auto close = util::make_scope_exit([=]() noexcept { - util::try_remove_dir_recursive(base_path); - }); + test_util::TestDirGuard temp_dir(base_path); SECTION("works for the basic case") { const auto identity = "testcase4a"; @@ -297,11 +275,7 @@ TEST_CASE("sync_metadata: persistence across metadata manager instances", "[sync } TEST_CASE("sync_metadata: encryption", "[sync][metadata]") { - util::try_make_dir(base_path); - auto close = util::make_scope_exit([=]() noexcept { - util::try_remove_dir_recursive(base_path); - }); - + test_util::TestDirGuard test_dir(base_path); const auto identity0 = "identity0"; SECTION("prohibits opening the metadata Realm with different keys") { SECTION("different keys") { @@ -447,10 +421,7 @@ TEST_CASE("sync_metadata: encryption", "[sync][metadata]") { #ifndef SWIFT_PACKAGE // The SPM build currently doesn't copy resource files TEST_CASE("sync metadata: can open old metadata realms", "[sync][metadata]") { - util::try_make_dir(base_path); - auto close = util::make_scope_exit([=]() noexcept { - util::try_remove_dir_recursive(base_path); - }); + test_util::TestDirGuard test_dir(base_path); const std::string provider_type = "https://realm.example.org"; const auto identity = "metadata migration test"; diff --git a/test/object-store/sync/sync_manager.cpp b/test/object-store/sync/sync_manager.cpp index 5e3e73e46c0..f6e086dfbda 100644 --- a/test/object-store/sync/sync_manager.cpp +++ b/test/object-store/sync/sync_manager.cpp @@ -35,7 +35,7 @@ using namespace realm; using namespace realm::util; using File = realm::util::File; -static const auto base_path = fs::path{util::make_temp_dir()}.make_preferred() / "realm_objectstore_sync_manager"; +static const auto base_path = fs::path{util::make_temp_dir()}.make_preferred() / "realm_objectstore_sync_manager.test-dir"; static const std::string dummy_device_id = "123400000000000000000000"; namespace { @@ -462,8 +462,9 @@ TEST_CASE("sync_manager: persistent user state management", "[sync][sync manager } TEST_CASE("sync_manager: file actions", "[sync][sync manager]") { + test_util::TestDirGuard guard(base_path.string()); + using Action = SyncFileActionMetadata::Action; - reset_test_directory(base_path.string()); auto file_manager = SyncFileManager(base_path.string(), "bar_app_id"); // Open the metadata separately, so we can investigate it ourselves. diff --git a/test/object-store/util/test_utils.cpp b/test/object-store/util/test_utils.cpp index 5e48f135234..550748c5693 100644 --- a/test/object-store/util/test_utils.cpp +++ b/test/object-store/util/test_utils.cpp @@ -95,12 +95,6 @@ bool create_dummy_realm(std::string path, std::shared_ptr* out) } } -void reset_test_directory(const std::string& base_path) -{ - util::try_remove_dir_recursive(base_path); - util::make_dir(base_path); -} - std::vector make_test_encryption_key(const char start) { std::vector vector; diff --git a/test/object-store/util/test_utils.hpp b/test/object-store/util/test_utils.hpp index b6a99a9497d..86a548a6b74 100644 --- a/test/object-store/util/test_utils.hpp +++ b/test/object-store/util/test_utils.hpp @@ -180,7 +180,6 @@ std::ostream& operator<<(std::ostream&, const Exception&); class Realm; /// Open a Realm at a given path, creating its files. bool create_dummy_realm(std::string path, std::shared_ptr* out = nullptr); -void reset_test_directory(const std::string& base_path); std::vector make_test_encryption_key(const char start = 0); void catch2_ensure_section_run_workaround(bool did_run_a_section, std::string section_name, util::FunctionRef func);