diff --git a/libdnf5/system/state.cpp b/libdnf5/system/state.cpp index f5a08d17e..7cff97e0b 100644 --- a/libdnf5/system/state.cpp +++ b/libdnf5/system/state.cpp @@ -46,10 +46,17 @@ struct from { template <> struct into { +#ifdef TOML11_COMPAT static toml::value into_toml(const libdnf5::system::PackageState & pkg_state) { toml::value res; - res["reason"] = pkg_state.reason; +#else + template + static toml::basic_value into_toml(const libdnf5::system::PackageState & pkg_state) { + toml::basic_value res; + res["reason"] = pkg_state.reason; + res.as_table_fmt().fmt = toml::table_format::oneline; +#endif // #ifdef TOML11_COMPAT return res; } @@ -72,10 +79,17 @@ struct from { template <> struct into { +#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 + static toml::basic_value into_toml(const libdnf5::system::NevraState & nevra_state) { + toml::basic_value res; + res["from_repo"] = nevra_state.from_repo; + res.as_table_fmt().fmt = toml::table_format::oneline; +#endif // #ifdef TOML11_COMPAT return res; } @@ -92,8 +106,16 @@ struct from { template <> struct into { +#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 + static toml::basic_value into_toml(const libdnf5::comps::PackageType & package_types) { + toml::basic_value res = libdnf5::comps::package_types_to_strings(package_types); + res.as_array_fmt().fmt = toml::array_format::oneline; + return res; +#endif // #ifdef TOML11_COMPAT } }; @@ -117,12 +139,20 @@ struct from { template <> struct into { +#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 + static toml::basic_value into_toml(const libdnf5::system::GroupState & group_state) { + toml::basic_value res; res["package_types"] = group_state.package_types; res["packages"] = group_state.packages; + res["userinstalled"] = group_state.userinstalled; +#endif // #ifdef TOML11_COMPAT return res; } @@ -145,10 +175,17 @@ struct from { template <> struct into { +#ifdef TOML11_COMPAT static toml::value into_toml(const libdnf5::system::EnvironmentState & environment_state) { toml::value res; - res["groups"] = environment_state.groups; +#else + template + static toml::basic_value into_toml(const libdnf5::system::EnvironmentState & environment_state) { + toml::basic_value res; + res["groups"] = environment_state.groups; + res.as_table_fmt().fmt = toml::table_format::oneline; +#endif // #ifdef TOML11_COMPAT return res; } @@ -172,12 +209,20 @@ struct from { template <> struct into { +#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 + static toml::basic_value into_toml(const libdnf5::system::ModuleState & module_state) { + toml::basic_value 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; } @@ -199,10 +244,17 @@ struct from { template <> struct into { +#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 + static toml::basic_value into_toml(const libdnf5::system::SystemState & system_state) { + toml::basic_value res; + res["rpmdb_cookie"] = system_state.rpmdb_cookie; + res.as_table_fmt().fmt = toml::table_format::oneline; +#endif // #ifdef TOML11_COMPAT return res; } @@ -499,15 +551,27 @@ void State::set_rpmdb_cookie(const std::string & cookie) { } +#ifdef TOML11_COMPAT template 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(value); } +#else +template +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() { diff --git a/test/libdnf5/system/test_state.cpp b/test/libdnf5/system/test_state.cpp index 67494f3fd..2995e7a7d 100644 --- a/test/libdnf5/system/test_state.cpp +++ b/test/libdnf5/system/test_state.cpp @@ -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; } @@ -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"); @@ -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] @@ -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" @@ -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 }