mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-08 11:59:48 -04:00
test(mkdwarfs): add output file tests
This commit is contained in:
parent
10cbbb3d58
commit
3c552eb3df
@ -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<std::string> 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<std::error_code>
|
||||
get_open_error(std::filesystem::path const& path) const;
|
||||
std::optional<std::error_code>
|
||||
get_close_error(std::filesystem::path const& path) const;
|
||||
|
||||
private:
|
||||
std::map<std::filesystem::path, std::string> mutable files_;
|
||||
std::map<std::filesystem::path, std::error_code> mutable open_errors_;
|
||||
std::map<std::filesystem::path, std::error_code> mutable close_errors_;
|
||||
};
|
||||
|
||||
class test_iolayer {
|
||||
|
@ -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<std::error_code>
|
||||
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<std::error_code>
|
||||
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<std::string>
|
||||
test_file_access::get_file(std::filesystem::path const& path) const {
|
||||
auto it = files_.find(path);
|
||||
|
@ -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"));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user