Skip to content

Commit

Permalink
Clean up code in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rikyoz committed Dec 5, 2023
1 parent a1242dd commit 5ad9678
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
39 changes: 19 additions & 20 deletions tests/src/test_bitinputarchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ void require_filesystem_item( const ExpectedItem& expectedItem, const SourceLoca

void require_extracts_to_filesystem( const BitArchiveReader& info, const ExpectedItems& expectedItems ) {
TempTestDirectory testDir{ "test_bitinputarchive" };
INFO( "Test directory: " << testDir.path() );
INFO( "Test directory: " << testDir );

REQUIRE_NOTHROW( info.extractTo( path_to_tstring( testDir.path() ) ) );
REQUIRE_NOTHROW( info.extractTo( testDir ) );
if ( expectedItems.empty() ) {
REQUIRE( fs::is_empty( testDir.path() ) );
} else {
Expand All @@ -94,21 +94,20 @@ void require_extracts_to_filesystem( const BitArchiveReader& info, const Expecte

void require_extracts_items_to_filesystem( const BitArchiveReader& info, const ExpectedItems& expectedItems ) {
TempTestDirectory testDir{ "test_bitinputarchive" };
INFO( "Test directory: " << testDir.path() );
INFO( "Test directory: " << testDir );

const auto testPath = path_to_tstring( testDir.path() );
for ( const auto& expectedItem : expectedItems ) {
const auto archiveItem = archive_item( info, expectedItem );
REQUIRE( archiveItem != info.cend() );
REQUIRE_NOTHROW( info.extractTo( testPath, { archiveItem->index() } ) );
REQUIRE_NOTHROW( info.extractTo( testDir, { archiveItem->index() } ) );
REQUIRE_FILESYSTEM_ITEM( expectedItem );
}
REQUIRE( fs::is_empty( testDir.path() ) );

std::vector< uint32_t > testIndices( info.itemsCount() );
std::iota( testIndices.begin(), testIndices.end(), 0 );
CAPTURE( testIndices );
REQUIRE_NOTHROW( info.extractTo( testPath, testIndices ) );
REQUIRE_NOTHROW( info.extractTo( testDir, testIndices ) );
for ( const auto& expectedItem : expectedItems ) {
REQUIRE_FILESYSTEM_ITEM( expectedItem );
}
Expand All @@ -117,14 +116,14 @@ void require_extracts_items_to_filesystem( const BitArchiveReader& info, const E
// For some reason, 7-Zip doesn't like reversed or random indices when extracting Rar archives.
if ( info.detectedFormat() != BitFormat::Rar ) {
std::reverse( testIndices.begin(), testIndices.end() );
REQUIRE_NOTHROW( info.extractTo( testPath, testIndices ) );
REQUIRE_NOTHROW( info.extractTo( testDir, testIndices ) );
for ( const auto& expectedItem : expectedItems ) {
REQUIRE_FILESYSTEM_ITEM( expectedItem );
}
REQUIRE( fs::is_empty( testDir.path() ) );

std::shuffle( testIndices.begin(), testIndices.end(), std::mt19937{ std::random_device{}() } );
REQUIRE_NOTHROW( info.extractTo( testPath, testIndices ) );
REQUIRE_NOTHROW( info.extractTo( testDir, testIndices ) );
for ( const auto& expectedItem : expectedItems ) {
REQUIRE_FILESYSTEM_ITEM( expectedItem );
}
Expand All @@ -135,14 +134,14 @@ void require_extracts_items_to_filesystem( const BitArchiveReader& info, const E
const auto archiveItem = archive_item( info, expectedItem );
REQUIRE( archiveItem != info.cend() );
// The vector of indices contains a valid index, and an invalid one, so the extraction should fail.
REQUIRE_THROWS( info.extractTo( testPath, { archiveItem->index(), info.itemsCount() } ) );
REQUIRE_THROWS( info.extractTo( testDir, { archiveItem->index(), info.itemsCount() } ) );
REQUIRE( fs::is_empty( testDir.path() ) );
}

REQUIRE_THROWS( info.extractTo( testPath, { info.itemsCount() } ) );
REQUIRE_THROWS( info.extractTo( testDir, { info.itemsCount() } ) );
REQUIRE( fs::is_empty( testDir.path() ) );

REQUIRE_THROWS( info.extractTo( testPath, { std::numeric_limits< uint32_t >::max() } ) );
REQUIRE_THROWS( info.extractTo( testDir, { std::numeric_limits< uint32_t >::max() } ) );
REQUIRE( fs::is_empty( testDir.path() ) );
}

Expand Down Expand Up @@ -339,8 +338,8 @@ void require_archive_extract_fails( const BitArchiveReader& info, const SourceLo

SECTION( "Extracting to a temporary filesystem folder should fail" ) {
TempTestDirectory testDir{ "test_bitinputarchive" };
INFO( "Test directory: " << testDir.path() );
REQUIRE_THROWS( info.extractTo( path_to_tstring( testDir.path() ) ) );
INFO( "Test directory: " << testDir );
REQUIRE_THROWS( info.extractTo( testDir ) );
// TODO: Make some guarantees on what remains after a failed extraction
for ( const auto& item : fs::directory_iterator( testDir.path() ) ) {
if ( item.is_regular_file() ) {
Expand Down Expand Up @@ -585,7 +584,7 @@ TEST_CASE( "BitInputArchive: Testing and extracting multi-volume archives", "[bi
REQUIRE_ARCHIVE_TESTS( info );

TempTestDirectory extractionTestDir{ "test_bitinputarchive" };
REQUIRE_NOTHROW( info.extractTo( path_to_tstring( extractionTestDir.path() ) ) );
REQUIRE_NOTHROW( info.extractTo( extractionTestDir ) );
REQUIRE( fs::exists( wholeArcFileName ) );
REQUIRE( fs::remove( wholeArcFileName ) );
}
Expand Down Expand Up @@ -961,7 +960,7 @@ TEMPLATE_TEST_CASE( "BitInputArchive: Extracting an archive using various Overwr
BitArchiveReader info( test::sevenzip_lib(), inputArchive, testFormat.format );

TempTestDirectory testOutDir{ "test_bitinputarchive" };
INFO( "Output directory: " << testOutDir.path() );
INFO( "Output directory: " << testOutDir );

const auto expectedFile = overwritten_file_path< TestType >( testFormat.format );
REQUIRE_FALSE( fs::exists( expectedFile ) );
Expand All @@ -976,13 +975,13 @@ TEMPLATE_TEST_CASE( "BitInputArchive: Extracting an archive using various Overwr
info.setOverwriteMode( OverwriteMode::None );

// By default, BitArchiveReader uses OverwriteMode::None, so extracting again should throw.
REQUIRE_THROWS( info.extractTo( path_to_tstring( testOutDir.path() ) ) );
REQUIRE_THROWS( info.extractTo( testOutDir ) );
REQUIRE( fs::exists( expectedFile ) );
REQUIRE( fs::is_empty( expectedFile ) );
REQUIRE( fs::remove( expectedFile ) );

// Verifying that if we remove the file, we can now extract it.
REQUIRE_NOTHROW( info.extractTo( path_to_tstring( testOutDir.path() ) ) );
REQUIRE_NOTHROW( info.extractTo( testOutDir ) );
REQUIRE( fs::exists( expectedFile ) );
REQUIRE( crc32( load_file( expectedFile ) ) == clouds.crc32 );
}
Expand All @@ -991,7 +990,7 @@ TEMPLATE_TEST_CASE( "BitInputArchive: Extracting an archive using various Overwr
// After setting OverwriteMode::Overwrite, extracting should not throw.
info.setOverwriteMode( OverwriteMode::Overwrite );

REQUIRE_NOTHROW( info.extractTo( path_to_tstring( testOutDir.path() ) ) );
REQUIRE_NOTHROW( info.extractTo( testOutDir ) );
REQUIRE( fs::exists( expectedFile ) );
REQUIRE( crc32( load_file( expectedFile ) ) == clouds.crc32 );
}
Expand All @@ -1000,7 +999,7 @@ TEMPLATE_TEST_CASE( "BitInputArchive: Extracting an archive using various Overwr
// After setting OverwriteMode::Skip, extracting should not throw and not extracting anything.
info.setOverwriteMode( OverwriteMode::Skip );

REQUIRE_NOTHROW( info.extractTo( path_to_tstring( testOutDir.path() ) ) );
REQUIRE_NOTHROW( info.extractTo( testOutDir ) );
REQUIRE( fs::exists( expectedFile ) );
REQUIRE( fs::is_empty( expectedFile ) );
}
Expand Down Expand Up @@ -1066,7 +1065,7 @@ TEMPLATE_TEST_CASE( "BitInputArchive: Using extraction callbacks", "[bitinputarc

SECTION( "When extracting to the filesystem" ) {
TempTestDirectory testOutDir{ "test_bitinputarchive" };
INFO( "Output directory: " << testOutDir.path() );
INFO( "Output directory: " << testOutDir );
require_extracts_to_filesystem( info, expectedItems );
}

Expand Down
8 changes: 7 additions & 1 deletion tests/src/utils/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include <catch2/catch_get_random_seed.hpp>

#include <internal/stringutil.hpp>

#include "filesystem.hpp"

#include <random>
Expand Down Expand Up @@ -297,10 +299,14 @@ TempDirectory::~TempDirectory() {
}
}

auto TempDirectory::path() -> const fs::path& {
auto TempDirectory::path() const -> const fs::path& {
return mDirectory;
}

TempDirectory::operator tstring() const {
return path_to_tstring( mDirectory );
}

TempTestDirectory::TempTestDirectory( const std::string& dirName )
: TempDirectory{ dirName }, TestDirectory{ path() } {}

Expand Down
9 changes: 8 additions & 1 deletion tests/src/utils/filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,19 @@ class TempDirectory {

~TempDirectory();

auto path() -> const fs::path&;
BIT7Z_NODISCARD
auto path() const -> const fs::path&;

operator tstring() const; // NOLINT(*-explicit-constructor)

private:
fs::path mDirectory;
};

inline auto operator<<( std::ostream& stream, const TempDirectory& dir ) -> std::ostream& {
return stream << dir.path().u8string();
}

struct TempTestDirectory : TempDirectory, TestDirectory {
explicit TempTestDirectory( const std::string& dirName );
};
Expand Down

0 comments on commit 5ad9678

Please sign in to comment.