From 703c0fd2b566d96579183be2f0b1e15684aaeae1 Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Thu, 29 May 2025 17:40:40 -0700 Subject: [PATCH] Remove std::filesystem from third_party/nearby. PiperOrigin-RevId: 764953786 --- internal/base/files_test.cc | 1 - internal/data/BUILD | 3 ++ internal/data/leveldb_data_set_test.cc | 35 ++++++------- .../apple/preferences_manager.mm | 3 +- internal/platform/implementation/g3/BUILD | 2 + .../platform/implementation/g3/platform.cc | 3 +- .../implementation/g3/preferences_manager.cc | 10 +--- .../implementation/g3/preferences_manager.h | 2 +- .../g3/preferences_repository.cc | 11 ++-- .../g3/preferences_repository.h | 12 ++--- .../g3/preferences_repository_test.cc | 15 ++---- .../implementation/preferences_manager.h | 2 +- .../implementation/windows/platform.cc | 2 +- .../windows/preferences_manager.cc | 6 +-- .../windows/preferences_manager.h | 4 +- .../windows/preferences_manager_test.cc | 52 ++++++++++--------- .../windows/preferences_repository_test.cc | 36 ++++++------- 17 files changed, 92 insertions(+), 107 deletions(-) diff --git a/internal/base/files_test.cc b/internal/base/files_test.cc index 2810038f..735b025d 100644 --- a/internal/base/files_test.cc +++ b/internal/base/files_test.cc @@ -15,7 +15,6 @@ #include "internal/base/files.h" #include -#include // NOLINT #include #include #include diff --git a/internal/data/BUILD b/internal/data/BUILD index 29ed7a0e..19e7a887 100644 --- a/internal/data/BUILD +++ b/internal/data/BUILD @@ -60,9 +60,12 @@ cc_test( deps = [ ":data_manager", ":leveldb_data_set_test_cc_proto", + "//internal/base:file_path", + "//internal/base:files", "//internal/platform/implementation/g3", # fixdeps: keep "@com_github_protobuf_matchers//protobuf-matchers", "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/strings", "@com_google_absl//absl/synchronization", "@com_google_absl//absl/time", "@com_google_googletest//:gtest_main", diff --git a/internal/data/leveldb_data_set_test.cc b/internal/data/leveldb_data_set_test.cc index f1843ee2..b469f019 100644 --- a/internal/data/leveldb_data_set_test.cc +++ b/internal/data/leveldb_data_set_test.cc @@ -16,11 +16,8 @@ #include -#include // NOLINT(build/c++17) -#include #include #include -#include #include #include #include @@ -29,8 +26,11 @@ #include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" #include "absl/container/flat_hash_map.h" +#include "absl/strings/str_cat.h" #include "absl/synchronization/notification.h" #include "absl/time/time.h" +#include "internal/base/file_path.h" +#include "internal/base/files.h" #include "internal/data/data_set.h" #include "internal/data/leveldb_data_set_test.proto.h" @@ -40,25 +40,24 @@ namespace { using ::testing::SizeIs; // Generate a unique directory under temp directory for leveldb storage -std::filesystem::path GenerateLeveldbPath() { - auto temp_directory_path = std::filesystem::temp_directory_path(); +FilePath GenerateLeveldbPath() { + FilePath temp_directory_path = Files::GetTemporaryDirectory(); std::random_device dev; std::mt19937 prng(dev()); std::uniform_int_distribution rand(0); - std::filesystem::path path; + FilePath path; do { - std::stringstream leveldb_directory; - leveldb_directory << std::hex << "nearby_db_" << rand(prng); - path = temp_directory_path / leveldb_directory.str(); - } while (std::filesystem::exists(path)); + path = temp_directory_path; + path.append(FilePath(absl::StrCat("nearby_db_", absl::Hex(rand(prng))))); + } while (Files::FileExists(path)); return path; } // Helper functions to synchronize LeveldbDataSet function calls for testing template std::unique_ptr> CreateDataSet( - const std::filesystem::path& path) { - return std::make_unique>(path.string()); + const FilePath& path) { + return std::make_unique>(path.ToString()); } template @@ -129,13 +128,13 @@ absl::flat_hash_map LoadEntriesWithKeysAndWait( template void WipeCleanAndWait(std::unique_ptr>& dataset, - std::filesystem::path path) { + FilePath path) { absl::Notification notification; dataset->Destroy([¬ification](bool) { notification.Notify(); }); notification.WaitForNotificationWithTimeout(absl::Seconds(5)); // Call the destructor before removing leveldb storage directory dataset.reset(); - std::filesystem::remove_all(path); + Files::RemoveDirectory(path); } DiceRoll GenerateDiceRoll(int value) { @@ -153,7 +152,7 @@ DiceRoll GenerateDiceRoll(int value) { } TEST(LeveldbDataSet, UpdateEntriesDiceRoll) { - std::filesystem::path path = GenerateLeveldbPath(); + FilePath path = GenerateLeveldbPath(); std::unique_ptr> diceroll_set = CreateDataSet(path); @@ -175,7 +174,7 @@ TEST(LeveldbDataSet, UpdateEntriesDiceRoll) { } TEST(LeveldbDataSet, LoadEntriesDiceRoll) { - std::filesystem::path path = GenerateLeveldbPath(); + FilePath path = GenerateLeveldbPath(); std::unique_ptr> diceroll_set = CreateDataSet(path); @@ -210,7 +209,7 @@ TEST(LeveldbDataSet, LoadEntriesDiceRoll) { } TEST(LeveldbDataSet, LoadEntrysDiceRoll) { - std::filesystem::path path = GenerateLeveldbPath(); + FilePath path = GenerateLeveldbPath(); std::unique_ptr> diceroll_set = CreateDataSet(path); @@ -240,7 +239,7 @@ TEST(LeveldbDataSet, LoadEntrysDiceRoll) { } TEST(LeveldbDataSet, RemoveEntriesDiceRoll) { - std::filesystem::path path = GenerateLeveldbPath(); + FilePath path = GenerateLeveldbPath(); std::unique_ptr> diceroll_set = CreateDataSet(path); diff --git a/internal/platform/implementation/apple/preferences_manager.mm b/internal/platform/implementation/apple/preferences_manager.mm index 47cbc176..bdb2baa3 100644 --- a/internal/platform/implementation/apple/preferences_manager.mm +++ b/internal/platform/implementation/apple/preferences_manager.mm @@ -26,8 +26,7 @@ namespace nearby::apple { -PreferencesManager::PreferencesManager(absl::string_view file_path) - : api::PreferencesManager(file_path) {} +PreferencesManager::PreferencesManager(absl::string_view file_path) {} bool PreferencesManager::Set(absl::string_view key, const nlohmann::json& value) { [[NSUserDefaults standardUserDefaults] setObject:@(value.dump().c_str()) diff --git a/internal/platform/implementation/g3/BUILD b/internal/platform/implementation/g3/BUILD index 4e7a2b8a..7b4cf50d 100644 --- a/internal/platform/implementation/g3/BUILD +++ b/internal/platform/implementation/g3/BUILD @@ -183,6 +183,8 @@ cc_library( srcs = ["preferences_repository.cc"], hdrs = ["preferences_repository.h"], deps = [ + "//internal/base:file_path", + "//internal/base:files", "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/strings", "@com_google_absl//absl/synchronization", diff --git a/internal/platform/implementation/g3/platform.cc b/internal/platform/implementation/g3/platform.cc index 0f0de685..61d98127 100644 --- a/internal/platform/implementation/g3/platform.cc +++ b/internal/platform/implementation/g3/platform.cc @@ -16,7 +16,6 @@ #include #include -#include // NOLINT #include #include @@ -269,7 +268,7 @@ ImplementationPlatform::CreateDeviceInfo() { std::unique_ptr ImplementationPlatform::CreatePreferencesManager(absl::string_view path) { - return std::make_unique(path); + return std::make_unique(); } } // namespace api diff --git a/internal/platform/implementation/g3/preferences_manager.cc b/internal/platform/implementation/g3/preferences_manager.cc index 37fca236..5ceea256 100644 --- a/internal/platform/implementation/g3/preferences_manager.cc +++ b/internal/platform/implementation/g3/preferences_manager.cc @@ -27,7 +27,6 @@ #include "absl/types/span.h" #include "nlohmann/json.hpp" #include "nlohmann/json_fwd.hpp" -#include "internal/base/file_path.h" #include "internal/platform/device_info_impl.h" #include "internal/platform/implementation/g3/preferences_repository.h" #include "internal/platform/implementation/preferences_manager.h" @@ -39,14 +38,9 @@ namespace { using json = ::nlohmann::json; } // namespace -PreferencesManager::PreferencesManager(absl::string_view file_path) - : api::PreferencesManager(file_path) { +PreferencesManager::PreferencesManager() { auto device_info = std::make_unique(); - FilePath path = device_info->GetAppDataPath(); - - path.append(FilePath(file_path)); - preferences_repository_ = - std::make_unique(path.ToString()); + preferences_repository_ = std::make_unique(); value_ = preferences_repository_->LoadPreferences(); } diff --git a/internal/platform/implementation/g3/preferences_manager.h b/internal/platform/implementation/g3/preferences_manager.h index 188d530d..d20c7adc 100644 --- a/internal/platform/implementation/g3/preferences_manager.h +++ b/internal/platform/implementation/g3/preferences_manager.h @@ -40,7 +40,7 @@ namespace g3 { // change by the observer. class PreferencesManager : public api::PreferencesManager { public: - explicit PreferencesManager(absl::string_view path); + PreferencesManager(); // Sets values diff --git a/internal/platform/implementation/g3/preferences_repository.cc b/internal/platform/implementation/g3/preferences_repository.cc index 0700743c..5c3a6abb 100644 --- a/internal/platform/implementation/g3/preferences_repository.cc +++ b/internal/platform/implementation/g3/preferences_repository.cc @@ -14,12 +14,13 @@ #include "internal/platform/implementation/g3/preferences_repository.h" -#include // NOLINT(build/c++17) #include #include "absl/synchronization/mutex.h" #include "nlohmann/json.hpp" #include "nlohmann/json_fwd.hpp" +#include "internal/base/file_path.h" +#include "internal/base/files.h" namespace nearby { namespace g3 { @@ -35,13 +36,13 @@ json PreferencesRepository::LoadPreferences() { // settings.json is used for testing, but we should look at having // an implementation override for G3 in PreferencesManager to override // the path for testing. - std::filesystem::path path = - std::filesystem::temp_directory_path() / "settings.json"; - if (!std::filesystem::exists(path)) { + FilePath path = + Files::GetTemporaryDirectory().append(FilePath("settings.json")); + if (!Files::FileExists(path)) { return value_; } - std::ifstream preferences_file(path.c_str()); + std::ifstream preferences_file(path.GetPath()); if (!preferences_file.good()) { return value_; } diff --git a/internal/platform/implementation/g3/preferences_repository.h b/internal/platform/implementation/g3/preferences_repository.h index bff1d61b..5615346c 100644 --- a/internal/platform/implementation/g3/preferences_repository.h +++ b/internal/platform/implementation/g3/preferences_repository.h @@ -15,20 +15,16 @@ #ifndef PLATFORM_IMPLEMENTATION_G3_PREFERENCES_REPOSITORY_H_ #define PLATFORM_IMPLEMENTATION_G3_PREFERENCES_REPOSITORY_H_ -#include - #include "absl/base/thread_annotations.h" -#include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" #include "nlohmann/json.hpp" #include "nlohmann/json_fwd.hpp" -namespace nearby { -namespace g3 { +namespace nearby::g3 { class PreferencesRepository { public: - explicit PreferencesRepository(absl::string_view path) : path_(path) {} + PreferencesRepository() = default; nlohmann::json LoadPreferences() ABSL_LOCKS_EXCLUDED(&mutex_); bool SavePreferences(nlohmann::json preferences) ABSL_LOCKS_EXCLUDED(&mutex_); @@ -38,10 +34,8 @@ class PreferencesRepository { // preferences storage nlohmann::json value_ = nlohmann::json::object(); absl::Mutex mutex_; - const std::string path_; }; -} // namespace g3 -} // namespace nearby +} // namespace nearby::g3 #endif // PLATFORM_IMPLEMENTATION_G3_PREFERENCES_REPOSITORY_H_ diff --git a/internal/platform/implementation/g3/preferences_repository_test.cc b/internal/platform/implementation/g3/preferences_repository_test.cc index b407f64f..bac55073 100644 --- a/internal/platform/implementation/g3/preferences_repository_test.cc +++ b/internal/platform/implementation/g3/preferences_repository_test.cc @@ -14,23 +14,19 @@ #include "internal/platform/implementation/g3/preferences_repository.h" -#include // NOLINT(build/c++17) #include #include "gtest/gtest.h" #include "nlohmann/json.hpp" #include "nlohmann/json_fwd.hpp" -namespace nearby { -namespace platform { -namespace g3 { +namespace nearby::g3 { namespace { using json = ::nlohmann::json; } // namespace TEST(Preferences, TestSaveAndGetPreferences) { - PreferencesRepository preferences_repository{ - std::filesystem::temp_directory_path().string()}; + PreferencesRepository preferences_repository; std::string string_key = "string_value"; std::string string_value = "hello world"; std::string int_key = "int_value"; @@ -44,8 +40,7 @@ TEST(Preferences, TestSaveAndGetPreferences) { } TEST(Preferences, TestMultipleSaveAndGetPreferences) { - PreferencesRepository preferences_repository{ - std::filesystem::temp_directory_path().string()}; + PreferencesRepository preferences_repository; std::string string_key = "string_value"; std::string string_value = "hello world"; std::string string_new_value = "again"; @@ -63,6 +58,4 @@ TEST(Preferences, TestMultipleSaveAndGetPreferences) { EXPECT_EQ(result[int_key].get(), 456); } -} // namespace g3 -} // namespace platform -} // namespace nearby +} // namespace nearby::g3 diff --git a/internal/platform/implementation/preferences_manager.h b/internal/platform/implementation/preferences_manager.h index bcba90f6..42944eea 100644 --- a/internal/platform/implementation/preferences_manager.h +++ b/internal/platform/implementation/preferences_manager.h @@ -36,7 +36,7 @@ namespace api { // repository, we use json as the parser for now. class PreferencesManager { public: - explicit PreferencesManager(absl::string_view path) {} + PreferencesManager() = default; virtual ~PreferencesManager() = default; // Sets values diff --git a/internal/platform/implementation/windows/platform.cc b/internal/platform/implementation/windows/platform.cc index 6bdbc19e..26524149 100644 --- a/internal/platform/implementation/windows/platform.cc +++ b/internal/platform/implementation/windows/platform.cc @@ -323,7 +323,7 @@ std::unique_ptr ImplementationPlatform::CreateDeviceInfo() { std::unique_ptr ImplementationPlatform::CreatePreferencesManager(absl::string_view path) { - return std::make_unique(path); + return std::make_unique(FilePath{path}); } } // namespace api diff --git a/internal/platform/implementation/windows/preferences_manager.cc b/internal/platform/implementation/windows/preferences_manager.cc index b1e4b265..00488997 100644 --- a/internal/platform/implementation/windows/preferences_manager.cc +++ b/internal/platform/implementation/windows/preferences_manager.cc @@ -31,7 +31,6 @@ #include "internal/base/files.h" #include "internal/base/file_path.h" #include "internal/platform/implementation/platform.h" -#include "internal/platform/implementation/preferences_manager.h" #include "internal/platform/implementation/windows/preferences_repository.h" #include "internal/platform/logging.h" @@ -40,8 +39,7 @@ namespace { using json = ::nlohmann::json; } // namespace -PreferencesManager::PreferencesManager(absl::string_view file_path) - : api::PreferencesManager(file_path) { +PreferencesManager::PreferencesManager(FilePath file_path) { std::optional path = nearby::api::ImplementationPlatform::CreateDeviceInfo() ->GetLocalAppDataPath(); @@ -49,7 +47,7 @@ PreferencesManager::PreferencesManager(absl::string_view file_path) path = Files::GetTemporaryDirectory(); } - path->append(FilePath(file_path)); + path->append(file_path); preferences_repository_ = std::make_unique(path->ToString()); value_ = preferences_repository_->LoadPreferences(); diff --git a/internal/platform/implementation/windows/preferences_manager.h b/internal/platform/implementation/windows/preferences_manager.h index a03803b9..25083502 100644 --- a/internal/platform/implementation/windows/preferences_manager.h +++ b/internal/platform/implementation/windows/preferences_manager.h @@ -28,6 +28,7 @@ #include "absl/types/span.h" #include "nlohmann/json.hpp" #include "nlohmann/json_fwd.hpp" +#include "internal/base/file_path.h" #include "internal/platform/implementation/preferences_manager.h" #include "internal/platform/implementation/windows/preferences_repository.h" @@ -40,7 +41,8 @@ namespace windows { // change by the observer. class PreferencesManager : public api::PreferencesManager { public: - explicit PreferencesManager(absl::string_view path); + // `path` is relative to the user's local app data directory. + explicit PreferencesManager(nearby::FilePath path); // Sets values diff --git a/internal/platform/implementation/windows/preferences_manager_test.cc b/internal/platform/implementation/windows/preferences_manager_test.cc index 1766f548..fc9bb9f4 100644 --- a/internal/platform/implementation/windows/preferences_manager_test.cc +++ b/internal/platform/implementation/windows/preferences_manager_test.cc @@ -16,10 +16,7 @@ #include -#include -#include // NOLINT(build/c++17) #include -#include #include #include #include @@ -31,40 +28,45 @@ #include "absl/types/span.h" #include "nlohmann/json.hpp" #include "nlohmann/json_fwd.hpp" +#include "internal/base/file_path.h" +#include "internal/base/files.h" #include "internal/platform/logging.h" namespace nearby { namespace windows { namespace { using json = ::nlohmann::json; -constexpr absl::Duration kTimeOut = absl::Milliseconds(200); -constexpr char kPreferencesFilePath[] = "Google/Nearby/Sharing"; +constexpr absl::string_view kPreferencesFilePath = "Google/Nearby/Sharing"; } // namespace TEST(PreferencesManager, CorruptedConfigFile) { - std::filesystem::path settingsPath = std::filesystem::temp_directory_path(); - std::ofstream output_stream{settingsPath / "preferences.json"}; + FilePath settingsPath = Files::GetTemporaryDirectory(); + FilePath preferencesPath = settingsPath; + preferencesPath.append(FilePath("preferences.json")); + std::ofstream output_stream{preferencesPath.GetPath()}; output_stream << "CORRUPTED" << std::endl; - LOG(INFO) << "Loading preferences from: " << settingsPath.string(); - EXPECT_EQ(PreferencesManager(settingsPath.string()).GetInteger("data", 100), + LOG(INFO) << "Loading preferences from: " << settingsPath.ToString(); + EXPECT_EQ(PreferencesManager(settingsPath).GetInteger("data", 100), 100); } TEST(PreferencesManager, ValidConfigFile) { - std::filesystem::path settingsPath = std::filesystem::temp_directory_path(); - std::ofstream output_stream{settingsPath / "preferences.json"}; + FilePath settingsPath = Files::GetTemporaryDirectory(); + FilePath preferencesPath = settingsPath; + preferencesPath.append(FilePath("preferences.json")); + std::ofstream output_stream{preferencesPath.GetPath()}; output_stream << "{\"data\":8, \"name\": \"Valid\"}" << std::endl; output_stream.close(); - LOG(INFO) << "Loading preferences from: " << settingsPath.string(); - EXPECT_EQ(PreferencesManager(settingsPath.string()).GetInteger("data", 100), + LOG(INFO) << "Loading preferences from: " << settingsPath.ToString(); + EXPECT_EQ(PreferencesManager(settingsPath).GetInteger("data", 100), 8); } TEST(PreferencesManager, SetAndGetBoolean) { std::string bool_key = "bool_key"; - PreferencesManager pm(kPreferencesFilePath); + PreferencesManager pm(FilePath{kPreferencesFilePath}); EXPECT_TRUE(pm.GetBoolean(bool_key, true)); pm.SetBoolean(bool_key, true); EXPECT_TRUE(pm.GetBoolean(bool_key, false)); @@ -72,7 +74,7 @@ TEST(PreferencesManager, SetAndGetBoolean) { TEST(PreferencesManager, SetAndGetInt) { std::string int_key = "int_key"; - PreferencesManager pm(kPreferencesFilePath); + PreferencesManager pm(FilePath{kPreferencesFilePath}); EXPECT_EQ(pm.GetInteger(int_key, 1234), 1234); pm.SetInteger(int_key, 6789); EXPECT_EQ(pm.GetInteger(int_key, 0), 6789); @@ -80,7 +82,7 @@ TEST(PreferencesManager, SetAndGetInt) { TEST(PreferencesManager, SetAndGetInt64) { std::string int64_key = "int64_key"; - PreferencesManager pm(kPreferencesFilePath); + PreferencesManager pm(FilePath{kPreferencesFilePath}); EXPECT_EQ(pm.GetInt64(int64_key, 1234), 1234); pm.SetInt64(int64_key, 56789); EXPECT_EQ(pm.GetInt64(int64_key, 0), 56789); @@ -88,7 +90,7 @@ TEST(PreferencesManager, SetAndGetInt64) { TEST(PreferencesManager, SetAndGetString) { std::string string_key = "string_key"; - PreferencesManager pm(kPreferencesFilePath); + PreferencesManager pm(FilePath{kPreferencesFilePath}); EXPECT_EQ(pm.GetString(string_key, "abcd"), "abcd"); pm.SetString(string_key, "this is a test string"); EXPECT_EQ(pm.GetString(string_key, ""), "this is a test string"); @@ -96,7 +98,7 @@ TEST(PreferencesManager, SetAndGetString) { TEST(PreferencesManager, SetAndGetTime) { std::string time_key = "time_key"; - PreferencesManager pm(kPreferencesFilePath); + PreferencesManager pm(FilePath{kPreferencesFilePath}); absl::Time time = absl::Now(); EXPECT_EQ(pm.GetTime(time_key, time), time); pm.SetTime(time_key, time); @@ -106,7 +108,7 @@ TEST(PreferencesManager, SetAndGetTime) { TEST(PreferencesManager, MultipleSetAndGetString) { std::string string1_key = "string1_key"; - PreferencesManager pm(kPreferencesFilePath); + PreferencesManager pm(FilePath{kPreferencesFilePath}); pm.SetString(string1_key, "this is first string"); pm.SetString(string1_key, "this is second string"); EXPECT_EQ(pm.GetString(string1_key, ""), "this is second string"); @@ -114,7 +116,7 @@ TEST(PreferencesManager, MultipleSetAndGetString) { TEST(PreferencesManager, SetAndGetValue) { std::string value_key = "value_key"; - PreferencesManager pm(kPreferencesFilePath); + PreferencesManager pm(FilePath{kPreferencesFilePath}); json value = {{"key1", "value1"}, {"key2", "value2"}}; EXPECT_TRUE(pm.Get(value_key, json()).empty()); pm.Set(value_key, value); @@ -126,7 +128,7 @@ TEST(PreferencesManager, SetAndGetValue) { TEST(PreferencesManager, SetAndGetBooleanArray) { std::string bool_array_key = "bool_array_key"; - auto pm = PreferencesManager(kPreferencesFilePath); + auto pm = PreferencesManager(FilePath{kPreferencesFilePath}); auto default_result = pm.GetBooleanArray(bool_array_key, absl::Span({true})); EXPECT_EQ(default_result[0], true); @@ -140,7 +142,7 @@ TEST(PreferencesManager, SetAndGetBooleanArray) { TEST(PreferencesManager, SetAndGetIntArray) { std::string int_array_key = "int_array_key"; - auto pm = PreferencesManager(kPreferencesFilePath); + auto pm = PreferencesManager(FilePath{kPreferencesFilePath}); auto result = pm.GetIntegerArray(int_array_key, std::vector{5, 6}); EXPECT_EQ(result[1], 6); pm.SetIntegerArray(int_array_key, std::vector{1, 7, 4, 10, 12}); @@ -150,7 +152,7 @@ TEST(PreferencesManager, SetAndGetIntArray) { TEST(PreferencesManager, SetAndGetInt64Array) { std::string int64_array_key = "int64_array_key"; - auto pm = PreferencesManager(kPreferencesFilePath); + auto pm = PreferencesManager(FilePath{kPreferencesFilePath}); auto result = pm.GetInt64Array(int64_array_key, std::vector{99}); EXPECT_EQ(result[0], 99); pm.SetInt64Array(int64_array_key, std::vector{16, 7, 64, 100, 12}); @@ -161,7 +163,7 @@ TEST(PreferencesManager, SetAndGetInt64Array) { TEST(PreferencesManager, SetAndGetStringArray) { std::string string_array_key = "string_array_key"; - auto pm = PreferencesManager(kPreferencesFilePath); + auto pm = PreferencesManager(FilePath{kPreferencesFilePath}); auto result = pm.GetStringArray(string_array_key, std::vector{"value", "morning"}); EXPECT_EQ(result[1], "morning"); @@ -175,7 +177,7 @@ TEST(PreferencesManager, SetAndGetStringArray) { TEST(PreferencesManager, RemoveKey) { std::string string_key = "string_key"; - auto pm = PreferencesManager(kPreferencesFilePath); + auto pm = PreferencesManager(FilePath{kPreferencesFilePath}); pm.SetString(string_key, "remove key"); pm.Remove(string_key); auto result = pm.GetString(string_key, "default key"); diff --git a/internal/platform/implementation/windows/preferences_repository_test.cc b/internal/platform/implementation/windows/preferences_repository_test.cc index 1533bdea..4e375d03 100644 --- a/internal/platform/implementation/windows/preferences_repository_test.cc +++ b/internal/platform/implementation/windows/preferences_repository_test.cc @@ -14,7 +14,6 @@ #include "internal/platform/implementation/windows/preferences_repository.h" -#include // NOLINT(build/c++17) #include #include @@ -22,6 +21,7 @@ #include "nlohmann/json.hpp" #include "nlohmann/json_fwd.hpp" #include "internal/base/file_path.h" +#include "internal/base/files.h" #include "internal/platform/implementation/device_info.h" #include "internal/platform/implementation/platform.h" @@ -48,8 +48,8 @@ TEST(PreferencesRepository, RecoverFromBadPreferences) { FilePath full_path = app_data_path->append(FilePath(kPreferencesPath)); FilePath full_name = app_data_path->append(FilePath(kPreferencesFileName)); - if (std::filesystem::exists(full_name.GetPath())) { - std::filesystem::remove(full_name.GetPath()); + if (Files::FileExists(full_name)) { + Files::RemoveFile(full_name); } std::ofstream pref_file(full_name.GetPath()); @@ -67,8 +67,8 @@ TEST(PreferencesRepository, SaveAndLoadPreferences) { FilePath full_path = app_data_path->append(FilePath(kPreferencesPath)); FilePath full_name = app_data_path->append(FilePath(kPreferencesFileName)); - if (std::filesystem::exists(full_name.GetPath())) { - std::filesystem::remove(full_name.GetPath()); + if (Files::FileExists(full_name)) { + Files::RemoveFile(full_name); } PreferencesRepository preferences_repository{full_path.ToString()}; @@ -80,7 +80,7 @@ TEST(PreferencesRepository, SaveAndLoadPreferences) { EXPECT_EQ(result.size(), 2); EXPECT_EQ(result["key1"], "value1"); EXPECT_EQ(result["key2"], "value2"); - std::filesystem::remove(full_name.GetPath()); + Files::RemoveFile(full_name); } TEST(PreferencesRepository, LoadFromBackup) { @@ -92,12 +92,12 @@ TEST(PreferencesRepository, LoadFromBackup) { FilePath full_name_backup = full_path; full_name_backup.append(FilePath(kPreferencesBackupFileName)); - if (std::filesystem::exists(full_name.GetPath())) { - std::filesystem::remove(full_name.GetPath()); + if (Files::FileExists(full_name)) { + Files::RemoveFile(full_name); } - if (std::filesystem::exists(full_name_backup.GetPath())) { - std::filesystem::remove(full_name_backup.GetPath()); + if (Files::FileExists(full_name_backup)) { + Files::RemoveFile(full_name_backup); } PreferencesRepository preferences_repository{full_path.ToString()}; @@ -116,8 +116,8 @@ TEST(PreferencesRepository, LoadFromBackup) { EXPECT_TRUE(result.has_value()); EXPECT_EQ(result.value()["key1"], "value1"); EXPECT_EQ(result.value()["key2"], "value2"); - std::filesystem::remove(full_name.GetPath()); - EXPECT_FALSE(std::filesystem::exists(full_name_backup.GetPath())); + Files::RemoveFile(full_name); + EXPECT_FALSE(Files::FileExists(full_name_backup)); } TEST(PreferencesRepository, RecoverFromCorruption) { @@ -129,12 +129,12 @@ TEST(PreferencesRepository, RecoverFromCorruption) { FilePath full_name_backup = full_path; full_name_backup.append(FilePath(kPreferencesBackupFileName)); - if (std::filesystem::exists(full_name.GetPath())) { - std::filesystem::remove(full_name.GetPath()); + if (Files::FileExists(full_name)) { + Files::RemoveFile(full_name); } - if (std::filesystem::exists(full_name_backup.GetPath())) { - std::filesystem::remove(full_name_backup.GetPath()); + if (Files::FileExists(full_name_backup)) { + Files::RemoveFile(full_name_backup); } PreferencesRepository preferences_repository{full_path.ToString()}; @@ -153,8 +153,8 @@ TEST(PreferencesRepository, RecoverFromCorruption) { std::optional result = preferences_repository.LoadPreferences(); EXPECT_EQ(result.value()["key1"], "value1"); EXPECT_EQ(result.value()["key2"], "value2"); - std::filesystem::remove(full_name.GetPath()); - EXPECT_FALSE(std::filesystem::exists(full_name_backup.GetPath())); + Files::RemoveFile(full_name); + EXPECT_FALSE(Files::FileExists(full_name_backup)); } } // namespace