mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-09 04:19:10 -04:00
test: add test_file_access abstraction
This commit is contained in:
parent
b81242d6b6
commit
1f8ad8fbc7
@ -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_file_access>();
|
||||
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<test::mmap_mock>(std::move(fsimage));
|
||||
EXPECT_TRUE(fsimage);
|
||||
|
||||
auto mm = std::make_shared<test::mmap_mock>(std::move(fsimage.value()));
|
||||
|
||||
test::test_logger lgr;
|
||||
filesystem_v2 fs(lgr, mm);
|
||||
@ -76,4 +80,6 @@ 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("error", "warn", "info", "verbose",
|
||||
"debug", "trace"));
|
||||
|
@ -33,6 +33,7 @@
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#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<output_stream>
|
||||
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<std::string> get_file(std::filesystem::path const& path) const;
|
||||
|
||||
private:
|
||||
std::map<std::filesystem::path, std::string> mutable files_;
|
||||
};
|
||||
|
||||
class test_iolayer {
|
||||
public:
|
||||
test_iolayer(std::shared_ptr<os_access_mock> os);
|
||||
test_iolayer(std::shared_ptr<os_access_mock> os,
|
||||
std::shared_ptr<file_access const> fa);
|
||||
~test_iolayer();
|
||||
|
||||
iolayer const& get() const;
|
||||
@ -171,6 +188,7 @@ class test_iolayer {
|
||||
private:
|
||||
std::shared_ptr<os_access_mock> os_;
|
||||
std::shared_ptr<test_terminal> term_;
|
||||
std::shared_ptr<file_access const> fa_;
|
||||
std::istringstream in_;
|
||||
std::ostringstream out_;
|
||||
std::ostringstream err_;
|
||||
|
@ -23,10 +23,68 @@
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#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<output_stream>
|
||||
test_file_access::open_output_binary(std::filesystem::path const& path,
|
||||
std::error_code& ec) const {
|
||||
auto rv = std::make_unique<test_output_stream>(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<std::string>
|
||||
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} {}
|
||||
@ -113,11 +171,17 @@ std::string test_terminal::colored(std::string text, termcolor color,
|
||||
}
|
||||
|
||||
test_iolayer::test_iolayer(std::shared_ptr<os_access_mock> os)
|
||||
: test_iolayer{std::move(os), create_file_access_generic()} {}
|
||||
|
||||
test_iolayer::test_iolayer(std::shared_ptr<os_access_mock> os,
|
||||
std::shared_ptr<file_access const> fa)
|
||||
: os_{std::move(os)}
|
||||
, term_{std::make_shared<test_terminal>(out_, err_)}
|
||||
, fa_{std::move(fa)}
|
||||
, iol_{std::make_unique<iolayer>(iolayer{
|
||||
.os = os_,
|
||||
.term = term_,
|
||||
.file = fa_,
|
||||
.in = in_,
|
||||
.out = out_,
|
||||
.err = err_,
|
||||
|
Loading…
x
Reference in New Issue
Block a user