mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Remove std::filesystem from third_party/nearby.
PiperOrigin-RevId: 764953786
This commit is contained in:
committed by
Copybara-Service
parent
09cdc14fb6
commit
703c0fd2b5
@@ -15,7 +15,6 @@
|
||||
#include "internal/base/files.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <filesystem> // NOLINT
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
#include <optional>
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -16,11 +16,8 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <filesystem> // NOLINT(build/c++17)
|
||||
#include <ios>
|
||||
#include <memory>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -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<uint64_t> 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 <typename T>
|
||||
std::unique_ptr<LeveldbDataSet<T>> CreateDataSet(
|
||||
const std::filesystem::path& path) {
|
||||
return std::make_unique<LeveldbDataSet<T>>(path.string());
|
||||
const FilePath& path) {
|
||||
return std::make_unique<LeveldbDataSet<T>>(path.ToString());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@@ -129,13 +128,13 @@ absl::flat_hash_map<std::string, T> LoadEntriesWithKeysAndWait(
|
||||
|
||||
template <typename T>
|
||||
void WipeCleanAndWait(std::unique_ptr<LeveldbDataSet<T>>& 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<LeveldbDataSet<DiceRoll>> diceroll_set =
|
||||
CreateDataSet<DiceRoll>(path);
|
||||
|
||||
@@ -175,7 +174,7 @@ TEST(LeveldbDataSet, UpdateEntriesDiceRoll) {
|
||||
}
|
||||
|
||||
TEST(LeveldbDataSet, LoadEntriesDiceRoll) {
|
||||
std::filesystem::path path = GenerateLeveldbPath();
|
||||
FilePath path = GenerateLeveldbPath();
|
||||
std::unique_ptr<LeveldbDataSet<DiceRoll>> diceroll_set =
|
||||
CreateDataSet<DiceRoll>(path);
|
||||
|
||||
@@ -210,7 +209,7 @@ TEST(LeveldbDataSet, LoadEntriesDiceRoll) {
|
||||
}
|
||||
|
||||
TEST(LeveldbDataSet, LoadEntrysDiceRoll) {
|
||||
std::filesystem::path path = GenerateLeveldbPath();
|
||||
FilePath path = GenerateLeveldbPath();
|
||||
std::unique_ptr<LeveldbDataSet<DiceRoll>> diceroll_set =
|
||||
CreateDataSet<DiceRoll>(path);
|
||||
|
||||
@@ -240,7 +239,7 @@ TEST(LeveldbDataSet, LoadEntrysDiceRoll) {
|
||||
}
|
||||
|
||||
TEST(LeveldbDataSet, RemoveEntriesDiceRoll) {
|
||||
std::filesystem::path path = GenerateLeveldbPath();
|
||||
FilePath path = GenerateLeveldbPath();
|
||||
std::unique_ptr<LeveldbDataSet<DiceRoll>> diceroll_set =
|
||||
CreateDataSet<DiceRoll>(path);
|
||||
|
||||
|
||||
@@ -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())
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <filesystem> // NOLINT
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
@@ -269,7 +268,7 @@ ImplementationPlatform::CreateDeviceInfo() {
|
||||
|
||||
std::unique_ptr<nearby::api::PreferencesManager>
|
||||
ImplementationPlatform::CreatePreferencesManager(absl::string_view path) {
|
||||
return std::make_unique<g3::PreferencesManager>(path);
|
||||
return std::make_unique<g3::PreferencesManager>();
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
||||
@@ -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<DeviceInfoImpl>();
|
||||
FilePath path = device_info->GetAppDataPath();
|
||||
|
||||
path.append(FilePath(file_path));
|
||||
preferences_repository_ =
|
||||
std::make_unique<PreferencesRepository>(path.ToString());
|
||||
preferences_repository_ = std::make_unique<PreferencesRepository>();
|
||||
value_ = preferences_repository_->LoadPreferences();
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -14,12 +14,13 @@
|
||||
|
||||
#include "internal/platform/implementation/g3/preferences_repository.h"
|
||||
|
||||
#include <filesystem> // NOLINT(build/c++17)
|
||||
#include <fstream>
|
||||
|
||||
#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_;
|
||||
}
|
||||
|
||||
@@ -15,20 +15,16 @@
|
||||
#ifndef PLATFORM_IMPLEMENTATION_G3_PREFERENCES_REPOSITORY_H_
|
||||
#define PLATFORM_IMPLEMENTATION_G3_PREFERENCES_REPOSITORY_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#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_
|
||||
|
||||
@@ -14,23 +14,19 @@
|
||||
|
||||
#include "internal/platform/implementation/g3/preferences_repository.h"
|
||||
|
||||
#include <filesystem> // NOLINT(build/c++17)
|
||||
#include <string>
|
||||
|
||||
#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<int>(), 456);
|
||||
}
|
||||
|
||||
} // namespace g3
|
||||
} // namespace platform
|
||||
} // namespace nearby
|
||||
} // namespace nearby::g3
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -323,7 +323,7 @@ std::unique_ptr<DeviceInfo> ImplementationPlatform::CreateDeviceInfo() {
|
||||
|
||||
std::unique_ptr<nearby::api::PreferencesManager>
|
||||
ImplementationPlatform::CreatePreferencesManager(absl::string_view path) {
|
||||
return std::make_unique<windows::PreferencesManager>(path);
|
||||
return std::make_unique<windows::PreferencesManager>(FilePath{path});
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
||||
@@ -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<FilePath> 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<PreferencesRepository>(path->ToString());
|
||||
value_ = preferences_repository_->LoadPreferences();
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -16,10 +16,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <codecvt>
|
||||
#include <filesystem> // NOLINT(build/c++17)
|
||||
#include <fstream>
|
||||
#include <locale>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -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<const bool>({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<int>{5, 6});
|
||||
EXPECT_EQ(result[1], 6);
|
||||
pm.SetIntegerArray(int_array_key, std::vector<int>{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<int64_t>{99});
|
||||
EXPECT_EQ(result[0], 99);
|
||||
pm.SetInt64Array(int64_array_key, std::vector<int64_t>{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<std::string>{"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");
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
|
||||
#include "internal/platform/implementation/windows/preferences_repository.h"
|
||||
|
||||
#include <filesystem> // NOLINT(build/c++17)
|
||||
#include <fstream>
|
||||
#include <optional>
|
||||
|
||||
@@ -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<json> 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
|
||||
|
||||
Reference in New Issue
Block a user