Skip to content

Commit

Permalink
state.cpp: update to new toml11-devel-4.0.0 version API
Browse files Browse the repository at this point in the history
Adds TC - `type_config` which is required for conversion.
https://toruniina.github.io/toml11/docs/features/value/#converting-to-tomlvalue

The `toml::format` was changed and no longer accepts template args:
https://toruniina.github.io/toml11/docs/reference/serializer/

Use `ordered_value` to preserve order, otherwise `std::unordered_map` is
used internally which may result in different order on each run which
may casue failing tests.

It was necessary to specify the formating for some values because the
tests rely on it. However even with the same formatting there still are
changes (spacing is different), update the tests to pass for both toml11
versions.
  • Loading branch information
kontura authored and jan-kolarik committed Aug 20, 2024
1 parent 7b2a6e5 commit 1746eab
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 19 deletions.
78 changes: 71 additions & 7 deletions libdnf5/system/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,17 @@ struct from<libdnf5::system::PackageState> {

template <>
struct into<libdnf5::system::PackageState> {
#ifdef TOML11_COMPAT
static toml::value into_toml(const libdnf5::system::PackageState & pkg_state) {
toml::value res;

res["reason"] = pkg_state.reason;
#else
template <typename TC>
static toml::basic_value<TC> into_toml(const libdnf5::system::PackageState & pkg_state) {
toml::basic_value<TC> res;
res["reason"] = pkg_state.reason;
res.as_table_fmt().fmt = toml::table_format::oneline;
#endif // #ifdef TOML11_COMPAT

return res;
}
Expand All @@ -72,10 +79,17 @@ struct from<libdnf5::system::NevraState> {

template <>
struct into<libdnf5::system::NevraState> {
#ifdef TOML11_COMPAT
static toml::value into_toml(const libdnf5::system::NevraState & nevra_state) {
toml::value res;

res["from_repo"] = nevra_state.from_repo;
#else
template <typename TC>
static toml::basic_value<TC> into_toml(const libdnf5::system::NevraState & nevra_state) {
toml::basic_value<TC> res;
res["from_repo"] = nevra_state.from_repo;
res.as_table_fmt().fmt = toml::table_format::oneline;
#endif // #ifdef TOML11_COMPAT

return res;
}
Expand All @@ -92,8 +106,16 @@ struct from<libdnf5::comps::PackageType> {

template <>
struct into<libdnf5::comps::PackageType> {
#ifdef TOML11_COMPAT
static toml::value into_toml(const libdnf5::comps::PackageType & package_types) {
return libdnf5::comps::package_types_to_strings(package_types);
#else
template <typename TC>
static toml::basic_value<TC> into_toml(const libdnf5::comps::PackageType & package_types) {
toml::basic_value<TC> res = libdnf5::comps::package_types_to_strings(package_types);
res.as_array_fmt().fmt = toml::array_format::oneline;
return res;
#endif // #ifdef TOML11_COMPAT
}
};

Expand All @@ -117,12 +139,20 @@ struct from<libdnf5::system::GroupState> {

template <>
struct into<libdnf5::system::GroupState> {
#ifdef TOML11_COMPAT
static toml::value into_toml(const libdnf5::system::GroupState & group_state) {
toml::value res;

res["package_types"] = group_state.package_types;
res["packages"] = group_state.packages;
res["userinstalled"] = group_state.userinstalled;
#else
template <typename TC>
static toml::basic_value<TC> into_toml(const libdnf5::system::GroupState & group_state) {
toml::basic_value<TC> res;
res["package_types"] = group_state.package_types;
res["packages"] = group_state.packages;
res["userinstalled"] = group_state.userinstalled;
#endif // #ifdef TOML11_COMPAT

return res;
}
Expand All @@ -145,10 +175,17 @@ struct from<libdnf5::system::EnvironmentState> {

template <>
struct into<libdnf5::system::EnvironmentState> {
#ifdef TOML11_COMPAT
static toml::value into_toml(const libdnf5::system::EnvironmentState & environment_state) {
toml::value res;

res["groups"] = environment_state.groups;
#else
template <typename TC>
static toml::basic_value<TC> into_toml(const libdnf5::system::EnvironmentState & environment_state) {
toml::basic_value<TC> res;
res["groups"] = environment_state.groups;
res.as_table_fmt().fmt = toml::table_format::oneline;
#endif // #ifdef TOML11_COMPAT

return res;
}
Expand All @@ -172,12 +209,20 @@ struct from<libdnf5::system::ModuleState> {

template <>
struct into<libdnf5::system::ModuleState> {
#ifdef TOML11_COMPAT
static toml::value into_toml(const libdnf5::system::ModuleState & module_state) {
toml::value res;

res["enabled_stream"] = module_state.enabled_stream;
res["state"] = libdnf5::module::module_status_to_string(module_state.status);
res["installed_profiles"] = module_state.installed_profiles;
#else
template <typename TC>
static toml::basic_value<TC> into_toml(const libdnf5::system::ModuleState & module_state) {
toml::basic_value<TC> res;
res["enabled_stream"] = module_state.enabled_stream;
res["installed_profiles"] = module_state.installed_profiles;
res["state"] = libdnf5::module::module_status_to_string(module_state.status);
#endif // #ifdef TOML11_COMPAT

return res;
}
Expand All @@ -199,10 +244,17 @@ struct from<libdnf5::system::SystemState> {

template <>
struct into<libdnf5::system::SystemState> {
#ifdef TOML11_COMPAT
static toml::value into_toml(const libdnf5::system::SystemState & system_state) {
toml::value res;

res["rpmdb_cookie"] = system_state.rpmdb_cookie;
#else
template <typename TC>
static toml::basic_value<TC> into_toml(const libdnf5::system::SystemState & system_state) {
toml::basic_value<TC> res;
res["rpmdb_cookie"] = system_state.rpmdb_cookie;
res.as_table_fmt().fmt = toml::table_format::oneline;
#endif // #ifdef TOML11_COMPAT

return res;
}
Expand Down Expand Up @@ -499,15 +551,27 @@ void State::set_rpmdb_cookie(const std::string & cookie) {
}


#ifdef TOML11_COMPAT
template <typename T>
static toml::value make_top_value(const std::string & key, const T & value) {
return toml::value({{key, value}, {"version", make_version()}});
return toml::value({{key, value}, { "version", make_version() }});
}


static std::string toml_format(const toml::value & value) {
return toml::format<toml::discard_comments, std::map, std::vector>(value);
}
#else
template <typename T>
static toml::ordered_value make_top_value(const std::string & key, const T & value) {
return toml::ordered_value({{"version", make_version()}, {key, toml::ordered_value(value)}});
}


static std::string toml_format(const toml::ordered_value & value) {
return toml::format(value);
}
#endif // #ifdef TOML11_COMPAT


void State::save() {
Expand Down
44 changes: 32 additions & 12 deletions test/libdnf5/system/test_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@ system = {rpmdb_cookie="foo"}
)"""};


// workaround, because of different behavior before toml 3.7.1
static std::string trim(std::string str) {
// workaround, because of different behavior before toml 3.7.1
str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](char c) { return c != '\n'; }));
str.erase(
std::unique(str.begin(), str.end(), [](char c1, char c2) { return c1 == '\n' && c2 == '\n'; }), str.end());

// workaround, because of different behavior before toml 4.0.0
// since 4.0.0 even oneline table format has all elements separated by spaces
str.erase(std::remove_if(str.begin(), str.end(), [](char c) { return c == ' '; }), str.end());
return str;
}

Expand Down Expand Up @@ -182,13 +185,13 @@ void StateTest::test_state_write() {

state.save();

CPPUNIT_ASSERT_EQUAL(packages_contents, trim(libdnf5::utils::fs::File(path / "packages.toml", "r").read()));
CPPUNIT_ASSERT_EQUAL(nevras_contents, trim(libdnf5::utils::fs::File(path / "nevras.toml", "r").read()));
CPPUNIT_ASSERT_EQUAL(groups_contents, trim(libdnf5::utils::fs::File(path / "groups.toml", "r").read()));
CPPUNIT_ASSERT_EQUAL(trim(packages_contents), trim(libdnf5::utils::fs::File(path / "packages.toml", "r").read()));
CPPUNIT_ASSERT_EQUAL(trim(nevras_contents), trim(libdnf5::utils::fs::File(path / "nevras.toml", "r").read()));
CPPUNIT_ASSERT_EQUAL(trim(groups_contents), trim(libdnf5::utils::fs::File(path / "groups.toml", "r").read()));
#ifdef WITH_MODULEMD
CPPUNIT_ASSERT_EQUAL(modules_contents, trim(libdnf5::utils::fs::File(path / "modules.toml", "r").read()));
CPPUNIT_ASSERT_EQUAL(trim(modules_contents), trim(libdnf5::utils::fs::File(path / "modules.toml", "r").read()));
#endif
CPPUNIT_ASSERT_EQUAL(system_contents, trim(libdnf5::utils::fs::File(path / "system.toml", "r").read()));
CPPUNIT_ASSERT_EQUAL(trim(system_contents), trim(libdnf5::utils::fs::File(path / "system.toml", "r").read()));

// Test removes
state.remove_package_na_state("pkg.x86_64");
Expand All @@ -207,7 +210,7 @@ void StateTest::test_state_write() {
)"""};

CPPUNIT_ASSERT_EQUAL(
packages_contents_after_remove, trim(libdnf5::utils::fs::File(path / "packages.toml", "r").read()));
trim(packages_contents_after_remove), trim(libdnf5::utils::fs::File(path / "packages.toml", "r").read()));

const std::string nevras_contents_after_remove{R"""(version = "1.0"
[nevras]
Expand All @@ -216,7 +219,7 @@ void StateTest::test_state_write() {
)"""};

CPPUNIT_ASSERT_EQUAL(
nevras_contents_after_remove, trim(libdnf5::utils::fs::File(path / "nevras.toml", "r").read()));
trim(nevras_contents_after_remove), trim(libdnf5::utils::fs::File(path / "nevras.toml", "r").read()));

const std::string groups_contents_after_remove{
R"""(version = "1.0"
Expand All @@ -228,15 +231,32 @@ userinstalled = false
)"""};

CPPUNIT_ASSERT_EQUAL(
groups_contents_after_remove, trim(libdnf5::utils::fs::File(path / "groups.toml", "r").read()));
trim(groups_contents_after_remove), trim(libdnf5::utils::fs::File(path / "groups.toml", "r").read()));

#ifdef WITH_MODULEMD
const std::string modules_contents_after_remove{R"""(version = "1.0"
// Before toml11-devel-4.0.0 the formatting was different
const std::string modules_contents_after_remove_compat{R"""(version = "1.0"
[modules]
module-2 = {enabled_stream="stream-2",installed_profiles=[],state="Disabled"}
)"""};

CPPUNIT_ASSERT_EQUAL(
modules_contents_after_remove, trim(libdnf5::utils::fs::File(path / "modules.toml", "r").read()));
const std::string modules_contents_after_remove{R"""(version = "1.0"
[modules]
[modules.module-2]
enabled_stream = "stream-2"
installed_profiles = []
state = "Disabled"
)"""};

const auto trimmed_read = trim(libdnf5::utils::fs::File(path / "modules.toml", "r").read());
if (trim(modules_contents_after_remove) != trimmed_read &&
trim(modules_contents_after_remove_compat) != trimmed_read) {
CPPUNIT_FAIL(fmt::format(
"Expected either (with toml11 > 4.0.0): {}\nor: {}\nFound: {}",
modules_contents_after_remove,
modules_contents_after_remove_compat,
trimmed_read));
}

#endif
}

0 comments on commit 1746eab

Please sign in to comment.