Skip to content

Commit

Permalink
test: factor out mtree parser
Browse files Browse the repository at this point in the history
  • Loading branch information
mhx committed Nov 17, 2024
1 parent 2129797 commit 213d2ef
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
27 changes: 4 additions & 23 deletions test/compat_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> parts;
split_to(line, ' ', parts);
auto name = parts.front().substr(2);
parts.erase(parts.begin());
std::unordered_map<std::string, std::string> 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());
Expand All @@ -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<std::string, std::vector<std::string>> testdirs{
{"empty", {"empty/alsoempty"}},
Expand Down
34 changes: 34 additions & 0 deletions test/test_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::unordered_map<std::string, std::string>>>
parse_mtree(std::string_view mtree) {
std::vector<
std::pair<std::string, std::unordered_map<std::string, std::string>>>
rv;
std::istringstream iss{std::string{mtree}};
std::string line;

while (std::getline(iss, line, '\n')) {
if (line == "#mtree") {
continue;
}

auto parts = split_to<std::vector<std::string>>(line, ' ');
auto path = parts.front();
parts.erase(parts.begin());

std::unordered_map<std::string, std::string> 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;
Expand Down
4 changes: 4 additions & 0 deletions test/test_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::unordered_map<std::string, std::string>>>
parse_mtree(std::string_view mtree);

bool skip_slow_tests();

#define DWARFS_SLOW_TEST() \
Expand Down

0 comments on commit 213d2ef

Please sign in to comment.