Skip to content

Commit

Permalink
test: add recompression test
Browse files Browse the repository at this point in the history
  • Loading branch information
mhx committed Jan 1, 2024
1 parent d2bb917 commit 37a6692
Showing 1 changed file with 43 additions and 9 deletions.
52 changes: 43 additions & 9 deletions test/tool_main_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ class mkdwarfs_tester {
mkdwarfs_tester(std::shared_ptr<test::os_access_mock> pos)
: fa{std::make_shared<test::test_file_access>()}
, os{std::move(pos)}
, iol{os, fa} {
, iol{std::make_unique<test::test_iolayer>(os, fa)}
, lgr{std::make_unique<test::test_logger>()} {
setup_locale();
}

Expand All @@ -106,9 +107,11 @@ class mkdwarfs_tester {
return mkdwarfs_tester(std::make_shared<test::os_access_mock>());
}

void add_root_dir() { os->add("", {1, 040755, 1, 0, 0, 10, 42, 0, 0, 0}); }

int run(std::vector<std::string> args) {
args.insert(args.begin(), "mkdwarfs");
return mkdwarfs_main(args, iol.get());
return mkdwarfs_main(args, iol->get());
}

int run(std::initializer_list<std::string> args) {
Expand All @@ -119,7 +122,7 @@ class mkdwarfs_tester {

filesystem_v2 fs_from_data(std::string data) {
auto mm = std::make_shared<test::mmap_mock>(std::move(data));
return filesystem_v2(lgr, mm);
return filesystem_v2(*lgr, mm);
}

filesystem_v2 fs_from_file(std::string path) {
Expand All @@ -130,13 +133,15 @@ class mkdwarfs_tester {
return fs_from_data(std::move(fsimage.value()));
}

std::string out() const { return iol.out(); }
std::string err() const { return iol.err(); }
filesystem_v2 fs_from_stdout() { return fs_from_data(out()); }

std::string out() const { return iol->out(); }
std::string err() const { return iol->err(); }

std::shared_ptr<test::test_file_access> fa;
std::shared_ptr<test::os_access_mock> os;
test::test_iolayer iol;
test::test_logger lgr;
std::unique_ptr<test::test_iolayer> iol;
std::unique_ptr<test::test_logger> lgr;
};

std::optional<filesystem_v2>
Expand Down Expand Up @@ -331,7 +336,7 @@ TEST_P(mkdwarfs_input_list_test, basic) {
t.fa->set_file(input_file, input_list);
} else {
input_file = "-";
t.iol.set_in(input_list);
t.iol->set_in(input_list);
}

EXPECT_EQ(0, t.run({"--input-list", input_file, "-o", image_file}));
Expand Down Expand Up @@ -371,7 +376,7 @@ TEST_P(categorizer_test, end_to_end) {

auto t = mkdwarfs_tester::create_empty();

t.os->add("", {1, 040755, 1, 0, 0, 10, 42, 0, 0, 0});
t.add_root_dir();
t.os->add_local_files(audio_data_dir);
t.os->add_file("random", 4096, true);

Expand Down Expand Up @@ -558,3 +563,32 @@ TEST(mkdwarfs_test, cannot_open_input_list_file) {
EXPECT_NE(0, t.run({"--input-list", "missing.list", "-o", "-"}));
EXPECT_THAT(t.err(), ::testing::HasSubstr("cannot open input list file"));
}

TEST(mkdwarfs_test, recompress) {
std::string const image_file = "test.dwarfs";
std::string image;

{
mkdwarfs_tester t;
t.os->add_local_files(audio_data_dir);
t.os->add_file("random", 4096, true);
EXPECT_EQ(0, t.run({"-i", "/", "-o", image_file, "--categorize"}));
auto img = t.fa->get_file(image_file);
EXPECT_TRUE(img);
image = std::move(img.value());
}

auto tester = [&] {
auto t = mkdwarfs_tester::create_empty();
t.add_root_dir();
t.os->add_file(image_file, image);
return t;
};

{
auto t = tester();
EXPECT_EQ(0, t.run({"-i", image_file, "-o", "-", "--recompress", "-l0"}));
auto fs = t.fs_from_stdout();
EXPECT_TRUE(fs.find("/random"));
}
}

0 comments on commit 37a6692

Please sign in to comment.