From 0efd8b2bbc9d98b64eb4109b51a27003ebd0c3a1 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Sat, 16 Nov 2024 20:12:54 +0100 Subject: [PATCH] test: factor out mtree parser --- test/compat_test.cpp | 27 ++++----------------------- test/test_helpers.cpp | 34 ++++++++++++++++++++++++++++++++++ test/test_helpers.h | 4 ++++ 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/test/compat_test.cpp b/test/compat_test.cpp index cf4540896..5427a4d27 100644 --- a/test/compat_test.cpp +++ b/test/compat_test.cpp @@ -1085,31 +1085,12 @@ void check_compat(logger& lgr, reader::filesystem_v2 const& fs, EXPECT_NO_THROW(ext.extract(fs)); EXPECT_NO_THROW(ext.close()); - std::istringstream iss(oss.str()); - std::string line; - size_t num = 0; ref_entries.erase(""); - while (std::getline(iss, line, '\n')) { - if (line == "#mtree") { - continue; - } - - std::vector parts; - split_to(line, ' ', parts); - auto name = parts.front().substr(2); - parts.erase(parts.begin()); - std::unordered_map kv; - - for (auto const& p : parts) { - auto pos = p.find('='); - if (pos == std::string::npos) { - throw std::runtime_error("unexpected mtree line: " + line); - } - kv[p.substr(0, pos)] = p.substr(pos + 1); - } + auto mtree = test::parse_mtree(oss.str()); - ++num; + for (auto [path, kv] : mtree) { + auto name = path.substr(2); auto ri = ref_entries.find(name); EXPECT_FALSE(ri == ref_entries.end()); @@ -1127,7 +1108,7 @@ void check_compat(logger& lgr, reader::filesystem_v2 const& fs, } } - EXPECT_EQ(ref_entries.size(), num); + EXPECT_EQ(ref_entries.size(), mtree.size()); std::map> testdirs{ {"empty", {"empty/alsoempty"}}, diff --git a/test/test_helpers.cpp b/test/test_helpers.cpp index 4333f74fa..38b47d8b5 100644 --- a/test/test_helpers.cpp +++ b/test/test_helpers.cpp @@ -576,6 +576,40 @@ std::string create_random_string(size_t size, size_t seed) { return create_random_string(size, tmprng); } +std::vector< + std::pair>> +parse_mtree(std::string_view mtree) { + std::vector< + std::pair>> + rv; + std::istringstream iss{std::string{mtree}}; + std::string line; + + while (std::getline(iss, line, '\n')) { + if (line == "#mtree") { + continue; + } + + auto parts = split_to>(line, ' '); + auto path = parts.front(); + parts.erase(parts.begin()); + + std::unordered_map attrs; + + for (auto const& p : parts) { + auto pos = p.find('='); + if (pos == std::string::npos) { + throw std::runtime_error("unexpected mtree line: " + line); + } + attrs[p.substr(0, pos)] = p.substr(pos + 1); + } + + rv.emplace_back(std::move(path), std::move(attrs)); + } + + return rv; +} + bool skip_slow_tests() { static bool skip = getenv_is_enabled("DWARFS_SKIP_SLOW_TESTS"); return skip; diff --git a/test/test_helpers.h b/test/test_helpers.h index 9452e05d1..25c1c5e75 100644 --- a/test/test_helpers.h +++ b/test/test_helpers.h @@ -364,6 +364,10 @@ std::string create_random_string(size_t size, uint8_t min, uint8_t max, std::string create_random_string(size_t size, std::mt19937_64& gen); std::string create_random_string(size_t size, size_t seed = 0); +std::vector< + std::pair>> +parse_mtree(std::string_view mtree); + bool skip_slow_tests(); #define DWARFS_SLOW_TEST() \