Skip to content

Commit

Permalink
test: add test_file_access abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
mhx committed Dec 28, 2023
1 parent 6666f19 commit 5aa234f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
15 changes: 10 additions & 5 deletions test/categorizer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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"));
18 changes: 18 additions & 0 deletions test/test_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand All @@ -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_;
Expand Down
64 changes: 64 additions & 0 deletions test/test_iolayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,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} {}
Expand Down Expand Up @@ -111,11 +169,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_,
Expand Down

0 comments on commit 5aa234f

Please sign in to comment.