From 5aa234fd56748f205c498c923b94acd496009356 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Thu, 28 Dec 2023 16:39:21 +0100 Subject: [PATCH] test: add test_file_access abstraction --- test/categorizer_test.cpp | 15 ++++++--- test/test_helpers.h | 18 +++++++++++ test/test_iolayer.cpp | 64 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 5 deletions(-) diff --git a/test/categorizer_test.cpp b/test/categorizer_test.cpp index 0d980632f..739d41dd6 100644 --- a/test/categorizer_test.cpp +++ b/test/categorizer_test.cpp @@ -55,16 +55,20 @@ TEST_P(categorizer_test, end_to_end) { input->add_local_files(audio_data_dir); input->add_file("random", 4096, true); - test::test_iolayer iolayer(input); + auto fa = std::make_shared(); + test::test_iolayer iolayer(input, fa); - auto args = test::parse_args("mkdwarfs -i / -o - --categorize"); + auto args = test::parse_args(fmt::format( + "mkdwarfs -i / -o test.dwarfs --categorize --log-level={}", level)); auto exit_code = mkdwarfs_main(args, iolayer.get()); EXPECT_EQ(exit_code, 0); - auto fsimage = iolayer.out(); + auto fsimage = fa->get_file("test.dwarfs"); - auto mm = std::make_shared(std::move(fsimage)); + EXPECT_TRUE(fsimage); + + auto mm = std::make_shared(std::move(fsimage.value())); test::test_logger lgr; filesystem_v2 fs(lgr, mm); @@ -76,4 +80,5 @@ TEST_P(categorizer_test, end_to_end) { EXPECT_TRUE(iv32); } -INSTANTIATE_TEST_SUITE_P(dwarfs, categorizer_test, ::testing::Values("debug")); +INSTANTIATE_TEST_SUITE_P(dwarfs, categorizer_test, + ::testing::Values("info", "verbose", "debug")); diff --git a/test/test_helpers.h b/test/test_helpers.h index 7db84222f..0825ad4f9 100644 --- a/test/test_helpers.h +++ b/test/test_helpers.h @@ -33,6 +33,7 @@ #include #include +#include "dwarfs/file_access.h" #include "dwarfs/file_stat.h" #include "dwarfs/iolayer.h" #include "dwarfs/os_access.h" @@ -154,9 +155,25 @@ class test_terminal : public terminal { size_t width_{80}; }; +class test_file_access : public file_access { + public: + bool exists(std::filesystem::path const& path) const override; + std::unique_ptr + open_output_binary(std::filesystem::path const& path, + std::error_code& ec) const override; + + void set_file(std::filesystem::path const& path, std::string contents) const; + std::optional get_file(std::filesystem::path const& path) const; + + private: + std::map mutable files_; +}; + class test_iolayer { public: test_iolayer(std::shared_ptr os); + test_iolayer(std::shared_ptr os, + std::shared_ptr fa); ~test_iolayer(); iolayer const& get() const; @@ -171,6 +188,7 @@ class test_iolayer { private: std::shared_ptr os_; std::shared_ptr term_; + std::shared_ptr fa_; std::istringstream in_; std::ostringstream out_; std::ostringstream err_; diff --git a/test/test_iolayer.cpp b/test/test_iolayer.cpp index 8e5752b90..a66338b45 100644 --- a/test/test_iolayer.cpp +++ b/test/test_iolayer.cpp @@ -21,10 +21,68 @@ #include +#include "dwarfs/file_access_generic.h" + #include "test_helpers.h" namespace dwarfs::test { +namespace { + +class test_output_stream : public output_stream { + public: + test_output_stream(std::filesystem::path const& path, std::error_code& ec, + test_file_access const* tfa) + : path_{path} + , tfa_{tfa} { + 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); + } + } + + std::ostream& os() override { return os_; } + + void close(std::error_code& ec) override { tfa_->set_file(path_, os_.str()); } + + private: + std::ostringstream os_; + std::filesystem::path path_; + test_file_access const* tfa_; +}; + +} // namespace + +bool test_file_access::exists(std::filesystem::path const& path) const { + return files_.find(path) != files_.end(); +} + +std::unique_ptr +test_file_access::open_output_binary(std::filesystem::path const& path, + std::error_code& ec) const { + auto rv = std::make_unique(path, ec, this); + if (ec) { + rv.reset(); + } + return rv; +} + +void test_file_access::set_file(std::filesystem::path const& path, + std::string content) const { + files_[path] = std::move(content); +} + +std::optional +test_file_access::get_file(std::filesystem::path const& path) const { + auto it = files_.find(path); + if (it != files_.end()) { + return it->second; + } + return std::nullopt; +} + test_terminal::test_terminal(std::ostream& out, std::ostream& err) : out_{&out} , err_{&err} {} @@ -111,11 +169,17 @@ std::string test_terminal::colored(std::string text, termcolor color, } test_iolayer::test_iolayer(std::shared_ptr os) + : test_iolayer{std::move(os), create_file_access_generic()} {} + +test_iolayer::test_iolayer(std::shared_ptr os, + std::shared_ptr fa) : os_{std::move(os)} , term_{std::make_shared(out_, err_)} + , fa_{std::move(fa)} , iol_{std::make_unique(iolayer{ .os = os_, .term = term_, + .file = fa_, .in = in_, .out = out_, .err = err_,