Skip to content

Commit

Permalink
Use TestDirGuard where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoyne committed Feb 16, 2024
1 parent d79b283 commit 33dfd8a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 51 deletions.
6 changes: 2 additions & 4 deletions test/object-store/c_api/c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <realm/object-store/sync/app_utils.hpp>
Expand Down Expand Up @@ -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);
Expand Down
47 changes: 9 additions & 38 deletions test/object-store/sync/metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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";

Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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") {
Expand All @@ -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";
Expand Down Expand Up @@ -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";
Expand All @@ -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") {
Expand Down Expand Up @@ -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";
Expand Down
5 changes: 3 additions & 2 deletions test/object-store/sync/sync_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 0 additions & 6 deletions test/object-store/util/test_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,6 @@ bool create_dummy_realm(std::string path, std::shared_ptr<Realm>* out)
}
}

void reset_test_directory(const std::string& base_path)
{
util::try_remove_dir_recursive(base_path);
util::make_dir(base_path);
}

std::vector<char> make_test_encryption_key(const char start)
{
std::vector<char> vector;
Expand Down
1 change: 0 additions & 1 deletion test/object-store/util/test_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Realm>* out = nullptr);
void reset_test_directory(const std::string& base_path);
std::vector<char> 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<void()> func);
Expand Down

0 comments on commit 33dfd8a

Please sign in to comment.