From 3c552eb3df6b66bf7814b0b43222419e167eacd0 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Wed, 3 Jan 2024 13:02:45 +0100 Subject: [PATCH] test(mkdwarfs): add output file tests --- test/test_helpers.h | 12 +++++++++++ test/test_iolayer.cpp | 46 +++++++++++++++++++++++++++++++++++++---- test/tool_main_test.cpp | 34 ++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 4 deletions(-) diff --git a/test/test_helpers.h b/test/test_helpers.h index 06d4f0fb..9cb8fbea 100644 --- a/test/test_helpers.h +++ b/test/test_helpers.h @@ -198,8 +198,20 @@ class test_file_access : public file_access { void set_file(std::filesystem::path const& path, std::string contents) const; std::optional get_file(std::filesystem::path const& path) const; + void + set_open_error(std::filesystem::path const& path, std::error_code ec) const; + void + set_close_error(std::filesystem::path const& path, std::error_code ec) const; + + std::optional + get_open_error(std::filesystem::path const& path) const; + std::optional + get_close_error(std::filesystem::path const& path) const; + private: std::map mutable files_; + std::map mutable open_errors_; + std::map mutable close_errors_; }; class test_iolayer { diff --git a/test/test_iolayer.cpp b/test/test_iolayer.cpp index e0a51465..3b81b0cc 100644 --- a/test/test_iolayer.cpp +++ b/test/test_iolayer.cpp @@ -54,16 +54,28 @@ class test_output_stream : public output_stream { if (path_.empty()) { ec = std::make_error_code(std::errc::invalid_argument); } - if (tfa_->exists(path_)) { - ec = std::make_error_code(std::errc::file_exists); + if (auto error = tfa_->get_open_error(path_)) { + ec = error.value(); } } std::ostream& os() override { return os_; } - void close(std::error_code& /*ec*/) override { close(); } + void close(std::error_code& ec) override { + if (auto error = tfa_->get_close_error(path_)) { + ec = error.value(); + } else { + tfa_->set_file(path_, os_.str()); + } + } - void close() override { tfa_->set_file(path_, os_.str()); } + void close() override { + std::error_code ec; + close(ec); + if (ec) { + throw std::system_error(ec, fmt::format("close('{}')", path_.string())); + } + } private: std::ostringstream os_; @@ -162,6 +174,32 @@ void test_file_access::set_file(std::filesystem::path const& path, files_[path] = std::move(content); } +void test_file_access::set_open_error(std::filesystem::path const& path, + std::error_code ec) const { + open_errors_[path] = ec; +} + +void test_file_access::set_close_error(std::filesystem::path const& path, + std::error_code ec) const { + close_errors_[path] = ec; +} + +std::optional +test_file_access::get_open_error(std::filesystem::path const& path) const { + if (auto it = open_errors_.find(path); it != open_errors_.end()) { + return it->second; + } + return std::nullopt; +} + +std::optional +test_file_access::get_close_error(std::filesystem::path const& path) const { + if (auto it = close_errors_.find(path); it != close_errors_.end()) { + return it->second; + } + return std::nullopt; +} + std::optional test_file_access::get_file(std::filesystem::path const& path) const { auto it = files_.find(path); diff --git a/test/tool_main_test.cpp b/test/tool_main_test.cpp index 2421fe9a..b67cbfec 100644 --- a/test/tool_main_test.cpp +++ b/test/tool_main_test.cpp @@ -1037,3 +1037,37 @@ TEST(mkdwarfs_test, filesystem_header_error) { EXPECT_NE(0, t.run({"-i", "/", "-o", "-", "--header=header.txt"})) << t.err(); EXPECT_THAT(t.err(), ::testing::HasSubstr("cannot open header file")); } + +TEST(mkdwarfs_test, output_file_exists) { + mkdwarfs_tester t; + t.fa->set_file("exists.dwarfs", "bla"); + EXPECT_NE(0, t.run({"-i", "/", "-o", "exists.dwarfs"})) << t.err(); + EXPECT_THAT(t.err(), ::testing::HasSubstr("output file already exists")); +} + +TEST(mkdwarfs_test, output_file_force) { + mkdwarfs_tester t; + t.fa->set_file("exists.dwarfs", "bla"); + EXPECT_EQ(0, t.run({"-i", "/", "-o", "exists.dwarfs", "-l1", "--force"})) + << t.err(); + auto fs = t.fs_from_file("exists.dwarfs"); + EXPECT_TRUE(fs.find("/foo.pl")); +} + +TEST(mkdwarfs_test, output_file_fail_open) { + mkdwarfs_tester t; + t.fa->set_file("exists.dwarfs", "bla"); + t.fa->set_open_error( + "exists.dwarfs", + std::make_error_code(std::errc::device_or_resource_busy)); + EXPECT_NE(0, t.run({"-i", "/", "-o", "exists.dwarfs", "--force"})) << t.err(); + EXPECT_THAT(t.err(), ::testing::HasSubstr("cannot open output file")); +} + +TEST(mkdwarfs_test, output_file_fail_close) { + mkdwarfs_tester t; + t.fa->set_close_error("test.dwarfs", + std::make_error_code(std::errc::no_space_on_device)); + EXPECT_NE(0, t.run({"-i", "/", "-o", "test.dwarfs"})) << t.err(); + EXPECT_THAT(t.err(), ::testing::HasSubstr("failed to close output file")); +}