Skip to content

Commit 0ae5939

Browse files
committed
Merge pull request #167 from tetengo/ensure_appdata_dir_created
Ensure appdata dir created (cherry picked from commit a033d25)
1 parent 078f3fb commit 0ae5939

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

library/property/cpp/src/tetengo.property.file_storage.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ namespace tetengo::property
4040
{
4141
const auto native_path =
4242
tetengo::platform_dependent::property_set_file_path::instance().to_native_path(m_path);
43+
if (!std::filesystem::exists(native_path.parent_path()))
44+
{
45+
std::filesystem::create_directories(native_path.parent_path());
46+
}
4347
std::ofstream stream{ native_path };
4448
if (!stream)
4549
{

library/property/test/src/test_tetengo.property.file_storage.cpp

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@ namespace
108108
class test_file : private boost::noncopyable
109109
{
110110
public:
111+
explicit test_file(const std::filesystem::path& path) : m_path{ path }
112+
{
113+
{
114+
const auto native_top_path =
115+
tetengo::platform_dependent::property_set_file_path::instance().to_native_top_path(m_path);
116+
std::error_code ec{};
117+
std::filesystem::remove_all(native_top_path, ec);
118+
}
119+
{
120+
const auto native_path =
121+
tetengo::platform_dependent::property_set_file_path::instance().to_native_path(m_path);
122+
std::error_code ec{};
123+
std::filesystem::remove_all(native_path, ec);
124+
}
125+
}
126+
111127
test_file(const std::filesystem::path& path, const std::string_view& content) : m_path{ path }
112128
{
113129
const auto native_path =
@@ -118,10 +134,18 @@ namespace
118134

119135
~test_file()
120136
{
121-
const auto native_top_path =
122-
tetengo::platform_dependent::property_set_file_path::instance().to_native_top_path(m_path);
123-
std::error_code ec{};
124-
std::filesystem::remove_all(native_top_path, ec);
137+
{
138+
const auto native_top_path =
139+
tetengo::platform_dependent::property_set_file_path::instance().to_native_top_path(m_path);
140+
std::error_code ec{};
141+
std::filesystem::remove_all(native_top_path, ec);
142+
}
143+
{
144+
const auto native_path =
145+
tetengo::platform_dependent::property_set_file_path::instance().to_native_path(m_path);
146+
std::error_code ec{};
147+
std::filesystem::remove_all(native_path, ec);
148+
}
125149
}
126150

127151
const std::filesystem::path& path() const
@@ -179,6 +203,20 @@ BOOST_AUTO_TEST_CASE(save)
179203
{
180204
BOOST_TEST_PASSPOINT();
181205

206+
{
207+
const test_file file{ generic_path() };
208+
const tetengo::property::file_storage_loader loader{};
209+
const auto p_storage = loader.load(file.path());
210+
BOOST_REQUIRE(p_storage);
211+
212+
p_storage->set_bool("alpha", true);
213+
p_storage->set_uint32("bravo", static_cast<std::uint32_t>(42));
214+
p_storage->set_string((std::filesystem::path{ "charlie" } / "delta").string(), std::string{ "echo" });
215+
216+
p_storage->save();
217+
218+
BOOST_TEST(file_content_equal_to(generic_path(), json1));
219+
}
182220
{
183221
const test_file file{ generic_path(), "" };
184222
const tetengo::property::file_storage_loader loader{};
@@ -206,6 +244,32 @@ BOOST_AUTO_TEST_CASE(save)
206244
BOOST_TEST(file_content_equal_to(generic_path(), json2));
207245
}
208246

247+
{
248+
const test_file file{ generic_path() };
249+
const auto* const p_loader = tetengo_property_storageLoader_createFileStorageLoader();
250+
BOOST_SCOPE_EXIT(p_loader)
251+
{
252+
tetengo_property_storageLoader_destroy(p_loader);
253+
}
254+
BOOST_SCOPE_EXIT_END;
255+
256+
auto* const p_storage = tetengo_property_storageLoader_load(p_loader, file.path().string().c_str());
257+
BOOST_SCOPE_EXIT(p_storage)
258+
{
259+
tetengo_property_storage_destroy(p_storage);
260+
}
261+
BOOST_SCOPE_EXIT_END;
262+
BOOST_TEST_REQUIRE(p_storage);
263+
264+
tetengo_property_storage_setBool(p_storage, "alpha", true);
265+
tetengo_property_storage_setUint32(p_storage, "bravo", static_cast<std::uint32_t>(42));
266+
tetengo_property_storage_setString(
267+
p_storage, (std::filesystem::path{ "charlie" } / "delta").string().c_str(), "echo");
268+
269+
tetengo_property_storage_save(p_storage);
270+
271+
BOOST_TEST(file_content_equal_to(generic_path(), json1));
272+
}
209273
{
210274
const test_file file{ generic_path(), "" };
211275
const auto* const p_loader = tetengo_property_storageLoader_createFileStorageLoader();
@@ -285,6 +349,13 @@ BOOST_AUTO_TEST_CASE(load)
285349
{
286350
BOOST_TEST_PASSPOINT();
287351

352+
{
353+
const test_file file{ generic_path() };
354+
const tetengo::property::file_storage_loader loader{};
355+
const auto p_storage = loader.load(file.path());
356+
357+
BOOST_CHECK(p_storage);
358+
}
288359
{
289360
const test_file file{ generic_path(), json3_with_comment };
290361
const tetengo::property::file_storage_loader loader{};

0 commit comments

Comments
 (0)