From 8abbf0cdc1cd89ca93e1eafa18857c0207818532 Mon Sep 17 00:00:00 2001 From: Oz Date: Sun, 8 Oct 2023 16:19:24 +0200 Subject: [PATCH 01/18] Update doc comment of bit7z::native_string --- include/bit7z/bittypes.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/bit7z/bittypes.hpp b/include/bit7z/bittypes.hpp index 86c8725a..d0ebfbfa 100644 --- a/include/bit7z/bittypes.hpp +++ b/include/bit7z/bittypes.hpp @@ -62,6 +62,7 @@ struct StringTraits< wchar_t > { /** * Native string type of the system. + * @note On Windows, it is an alias of `std::wstring`. */ #ifdef _WIN32 using native_string = std::wstring; From 4d3d89aaf497fa40336dd6da9976243592f028ea Mon Sep 17 00:00:00 2001 From: Oz Date: Sun, 8 Oct 2023 16:19:38 +0200 Subject: [PATCH 02/18] Remove fast_finish from appveyor.yml --- appveyor.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 87421ccb..d81934f8 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -62,8 +62,6 @@ environment: CXX: /usr/bin/clang++ matrix: - fast_finish: true - exclude: # Excluding GCC and Clang on Windows images, and MSVC on Ubuntu image. # Also, we compile MinGW binaries only on Visual Studio 2022 image. From fc49b168282735a67e29399cff5a7291219d0307 Mon Sep 17 00:00:00 2001 From: Oz Date: Sun, 8 Oct 2023 16:19:53 +0200 Subject: [PATCH 03/18] Fix code formatting in CMultiVolumeOutStream --- src/internal/cmultivolumeoutstream.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/internal/cmultivolumeoutstream.hpp b/src/internal/cmultivolumeoutstream.hpp index ff5cef0e..f5df589f 100644 --- a/src/internal/cmultivolumeoutstream.hpp +++ b/src/internal/cmultivolumeoutstream.hpp @@ -45,7 +45,7 @@ class CMultiVolumeOutStream final : public IOutStream, public CMyUnknownImp { // Total size of the output archive (sum of the volumes' sizes). uint64_t mFullSize; - vector > mVolumes; + vector< CMyComPtr< CVolumeOutStream > > mVolumes; public: CMultiVolumeOutStream( uint64_t volSize, fs::path archiveName ); From 80faff4b1ef6d7cbd20a339658597b0d1f7e5be4 Mon Sep 17 00:00:00 2001 From: Oz Date: Sun, 8 Oct 2023 17:03:25 +0200 Subject: [PATCH 04/18] Fix flawfinder warning in util.cpp --- src/internal/util.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/internal/util.cpp b/src/internal/util.cpp index 2ab1bb92..000e52c8 100644 --- a/src/internal/util.cpp +++ b/src/internal/util.cpp @@ -79,10 +79,11 @@ auto narrow( const wchar_t* wideString, size_t size ) -> std::string { auto widen( const std::string& narrowString ) -> std::wstring { #ifdef _WIN32 + const int narrowStringSize = static_cast< int >( narrowString.size() ); const int wideStringSize = MultiByteToWideChar( CODEPAGE, 0, narrowString.c_str(), - static_cast< int >( narrowString.size() ), + narrowStringSize, nullptr, 0 ); if ( wideStringSize == 0 ) { @@ -93,7 +94,7 @@ auto widen( const std::string& narrowString ) -> std::wstring { MultiByteToWideChar( CODEPAGE, 0, narrowString.c_str(), - static_cast< int >( narrowString.size() ), + narrowStringSize, &result[ 0 ], // NOLINT(readability-container-data-pointer) wideStringSize ); return result; From d65a161748d49ad24af7ae6430500fa8a7f402d6 Mon Sep 17 00:00:00 2001 From: Oz Date: Sun, 8 Oct 2023 17:33:14 +0200 Subject: [PATCH 05/18] Add placeholder tests for missing unit tests --- tests/CMakeLists.txt | 9 ++++++++- tests/src/test_bitarchiveeditor.cpp | 19 +++++++++++++++++++ tests/src/test_bitarchivewriter.cpp | 26 ++++++++++++++++++++++++++ tests/src/test_bitfileextractor.cpp | 26 ++++++++++++++++++++++++++ tests/src/test_bitmemcompressor.cpp | 26 ++++++++++++++++++++++++++ tests/src/test_bitmemextractor.cpp | 26 ++++++++++++++++++++++++++ tests/src/test_bitstreamcompressor.cpp | 26 ++++++++++++++++++++++++++ tests/src/test_bitstreamextractor.cpp | 26 ++++++++++++++++++++++++++ 8 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 tests/src/test_bitarchiveeditor.cpp create mode 100644 tests/src/test_bitarchivewriter.cpp create mode 100644 tests/src/test_bitfileextractor.cpp create mode 100644 tests/src/test_bitmemcompressor.cpp create mode 100644 tests/src/test_bitmemextractor.cpp create mode 100644 tests/src/test_bitstreamcompressor.cpp create mode 100644 tests/src/test_bitstreamextractor.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c9b7d5ba..d8000e65 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -16,11 +16,18 @@ set( SOURCE_FILES # public API test sources set( PUBLIC_API_SOURCE_FILES src/test_bit7zlibrary.cpp + src/test_bitarchiveeditor.cpp src/test_bitarchivereader.cpp + src/test_bitarchivewriter.cpp src/test_biterror.cpp src/test_bitexception.cpp src/test_bitfilecompressor.cpp - src/test_bitpropvariant.cpp ) + src/test_bitfileextractor.cpp + src/test_bitmemcompressor.cpp + src/test_bitmemextractor.cpp + src/test_bitpropvariant.cpp + src/test_bitstreamcompressor.cpp + src/test_bitstreamextractor.cpp ) # internal API sources set( INTERNAL_API_SOURCE_FILES diff --git a/tests/src/test_bitarchiveeditor.cpp b/tests/src/test_bitarchiveeditor.cpp new file mode 100644 index 00000000..49f26cb4 --- /dev/null +++ b/tests/src/test_bitarchiveeditor.cpp @@ -0,0 +1,19 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + +/* + * bit7z - A C++ static library to interface with the 7-zip shared libraries. + * Copyright (c) 2014-2023 Riccardo Ostani - All Rights Reserved. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include + +#include + +TEST_CASE( "BitArchiveEditor: TODO", "[bitarchiveeditor]" ) { + +} \ No newline at end of file diff --git a/tests/src/test_bitarchivewriter.cpp b/tests/src/test_bitarchivewriter.cpp new file mode 100644 index 00000000..6928a298 --- /dev/null +++ b/tests/src/test_bitarchivewriter.cpp @@ -0,0 +1,26 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + +/* + * bit7z - A C++ static library to interface with the 7-zip shared libraries. + * Copyright (c) 2014-2023 Riccardo Ostani - All Rights Reserved. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include + +#include "utils/shared_lib.hpp" + +#include + +using namespace bit7z; + +TEST_CASE( "BitArchiveWriter: TODO", "[bitarchivewriter]" ) { + const Bit7zLibrary lib{ test::sevenzip_lib_path() }; + + const BitArchiveWriter writer{lib, BitFormat::SevenZip}; + REQUIRE( writer.compressionFormat() == BitFormat::SevenZip ); // Just a placeholder test. +} \ No newline at end of file diff --git a/tests/src/test_bitfileextractor.cpp b/tests/src/test_bitfileextractor.cpp new file mode 100644 index 00000000..b4d031cc --- /dev/null +++ b/tests/src/test_bitfileextractor.cpp @@ -0,0 +1,26 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + +/* + * bit7z - A C++ static library to interface with the 7-zip shared libraries. + * Copyright (c) 2014-2023 Riccardo Ostani - All Rights Reserved. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include + +#include "utils/shared_lib.hpp" + +#include + +using namespace bit7z; + +TEST_CASE( "BitFileExtractor: TODO", "[bitfileextractor]" ) { + const Bit7zLibrary lib{ test::sevenzip_lib_path() }; + + const BitFileExtractor extractor{lib, BitFormat::SevenZip}; + REQUIRE( extractor.extractionFormat() == BitFormat::SevenZip ); // Just a placeholder test. +} \ No newline at end of file diff --git a/tests/src/test_bitmemcompressor.cpp b/tests/src/test_bitmemcompressor.cpp new file mode 100644 index 00000000..462cb84a --- /dev/null +++ b/tests/src/test_bitmemcompressor.cpp @@ -0,0 +1,26 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + +/* + * bit7z - A C++ static library to interface with the 7-zip shared libraries. + * Copyright (c) 2014-2023 Riccardo Ostani - All Rights Reserved. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include + +#include "utils/shared_lib.hpp" + +#include + +using namespace bit7z; + +TEST_CASE( "BitMemCompressor: TODO", "[bitmemcompressor]" ) { + const Bit7zLibrary lib{ test::sevenzip_lib_path() }; + + const BitMemCompressor memCompressor{lib, BitFormat::SevenZip}; + REQUIRE( memCompressor.compressionFormat() == BitFormat::SevenZip ); // Just a placeholder test. +} \ No newline at end of file diff --git a/tests/src/test_bitmemextractor.cpp b/tests/src/test_bitmemextractor.cpp new file mode 100644 index 00000000..a84e08db --- /dev/null +++ b/tests/src/test_bitmemextractor.cpp @@ -0,0 +1,26 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + +/* + * bit7z - A C++ static library to interface with the 7-zip shared libraries. + * Copyright (c) 2014-2023 Riccardo Ostani - All Rights Reserved. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include + +#include "utils/shared_lib.hpp" + +#include + +using namespace bit7z; + +TEST_CASE( "BitMemExtractor: TODO", "[bitmemxtractor]" ) { + const Bit7zLibrary lib{ test::sevenzip_lib_path() }; + + const BitMemExtractor memExtractor{lib, BitFormat::SevenZip}; + REQUIRE( memExtractor.extractionFormat() == BitFormat::SevenZip ); // Just a placeholder test. +} \ No newline at end of file diff --git a/tests/src/test_bitstreamcompressor.cpp b/tests/src/test_bitstreamcompressor.cpp new file mode 100644 index 00000000..b5b33c77 --- /dev/null +++ b/tests/src/test_bitstreamcompressor.cpp @@ -0,0 +1,26 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + +/* + * bit7z - A C++ static library to interface with the 7-zip shared libraries. + * Copyright (c) 2014-2023 Riccardo Ostani - All Rights Reserved. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include + +#include "utils/shared_lib.hpp" + +#include + +using namespace bit7z; + +TEST_CASE( "BitStreamCompressor: TODO", "[bitstreamcompressor]" ) { + const Bit7zLibrary lib{ test::sevenzip_lib_path() }; + + const BitStreamCompressor streamCompressor{lib, BitFormat::SevenZip}; + REQUIRE( streamCompressor.compressionFormat() == BitFormat::SevenZip ); // Just a placeholder test. +} \ No newline at end of file diff --git a/tests/src/test_bitstreamextractor.cpp b/tests/src/test_bitstreamextractor.cpp new file mode 100644 index 00000000..1b2b9ba9 --- /dev/null +++ b/tests/src/test_bitstreamextractor.cpp @@ -0,0 +1,26 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + +/* + * bit7z - A C++ static library to interface with the 7-zip shared libraries. + * Copyright (c) 2014-2023 Riccardo Ostani - All Rights Reserved. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include + +#include "utils/shared_lib.hpp" + +#include + +using namespace bit7z; + +TEST_CASE( "BitStreamExtractor: TODO", "[bitstreamxtractor]" ) { + const Bit7zLibrary lib{ test::sevenzip_lib_path() }; + + const BitStreamExtractor streamExtractor{lib, BitFormat::SevenZip}; + REQUIRE( streamExtractor.extractionFormat() == BitFormat::SevenZip ); // Just a placeholder test. +} \ No newline at end of file From 28f1a0bdf64966f12b8e84c8cdb1dceb7de16319 Mon Sep 17 00:00:00 2001 From: Oz Date: Sun, 8 Oct 2023 19:21:50 +0200 Subject: [PATCH 06/18] Use a RAII class for setting/resetting the current test directory to use --- tests/src/test_bitarchivereader.cpp | 98 +--- tests/src/test_bititemsvector.cpp | 768 +++++++++++++--------------- tests/src/test_formatdetect.cpp | 32 +- tests/src/utils/filesystem.cpp | 8 + tests/src/utils/filesystem.hpp | 16 + 5 files changed, 404 insertions(+), 518 deletions(-) diff --git a/tests/src/test_bitarchivereader.cpp b/tests/src/test_bitarchivereader.cpp index 227ca784..bf67638c 100644 --- a/tests/src/test_bitarchivereader.cpp +++ b/tests/src/test_bitarchivereader.cpp @@ -138,9 +138,7 @@ struct SingleFileArchive : public TestInputArchive { }; TEST_CASE( "BitArchiveReader: Reading archives containing only a single file", "[bitarchivereader]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "extraction" / "single_file"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "single_file" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -193,8 +191,6 @@ TEST_CASE( "BitArchiveReader: Reading archives containing only a single file", " REQUIRE_ARCHIVE_TESTS( info ); } } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } struct MultipleFilesArchive : public TestInputArchive { @@ -203,9 +199,7 @@ struct MultipleFilesArchive : public TestInputArchive { }; TEST_CASE( "BitArchiveReader: Reading archives containing multiple files", "[bitarchivereader]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "extraction" / "multiple_files"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "multiple_files" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -252,8 +246,6 @@ TEST_CASE( "BitArchiveReader: Reading archives containing multiple files", "[bit REQUIRE_ARCHIVE_TESTS( info ); } } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } struct MultipleItemsArchive : public TestInputArchive { @@ -262,9 +254,7 @@ struct MultipleItemsArchive : public TestInputArchive { }; TEST_CASE( "BitArchiveReader: Reading archives containing multiple items (files and folders)", "[bitarchivereader]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "extraction" / "multiple_items"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "multiple_items" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -312,8 +302,6 @@ TEST_CASE( "BitArchiveReader: Reading archives containing multiple items (files REQUIRE_ARCHIVE_TESTS( info ); } } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } struct EncryptedArchive : public TestInputArchive { @@ -322,9 +310,7 @@ struct EncryptedArchive : public TestInputArchive { }; TEST_CASE( "BitArchiveReader: Reading archives containing encrypted items", "[bitarchivereader]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "extraction" / "encrypted"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "encrypted" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -399,15 +385,11 @@ TEST_CASE( "BitArchiveReader: Reading archives containing encrypted items", "[bi REQUIRE_ARCHIVE_TESTS( info ); } } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } /* Pull request #36 */ TEST_CASE( "BitArchiveReader: Reading header-encrypted archives", "[bitarchivereader]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "extraction" / "header_encrypted"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "header_encrypted" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -499,9 +481,7 @@ TEST_CASE( "BitArchiveReader: Reading header-encrypted archives", "[bitarchivere } TEST_CASE( "BitArchiveReader: Reading metadata of multi-volume archives", "[bitarchivereader]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "extraction" / "split"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "split" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -572,9 +552,7 @@ struct EmptyArchive : public TestInputArchive { }; TEST_CASE( "BitArchiveReader: Reading an empty archive", "[bitarchivereader]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "extraction" / "empty"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "empty" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -610,14 +588,10 @@ TEST_CASE( "BitArchiveReader: Reading an empty archive", "[bitarchivereader]" ) REQUIRE_ARCHIVE_TESTS( info ); } } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitArchiveReader: Solid archive detection", "[bitarchivereader]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "solid"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "solid" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -644,8 +618,6 @@ TEST_CASE( "BitArchiveReader: Solid archive detection", "[bitarchivereader]" ) { REQUIRE( !info.isSolid() ); REQUIRE_ARCHIVE_TESTS( info ); } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } /** @@ -666,9 +638,7 @@ auto test_open_rar_archive( const Bit7zLibrary& lib, const tstring& inFile ) -> } TEST_CASE( "BitArchiveReader: Opening RAR archives using the correct RAR format version", "[bitarchivereader]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "detection" / "valid"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "detection" / "valid" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -680,8 +650,6 @@ TEST_CASE( "BitArchiveReader: Opening RAR archives using the correct RAR format SECTION( "Non-RAR archive" ) { REQUIRE_THROWS( test_open_rar_archive( lib, BIT7Z_STRING( "valid.zip" ) ) ); } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } #define REQUIRE_ITEM_EQUAL( first, second ) \ @@ -699,9 +667,7 @@ TEST_CASE( "BitArchiveReader: Opening RAR archives using the correct RAR format } while ( false ) TEST_CASE( "BitArchiveReader: Checking consistency between items() and iterators", "[bitarchivereader]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "extraction" / "multiple_items"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "multiple_items" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -762,14 +728,10 @@ TEST_CASE( "BitArchiveReader: Checking consistency between items() and iterators } } } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitArchiveReader: Reading invalid archives", "[bitarchivereader]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "testing"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "testing" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -806,14 +768,10 @@ TEST_CASE( "BitArchiveReader: Reading invalid archives", "[bitarchivereader]" ) REQUIRE_THROWS( info.test() ); } } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitArchiveReader: Reading archives using the wrong format should throw", "[bitarchivereader]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "extraction" / "single_file"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "single_file" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -869,8 +827,6 @@ TEST_CASE( "BitArchiveReader: Reading archives using the wrong format should thr } } } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } #ifndef FILE_ATTRIBUTE_WINDOWS_MASK @@ -982,9 +938,7 @@ constexpr auto FILE_ATTRIBUTE_WINDOWS_MASK = 0x07FFF; } while ( false ) TEST_CASE( "BitArchiveReader: Correctly reading file type inside archives", "[bitarchivereader]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "metadata" / "file_type"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "metadata" / "file_type" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -1030,8 +984,6 @@ TEST_CASE( "BitArchiveReader: Correctly reading file type inside archives", "[bi REQUIRE_ITEM_READONLY( info, "read_only" ); } } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } #ifndef BIT7Z_USE_SYSTEM_CODEPAGE @@ -1056,9 +1008,7 @@ TEST_CASE( "BitArchiveReader: Correctly reading file type inside archives", "[bi } while ( false ) TEST_CASE( "BitArchiveReader: Correctly reading archive items with Unicode names", "[bitarchivereader]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "metadata" / "unicode"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "metadata" / "unicode" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -1101,14 +1051,10 @@ TEST_CASE( "BitArchiveReader: Correctly reading archive items with Unicode names REQUIRE_ITEM_UNICODE( info, "ユニコード.pdf" ); } } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitArchiveReader: Reading an archive with a Unicode file name", "[bitarchivereader]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "metadata" / "unicode"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "metadata" / "unicode" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -1145,14 +1091,10 @@ TEST_CASE( "BitArchiveReader: Reading an archive with a Unicode file name", "[bi REQUIRE_ITEM_UNICODE( info, "юнікод.svg" ); REQUIRE_ITEM_UNICODE( info, "ユニコード.pdf" ); } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitArchiveReader: Reading an archive with a Unicode file name (bzip2)", "[bitarchivereader]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "metadata" / "unicode"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "metadata" / "unicode" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -1163,17 +1105,13 @@ TEST_CASE( "BitArchiveReader: Reading an archive with a Unicode file name (bzip2 const BitArchiveReader info( lib, arcFileName.u8string(), BitFormat::BZip2 ); #endif REQUIRE_ITEM_UNICODE( info, "クラウド.jpg" ); - - REQUIRE( set_current_dir( oldCurrentDir ) ); } #endif #ifdef BIT7Z_AUTO_FORMAT TEST_CASE( "BitArchiveReader: Format detection of archives", "[bitarchivereader]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "detection" / "valid"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "detection" / "valid" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -1279,8 +1217,6 @@ TEST_CASE( "BitArchiveReader: Format detection of archives", "[bitarchivereader] } } } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } #endif \ No newline at end of file diff --git a/tests/src/test_bititemsvector.cpp b/tests/src/test_bititemsvector.cpp index 3905940b..819811b4 100644 --- a/tests/src/test_bititemsvector.cpp +++ b/tests/src/test_bititemsvector.cpp @@ -63,19 +63,18 @@ auto in_archive_paths( const BitItemsVector& vector ) -> std::vector< fs::path > return paths; } -struct TestDirectory { +struct TestInputPath { fs::path path; vector< fs::path > expectedItems; }; TEST_CASE( "BitItemsVector: Indexing a valid directory", "[bititemsvector]" ) { - const fs::path oldCurrentDir = current_dir(); - REQUIRE( set_current_dir( test_filesystem_dir ) ); + const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; - const auto testDirectory = GENERATE( - TestDirectory{ + const auto testInput = GENERATE( + TestInputPath{ ".", { "italy.svg", @@ -94,9 +93,9 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory", "[bititemsvector]" ) { "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "empty", { "empty" } }, - TestDirectory{ "./empty", { "empty" } }, - TestDirectory{ + TestInputPath{ "empty", { "empty" } }, + TestInputPath{ "./empty", { "empty" } }, + TestInputPath{ "folder", { "folder", @@ -108,7 +107,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory", "[bititemsvector]" ) { "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder", { "folder", @@ -120,7 +119,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory", "[bititemsvector]" ) { "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "folder/subfolder2", { "folder/subfolder2", @@ -129,7 +128,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory", "[bititemsvector]" ) { "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder/subfolder2", { "subfolder2", @@ -140,27 +139,24 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory", "[bititemsvector]" ) { } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path ) ); + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitItemsVector: Indexing a valid directory (only files)", "[bititemsvector]" ) { - const fs::path oldCurrentDir = current_dir(); - REQUIRE( set_current_dir( test_filesystem_dir ) ); + const TestDirectory testDir{ test_filesystem_dir }; IndexingOptions options{}; options.onlyFiles = true; BitItemsVector itemsVector; - const auto testDirectory = GENERATE( - TestDirectory{ + const auto testInput = GENERATE( + TestInputPath{ ".", { "italy.svg", @@ -174,9 +170,9 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (only files)", "[bititems "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "empty", { "empty" } }, - TestDirectory{ "./empty", { "empty" } }, - TestDirectory{ + TestInputPath{ "empty", { "empty" } }, + TestInputPath{ "./empty", { "empty" } }, + TestInputPath{ "folder", { "folder", @@ -186,7 +182,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (only files)", "[bititems "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder", { "folder", @@ -196,7 +192,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (only files)", "[bititems "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "folder/subfolder2", { "folder/subfolder2", @@ -205,7 +201,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (only files)", "[bititems "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder/subfolder2", { "subfolder2", @@ -216,28 +212,25 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (only files)", "[bititems } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "" ), FilterPolicy::Include, options ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitItemsVector: Indexing a valid directory (retaining folder structure)", "[bititemsvector]" ) { - const fs::path oldCurrentDir = current_dir(); - REQUIRE( set_current_dir( test_filesystem_dir ) ); + const TestDirectory testDir{ test_filesystem_dir }; IndexingOptions options{}; options.retainFolderStructure = true; BitItemsVector itemsVector; - const auto testDirectory = GENERATE( - TestDirectory{ + const auto testInput = GENERATE( + TestInputPath{ ".", { ".", @@ -257,9 +250,9 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (retaining folder structu "./folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "empty", { "empty" } }, - TestDirectory{ "./empty", { "./empty" } }, - TestDirectory{ + TestInputPath{ "empty", { "empty" } }, + TestInputPath{ "./empty", { "./empty" } }, + TestInputPath{ "folder", { "folder", @@ -271,7 +264,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (retaining folder structu "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder", { "./folder", @@ -283,7 +276,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (retaining folder structu "./folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "folder/subfolder2", { "folder/subfolder2", @@ -292,7 +285,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (retaining folder structu "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder/subfolder2", { "./folder/subfolder2", @@ -303,26 +296,23 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (retaining folder structu } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "" ), FilterPolicy::Include, options ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsvector]" ) { - const fs::path oldCurrentDir = current_dir(); - REQUIRE( set_current_dir( test_filesystem_dir ) ); + const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; SECTION( "Wildcard filter *" ) { - const auto testDirectory = GENERATE( - TestDirectory{ + const auto testInput = GENERATE( + TestInputPath{ ".", { "italy.svg", @@ -341,9 +331,9 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", { "folder/clouds.jpg", @@ -354,7 +344,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder", { "clouds.jpg", @@ -365,7 +355,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "folder/subfolder2", { "folder/subfolder2/homework.doc", @@ -373,7 +363,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder/subfolder2", { "homework.doc", @@ -383,11 +373,11 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*" ) ) ); + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*" ) ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } @@ -395,8 +385,8 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve IndexingOptions options{}; options.onlyFiles = true; - const auto testDirectory = GENERATE( - TestDirectory{ + const auto testInput = GENERATE( + TestInputPath{ ".", { "italy.svg", @@ -410,9 +400,9 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", { "folder/clouds.jpg", @@ -421,7 +411,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder", { "clouds.jpg", @@ -430,7 +420,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "folder/subfolder2", { "folder/subfolder2/homework.doc", @@ -438,7 +428,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder/subfolder2", { "homework.doc", @@ -448,18 +438,18 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*" ), FilterPolicy::Include, options ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } SECTION( "Wildcard filter *.*" ) { - const auto testDirectory = GENERATE( - TestDirectory{ + const auto testInput = GENERATE( + TestInputPath{ ".", { "dot.folder", @@ -473,9 +463,9 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", { "folder/clouds.jpg", @@ -484,7 +474,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder", { "clouds.jpg", @@ -493,7 +483,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "folder/subfolder2", { "folder/subfolder2/homework.doc", @@ -501,7 +491,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder/subfolder2", { "homework.doc", @@ -511,11 +501,11 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*.*" ) ) ); + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*.*" ) ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } @@ -523,8 +513,8 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve IndexingOptions options{}; options.onlyFiles = true; - const auto testDirectory = GENERATE( - TestDirectory{ + const auto testInput = GENERATE( + TestInputPath{ ".", { "dot.folder/hello.json", @@ -537,9 +527,9 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", { "folder/clouds.jpg", @@ -548,7 +538,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder", { "clouds.jpg", @@ -557,7 +547,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "folder/subfolder2", { "folder/subfolder2/homework.doc", @@ -565,7 +555,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder/subfolder2", { "homework.doc", @@ -575,12 +565,12 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*.*" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*.*" ), FilterPolicy::Include, options ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } @@ -604,102 +594,102 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve } SECTION( "Only PDF files" ) { - const auto testDirectory = GENERATE( - TestDirectory{ ".", { "Lorem Ipsum.pdf", "folder/subfolder2/The quick brown fox.pdf" } }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ "folder", { "folder/subfolder2/The quick brown fox.pdf" } }, - TestDirectory{ "./folder", { "subfolder2/The quick brown fox.pdf" } }, - TestDirectory{ "folder/subfolder2", { "folder/subfolder2/The quick brown fox.pdf" } }, - TestDirectory{ "./folder/subfolder2", { "The quick brown fox.pdf" } } + const auto testInput = GENERATE( + TestInputPath{ ".", { "Lorem Ipsum.pdf", "folder/subfolder2/The quick brown fox.pdf" } }, + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", { "folder/subfolder2/The quick brown fox.pdf" } }, + TestInputPath{ "./folder", { "subfolder2/The quick brown fox.pdf" } }, + TestInputPath{ "folder/subfolder2", { "folder/subfolder2/The quick brown fox.pdf" } }, + TestInputPath{ "./folder/subfolder2", { "The quick brown fox.pdf" } } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*.pdf" ) ) ); + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*.pdf" ) ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } SECTION( "Only SVG files" ) { - const auto testDirectory = GENERATE( - TestDirectory{ ".", { "italy.svg" } }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ "folder", {} }, - TestDirectory{ "./folder", {} }, - TestDirectory{ "folder/subfolder2", {} }, - TestDirectory{ "./folder/subfolder2", {} } + const auto testInput = GENERATE( + TestInputPath{ ".", { "italy.svg" } }, + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", {} }, + TestInputPath{ "./folder", {} }, + TestInputPath{ "folder/subfolder2", {} }, + TestInputPath{ "./folder/subfolder2", {} } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*.svg" ) ) ); + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*.svg" ) ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } SECTION( "Only JPG files" ) { - const auto testDirectory = GENERATE( - TestDirectory{ ".", { "folder/clouds.jpg" } }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ "folder", { "folder/clouds.jpg" } }, - TestDirectory{ "./folder", { "clouds.jpg" } }, - TestDirectory{ "folder/subfolder2", {} }, - TestDirectory{ "./folder/subfolder2", {} } + const auto testInput = GENERATE( + TestInputPath{ ".", { "folder/clouds.jpg" } }, + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", { "folder/clouds.jpg" } }, + TestInputPath{ "./folder", { "clouds.jpg" } }, + TestInputPath{ "folder/subfolder2", {} }, + TestInputPath{ "./folder/subfolder2", {} } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*.jpg" ) ) ); + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*.jpg" ) ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } SECTION( "Only DOC files" ) { - const auto testDirectory = GENERATE( - TestDirectory{ ".", { "folder/subfolder2/homework.doc" } }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ "folder", { "folder/subfolder2/homework.doc" } }, - TestDirectory{ "./folder", { "subfolder2/homework.doc" } }, - TestDirectory{ "folder/subfolder2", { "folder/subfolder2/homework.doc" } }, - TestDirectory{ "./folder/subfolder2", { "homework.doc" } } + const auto testInput = GENERATE( + TestInputPath{ ".", { "folder/subfolder2/homework.doc" } }, + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", { "folder/subfolder2/homework.doc" } }, + TestInputPath{ "./folder", { "subfolder2/homework.doc" } }, + TestInputPath{ "folder/subfolder2", { "folder/subfolder2/homework.doc" } }, + TestInputPath{ "./folder/subfolder2", { "homework.doc" } } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*.doc" ) ) ); + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*.doc" ) ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } SECTION( "Only XLSX files" ) { - const auto testDirectory = GENERATE( - TestDirectory{ ".", { "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ "folder", { "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "./folder", { "subfolder2/frequency.xlsx" } }, - TestDirectory{ "folder/subfolder2", { "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "./folder/subfolder2", { "frequency.xlsx" } } + const auto testInput = GENERATE( + TestInputPath{ ".", { "folder/subfolder2/frequency.xlsx" } }, + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", { "folder/subfolder2/frequency.xlsx" } }, + TestInputPath{ "./folder", { "subfolder2/frequency.xlsx" } }, + TestInputPath{ "folder/subfolder2", { "folder/subfolder2/frequency.xlsx" } }, + TestInputPath{ "./folder/subfolder2", { "frequency.xlsx" } } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*.xlsx" ) ) ); + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*.xlsx" ) ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } SECTION( "Only PNG files (no matching file)" ) { - const auto testDirectory = GENERATE( as < fs::path > {}, + const auto testInput = GENERATE( as < fs::path > {}, ".", "empty", "./empty", @@ -708,15 +698,15 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "folder/subfolder2", "./folder/subfolder2" ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory, BIT7Z_STRING( "*.png" ) ) ); + DYNAMIC_SECTION ( "Indexing directory " << testInput ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput, BIT7Z_STRING( "*.png" ) ) ); REQUIRE( itemsVector.size() == 0 ); } } SECTION( "Only non-PDF files" ) { - const auto testDirectory = GENERATE( - TestDirectory{ + const auto testInput = GENERATE( + TestInputPath{ ".", { "italy.svg", @@ -733,9 +723,9 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", { "folder/clouds.jpg", @@ -745,7 +735,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder", { "clouds.jpg", @@ -755,14 +745,14 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve "subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "folder/subfolder2", { "folder/subfolder2/homework.doc", "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder/subfolder2", { "homework.doc", @@ -771,61 +761,58 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*.pdf" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*.pdf" ), FilterPolicy::Exclude ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } SECTION( "Wildcard filter * (excluding)" ) { - const auto testDirectory = GENERATE( - TestDirectory{ ".", {} }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ "folder", {} }, - TestDirectory{ "./folder", {} }, - TestDirectory{ "folder/subfolder2", {} }, - TestDirectory{ "./folder/subfolder2", {} } + const auto testInput = GENERATE( + TestInputPath{ ".", {} }, + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", {} }, + TestInputPath{ "./folder", {} }, + TestInputPath{ "folder/subfolder2", {} }, + TestInputPath{ "./folder/subfolder2", {} } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*" ), FilterPolicy::Exclude ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } SECTION( "Empty filter (excluding)" ) { - const auto testDirectory = GENERATE( - TestDirectory{ ".", {} }, - TestDirectory{ "empty", { "empty" } }, - TestDirectory{ "./empty", { "empty" } }, - TestDirectory{ "folder", { "folder" } }, - TestDirectory{ "./folder", { "folder" } }, - TestDirectory{ "folder/subfolder2", { "folder/subfolder2" } }, - TestDirectory{ "./folder/subfolder2", { "subfolder2" } } + const auto testInput = GENERATE( + TestInputPath{ ".", {} }, + TestInputPath{ "empty", { "empty" } }, + TestInputPath{ "./empty", { "empty" } }, + TestInputPath{ "folder", { "folder" } }, + TestInputPath{ "./folder", { "folder" } }, + TestInputPath{ "folder/subfolder2", { "folder/subfolder2" } }, + TestInputPath{ "./folder/subfolder2", { "subfolder2" } } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "" ), FilterPolicy::Exclude ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtered)", "[bititemsvector]" ) { - const fs::path oldCurrentDir = current_dir(); - REQUIRE( set_current_dir( test_filesystem_dir ) ); + const TestDirectory testDir{ test_filesystem_dir }; IndexingOptions options{}; options.recursive = false; @@ -833,8 +820,8 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtere BitItemsVector itemsVector; SECTION( "Wildcard filter *" ) { - const auto testDirectory = GENERATE( - TestDirectory{ + const auto testInput = GENERATE( + TestInputPath{ ".", { "italy.svg", @@ -853,9 +840,9 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtere "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", { "folder/clouds.jpg", @@ -866,7 +853,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtere "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder", { "clouds.jpg", @@ -877,7 +864,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtere "subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "folder/subfolder2", { "folder/subfolder2/homework.doc", @@ -885,7 +872,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtere "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder/subfolder2", { "homework.doc", @@ -893,7 +880,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtere "frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "../test_filesystem/folder/subfolder2", { "homework.doc", @@ -903,20 +890,20 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtere } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*" ), FilterPolicy::Include, options ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } SECTION( "Wildcard filter * (only files)" ) { options.onlyFiles = true; - const auto testDirectory = GENERATE( - TestDirectory{ + const auto testInput = GENERATE( + TestInputPath{ ".", { "italy.svg", @@ -925,21 +912,21 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtere BIT7Z_NATIVE_STRING( "σαράντα δύο.txt" ), } }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", { "folder/clouds.jpg" } }, - TestDirectory{ + TestInputPath{ "./folder", { "clouds.jpg" } }, - TestDirectory{ + TestInputPath{ "folder/subfolder2", { "folder/subfolder2/homework.doc", @@ -947,7 +934,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtere "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder/subfolder2", { "homework.doc", @@ -957,18 +944,18 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtere } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*" ), FilterPolicy::Include, options ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } SECTION( "Wildcard filter *.*" ) { - const auto testDirectory = GENERATE( - TestDirectory{ + const auto testInput = GENERATE( + TestInputPath{ ".", { "dot.folder", @@ -978,21 +965,21 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtere BIT7Z_NATIVE_STRING( "σαράντα δύο.txt" ), } }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", { "folder/clouds.jpg" } }, - TestDirectory{ + TestInputPath{ "./folder", { "clouds.jpg" } }, - TestDirectory{ + TestInputPath{ "folder/subfolder2", { "folder/subfolder2/homework.doc", @@ -1000,7 +987,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtere "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder/subfolder2", { "homework.doc", @@ -1008,7 +995,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtere "frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "../test_filesystem/folder/subfolder2", { "homework.doc", @@ -1018,25 +1005,25 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtere } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*.*" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*.*" ), FilterPolicy::Include, options ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } SECTION( "Wildcard filter *.* (only files)" ) { options.onlyFiles = true; - const auto testDirectory = GENERATE( - TestDirectory{ ".", { "italy.svg", "Lorem Ipsum.pdf", BIT7Z_NATIVE_STRING( "σαράντα δύο.txt" ), } }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ "folder", { "folder/clouds.jpg" } }, - TestDirectory{ "./folder", { "clouds.jpg" } }, - TestDirectory{ + const auto testInput = GENERATE( + TestInputPath{ ".", { "italy.svg", "Lorem Ipsum.pdf", BIT7Z_NATIVE_STRING( "σαράντα δύο.txt" ), } }, + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", { "folder/clouds.jpg" } }, + TestInputPath{ "./folder", { "clouds.jpg" } }, + TestInputPath{ "folder/subfolder2", { "folder/subfolder2/homework.doc", @@ -1044,15 +1031,15 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtere "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "./folder/subfolder2", { "homework.doc", "The quick brown fox.pdf", "frequency.xlsx" } } + TestInputPath{ "./folder/subfolder2", { "homework.doc", "The quick brown fox.pdf", "frequency.xlsx" } } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*.*" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*.*" ), FilterPolicy::Include, options ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } @@ -1076,107 +1063,107 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtere } SECTION( "Only PDF files" ) { - const auto testDirectory = GENERATE( - TestDirectory{ ".", { "Lorem Ipsum.pdf" } }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ "folder", {} }, - TestDirectory{ "./folder", {} }, - TestDirectory{ "folder/subfolder2", { "folder/subfolder2/The quick brown fox.pdf" } }, - TestDirectory{ "./folder/subfolder2", { "The quick brown fox.pdf" } } + const auto testInput = GENERATE( + TestInputPath{ ".", { "Lorem Ipsum.pdf" } }, + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", {} }, + TestInputPath{ "./folder", {} }, + TestInputPath{ "folder/subfolder2", { "folder/subfolder2/The quick brown fox.pdf" } }, + TestInputPath{ "./folder/subfolder2", { "The quick brown fox.pdf" } } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*.pdf" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*.pdf" ), FilterPolicy::Include, options ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } SECTION( "Only SVG files" ) { - const auto testDirectory = GENERATE( - TestDirectory{ ".", { "italy.svg" } }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ "folder", {} }, - TestDirectory{ "./folder", {} }, - TestDirectory{ "folder/subfolder2", {} }, - TestDirectory{ "./folder/subfolder2", {} } + const auto testInput = GENERATE( + TestInputPath{ ".", { "italy.svg" } }, + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", {} }, + TestInputPath{ "./folder", {} }, + TestInputPath{ "folder/subfolder2", {} }, + TestInputPath{ "./folder/subfolder2", {} } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*.svg" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*.svg" ), FilterPolicy::Include, options ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } SECTION( "Only JPG files" ) { - const auto testDirectory = GENERATE( - TestDirectory{ ".", {} }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ "folder", { "folder/clouds.jpg" } }, - TestDirectory{ "./folder", { "clouds.jpg" } }, - TestDirectory{ "folder/subfolder2", {} }, - TestDirectory{ "./folder/subfolder2", {} } + const auto testInput = GENERATE( + TestInputPath{ ".", {} }, + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", { "folder/clouds.jpg" } }, + TestInputPath{ "./folder", { "clouds.jpg" } }, + TestInputPath{ "folder/subfolder2", {} }, + TestInputPath{ "./folder/subfolder2", {} } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*.jpg" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*.jpg" ), FilterPolicy::Include, options ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } SECTION( "Only DOC files" ) { - const auto testDirectory = GENERATE( - TestDirectory{ ".", {} }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ "folder", {} }, - TestDirectory{ "./folder", {} }, - TestDirectory{ "folder/subfolder2", { "folder/subfolder2/homework.doc" } }, - TestDirectory{ "./folder/subfolder2", { "homework.doc" } } + const auto testInput = GENERATE( + TestInputPath{ ".", {} }, + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", {} }, + TestInputPath{ "./folder", {} }, + TestInputPath{ "folder/subfolder2", { "folder/subfolder2/homework.doc" } }, + TestInputPath{ "./folder/subfolder2", { "homework.doc" } } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*.doc" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*.doc" ), FilterPolicy::Include, options ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } SECTION( "Only XLSX files" ) { - const auto testDirectory = GENERATE( - TestDirectory{ ".", {} }, - TestDirectory{ "empty", {} }, - TestDirectory{ "./empty", {} }, - TestDirectory{ "folder", {} }, - TestDirectory{ "./folder", {} }, - TestDirectory{ "folder/subfolder2", { "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "./folder/subfolder2", { "frequency.xlsx" } } + const auto testInput = GENERATE( + TestInputPath{ ".", {} }, + TestInputPath{ "empty", {} }, + TestInputPath{ "./empty", {} }, + TestInputPath{ "folder", {} }, + TestInputPath{ "./folder", {} }, + TestInputPath{ "folder/subfolder2", { "folder/subfolder2/frequency.xlsx" } }, + TestInputPath{ "./folder/subfolder2", { "frequency.xlsx" } } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "*.xlsx" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "*.xlsx" ), FilterPolicy::Include, options ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } } SECTION( "Only PNG files (no matching file)" ) { - const auto testDirectory = GENERATE( as < fs::path > {}, + const auto testInput = GENERATE( as < fs::path > {}, ".", "empty", "./empty", @@ -1185,27 +1172,24 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtere "folder/subfolder2", "./folder/subfolder2" ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory, BIT7Z_STRING( "*.png" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput, BIT7Z_STRING( "*.png" ), FilterPolicy::Include, options ) ); REQUIRE( itemsVector.size() == 0 ); } } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively)", "[bititemsvector]" ) { - const fs::path oldCurrentDir = current_dir(); - REQUIRE( set_current_dir( test_filesystem_dir ) ); + const TestDirectory testDir{ test_filesystem_dir }; IndexingOptions options{}; options.recursive = false; BitItemsVector itemsVector; - const auto testDirectory = GENERATE( - TestDirectory{ + const auto testInput = GENERATE( + TestInputPath{ ".", { "dot.folder", @@ -1224,9 +1208,9 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively)", "[bit "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "empty", { "empty" } }, - TestDirectory{ "./empty", { "empty" } }, - TestDirectory{ + TestInputPath{ "empty", { "empty" } }, + TestInputPath{ "./empty", { "empty" } }, + TestInputPath{ "folder", { "folder", @@ -1238,7 +1222,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively)", "[bit "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder", { "folder", @@ -1250,7 +1234,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively)", "[bit "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "folder/subfolder2", { "folder/subfolder2", @@ -1259,7 +1243,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively)", "[bit "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder/subfolder2", { "subfolder2", @@ -1270,25 +1254,22 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively)", "[bit } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "" ), FilterPolicy::Include, options ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitItemsVector: Indexing a valid directory (relative path)", "[bititemsvector]" ) { - const fs::path oldCurrentDir = current_dir(); - REQUIRE( set_current_dir( test_filesystem_dir ) ); + const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; - const auto testDirectory = GENERATE( - TestDirectory{ + const auto testInput = GENERATE( + TestInputPath{ "../test_filesystem", { "test_filesystem", @@ -1308,8 +1289,8 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (relative path)", "[bitit "test_filesystem/folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "../test_filesystem/empty", { "empty" } }, - TestDirectory{ + TestInputPath{ "../test_filesystem/empty", { "empty" } }, + TestInputPath{ "../test_filesystem/folder", { "folder", @@ -1321,7 +1302,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (relative path)", "[bitit "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "../test_filesystem/folder/subfolder2", { "subfolder2", @@ -1332,27 +1313,24 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (relative path)", "[bitit } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path ) ); + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitItemsVector: Indexing a valid directory (relative path, non-recursively)", "[bititemsvector]" ) { - const fs::path oldCurrentDir = current_dir(); - REQUIRE( set_current_dir( test_filesystem_dir ) ); + const TestDirectory testDir{ test_filesystem_dir }; IndexingOptions options{}; options.recursive = false; BitItemsVector itemsVector; - const auto testDirectory = GENERATE( - TestDirectory{ + const auto testInput = GENERATE( + TestInputPath{ "../test_filesystem", { "test_filesystem", @@ -1372,8 +1350,8 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (relative path, non-recur "test_filesystem/folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "../test_filesystem/empty", { "empty" } }, - TestDirectory{ + TestInputPath{ "../test_filesystem/empty", { "empty" } }, + TestInputPath{ "../test_filesystem/folder", { "folder", @@ -1385,7 +1363,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (relative path, non-recur "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "../test_filesystem/folder/subfolder2", { "subfolder2", @@ -1396,25 +1374,22 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (relative path, non-recur } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { - REQUIRE_NOTHROW( itemsVector.indexDirectory( testDirectory.path, BIT7Z_STRING( "" ), + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { + REQUIRE_NOTHROW( itemsVector.indexDirectory( testInput.path, BIT7Z_STRING( "" ), FilterPolicy::Include, options ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitItemsVector: Indexing a valid directory (custom path mapping)", "[bititemsvector]" ) { - const fs::path oldCurrentDir = current_dir(); - REQUIRE( set_current_dir( test_filesystem_dir ) ); + const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; - const auto testDirectory = GENERATE( - TestDirectory{ + const auto testInput = GENERATE( + TestInputPath{ ".", { "custom_folder", @@ -1434,9 +1409,9 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (custom path mapping)", " "custom_folder/folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "empty", { "custom_folder" } }, - TestDirectory{ "./empty", { "custom_folder" } }, - TestDirectory{ + TestInputPath{ "empty", { "custom_folder" } }, + TestInputPath{ "./empty", { "custom_folder" } }, + TestInputPath{ "folder", { "custom_folder", @@ -1448,7 +1423,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (custom path mapping)", " "custom_folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder", { "custom_folder", @@ -1460,7 +1435,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (custom path mapping)", " "custom_folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "folder/subfolder2", { "custom_folder", @@ -1469,7 +1444,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (custom path mapping)", " "custom_folder/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder/subfolder2", { "custom_folder", @@ -1478,7 +1453,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (custom path mapping)", " "custom_folder/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "../test_filesystem/folder/subfolder2", { "custom_folder", @@ -1487,7 +1462,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (custom path mapping)", " "custom_folder/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ fs::absolute( "../test_filesystem/folder/subfolder2" ), { "custom_folder", @@ -1497,27 +1472,24 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (custom path mapping)", " } } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { const std::map< bit7z::tstring, bit7z::tstring > pathMap{ - { testDirectory.path.string< bit7z::tchar >(), BIT7Z_STRING( "custom_folder" ) } + { testInput.path.string< bit7z::tchar >(), BIT7Z_STRING( "custom_folder" ) } }; REQUIRE_NOTHROW( itemsVector.indexPathsMap( pathMap ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitItemsVector: Indexing a valid directory (empty custom path mapping)", "[bititemsvector]" ) { - const fs::path oldCurrentDir = current_dir(); - REQUIRE( set_current_dir( test_filesystem_dir ) ); + const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; - const auto testDirectory = GENERATE( - TestDirectory{ + const auto testInput = GENERATE( + TestInputPath{ ".", { "dot.folder", @@ -1536,9 +1508,9 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (empty custom path mappin "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ "empty", { "empty" } }, - TestDirectory{ "./empty", { "empty" } }, - TestDirectory{ + TestInputPath{ "empty", { "empty" } }, + TestInputPath{ "./empty", { "empty" } }, + TestInputPath{ "folder", { "folder", @@ -1550,7 +1522,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (empty custom path mappin "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder", { "folder", @@ -1562,7 +1534,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (empty custom path mappin "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "folder/subfolder2", { "folder/subfolder2", @@ -1571,7 +1543,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (empty custom path mappin "folder/subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "./folder/subfolder2", { "subfolder2", @@ -1580,7 +1552,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (empty custom path mappin "subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ "../test_filesystem/folder/subfolder2", { "subfolder2", @@ -1589,7 +1561,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (empty custom path mappin "subfolder2/frequency.xlsx" } }, - TestDirectory{ + TestInputPath{ fs::absolute( "../test_filesystem/folder/subfolder2" ), { "subfolder2", @@ -1599,32 +1571,29 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (empty custom path mappin } } ); - DYNAMIC_SECTION ( "Indexing directory " << testDirectory.path ) { + DYNAMIC_SECTION ( "Indexing directory " << testInput.path ) { const std::map< bit7z::tstring, bit7z::tstring > pathMap{ - { testDirectory.path.string< bit7z::tchar >(), BIT7Z_STRING( "" ) } + { testInput.path.string< bit7z::tchar >(), BIT7Z_STRING( "" ) } }; REQUIRE_NOTHROW( itemsVector.indexPathsMap( pathMap ) ); const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); - REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testDirectory.expectedItems ) ); + REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } -struct TestPaths { +struct TestInputPaths { vector< bit7z::tstring > inputPaths; vector< fs::path > expectedItems; }; TEST_CASE( "BitItemsVector: Indexing a vector of paths", "[bititemsvector]" ) { - const fs::path oldCurrentDir = current_dir(); - REQUIRE( set_current_dir( test_filesystem_dir ) ); + const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; const auto testInput = GENERATE( - TestPaths{ + TestInputPaths{ { BIT7Z_STRING( "Lorem Ipsum.pdf" ), BIT7Z_STRING( "folder/clouds.jpg" ), @@ -1636,7 +1605,7 @@ TEST_CASE( "BitItemsVector: Indexing a vector of paths", "[bititemsvector]" ) { "folder/subfolder2/frequency.xlsx" } }, - TestPaths{ + TestInputPaths{ { BIT7Z_STRING( "italy.svg" ), BIT7Z_STRING( "dot.folder/hello.json" ), @@ -1651,7 +1620,7 @@ TEST_CASE( "BitItemsVector: Indexing a vector of paths", "[bititemsvector]" ) { "folder/subfolder2/frequency.xlsx" } }, - TestPaths{ + TestInputPaths{ { BIT7Z_STRING( "dot.folder" ), BIT7Z_STRING( "empty" ), @@ -1676,13 +1645,10 @@ TEST_CASE( "BitItemsVector: Indexing a vector of paths", "[bititemsvector]" ) { const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitItemsVector: Indexing a vector of paths (retaining folder structure)", "[bititemsvector]" ) { - const fs::path oldCurrentDir = current_dir(); - REQUIRE( set_current_dir( test_filesystem_dir ) ); + const TestDirectory testDir{ test_filesystem_dir }; IndexingOptions options{}; options.retainFolderStructure = true; @@ -1690,7 +1656,7 @@ TEST_CASE( "BitItemsVector: Indexing a vector of paths (retaining folder structu BitItemsVector itemsVector; const auto testInput = GENERATE( - TestPaths{ + TestInputPaths{ { BIT7Z_STRING( "Lorem Ipsum.pdf" ), BIT7Z_STRING( "folder/clouds.jpg" ), @@ -1702,7 +1668,7 @@ TEST_CASE( "BitItemsVector: Indexing a vector of paths (retaining folder structu "folder/subfolder2/frequency.xlsx" } }, - TestPaths{ + TestInputPaths{ { BIT7Z_STRING( "italy.svg" ), BIT7Z_STRING( "dot.folder/hello.json" ), @@ -1717,7 +1683,7 @@ TEST_CASE( "BitItemsVector: Indexing a vector of paths (retaining folder structu "folder/subfolder2/frequency.xlsx" } }, - TestPaths{ + TestInputPaths{ { BIT7Z_STRING( "dot.folder" ), BIT7Z_STRING( "empty" ), @@ -1742,13 +1708,10 @@ TEST_CASE( "BitItemsVector: Indexing a vector of paths (retaining folder structu const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitItemsVector: Indexing a vector of paths (only files)", "[bititemsvector]" ) { - const fs::path oldCurrentDir = current_dir(); - REQUIRE( set_current_dir( test_filesystem_dir ) ); + const TestDirectory testDir{ test_filesystem_dir }; IndexingOptions options{}; options.onlyFiles = true; @@ -1756,7 +1719,7 @@ TEST_CASE( "BitItemsVector: Indexing a vector of paths (only files)", "[bititems BitItemsVector itemsVector; const auto testInput = GENERATE( - TestPaths{ + TestInputPaths{ { BIT7Z_STRING( "Lorem Ipsum.pdf" ), BIT7Z_STRING( "folder/clouds.jpg" ), @@ -1768,7 +1731,7 @@ TEST_CASE( "BitItemsVector: Indexing a vector of paths (only files)", "[bititems "folder/subfolder2/frequency.xlsx" } }, - TestPaths{ + TestInputPaths{ { BIT7Z_STRING( "italy.svg" ), BIT7Z_STRING( "dot.folder/hello.json" ), @@ -1783,7 +1746,7 @@ TEST_CASE( "BitItemsVector: Indexing a vector of paths (only files)", "[bititems "folder/subfolder2/frequency.xlsx" } }, - TestPaths{ + TestInputPaths{ { BIT7Z_STRING( "dot.folder" ), BIT7Z_STRING( "empty" ), @@ -1806,13 +1769,10 @@ TEST_CASE( "BitItemsVector: Indexing a vector of paths (only files)", "[bititems const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitItemsVector: Indexing a vector of paths (non-recursively)", "[bititemsvector]" ) { - const fs::path oldCurrentDir = current_dir(); - REQUIRE( set_current_dir( test_filesystem_dir ) ); + const TestDirectory testDir{ test_filesystem_dir }; IndexingOptions options{}; options.recursive = false; @@ -1820,7 +1780,7 @@ TEST_CASE( "BitItemsVector: Indexing a vector of paths (non-recursively)", "[bit BitItemsVector itemsVector; const auto testInput = GENERATE( - TestPaths{ + TestInputPaths{ { BIT7Z_STRING( "Lorem Ipsum.pdf" ), BIT7Z_STRING( "folder/clouds.jpg" ), @@ -1832,7 +1792,7 @@ TEST_CASE( "BitItemsVector: Indexing a vector of paths (non-recursively)", "[bit "folder/subfolder2/frequency.xlsx" } }, - TestPaths{ + TestInputPaths{ { BIT7Z_STRING( "italy.svg" ), BIT7Z_STRING( "dot.folder/hello.json" ), @@ -1843,7 +1803,7 @@ TEST_CASE( "BitItemsVector: Indexing a vector of paths (non-recursively)", "[bit "dot.folder/hello.json" } }, - TestPaths{ + TestInputPaths{ { BIT7Z_STRING( "dot.folder" ), BIT7Z_STRING( "empty" ), @@ -1857,8 +1817,6 @@ TEST_CASE( "BitItemsVector: Indexing a vector of paths (non-recursively)", "[bit const vector< fs::path > indexedPaths = in_archive_paths( itemsVector ); REQUIRE_THAT( indexedPaths, Catch::UnorderedEquals( testInput.expectedItems ) ); - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitItemsVector: Indexing a directory as a file should fail", "[bititemsvector]" ) { @@ -1882,8 +1840,7 @@ struct TestFile { }; TEST_CASE( "BitItemsVector: Indexing a single file", "[bititemsvector]" ) { - const fs::path oldCurrentDir = current_dir(); - REQUIRE( set_current_dir( test_filesystem_dir ) ); + const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; @@ -1931,8 +1888,6 @@ TEST_CASE( "BitItemsVector: Indexing a single file", "[bititemsvector]" ) { #endif REQUIRE( itemsVector[ 0 ].size() == fs::file_size( testInput.inputFile ) ); } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } #ifndef BIT7Z_USE_SYSTEM_CODEPAGE @@ -1942,8 +1897,7 @@ TEST_CASE( "BitItemsVector: Indexing a single file", "[bititemsvector]" ) { #endif TEST_CASE( "BitItemsVector: Indexing a single file with a custom name", "[bititemsvector]" ) { - const fs::path oldCurrentDir = current_dir(); - REQUIRE( set_current_dir( test_filesystem_dir ) ); + const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; @@ -1975,13 +1929,10 @@ TEST_CASE( "BitItemsVector: Indexing a single file with a custom name", "[bitite #endif REQUIRE( itemsVector[ 0 ].size() == fs::file_size( testInput ) ); } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitItemsVector: Indexing a single stream", "[bititemsvector]" ) { - const fs::path oldCurrentDir = current_dir(); - REQUIRE( set_current_dir( test_filesystem_dir ) ); + const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; @@ -2004,13 +1955,10 @@ TEST_CASE( "BitItemsVector: Indexing a single stream", "[bititemsvector]" ) { REQUIRE( itemsVector[ 0 ].path() == BIT7Z_STRING( "custom_name.ext" ) ); REQUIRE( itemsVector[ 0 ].size() == fs::file_size( testInput ) ); } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitItemsVector: Indexing a single buffer", "[bititemsvector]" ) { - const fs::path oldCurrentDir = current_dir(); - REQUIRE( set_current_dir( test_filesystem_dir ) ); + const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; @@ -2033,6 +1981,4 @@ TEST_CASE( "BitItemsVector: Indexing a single buffer", "[bititemsvector]" ) { REQUIRE( itemsVector[ 0 ].path() == BIT7Z_STRING( "custom_name.ext" ) ); REQUIRE( itemsVector[ 0 ].size() == fs::file_size( testInput ) ); } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } \ No newline at end of file diff --git a/tests/src/test_formatdetect.cpp b/tests/src/test_formatdetect.cpp index 96ad2380..7ce1a3a3 100644 --- a/tests/src/test_formatdetect.cpp +++ b/tests/src/test_formatdetect.cpp @@ -30,9 +30,7 @@ using namespace bit7z::test::filesystem; // Note: format detection by extension doesn't actually require the file to exist! TEST_CASE( "formatdetect: Format detection by extension", "[formatdetect]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "detection" / "valid"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "detection" / "valid" }; auto test = GENERATE( TestInputFormat{ "7z", BitFormat::SevenZip }, TestInputFormat{ "7z.001", BitFormat::Split }, @@ -130,14 +128,10 @@ TEST_CASE( "formatdetect: Format detection by extension", "[formatdetect]" ) { DYNAMIC_SECTION( "Extension: " << test.extension ) { REQUIRE( detect_format_from_extension( "valid." + test.extension ) == test.format ); } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "formatdetect: Format detection by signature", "[formatdetect]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "detection" / "valid"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "detection" / "valid" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -224,14 +218,12 @@ TEST_CASE( "formatdetect: Format detection by signature", "[formatdetect]" ) { DYNAMIC_SECTION( "Extension: " << test.extension ) { // We might directly test the detect_format_from_signature function, but it would require us to include // too many internal headers, and it easily gives compilation problems. - // Hence, we use BitArchiveReader for reading the file from a buffer (to avoid detection via file extension). + // Hence, we use BitArchiveReader for reading the file from a buffer (to avoid detection via file extensions). REQUIRE_LOAD_FILE( fileBuffer, "valid." + test.extension ); const BitArchiveReader reader{ lib, fileBuffer }; REQUIRE( reader.detectedFormat() == test.format ); } - - REQUIRE( set_current_dir( oldCurrentDir ) ); } #ifdef _WIN32 @@ -260,22 +252,16 @@ TEST_CASE( "formatdetect: Format detection by signature (UDF files)", "[formatde #endif TEST_CASE( "formatdetect: Format detection of invalid archives", "[formatdetect]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "detection"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "detection" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; REQUIRE_THROWS_AS( BitArchiveReader( lib, BIT7Z_STRING( "small" ) ), BitException ); REQUIRE_THROWS_AS( BitArchiveReader( lib, BIT7Z_STRING( "invalid" ) ), BitException ); - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "formatdetect: Format detection of archive with a wrong extension (Issue #134)", "[formatdetect]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "detection"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "detection" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -289,14 +275,10 @@ TEST_CASE( "formatdetect: Format detection of archive with a wrong extension (Is REQUIRE_THROWS( BitArchiveReader( lib, fileBuffer, BitFormat::Rar ) ); REQUIRE_NOTHROW( BitArchiveReader( lib, fileBuffer, BitFormat::SevenZip ) ); REQUIRE_NOTHROW( BitArchiveReader( lib, fileBuffer ) ); - - REQUIRE( set_current_dir( oldCurrentDir ) ); } TEST_CASE( "BitArchiveReader: Format detection of an archive file without extension", "[bitarchivereader]" ) { - const fs::path oldCurrentDir = current_dir(); - const auto testDir = fs::path{ test_archives_dir } / "detection"; - REQUIRE( set_current_dir( testDir ) ); + const TestDirectory testDir{ fs::path{ test_archives_dir } / "detection" }; REQUIRE( detect_format_from_extension( "noextension" ) == BitFormat::Auto ); @@ -304,8 +286,6 @@ TEST_CASE( "BitArchiveReader: Format detection of an archive file without extens const BitArchiveReader reader{ lib, BIT7Z_STRING( "noextension" ) }; REQUIRE( reader.detectedFormat() == BitFormat::SevenZip ); REQUIRE_NOTHROW( reader.test() ); - - REQUIRE( set_current_dir( oldCurrentDir ) ); } #endif \ No newline at end of file diff --git a/tests/src/utils/filesystem.cpp b/tests/src/utils/filesystem.cpp index eaa975b6..bd586590 100644 --- a/tests/src/utils/filesystem.cpp +++ b/tests/src/utils/filesystem.cpp @@ -150,6 +150,14 @@ auto empty_content() -> const ArchiveContent& { return instance; } +TestDirectory::TestDirectory( const fs::path& testDir ) : mOldCurrentDirectory{ current_dir() } { + set_current_dir( testDir ); +} + +TestDirectory::~TestDirectory() { + set_current_dir( mOldCurrentDirectory ); +} + } // namespace filesystem } // namespace test } // namespace bit7z \ No newline at end of file diff --git a/tests/src/utils/filesystem.hpp b/tests/src/utils/filesystem.hpp index 9acf17e9..628af3fe 100644 --- a/tests/src/utils/filesystem.hpp +++ b/tests/src/utils/filesystem.hpp @@ -137,6 +137,22 @@ auto encrypted_content() -> const ArchiveContent&; auto empty_content() -> const ArchiveContent&; +class TestDirectory { + fs::path mOldCurrentDirectory; + public: + explicit TestDirectory( const fs::path& testDir ); + + explicit TestDirectory( const TestDirectory& ) = delete; + + explicit TestDirectory( TestDirectory&& ) = delete; + + auto operator=( const TestDirectory& ) -> TestDirectory& = delete; + + auto operator=( TestDirectory&& ) -> TestDirectory& = delete; + + ~TestDirectory(); +}; + #endif } // namespace filesystem From 7838b31ae30068e54decd923b6da6ad84fd34bce Mon Sep 17 00:00:00 2001 From: Oz Date: Sun, 8 Oct 2023 20:31:42 +0200 Subject: [PATCH 07/18] Use path_to_tstring in BitArchiveReader tests --- tests/src/test_bitarchivereader.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/tests/src/test_bitarchivereader.cpp b/tests/src/test_bitarchivereader.cpp index bf67638c..4f385270 100644 --- a/tests/src/test_bitarchivereader.cpp +++ b/tests/src/test_bitarchivereader.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include @@ -1061,11 +1062,7 @@ TEST_CASE( "BitArchiveReader: Reading an archive with a Unicode file name", "[bi fs::path arcFileName{ BIT7Z_NATIVE_STRING( "αρχείο.7z" ) }; SECTION( "Filesystem archive" ) { -#ifdef BIT7Z_USE_NATIVE_STRING - const BitArchiveReader info( lib, arcFileName.native(), BitFormat::SevenZip ); -#else - const BitArchiveReader info( lib, arcFileName.u8string(), BitFormat::SevenZip ); -#endif + const BitArchiveReader info( lib, path_to_tstring( arcFileName ), BitFormat::SevenZip ); REQUIRE_ITEM_UNICODE( info, "¡Porque sí!.doc" ); REQUIRE_ITEM_UNICODE( info, "σύννεφα.jpg" ); REQUIRE_ITEM_UNICODE( info, "юнікод.svg" ); @@ -1099,11 +1096,7 @@ TEST_CASE( "BitArchiveReader: Reading an archive with a Unicode file name (bzip2 const Bit7zLibrary lib{ test::sevenzip_lib_path() }; fs::path arcFileName{ BIT7Z_NATIVE_STRING( "クラウド.jpg.bz2" ) }; -#ifdef BIT7Z_USE_NATIVE_STRING - const BitArchiveReader info( lib, arcFileName.native(), BitFormat::BZip2 ); -#else - const BitArchiveReader info( lib, arcFileName.u8string(), BitFormat::BZip2 ); -#endif + const BitArchiveReader info( lib, path_to_tstring( arcFileName ), BitFormat::BZip2 ); REQUIRE_ITEM_UNICODE( info, "クラウド.jpg" ); } #endif From 9d55ce5b08a6e7595c7eb3f43c35a7ac1d45282d Mon Sep 17 00:00:00 2001 From: Oz Date: Sun, 8 Oct 2023 21:36:20 +0200 Subject: [PATCH 08/18] Clean up and improve tests --- tests/src/test_bitarchivereader.cpp | 73 +++++++++++++++-------------- tests/src/test_bititemsvector.cpp | 36 +++++++------- 2 files changed, 55 insertions(+), 54 deletions(-) diff --git a/tests/src/test_bitarchivereader.cpp b/tests/src/test_bitarchivereader.cpp index 4f385270..643ff15f 100644 --- a/tests/src/test_bitarchivereader.cpp +++ b/tests/src/test_bitarchivereader.cpp @@ -90,7 +90,7 @@ static_assert( std::is_move_assignable< BitArchiveItemInfo >::value, } \ } while( false ) -#define REQUIRE_ARCHIVE_CONTENT( info, input, from_filesystem ) \ +#define REQUIRE_ARCHIVE_CONTENT( info, input ) \ do { \ REQUIRE_FALSE( (info).archiveProperties().empty() ); \ \ @@ -113,6 +113,7 @@ static_assert( std::is_move_assignable< BitArchiveItemInfo >::value, REQUIRE( items.size() == (info).itemsCount() ); \ \ const bool archive_stores_paths = format_has_path_metadata( format ); \ + const bool from_filesystem = !(info).archivePath().empty(); \ size_t found_items = 0; \ for ( const auto& archivedItem : archive_content.items ) { \ for ( const auto& item : items ) { \ @@ -139,7 +140,7 @@ struct SingleFileArchive : public TestInputArchive { }; TEST_CASE( "BitArchiveReader: Reading archives containing only a single file", "[bitarchivereader]" ) { - const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "single_file" }; + static const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "single_file" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -165,7 +166,7 @@ TEST_CASE( "BitArchiveReader: Reading archives containing only a single file", " REQUIRE( info.archivePath() == arcFileName ); REQUIRE_FALSE( info.hasEncryptedItems() ); REQUIRE_FALSE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive, true ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_ARCHIVE_TESTS( info ); } @@ -176,7 +177,7 @@ TEST_CASE( "BitArchiveReader: Reading archives containing only a single file", " REQUIRE( info.archivePath().empty() ); // No archive path for buffered archives REQUIRE_FALSE( info.hasEncryptedItems() ); REQUIRE_FALSE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive, false ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_ARCHIVE_TESTS( info ); } @@ -188,7 +189,7 @@ TEST_CASE( "BitArchiveReader: Reading archives containing only a single file", " REQUIRE( info.archivePath().empty() ); // No archive path for streamed archives REQUIRE_FALSE( info.hasEncryptedItems() ); REQUIRE_FALSE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive, false ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_ARCHIVE_TESTS( info ); } } @@ -200,7 +201,7 @@ struct MultipleFilesArchive : public TestInputArchive { }; TEST_CASE( "BitArchiveReader: Reading archives containing multiple files", "[bitarchivereader]" ) { - const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "multiple_files" }; + static const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "multiple_files" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -220,7 +221,7 @@ TEST_CASE( "BitArchiveReader: Reading archives containing multiple files", "[bit REQUIRE( info.archivePath() == arcFileName ); REQUIRE_FALSE( info.hasEncryptedItems() ); REQUIRE_FALSE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive, true ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_ARCHIVE_TESTS( info ); } @@ -231,7 +232,7 @@ TEST_CASE( "BitArchiveReader: Reading archives containing multiple files", "[bit REQUIRE( info.archivePath().empty() ); // No archive path for buffered archives REQUIRE_FALSE( info.hasEncryptedItems() ); REQUIRE_FALSE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive, false ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_ARCHIVE_TESTS( info ); } @@ -243,7 +244,7 @@ TEST_CASE( "BitArchiveReader: Reading archives containing multiple files", "[bit REQUIRE( info.archivePath().empty() ); // No archive path for streamed archives REQUIRE_FALSE( info.hasEncryptedItems() ); REQUIRE_FALSE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive, false ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_ARCHIVE_TESTS( info ); } } @@ -255,7 +256,7 @@ struct MultipleItemsArchive : public TestInputArchive { }; TEST_CASE( "BitArchiveReader: Reading archives containing multiple items (files and folders)", "[bitarchivereader]" ) { - const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "multiple_items" }; + static const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "multiple_items" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -276,7 +277,7 @@ TEST_CASE( "BitArchiveReader: Reading archives containing multiple items (files REQUIRE( info.archivePath() == arcFileName ); REQUIRE_FALSE( info.hasEncryptedItems() ); REQUIRE_FALSE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive, true ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_ARCHIVE_TESTS( info ); } @@ -287,7 +288,7 @@ TEST_CASE( "BitArchiveReader: Reading archives containing multiple items (files REQUIRE( info.archivePath().empty() ); // No archive path for buffered archives REQUIRE_FALSE( info.hasEncryptedItems() ); REQUIRE_FALSE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive, false ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_ARCHIVE_TESTS( info ); } @@ -299,7 +300,7 @@ TEST_CASE( "BitArchiveReader: Reading archives containing multiple items (files REQUIRE( info.archivePath().empty() ); // No archive path for streamed archives REQUIRE_FALSE( info.hasEncryptedItems() ); REQUIRE_FALSE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive, false ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_ARCHIVE_TESTS( info ); } } @@ -311,7 +312,7 @@ struct EncryptedArchive : public TestInputArchive { }; TEST_CASE( "BitArchiveReader: Reading archives containing encrypted items", "[bitarchivereader]" ) { - const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "encrypted" }; + static const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "encrypted" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -339,7 +340,7 @@ TEST_CASE( "BitArchiveReader: Reading archives containing encrypted items", "[bi BitArchiveReader info( lib, arcFileName.string< tchar >(), testArchive.format() ); REQUIRE( info.hasEncryptedItems() ); REQUIRE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive, true ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_THROWS( info.test() ); REQUIRE_NOTHROW( info.setPassword( password ) ); REQUIRE_ARCHIVE_TESTS( info ); @@ -357,7 +358,7 @@ TEST_CASE( "BitArchiveReader: Reading archives containing encrypted items", "[bi BitArchiveReader info( lib, fileBuffer, testArchive.format() ); REQUIRE( info.hasEncryptedItems() ); REQUIRE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive, false ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_THROWS( info.test() ); REQUIRE_NOTHROW( info.setPassword( password ) ); REQUIRE_ARCHIVE_TESTS( info ); @@ -380,7 +381,7 @@ TEST_CASE( "BitArchiveReader: Reading archives containing encrypted items", "[bi BitArchiveReader info( lib, fileStream, testArchive.format() ); REQUIRE( info.hasEncryptedItems() ); REQUIRE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive, false ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_THROWS( info.test() ); REQUIRE_NOTHROW( info.setPassword( password ) ); REQUIRE_ARCHIVE_TESTS( info ); @@ -390,7 +391,7 @@ TEST_CASE( "BitArchiveReader: Reading archives containing encrypted items", "[bi /* Pull request #36 */ TEST_CASE( "BitArchiveReader: Reading header-encrypted archives", "[bitarchivereader]" ) { - const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "header_encrypted" }; + static const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "header_encrypted" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -421,7 +422,7 @@ TEST_CASE( "BitArchiveReader: Reading header-encrypted archives", "[bitarchivere const BitArchiveReader info( lib, arcFileName.string< tchar >(), testArchive.format(), password ); REQUIRE( info.hasEncryptedItems() ); REQUIRE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive, true ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_ARCHIVE_TESTS( info ); } @@ -444,7 +445,7 @@ TEST_CASE( "BitArchiveReader: Reading header-encrypted archives", "[bitarchivere const BitArchiveReader info( lib, fileBuffer, testArchive.format(), password ); REQUIRE( info.hasEncryptedItems() ); REQUIRE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive, false ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_ARCHIVE_TESTS( info ); } @@ -475,14 +476,14 @@ TEST_CASE( "BitArchiveReader: Reading header-encrypted archives", "[bitarchivere const BitArchiveReader info( lib, fileStream, testArchive.format(), password ); REQUIRE( info.hasEncryptedItems() ); REQUIRE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive, false ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_ARCHIVE_TESTS( info ); } } } TEST_CASE( "BitArchiveReader: Reading metadata of multi-volume archives", "[bitarchivereader]" ) { - const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "split" }; + static const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "split" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -553,7 +554,7 @@ struct EmptyArchive : public TestInputArchive { }; TEST_CASE( "BitArchiveReader: Reading an empty archive", "[bitarchivereader]" ) { - const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "empty" }; + static const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "empty" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -568,7 +569,7 @@ TEST_CASE( "BitArchiveReader: Reading an empty archive", "[bitarchivereader]" ) SECTION( "Filesystem archive" ) { const BitArchiveReader info( lib, arcFileName.string< tchar >(), testArchive.format() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive, true ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_ARCHIVE_TESTS( info ); } @@ -576,7 +577,7 @@ TEST_CASE( "BitArchiveReader: Reading an empty archive", "[bitarchivereader]" ) REQUIRE_LOAD_FILE( fileBuffer, arcFileName ); const BitArchiveReader info( lib, fileBuffer, testArchive.format() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive, false ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_ARCHIVE_TESTS( info ); } @@ -585,14 +586,14 @@ TEST_CASE( "BitArchiveReader: Reading an empty archive", "[bitarchivereader]" ) REQUIRE( fileStream.is_open() ); const BitArchiveReader info( lib, fileStream, testArchive.format() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive, false ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_ARCHIVE_TESTS( info ); } } } TEST_CASE( "BitArchiveReader: Solid archive detection", "[bitarchivereader]" ) { - const TestDirectory testDir{ fs::path{ test_archives_dir } / "solid" }; + static const TestDirectory testDir{ fs::path{ test_archives_dir } / "solid" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -639,7 +640,7 @@ auto test_open_rar_archive( const Bit7zLibrary& lib, const tstring& inFile ) -> } TEST_CASE( "BitArchiveReader: Opening RAR archives using the correct RAR format version", "[bitarchivereader]" ) { - const TestDirectory testDir{ fs::path{ test_archives_dir } / "detection" / "valid" }; + static const TestDirectory testDir{ fs::path{ test_archives_dir } / "detection" / "valid" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -668,7 +669,7 @@ TEST_CASE( "BitArchiveReader: Opening RAR archives using the correct RAR format } while ( false ) TEST_CASE( "BitArchiveReader: Checking consistency between items() and iterators", "[bitarchivereader]" ) { - const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "multiple_items" }; + static const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "multiple_items" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -732,7 +733,7 @@ TEST_CASE( "BitArchiveReader: Checking consistency between items() and iterators } TEST_CASE( "BitArchiveReader: Reading invalid archives", "[bitarchivereader]" ) { - const TestDirectory testDir{ fs::path{ test_archives_dir } / "testing" }; + static const TestDirectory testDir{ fs::path{ test_archives_dir } / "testing" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -772,7 +773,7 @@ TEST_CASE( "BitArchiveReader: Reading invalid archives", "[bitarchivereader]" ) } TEST_CASE( "BitArchiveReader: Reading archives using the wrong format should throw", "[bitarchivereader]" ) { - const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "single_file" }; + static const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "single_file" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -939,7 +940,7 @@ constexpr auto FILE_ATTRIBUTE_WINDOWS_MASK = 0x07FFF; } while ( false ) TEST_CASE( "BitArchiveReader: Correctly reading file type inside archives", "[bitarchivereader]" ) { - const TestDirectory testDir{ fs::path{ test_archives_dir } / "metadata" / "file_type" }; + static const TestDirectory testDir{ fs::path{ test_archives_dir } / "metadata" / "file_type" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -1009,7 +1010,7 @@ TEST_CASE( "BitArchiveReader: Correctly reading file type inside archives", "[bi } while ( false ) TEST_CASE( "BitArchiveReader: Correctly reading archive items with Unicode names", "[bitarchivereader]" ) { - const TestDirectory testDir{ fs::path{ test_archives_dir } / "metadata" / "unicode" }; + static const TestDirectory testDir{ fs::path{ test_archives_dir } / "metadata" / "unicode" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -1055,7 +1056,7 @@ TEST_CASE( "BitArchiveReader: Correctly reading archive items with Unicode names } TEST_CASE( "BitArchiveReader: Reading an archive with a Unicode file name", "[bitarchivereader]" ) { - const TestDirectory testDir{ fs::path{ test_archives_dir } / "metadata" / "unicode" }; + static const TestDirectory testDir{ fs::path{ test_archives_dir } / "metadata" / "unicode" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -1091,7 +1092,7 @@ TEST_CASE( "BitArchiveReader: Reading an archive with a Unicode file name", "[bi } TEST_CASE( "BitArchiveReader: Reading an archive with a Unicode file name (bzip2)", "[bitarchivereader]" ) { - const TestDirectory testDir{ fs::path{ test_archives_dir } / "metadata" / "unicode" }; + static const TestDirectory testDir{ fs::path{ test_archives_dir } / "metadata" / "unicode" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -1104,7 +1105,7 @@ TEST_CASE( "BitArchiveReader: Reading an archive with a Unicode file name (bzip2 #ifdef BIT7Z_AUTO_FORMAT TEST_CASE( "BitArchiveReader: Format detection of archives", "[bitarchivereader]" ) { - const TestDirectory testDir{ fs::path{ test_archives_dir } / "detection" / "valid" }; + static const TestDirectory testDir{ fs::path{ test_archives_dir } / "detection" / "valid" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; diff --git a/tests/src/test_bititemsvector.cpp b/tests/src/test_bititemsvector.cpp index 819811b4..2bb632bb 100644 --- a/tests/src/test_bititemsvector.cpp +++ b/tests/src/test_bititemsvector.cpp @@ -69,7 +69,7 @@ struct TestInputPath { }; TEST_CASE( "BitItemsVector: Indexing a valid directory", "[bititemsvector]" ) { - const TestDirectory testDir{ test_filesystem_dir }; + static const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; @@ -148,7 +148,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory", "[bititemsvector]" ) { } TEST_CASE( "BitItemsVector: Indexing a valid directory (only files)", "[bititemsvector]" ) { - const TestDirectory testDir{ test_filesystem_dir }; + static const TestDirectory testDir{ test_filesystem_dir }; IndexingOptions options{}; options.onlyFiles = true; @@ -222,7 +222,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (only files)", "[bititems } TEST_CASE( "BitItemsVector: Indexing a valid directory (retaining folder structure)", "[bititemsvector]" ) { - const TestDirectory testDir{ test_filesystem_dir }; + static const TestDirectory testDir{ test_filesystem_dir }; IndexingOptions options{}; options.retainFolderStructure = true; @@ -306,7 +306,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (retaining folder structu } TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsvector]" ) { - const TestDirectory testDir{ test_filesystem_dir }; + static const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; @@ -812,7 +812,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (filtered)", "[bititemsve } TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtered)", "[bititemsvector]" ) { - const TestDirectory testDir{ test_filesystem_dir }; + static const TestDirectory testDir{ test_filesystem_dir }; IndexingOptions options{}; options.recursive = false; @@ -1181,7 +1181,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively, filtere } TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively)", "[bititemsvector]" ) { - const TestDirectory testDir{ test_filesystem_dir }; + static const TestDirectory testDir{ test_filesystem_dir }; IndexingOptions options{}; options.recursive = false; @@ -1264,7 +1264,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (non-recursively)", "[bit } TEST_CASE( "BitItemsVector: Indexing a valid directory (relative path)", "[bititemsvector]" ) { - const TestDirectory testDir{ test_filesystem_dir }; + static const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; @@ -1322,7 +1322,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (relative path)", "[bitit } TEST_CASE( "BitItemsVector: Indexing a valid directory (relative path, non-recursively)", "[bititemsvector]" ) { - const TestDirectory testDir{ test_filesystem_dir }; + static const TestDirectory testDir{ test_filesystem_dir }; IndexingOptions options{}; options.recursive = false; @@ -1384,7 +1384,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (relative path, non-recur } TEST_CASE( "BitItemsVector: Indexing a valid directory (custom path mapping)", "[bititemsvector]" ) { - const TestDirectory testDir{ test_filesystem_dir }; + static const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; @@ -1484,7 +1484,7 @@ TEST_CASE( "BitItemsVector: Indexing a valid directory (custom path mapping)", " } TEST_CASE( "BitItemsVector: Indexing a valid directory (empty custom path mapping)", "[bititemsvector]" ) { - const TestDirectory testDir{ test_filesystem_dir }; + static const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; @@ -1588,7 +1588,7 @@ struct TestInputPaths { }; TEST_CASE( "BitItemsVector: Indexing a vector of paths", "[bititemsvector]" ) { - const TestDirectory testDir{ test_filesystem_dir }; + static const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; @@ -1648,7 +1648,7 @@ TEST_CASE( "BitItemsVector: Indexing a vector of paths", "[bititemsvector]" ) { } TEST_CASE( "BitItemsVector: Indexing a vector of paths (retaining folder structure)", "[bititemsvector]" ) { - const TestDirectory testDir{ test_filesystem_dir }; + static const TestDirectory testDir{ test_filesystem_dir }; IndexingOptions options{}; options.retainFolderStructure = true; @@ -1711,7 +1711,7 @@ TEST_CASE( "BitItemsVector: Indexing a vector of paths (retaining folder structu } TEST_CASE( "BitItemsVector: Indexing a vector of paths (only files)", "[bititemsvector]" ) { - const TestDirectory testDir{ test_filesystem_dir }; + static const TestDirectory testDir{ test_filesystem_dir }; IndexingOptions options{}; options.onlyFiles = true; @@ -1772,7 +1772,7 @@ TEST_CASE( "BitItemsVector: Indexing a vector of paths (only files)", "[bititems } TEST_CASE( "BitItemsVector: Indexing a vector of paths (non-recursively)", "[bititemsvector]" ) { - const TestDirectory testDir{ test_filesystem_dir }; + static const TestDirectory testDir{ test_filesystem_dir }; IndexingOptions options{}; options.recursive = false; @@ -1840,7 +1840,7 @@ struct TestFile { }; TEST_CASE( "BitItemsVector: Indexing a single file", "[bititemsvector]" ) { - const TestDirectory testDir{ test_filesystem_dir }; + static const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; @@ -1897,7 +1897,7 @@ TEST_CASE( "BitItemsVector: Indexing a single file", "[bititemsvector]" ) { #endif TEST_CASE( "BitItemsVector: Indexing a single file with a custom name", "[bititemsvector]" ) { - const TestDirectory testDir{ test_filesystem_dir }; + static const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; @@ -1932,7 +1932,7 @@ TEST_CASE( "BitItemsVector: Indexing a single file with a custom name", "[bitite } TEST_CASE( "BitItemsVector: Indexing a single stream", "[bititemsvector]" ) { - const TestDirectory testDir{ test_filesystem_dir }; + static const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; @@ -1958,7 +1958,7 @@ TEST_CASE( "BitItemsVector: Indexing a single stream", "[bititemsvector]" ) { } TEST_CASE( "BitItemsVector: Indexing a single buffer", "[bititemsvector]" ) { - const TestDirectory testDir{ test_filesystem_dir }; + static const TestDirectory testDir{ test_filesystem_dir }; BitItemsVector itemsVector; From 00e05b78e5c6dd480bb9e770b126ceacba45d812 Mon Sep 17 00:00:00 2001 From: Oz Date: Mon, 9 Oct 2023 21:31:45 +0200 Subject: [PATCH 09/18] Reduce duplicated code in tests --- tests/src/test_bitarchivereader.cpp | 531 ++++++++-------------------- 1 file changed, 145 insertions(+), 386 deletions(-) diff --git a/tests/src/test_bitarchivereader.cpp b/tests/src/test_bitarchivereader.cpp index 643ff15f..d578054f 100644 --- a/tests/src/test_bitarchivereader.cpp +++ b/tests/src/test_bitarchivereader.cpp @@ -26,7 +26,7 @@ // For checking posix file attributes #include -// On MSVC, these macros are not defined! +// MSVC doesn't define these macros! #if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG) #define S_ISREG( m ) (((m) & S_IFMT) == S_IFREG) #endif @@ -139,7 +139,35 @@ struct SingleFileArchive : public TestInputArchive { : TestInputArchive{ std::move( extension ), format, packedSize, single_file_content() } {} }; -TEST_CASE( "BitArchiveReader: Reading archives containing only a single file", "[bitarchivereader]" ) { +class InputArchiveProxy { + const fs::path& mPath; + public: + InputArchiveProxy( const fs::path& path ) : mPath{ path } {} + + operator tstring() const { + return path_to_tstring( mPath ); + } + + operator buffer_t() const { + return load_file( mPath ); + } + + operator std::ifstream() const { + return std::ifstream{ mPath, std::ios::binary }; + } +}; + +template +using is_filesystem_archive = std::is_same< bit7z::tstring, std::decay_t< T > >; + +template +using is_buffer_archive = std::is_same< bit7z::buffer_t, std::decay_t< T > >; + +template +using is_stream_archive = std::is_same< std::ifstream, std::decay_t< T > >; + +TEMPLATE_TEST_CASE( "BitArchiveReader: Reading archives containing only a single file", + "[bitarchivereader]", tstring, buffer_t, std::ifstream ) { static const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "single_file" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -161,37 +189,17 @@ TEST_CASE( "BitArchiveReader: Reading archives containing only a single file", " DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const auto arcFileName = fs::path{ clouds.name }.concat( "." + testArchive.extension() ); - SECTION( "Filesystem archive" ) { - const BitArchiveReader info( lib, arcFileName.string< tchar >(), testArchive.format() ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; + const BitArchiveReader info( lib, inputArchive, testArchive.format() ); + if( is_filesystem_archive< TestType >::value ) { REQUIRE( info.archivePath() == arcFileName ); - REQUIRE_FALSE( info.hasEncryptedItems() ); - REQUIRE_FALSE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive ); - REQUIRE_ARCHIVE_TESTS( info ); - } - - SECTION( "Buffer archive" ) { - REQUIRE_LOAD_FILE( fileBuffer, arcFileName ); - - const BitArchiveReader info( lib, fileBuffer, testArchive.format() ); - REQUIRE( info.archivePath().empty() ); // No archive path for buffered archives - REQUIRE_FALSE( info.hasEncryptedItems() ); - REQUIRE_FALSE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive ); - REQUIRE_ARCHIVE_TESTS( info ); - } - - SECTION( "Stream archive" ) { - fs::ifstream fileStream{ arcFileName, std::ios::binary }; - REQUIRE( fileStream.is_open() ); - - const BitArchiveReader info( lib, fileStream, testArchive.format() ); - REQUIRE( info.archivePath().empty() ); // No archive path for streamed archives - REQUIRE_FALSE( info.hasEncryptedItems() ); - REQUIRE_FALSE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive ); - REQUIRE_ARCHIVE_TESTS( info ); + } else { + REQUIRE( info.archivePath().empty() ); // No archive path for buffer/streamed archives } + REQUIRE_FALSE( info.hasEncryptedItems() ); + REQUIRE_FALSE( info.isEncrypted() ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); + REQUIRE_ARCHIVE_TESTS( info ); } } @@ -200,7 +208,8 @@ struct MultipleFilesArchive : public TestInputArchive { : TestInputArchive{ std::move( extension ), format, packedSize, multiple_files_content() } {} }; -TEST_CASE( "BitArchiveReader: Reading archives containing multiple files", "[bitarchivereader]" ) { +TEMPLATE_TEST_CASE( "BitArchiveReader: Reading archives containing multiple files", + "[bitarchivereader]", tstring, buffer_t, std::ifstream ) { static const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "multiple_files" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -216,37 +225,17 @@ TEST_CASE( "BitArchiveReader: Reading archives containing multiple files", "[bit DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "multiple_files." + testArchive.extension(); - SECTION( "Filesystem archive" ) { - const BitArchiveReader info( lib, arcFileName.string< tchar >(), testArchive.format() ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; + const BitArchiveReader info( lib, inputArchive, testArchive.format() ); + if( is_filesystem_archive< TestType >::value ) { REQUIRE( info.archivePath() == arcFileName ); - REQUIRE_FALSE( info.hasEncryptedItems() ); - REQUIRE_FALSE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive ); - REQUIRE_ARCHIVE_TESTS( info ); - } - - SECTION( "Buffer archive" ) { - REQUIRE_LOAD_FILE( fileBuffer, arcFileName ); - - const BitArchiveReader info( lib, fileBuffer, testArchive.format() ); - REQUIRE( info.archivePath().empty() ); // No archive path for buffered archives - REQUIRE_FALSE( info.hasEncryptedItems() ); - REQUIRE_FALSE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive ); - REQUIRE_ARCHIVE_TESTS( info ); - } - - SECTION( "Stream archive" ) { - fs::ifstream fileStream{ arcFileName, std::ios::binary }; - REQUIRE( fileStream.is_open() ); - - const BitArchiveReader info( lib, fileStream, testArchive.format() ); - REQUIRE( info.archivePath().empty() ); // No archive path for streamed archives - REQUIRE_FALSE( info.hasEncryptedItems() ); - REQUIRE_FALSE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive ); - REQUIRE_ARCHIVE_TESTS( info ); + } else { + REQUIRE( info.archivePath().empty() ); // No archive path for buffer/streamed archives } + REQUIRE_FALSE( info.hasEncryptedItems() ); + REQUIRE_FALSE( info.isEncrypted() ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); + REQUIRE_ARCHIVE_TESTS( info ); } } @@ -255,7 +244,8 @@ struct MultipleItemsArchive : public TestInputArchive { : TestInputArchive{ std::move( extension ), format, packedSize, multiple_items_content() } {} }; -TEST_CASE( "BitArchiveReader: Reading archives containing multiple items (files and folders)", "[bitarchivereader]" ) { +TEMPLATE_TEST_CASE( "BitArchiveReader: Reading archives containing multiple items (files and folders)", + "[bitarchivereader]", tstring, buffer_t, std::ifstream ) { static const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "multiple_items" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -272,37 +262,17 @@ TEST_CASE( "BitArchiveReader: Reading archives containing multiple items (files DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "multiple_items." + testArchive.extension(); - SECTION( "Filesystem archive" ) { - const BitArchiveReader info( lib, arcFileName.string< tchar >(), testArchive.format() ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; + const BitArchiveReader info( lib, inputArchive, testArchive.format() ); + if( is_filesystem_archive< TestType >::value ) { REQUIRE( info.archivePath() == arcFileName ); - REQUIRE_FALSE( info.hasEncryptedItems() ); - REQUIRE_FALSE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive ); - REQUIRE_ARCHIVE_TESTS( info ); - } - - SECTION( "Buffer archive" ) { - REQUIRE_LOAD_FILE( fileBuffer, arcFileName ); - - const BitArchiveReader info( lib, fileBuffer, testArchive.format() ); - REQUIRE( info.archivePath().empty() ); // No archive path for buffered archives - REQUIRE_FALSE( info.hasEncryptedItems() ); - REQUIRE_FALSE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive ); - REQUIRE_ARCHIVE_TESTS( info ); - } - - SECTION( "Stream archive" ) { - fs::ifstream fileStream{ arcFileName, std::ios::binary }; - REQUIRE( fileStream.is_open() ); - - const BitArchiveReader info( lib, fileStream, testArchive.format() ); - REQUIRE( info.archivePath().empty() ); // No archive path for streamed archives - REQUIRE_FALSE( info.hasEncryptedItems() ); - REQUIRE_FALSE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive ); - REQUIRE_ARCHIVE_TESTS( info ); + } else { + REQUIRE( info.archivePath().empty() ); // No archive path for buffer/streamed archives } + REQUIRE_FALSE( info.hasEncryptedItems() ); + REQUIRE_FALSE( info.isEncrypted() ); + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); + REQUIRE_ARCHIVE_TESTS( info ); } } @@ -311,7 +281,8 @@ struct EncryptedArchive : public TestInputArchive { : TestInputArchive{ std::move( extension ), format, packedSize, encrypted_content() } {} }; -TEST_CASE( "BitArchiveReader: Reading archives containing encrypted items", "[bitarchivereader]" ) { +TEMPLATE_TEST_CASE( "BitArchiveReader: Reading archives containing encrypted items", + "[bitarchivereader]", tstring, buffer_t, std::ifstream ) { static const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "encrypted" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -328,69 +299,37 @@ TEST_CASE( "BitArchiveReader: Reading archives containing encrypted items", "[bi DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "encrypted." + testArchive.extension(); - SECTION( "Filesystem archive" ) { - REQUIRE_FALSE( BitArchiveReader::isHeaderEncrypted( lib, - arcFileName.string< tchar >(), - testArchive.format() ) ); - - REQUIRE( BitArchiveReader::isEncrypted( lib, - arcFileName.string< tchar >(), - testArchive.format() ) ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; - BitArchiveReader info( lib, arcFileName.string< tchar >(), testArchive.format() ); - REQUIRE( info.hasEncryptedItems() ); - REQUIRE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive ); - REQUIRE_THROWS( info.test() ); - REQUIRE_NOTHROW( info.setPassword( password ) ); - REQUIRE_ARCHIVE_TESTS( info ); + SECTION( "BitArchiveReader::isHeaderEncrypted must return false" ){ + REQUIRE_FALSE( BitArchiveReader::isHeaderEncrypted( lib, inputArchive, testArchive.format() ) ); } - SECTION( "Buffer archive" ) { - REQUIRE_LOAD_FILE( fileBuffer, arcFileName ); - - REQUIRE_FALSE( BitArchiveReader::isHeaderEncrypted( lib, - fileBuffer, - testArchive.format() ) ); - - REQUIRE( BitArchiveReader::isEncrypted( lib, fileBuffer, testArchive.format() ) ); + SECTION( "BitArchiveReader::isEncrypted must return true" ){ + REQUIRE( BitArchiveReader::isEncrypted( lib, inputArchive, testArchive.format() ) ); + } - BitArchiveReader info( lib, fileBuffer, testArchive.format() ); + SECTION( "Opening the archive with no password should allow reading the archive, but tests() should throw" ) { + BitArchiveReader info( lib, inputArchive, testArchive.format() ); REQUIRE( info.hasEncryptedItems() ); REQUIRE( info.isEncrypted() ); REQUIRE_ARCHIVE_CONTENT( info, testArchive ); REQUIRE_THROWS( info.test() ); - REQUIRE_NOTHROW( info.setPassword( password ) ); - REQUIRE_ARCHIVE_TESTS( info ); } - SECTION( "Stream archive" ) { - fs::ifstream fileStream{ arcFileName, std::ios::binary }; - REQUIRE( fileStream.is_open() ); - - REQUIRE_FALSE( BitArchiveReader::isHeaderEncrypted( lib, - fileStream, - testArchive.format() ) ); - fileStream.clear(); - fileStream.seekg( 0, std::ios::beg ); - - REQUIRE( BitArchiveReader::isEncrypted( lib, fileStream, testArchive.format() ) ); - fileStream.clear(); - fileStream.seekg( 0, std::ios::beg ); - - BitArchiveReader info( lib, fileStream, testArchive.format() ); + SECTION( "Opening the archive with the correct password should pass all the checks" ) { + const BitArchiveReader info( lib, inputArchive, testArchive.format(), password ); REQUIRE( info.hasEncryptedItems() ); REQUIRE( info.isEncrypted() ); REQUIRE_ARCHIVE_CONTENT( info, testArchive ); - REQUIRE_THROWS( info.test() ); - REQUIRE_NOTHROW( info.setPassword( password ) ); REQUIRE_ARCHIVE_TESTS( info ); } } } /* Pull request #36 */ -TEST_CASE( "BitArchiveReader: Reading header-encrypted archives", "[bitarchivereader]" ) { +TEMPLATE_TEST_CASE( "BitArchiveReader: Reading header-encrypted archives", + "[bitarchivereader]", tstring, buffer_t, std::ifstream ) { static const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "header_encrypted" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -405,75 +344,29 @@ TEST_CASE( "BitArchiveReader: Reading header-encrypted archives", "[bitarchivere DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "header_encrypted." + testArchive.extension(); - SECTION( "Filesystem archive" ) { - REQUIRE( BitArchiveReader::isHeaderEncrypted( lib, - arcFileName.string< tchar >(), - testArchive.format() ) ); - - REQUIRE( BitArchiveReader::isEncrypted( lib, arcFileName.string< tchar >(), testArchive.format() ) ); - - // no password specified! - REQUIRE_THROWS( BitArchiveReader( lib, arcFileName.string< tchar >(), testArchive.format() ) ); - - // wrong password specified! - REQUIRE_THROWS( BitArchiveReader( lib, arcFileName.string< tchar >(), testArchive.format(), - BIT7Z_STRING( "wrong_password" ) ) ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; - const BitArchiveReader info( lib, arcFileName.string< tchar >(), testArchive.format(), password ); - REQUIRE( info.hasEncryptedItems() ); - REQUIRE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive ); - REQUIRE_ARCHIVE_TESTS( info ); + SECTION( "BitArchiveReader::isHeaderEncrypted must return true" ){ + REQUIRE( BitArchiveReader::isHeaderEncrypted( lib, inputArchive, testArchive.format() ) ); } - SECTION( "Buffer archive" ) { - REQUIRE_LOAD_FILE( fileBuffer, arcFileName ); - - REQUIRE( BitArchiveReader::isHeaderEncrypted( lib, - fileBuffer, - testArchive.format() ) ); - - REQUIRE( BitArchiveReader::isEncrypted( lib, fileBuffer, testArchive.format() ) ); - - // no password specified! - REQUIRE_THROWS( BitArchiveReader( lib, fileBuffer, testArchive.format() ) ); - - // wrong password specified! - REQUIRE_THROWS( BitArchiveReader( lib, fileBuffer, testArchive.format(), - BIT7Z_STRING( "wrong_password" ) ) ); - - const BitArchiveReader info( lib, fileBuffer, testArchive.format(), password ); - REQUIRE( info.hasEncryptedItems() ); - REQUIRE( info.isEncrypted() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive ); - REQUIRE_ARCHIVE_TESTS( info ); + SECTION( "BitArchiveReader::isEncrypted must return true" ){ + REQUIRE( BitArchiveReader::isEncrypted( lib, inputArchive, testArchive.format() ) ); } - SECTION( "Stream archive" ) { - fs::ifstream fileStream{ arcFileName, std::ios::binary }; - REQUIRE( fileStream.is_open() ); - - REQUIRE( BitArchiveReader::isHeaderEncrypted( lib, - fileStream, - testArchive.format() ) ); - fileStream.clear(); - fileStream.seekg( 0, std::ios::beg ); - REQUIRE( BitArchiveReader::isEncrypted( lib, fileStream, testArchive.format() ) ); - fileStream.clear(); - fileStream.seekg( 0, std::ios::beg ); + SECTION( "Opening the archive with no password should throw an exception" ) { + REQUIRE_THROWS( BitArchiveReader( lib, inputArchive, testArchive.format() ) ); + } - // no password specified! - REQUIRE_THROWS( BitArchiveReader( lib, fileStream, testArchive.format() ) ); - // wrong password specified! - REQUIRE_THROWS( BitArchiveReader( lib, fileStream, testArchive.format(), + SECTION( "Opening the archive with a wrong password should throw an exception" ) { + REQUIRE_THROWS( BitArchiveReader( lib, inputArchive, testArchive.format(), BIT7Z_STRING( "wrong_password" ) ) ); + } - fileStream.clear(); - fileStream.seekg( 0, std::ios::beg ); - REQUIRE( fileStream.is_open() ); - const BitArchiveReader info( lib, fileStream, testArchive.format(), password ); + SECTION( "Opening the archive with the correct password should pass the tests" ) { + const BitArchiveReader info( lib, inputArchive, testArchive.format(), password ); REQUIRE( info.hasEncryptedItems() ); REQUIRE( info.isEncrypted() ); REQUIRE_ARCHIVE_CONTENT( info, testArchive ); @@ -553,7 +446,8 @@ struct EmptyArchive : public TestInputArchive { : TestInputArchive{ std::move( extension ), format, packedSize, empty_content() } {} }; -TEST_CASE( "BitArchiveReader: Reading an empty archive", "[bitarchivereader]" ) { +TEMPLATE_TEST_CASE( "BitArchiveReader: Reading an empty archive", + "[bitarchivereader]", tstring, buffer_t, std::ifstream ) { static const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "empty" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -567,28 +461,15 @@ TEST_CASE( "BitArchiveReader: Reading an empty archive", "[bitarchivereader]" ) DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "empty." + testArchive.extension(); - SECTION( "Filesystem archive" ) { - const BitArchiveReader info( lib, arcFileName.string< tchar >(), testArchive.format() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive ); - REQUIRE_ARCHIVE_TESTS( info ); - } - - SECTION( "Buffer archive" ) { - REQUIRE_LOAD_FILE( fileBuffer, arcFileName ); - - const BitArchiveReader info( lib, fileBuffer, testArchive.format() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive ); - REQUIRE_ARCHIVE_TESTS( info ); - } - - SECTION( "Stream archive" ) { - fs::ifstream fileStream{ arcFileName, std::ios::binary }; - REQUIRE( fileStream.is_open() ); - - const BitArchiveReader info( lib, fileStream, testArchive.format() ); - REQUIRE_ARCHIVE_CONTENT( info, testArchive ); - REQUIRE_ARCHIVE_TESTS( info ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; + const BitArchiveReader info( lib, inputArchive, testArchive.format() ); + if( is_filesystem_archive< TestType >::value ) { + REQUIRE( info.archivePath() == arcFileName ); + } else { + REQUIRE( info.archivePath().empty() ); // No archive path for buffer/streamed archives } + REQUIRE_ARCHIVE_CONTENT( info, testArchive ); + REQUIRE_ARCHIVE_TESTS( info ); } } @@ -668,7 +549,8 @@ TEST_CASE( "BitArchiveReader: Opening RAR archives using the correct RAR format REQUIRE( (first).attributes() == (second).attributes() ); \ } while ( false ) -TEST_CASE( "BitArchiveReader: Checking consistency between items() and iterators", "[bitarchivereader]" ) { +TEMPLATE_TEST_CASE( "BitArchiveReader: Checking consistency between items() and iterators", + "[bitarchivereader]", tstring, buffer_t, std::ifstream ) { static const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "multiple_items" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -685,54 +567,23 @@ TEST_CASE( "BitArchiveReader: Checking consistency between items() and iterators DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "multiple_items." + testArchive.extension(); - SECTION( "Filesystem archive" ) { - const BitArchiveReader info( lib, arcFileName.string< tchar >(), testArchive.format() ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; + const BitArchiveReader info( lib, inputArchive, testArchive.format() ); - const auto archiveItems = info.items(); + const auto archiveItems = info.items(); - REQUIRE( info.begin() == info.cbegin() ); - REQUIRE( info.end() == info.cend() ); + REQUIRE( info.begin() == info.cbegin() ); + REQUIRE( info.end() == info.cend() ); - for ( const auto& iteratedItem : info ) { - const auto& archivedItem = archiveItems[ iteratedItem.index() ]; - REQUIRE_ITEM_EQUAL( archivedItem, iteratedItem ); - } - } - - SECTION( "Buffer archive" ) { - REQUIRE_LOAD_FILE( fileBuffer, arcFileName ); - const BitArchiveReader info( lib, fileBuffer, testArchive.format() ); - - const auto archiveItems = info.items(); - - REQUIRE( info.begin() == info.cbegin() ); - REQUIRE( info.end() == info.cend() ); - - for ( const auto& iteratedItem : info ) { - const auto& archivedItem = archiveItems[ iteratedItem.index() ]; - REQUIRE_ITEM_EQUAL( archivedItem, iteratedItem ); - } - } - - SECTION( "Stream archive" ) { - REQUIRE_OPEN_IFSTREAM( fileStream, arcFileName ); - - const BitArchiveReader info( lib, fileStream, testArchive.format() ); - - const auto archiveItems = info.items(); - - REQUIRE( info.begin() == info.cbegin() ); - REQUIRE( info.end() == info.cend() ); - - for ( const auto& iteratedItem : info ) { - const auto& archivedItem = archiveItems[ iteratedItem.index() ]; - REQUIRE_ITEM_EQUAL( archivedItem, iteratedItem ); - } + for ( const auto& iteratedItem : info ) { + const auto& archivedItem = archiveItems[ iteratedItem.index() ]; + REQUIRE_ITEM_EQUAL( archivedItem, iteratedItem ); } } } -TEST_CASE( "BitArchiveReader: Reading invalid archives", "[bitarchivereader]" ) { +TEMPLATE_TEST_CASE( "BitArchiveReader: Reading invalid archives", + "[bitarchivereader]", tstring, buffer_t, std::ifstream ) { static const TestDirectory testDir{ fs::path{ test_archives_dir } / "testing" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -750,29 +601,14 @@ TEST_CASE( "BitArchiveReader: Reading invalid archives", "[bitarchivereader]" ) DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "ko_test." + testArchive.extension(); - SECTION( "Filesystem archive" ) { - const BitArchiveReader info( lib, arcFileName.string< tchar >(), testArchive.format() ); - REQUIRE_THROWS( info.test() ); - } - - SECTION( "Buffer archive" ) { - REQUIRE_LOAD_FILE( fileBuffer, arcFileName ); - - const BitArchiveReader info( lib, fileBuffer, testArchive.format() ); - REQUIRE_THROWS( info.test() ); - } - - SECTION( "Stream archive" ) { - fs::ifstream fileStream{ arcFileName, std::ios::binary }; - REQUIRE( fileStream.is_open() ); - - const BitArchiveReader info( lib, fileStream, testArchive.format() ); - REQUIRE_THROWS( info.test() ); - } + TestType inputArchive = InputArchiveProxy{ arcFileName }; + const BitArchiveReader info( lib, inputArchive, testArchive.format() ); + REQUIRE_THROWS( info.test() ); } } -TEST_CASE( "BitArchiveReader: Reading archives using the wrong format should throw", "[bitarchivereader]" ) { +TEMPLATE_TEST_CASE( "BitArchiveReader: Reading archives using the wrong format should throw", + "[bitarchivereader]", tstring, buffer_t, std::ifstream ) { static const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "single_file" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -810,22 +646,8 @@ TEST_CASE( "BitArchiveReader: Reading archives using the wrong format should thr if ( correctFormat.extension != wrongFormat.extension ) { DYNAMIC_SECTION( "Wrong format: " << wrongFormat.extension ) { - SECTION( "Filesystem archive" ) { - REQUIRE_THROWS( BitArchiveReader( lib, arcFileName.string< tchar >(), wrongFormat.format ) ); - } - - SECTION( "Buffer archive" ) { - REQUIRE_LOAD_FILE( fileBuffer, arcFileName ); - - REQUIRE_THROWS( BitArchiveReader( lib, fileBuffer, wrongFormat.format ) ); - } - - SECTION( "Stream archive" ) { - fs::ifstream fileStream{ arcFileName, std::ios::binary }; - REQUIRE( fileStream.is_open() ); - - REQUIRE_THROWS( BitArchiveReader( lib, fileStream, wrongFormat.format ) ); - } + TestType inputArchive = InputArchiveProxy{ arcFileName }; + REQUIRE_THROWS( BitArchiveReader( lib, inputArchive, wrongFormat.format ) ); } } } @@ -939,7 +761,8 @@ constexpr auto FILE_ATTRIBUTE_WINDOWS_MASK = 0x07FFF; REQUIRE( iterator->name() == BIT7Z_STRING( item_name ) ); \ } while ( false ) -TEST_CASE( "BitArchiveReader: Correctly reading file type inside archives", "[bitarchivereader]" ) { +TEMPLATE_TEST_CASE( "BitArchiveReader: Correctly reading file type inside archives", + "[bitarchivereader]", tstring, buffer_t, std::ifstream ) { static const TestDirectory testDir{ fs::path{ test_archives_dir } / "metadata" / "file_type" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -954,37 +777,13 @@ TEST_CASE( "BitArchiveReader: Correctly reading file type inside archives", "[bi DYNAMIC_SECTION( "Archive format: " << testFormat.extension ) { const fs::path arcFileName = "file_type." + testFormat.extension; - SECTION( "Filesystem archive" ) { - const BitArchiveReader info( lib, arcFileName.string< tchar >(), testFormat.format ); - REQUIRE_ITEM_DIRECTORY( info, "dir" ); - REQUIRE_ITEM_REGULAR( info, "regular" ); - REQUIRE_ITEM_SYMLINK( info, "symlink" ); - REQUIRE_ITEM_HIDDEN( info, "hidden" ); - REQUIRE_ITEM_READONLY( info, "read_only" ); - } - - SECTION( "Buffer archive" ) { - REQUIRE_LOAD_FILE( fileBuffer, arcFileName ); - - const BitArchiveReader info( lib, fileBuffer, testFormat.format ); - REQUIRE_ITEM_DIRECTORY( info, "dir" ); - REQUIRE_ITEM_REGULAR( info, "regular" ); - REQUIRE_ITEM_SYMLINK( info, "symlink" ); - REQUIRE_ITEM_HIDDEN( info, "hidden" ); - REQUIRE_ITEM_READONLY( info, "read_only" ); - } - - SECTION( "Stream archive" ) { - fs::ifstream fileStream{ arcFileName, std::ios::binary }; - REQUIRE( fileStream.is_open() ); - - const BitArchiveReader info( lib, fileStream, testFormat.format ); - REQUIRE_ITEM_DIRECTORY( info, "dir" ); - REQUIRE_ITEM_REGULAR( info, "regular" ); - REQUIRE_ITEM_SYMLINK( info, "symlink" ); - REQUIRE_ITEM_HIDDEN( info, "hidden" ); - REQUIRE_ITEM_READONLY( info, "read_only" ); - } + TestType inputArchive = InputArchiveProxy{ arcFileName }; + const BitArchiveReader info( lib, inputArchive, testFormat.format ); + REQUIRE_ITEM_DIRECTORY( info, "dir" ); + REQUIRE_ITEM_REGULAR( info, "regular" ); + REQUIRE_ITEM_SYMLINK( info, "symlink" ); + REQUIRE_ITEM_HIDDEN( info, "hidden" ); + REQUIRE_ITEM_READONLY( info, "read_only" ); } } @@ -1009,7 +808,8 @@ TEST_CASE( "BitArchiveReader: Correctly reading file type inside archives", "[bi REQUIRE( iterator->name() == BIT7Z_STRING( item_name ) ); \ } while ( false ) -TEST_CASE( "BitArchiveReader: Correctly reading archive items with Unicode names", "[bitarchivereader]" ) { +TEMPLATE_TEST_CASE( "BitArchiveReader: Correctly reading archive items with Unicode names", + "[bitarchivereader]", tstring, buffer_t, std::ifstream ) { static const TestDirectory testDir{ fs::path{ test_archives_dir } / "metadata" / "unicode" }; const Bit7zLibrary lib{ test::sevenzip_lib_path() }; @@ -1024,71 +824,29 @@ TEST_CASE( "BitArchiveReader: Correctly reading archive items with Unicode names DYNAMIC_SECTION( "Archive format: " << testFormat.extension ) { const fs::path arcFileName = "unicode." + testFormat.extension; - SECTION( "Filesystem archive" ) { - const BitArchiveReader info( lib, arcFileName.string< tchar >(), testFormat.format ); - REQUIRE_ITEM_UNICODE( info, "¡Porque sí!.doc" ); - REQUIRE_ITEM_UNICODE( info, "σύννεφα.jpg" ); - REQUIRE_ITEM_UNICODE( info, "юнікод.svg" ); - REQUIRE_ITEM_UNICODE( info, "ユニコード.pdf" ); - } - - SECTION( "Buffer archive" ) { - REQUIRE_LOAD_FILE( fileBuffer, arcFileName ); - - const BitArchiveReader info( lib, fileBuffer, testFormat.format ); - REQUIRE_ITEM_UNICODE( info, "¡Porque sí!.doc" ); - REQUIRE_ITEM_UNICODE( info, "σύννεφα.jpg" ); - REQUIRE_ITEM_UNICODE( info, "юнікод.svg" ); - REQUIRE_ITEM_UNICODE( info, "ユニコード.pdf" ); - } - - SECTION( "Stream archive" ) { - fs::ifstream fileStream{ arcFileName, std::ios::binary }; - REQUIRE( fileStream.is_open() ); - - const BitArchiveReader info( lib, fileStream, testFormat.format ); - REQUIRE_ITEM_UNICODE( info, "¡Porque sí!.doc" ); - REQUIRE_ITEM_UNICODE( info, "σύννεφα.jpg" ); - REQUIRE_ITEM_UNICODE( info, "юнікод.svg" ); - REQUIRE_ITEM_UNICODE( info, "ユニコード.pdf" ); - } - } -} - -TEST_CASE( "BitArchiveReader: Reading an archive with a Unicode file name", "[bitarchivereader]" ) { - static const TestDirectory testDir{ fs::path{ test_archives_dir } / "metadata" / "unicode" }; - - const Bit7zLibrary lib{ test::sevenzip_lib_path() }; - - fs::path arcFileName{ BIT7Z_NATIVE_STRING( "αρχείο.7z" ) }; - - SECTION( "Filesystem archive" ) { - const BitArchiveReader info( lib, path_to_tstring( arcFileName ), BitFormat::SevenZip ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; + const BitArchiveReader info( lib, inputArchive, testFormat.format ); REQUIRE_ITEM_UNICODE( info, "¡Porque sí!.doc" ); REQUIRE_ITEM_UNICODE( info, "σύννεφα.jpg" ); REQUIRE_ITEM_UNICODE( info, "юнікод.svg" ); REQUIRE_ITEM_UNICODE( info, "ユニコード.pdf" ); } +} - SECTION( "Buffer archive" ) { - REQUIRE_LOAD_FILE( fileBuffer, arcFileName ); +TEMPLATE_TEST_CASE( "BitArchiveReader: Reading an archive with a Unicode file name", + "[bitarchivereader]", tstring, buffer_t, std::ifstream ) { + static const TestDirectory testDir{ fs::path{ test_archives_dir } / "metadata" / "unicode" }; - const BitArchiveReader info( lib, fileBuffer, BitFormat::SevenZip ); - REQUIRE_ITEM_UNICODE( info, "¡Porque sí!.doc" ); - REQUIRE_ITEM_UNICODE( info, "σύννεφα.jpg" ); - REQUIRE_ITEM_UNICODE( info, "юнікод.svg" ); - REQUIRE_ITEM_UNICODE( info, "ユニコード.pdf" ); - } + const Bit7zLibrary lib{ test::sevenzip_lib_path() }; - SECTION( "Stream archive" ) { - REQUIRE_OPEN_IFSTREAM( fileStream, fs::path{ arcFileName } ); + fs::path arcFileName{ BIT7Z_NATIVE_STRING( "αρχείο.7z" ) }; - const BitArchiveReader info( lib, fileStream, BitFormat::SevenZip ); - REQUIRE_ITEM_UNICODE( info, "¡Porque sí!.doc" ); - REQUIRE_ITEM_UNICODE( info, "σύννεφα.jpg" ); - REQUIRE_ITEM_UNICODE( info, "юнікод.svg" ); - REQUIRE_ITEM_UNICODE( info, "ユニコード.pdf" ); - } + TestType inputArchive = InputArchiveProxy{ arcFileName }; + const BitArchiveReader info( lib, inputArchive, BitFormat::SevenZip ); + REQUIRE_ITEM_UNICODE( info, "¡Porque sí!.doc" ); + REQUIRE_ITEM_UNICODE( info, "σύννεφα.jpg" ); + REQUIRE_ITEM_UNICODE( info, "юнікод.svg" ); + REQUIRE_ITEM_UNICODE( info, "ユニコード.pdf" ); } TEST_CASE( "BitArchiveReader: Reading an archive with a Unicode file name (bzip2)", "[bitarchivereader]" ) { @@ -1201,8 +959,9 @@ TEST_CASE( "BitArchiveReader: Format detection of archives", "[bitarchivereader] } SECTION( "Archive stream (signature) from a file" ) { - std::ifstream stream{ "valid." + test.extension, std::ios::binary }; - const BitArchiveReader reader{ lib, stream }; + REQUIRE_OPEN_IFSTREAM( fileStream, "valid." + test.extension ); + + const BitArchiveReader reader{ lib, fileStream }; REQUIRE( reader.detectedFormat() == test.format ); // TODO: Verify why testing of Mslz and multi-volume RAR archives fails From c46eb52eb9e58d4fb50754131f7e369f9fc2dd6a Mon Sep 17 00:00:00 2001 From: Oz Date: Mon, 9 Oct 2023 22:27:25 +0200 Subject: [PATCH 10/18] Fix build on GCC 5 --- include/bit7z/bitarchivereader.hpp | 2 +- include/bit7z/bitdefines.hpp | 10 ++++++++++ include/bit7z/bitinputarchive.hpp | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/bit7z/bitarchivereader.hpp b/include/bit7z/bitarchivereader.hpp index 3a070e98..dcc7fe2a 100644 --- a/include/bit7z/bitarchivereader.hpp +++ b/include/bit7z/bitarchivereader.hpp @@ -207,7 +207,7 @@ class BitArchiveReader final : public BitAbstractArchiveOpener, public BitInputA static auto isOpenEncryptedError( std::error_code error ) -> bool; }; -using BitArchiveInfo BIT7Z_MAYBE_UNUSED BIT7Z_DEPRECATED_MSG("Since v4.0; please use BitArchiveReader.") = BitArchiveReader; +BIT7Z_DEPRECATED_TYPEDEF( BitArchiveInfo, BitArchiveReader, "Since v4.0; please use BitArchiveReader." ); } // namespace bit7z diff --git a/include/bit7z/bitdefines.hpp b/include/bit7z/bitdefines.hpp index 94994bcf..4e22bad5 100644 --- a/include/bit7z/bitdefines.hpp +++ b/include/bit7z/bitdefines.hpp @@ -105,4 +105,14 @@ # endif #endif +#ifndef BIT7Z_DEPRECATED_TYPEDEF +# if defined( __GNUC__ ) && !defined( __clang__ ) && __GNUC__ < 7 +# define BIT7Z_DEPRECATED_TYPEDEF( alias_name, alias_value, msg ) \ + using alias_name BIT7Z_MAYBE_UNUSED __attribute__(( __deprecated__( msg ) )) = alias_value +# else +# define BIT7Z_DEPRECATED_TYPEDEF( alias_name, alias_value, msg ) \ + using alias_name BIT7Z_MAYBE_UNUSED BIT7Z_DEPRECATED_MSG( msg ) = alias_value +# endif +#endif + #endif //BITDEFINES_HPP diff --git a/include/bit7z/bitinputarchive.hpp b/include/bit7z/bitinputarchive.hpp index cb2526b3..0f6883b1 100644 --- a/include/bit7z/bitinputarchive.hpp +++ b/include/bit7z/bitinputarchive.hpp @@ -337,7 +337,7 @@ class BitInputArchive { friend class BitInputArchive; }; - using const_iterator BIT7Z_MAYBE_UNUSED BIT7Z_DEPRECATED_MSG("Use ConstIterator") = ConstIterator; + BIT7Z_DEPRECATED_TYPEDEF( const_iterator, ConstIterator, "Use ConstIterator" ); /** * @return an iterator to the first element of the archive; if the archive is empty, From 2e2e736378299c00e4826bfdc9a3bed7faf85031 Mon Sep 17 00:00:00 2001 From: Oz Date: Mon, 9 Oct 2023 22:27:40 +0200 Subject: [PATCH 11/18] Fix dateutil tests on GCC 5 --- tests/src/test_dateutil.cpp | 48 ++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/tests/src/test_dateutil.cpp b/tests/src/test_dateutil.cpp index 83c9c91f..4e259545 100644 --- a/tests/src/test_dateutil.cpp +++ b/tests/src/test_dateutil.cpp @@ -21,23 +21,23 @@ using namespace bit7z; * We do not expect the conversion function to check whether the input is correct, * so we don't perform tests with wrong inputs. */ +struct DateConversionTest { + const char* name; + std::time_t dateTime; + FILETIME fileTime; +}; + #ifndef _WIN32 TEST_CASE( "fsutil: Date conversion from std::time_t to FILETIME", "[fsutil][date functions]" ) { - auto testDate = GENERATE( table< const char*, std::time_t, FILETIME >( - { - { "21 December 2012, 12:00", 1356091200, { 3017121792, 30269298 } }, - { "1 January 1970, 00:00", 0, { 3577643008, 27111902 } } - } - ) ); - - DYNAMIC_SECTION( "Date: " << std::get< 0 >( testDate ) ) { - const std::time_t input = std::get< 1 >( testDate ); - const FILETIME expectedOutput = std::get< 2 >( testDate ); - - auto output = time_to_FILETIME( input ); - REQUIRE( output.dwHighDateTime == expectedOutput.dwHighDateTime ); - REQUIRE( output.dwLowDateTime == expectedOutput.dwLowDateTime ); + auto testDate = GENERATE( as< DateConversionTest >(), + DateConversionTest{ "21 December 2012, 12:00", 1356091200, { 3017121792, 30269298 } }, + DateConversionTest{ "1 January 1970, 00:00", 0, { 3577643008, 27111902 } } ); + + DYNAMIC_SECTION( "Date: " << testDate.name ) { + auto output = time_to_FILETIME( testDate.dateTime ); + REQUIRE( output.dwHighDateTime == testDate.fileTime.dwHighDateTime ); + REQUIRE( output.dwLowDateTime == testDate.fileTime.dwLowDateTime ); } } @@ -47,32 +47,26 @@ TEST_CASE( "fsutil: Date conversion from FILETIME to time types", "[fsutil][date using namespace std::chrono; using std::chrono::seconds; - auto testDate = GENERATE( table< const char*, FILETIME, std::time_t >( - { - { "21 December 2012, 12:00", { 3017121792, 30269298 }, 1356091200 }, - { "1 January 1970, 00:00", { 3577643008, 27111902 }, 0 } - } - ) ); - - DYNAMIC_SECTION( "Date: " << std::get< 0 >( testDate ) ) { - const FILETIME input = std::get< 1 >( testDate ); - const std::time_t expectedOutput = std::get< 2 >( testDate ); + auto testDate = GENERATE( as< DateConversionTest >(), + DateConversionTest{ "21 December 2012, 12:00", 1356091200, { 3017121792, 30269298 } }, + DateConversionTest{ "1 January 1970, 00:00", 0, { 3577643008, 27111902 } } ); + DYNAMIC_SECTION( "Date: " << testDate.name ) { std::time_t output{}; #ifndef _WIN32 SECTION( "FILETIME to std::filesystem::file_time_type" ) { - auto result = FILETIME_to_file_time_type( input ); + auto result = FILETIME_to_file_time_type( testDate.fileTime ); output = std::time_t{ duration_cast< seconds >( result.time_since_epoch() ).count() }; } #endif SECTION( "FILETIME to bit7z::time_type" ) { - auto result = FILETIME_to_time_type( input ); + auto result = FILETIME_to_time_type( testDate.fileTime ); output = bit7z::time_type::clock::to_time_t( result ); } - REQUIRE( output == expectedOutput ); + REQUIRE( output == testDate.dateTime ); } } #endif \ No newline at end of file From 761bbadafa9a11068470372c071207522fab1b9b Mon Sep 17 00:00:00 2001 From: Oz Date: Tue, 10 Oct 2023 18:13:03 +0200 Subject: [PATCH 12/18] Improve tests of dateutil --- tests/src/test_dateutil.cpp | 46 ++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/tests/src/test_dateutil.cpp b/tests/src/test_dateutil.cpp index 4e259545..1519fe7e 100644 --- a/tests/src/test_dateutil.cpp +++ b/tests/src/test_dateutil.cpp @@ -27,23 +27,7 @@ struct DateConversionTest { FILETIME fileTime; }; -#ifndef _WIN32 - -TEST_CASE( "fsutil: Date conversion from std::time_t to FILETIME", "[fsutil][date functions]" ) { - auto testDate = GENERATE( as< DateConversionTest >(), - DateConversionTest{ "21 December 2012, 12:00", 1356091200, { 3017121792, 30269298 } }, - DateConversionTest{ "1 January 1970, 00:00", 0, { 3577643008, 27111902 } } ); - - DYNAMIC_SECTION( "Date: " << testDate.name ) { - auto output = time_to_FILETIME( testDate.dateTime ); - REQUIRE( output.dwHighDateTime == testDate.fileTime.dwHighDateTime ); - REQUIRE( output.dwLowDateTime == testDate.fileTime.dwLowDateTime ); - } -} - -#endif - -TEST_CASE( "fsutil: Date conversion from FILETIME to time types", "[fsutil][date functions]" ) { +TEST_CASE( "fsutil: Date conversions", "[fsutil][date functions]" ) { using namespace std::chrono; using std::chrono::seconds; @@ -52,21 +36,31 @@ TEST_CASE( "fsutil: Date conversion from FILETIME to time types", "[fsutil][date DateConversionTest{ "1 January 1970, 00:00", 0, { 3577643008, 27111902 } } ); DYNAMIC_SECTION( "Date: " << testDate.name ) { - std::time_t output{}; #ifndef _WIN32 - SECTION( "FILETIME to std::filesystem::file_time_type" ) { - auto result = FILETIME_to_file_time_type( testDate.fileTime ); - output = std::time_t{ duration_cast< seconds >( result.time_since_epoch() ).count() }; + SECTION( "From std::time_t to FILETIME" ) { + auto output = time_to_FILETIME( testDate.dateTime ); + REQUIRE( output.dwHighDateTime == testDate.fileTime.dwHighDateTime ); + REQUIRE( output.dwLowDateTime == testDate.fileTime.dwLowDateTime ); } #endif - SECTION( "FILETIME to bit7z::time_type" ) { - auto result = FILETIME_to_time_type( testDate.fileTime ); - output = bit7z::time_type::clock::to_time_t( result ); - } + SECTION( "From FILETIME...") { + std::time_t output{}; +#ifndef _WIN32 + SECTION( "...to std::filesystem::file_time_type" ) { + auto result = FILETIME_to_file_time_type( testDate.fileTime ); + output = std::time_t{ duration_cast< seconds >( result.time_since_epoch() ).count() }; + } +#endif - REQUIRE( output == testDate.dateTime ); + SECTION( "...to bit7z::time_type" ) { + auto result = FILETIME_to_time_type( testDate.fileTime ); + output = bit7z::time_type::clock::to_time_t( result ); + } + + REQUIRE( output == testDate.dateTime ); + } } } #endif \ No newline at end of file From 023387505c5bc9e4431fa07c0ad056501e0656c4 Mon Sep 17 00:00:00 2001 From: Oz Date: Tue, 10 Oct 2023 18:13:44 +0200 Subject: [PATCH 13/18] Improve tests of BitArchiveReader --- tests/src/test_bitarchivereader.cpp | 60 +++++++++++++---------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/tests/src/test_bitarchivereader.cpp b/tests/src/test_bitarchivereader.cpp index d578054f..23330122 100644 --- a/tests/src/test_bitarchivereader.cpp +++ b/tests/src/test_bitarchivereader.cpp @@ -139,33 +139,27 @@ struct SingleFileArchive : public TestInputArchive { : TestInputArchive{ std::move( extension ), format, packedSize, single_file_content() } {} }; -class InputArchiveProxy { - const fs::path& mPath; - public: - InputArchiveProxy( const fs::path& path ) : mPath{ path } {} +template< typename T > +auto getInputArchive( const fs::path& path ) -> T = delete; - operator tstring() const { - return path_to_tstring( mPath ); - } +template<> +inline auto getInputArchive< tstring >( const fs::path& path ) -> tstring { + return path_to_tstring( path ); +} - operator buffer_t() const { - return load_file( mPath ); - } +template<> +inline auto getInputArchive< buffer_t >( const fs::path& path ) -> buffer_t { + return load_file( path ); +} - operator std::ifstream() const { - return std::ifstream{ mPath, std::ios::binary }; - } -}; +template<> +inline auto getInputArchive< std::ifstream >( const fs::path& path ) -> std::ifstream { + return fs::ifstream{ path, std::ios::binary }; +} -template +template< typename T > using is_filesystem_archive = std::is_same< bit7z::tstring, std::decay_t< T > >; -template -using is_buffer_archive = std::is_same< bit7z::buffer_t, std::decay_t< T > >; - -template -using is_stream_archive = std::is_same< std::ifstream, std::decay_t< T > >; - TEMPLATE_TEST_CASE( "BitArchiveReader: Reading archives containing only a single file", "[bitarchivereader]", tstring, buffer_t, std::ifstream ) { static const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "single_file" }; @@ -189,7 +183,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Reading archives containing only a single DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const auto arcFileName = fs::path{ clouds.name }.concat( "." + testArchive.extension() ); - TestType inputArchive = InputArchiveProxy{ arcFileName }; + TestType inputArchive = getInputArchive< TestType >( arcFileName ); const BitArchiveReader info( lib, inputArchive, testArchive.format() ); if( is_filesystem_archive< TestType >::value ) { REQUIRE( info.archivePath() == arcFileName ); @@ -225,7 +219,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Reading archives containing multiple file DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "multiple_files." + testArchive.extension(); - TestType inputArchive = InputArchiveProxy{ arcFileName }; + TestType inputArchive = getInputArchive< TestType >( arcFileName ); const BitArchiveReader info( lib, inputArchive, testArchive.format() ); if( is_filesystem_archive< TestType >::value ) { REQUIRE( info.archivePath() == arcFileName ); @@ -262,7 +256,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Reading archives containing multiple item DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "multiple_items." + testArchive.extension(); - TestType inputArchive = InputArchiveProxy{ arcFileName }; + TestType inputArchive = getInputArchive< TestType >( arcFileName ); const BitArchiveReader info( lib, inputArchive, testArchive.format() ); if( is_filesystem_archive< TestType >::value ) { REQUIRE( info.archivePath() == arcFileName ); @@ -299,7 +293,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Reading archives containing encrypted ite DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "encrypted." + testArchive.extension(); - TestType inputArchive = InputArchiveProxy{ arcFileName }; + TestType inputArchive = getInputArchive< TestType >( arcFileName ); SECTION( "BitArchiveReader::isHeaderEncrypted must return false" ){ REQUIRE_FALSE( BitArchiveReader::isHeaderEncrypted( lib, inputArchive, testArchive.format() ) ); @@ -344,7 +338,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Reading header-encrypted archives", DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "header_encrypted." + testArchive.extension(); - TestType inputArchive = InputArchiveProxy{ arcFileName }; + TestType inputArchive = getInputArchive< TestType >( arcFileName ); SECTION( "BitArchiveReader::isHeaderEncrypted must return true" ){ REQUIRE( BitArchiveReader::isHeaderEncrypted( lib, inputArchive, testArchive.format() ) ); @@ -461,7 +455,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Reading an empty archive", DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "empty." + testArchive.extension(); - TestType inputArchive = InputArchiveProxy{ arcFileName }; + TestType inputArchive = getInputArchive< TestType >( arcFileName ); const BitArchiveReader info( lib, inputArchive, testArchive.format() ); if( is_filesystem_archive< TestType >::value ) { REQUIRE( info.archivePath() == arcFileName ); @@ -567,7 +561,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Checking consistency between items() and DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "multiple_items." + testArchive.extension(); - TestType inputArchive = InputArchiveProxy{ arcFileName }; + TestType inputArchive = getInputArchive< TestType >( arcFileName ); const BitArchiveReader info( lib, inputArchive, testArchive.format() ); const auto archiveItems = info.items(); @@ -601,7 +595,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Reading invalid archives", DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "ko_test." + testArchive.extension(); - TestType inputArchive = InputArchiveProxy{ arcFileName }; + TestType inputArchive = getInputArchive< TestType >( arcFileName ); const BitArchiveReader info( lib, inputArchive, testArchive.format() ); REQUIRE_THROWS( info.test() ); } @@ -646,7 +640,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Reading archives using the wrong format s if ( correctFormat.extension != wrongFormat.extension ) { DYNAMIC_SECTION( "Wrong format: " << wrongFormat.extension ) { - TestType inputArchive = InputArchiveProxy{ arcFileName }; + TestType inputArchive = getInputArchive< TestType >( arcFileName ); REQUIRE_THROWS( BitArchiveReader( lib, inputArchive, wrongFormat.format ) ); } } @@ -777,7 +771,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Correctly reading file type inside archiv DYNAMIC_SECTION( "Archive format: " << testFormat.extension ) { const fs::path arcFileName = "file_type." + testFormat.extension; - TestType inputArchive = InputArchiveProxy{ arcFileName }; + TestType inputArchive = getInputArchive< TestType >( arcFileName ); const BitArchiveReader info( lib, inputArchive, testFormat.format ); REQUIRE_ITEM_DIRECTORY( info, "dir" ); REQUIRE_ITEM_REGULAR( info, "regular" ); @@ -824,7 +818,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Correctly reading archive items with Unic DYNAMIC_SECTION( "Archive format: " << testFormat.extension ) { const fs::path arcFileName = "unicode." + testFormat.extension; - TestType inputArchive = InputArchiveProxy{ arcFileName }; + TestType inputArchive = getInputArchive< TestType >( arcFileName ); const BitArchiveReader info( lib, inputArchive, testFormat.format ); REQUIRE_ITEM_UNICODE( info, "¡Porque sí!.doc" ); REQUIRE_ITEM_UNICODE( info, "σύννεφα.jpg" ); @@ -841,7 +835,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Reading an archive with a Unicode file na fs::path arcFileName{ BIT7Z_NATIVE_STRING( "αρχείο.7z" ) }; - TestType inputArchive = InputArchiveProxy{ arcFileName }; + TestType inputArchive = getInputArchive< TestType >( arcFileName ); const BitArchiveReader info( lib, inputArchive, BitFormat::SevenZip ); REQUIRE_ITEM_UNICODE( info, "¡Porque sí!.doc" ); REQUIRE_ITEM_UNICODE( info, "σύννεφα.jpg" ); From 5b8d824e1dedca0d1fa2bf3c34a47eacac05680d Mon Sep 17 00:00:00 2001 From: Oz Date: Tue, 10 Oct 2023 21:14:50 +0200 Subject: [PATCH 14/18] Improve tests of string conversion functions --- tests/src/test_util.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/tests/src/test_util.cpp b/tests/src/test_util.cpp index c01215f9..6ac59c78 100644 --- a/tests/src/test_util.cpp +++ b/tests/src/test_util.cpp @@ -21,12 +21,13 @@ TEST_CASE( "util: Narrowing wide string to std::string", "[util][narrow]" ) { using bit7z::narrow; - REQUIRE( narrow( nullptr, 0 ).empty() ); - REQUIRE( narrow( nullptr, 42 ).empty() ); - REQUIRE( narrow( L"", 0 ).empty() ); + SECTION( "Converting from nullptr C wide string" ) { + REQUIRE( narrow( nullptr, 0 ).empty() ); + REQUIRE( narrow( nullptr, 42 ).empty() ); + } - const wchar_t* testInput = nullptr; - const char* testOutput = nullptr; + std::wstring testInput; + std::string testOutput; std::tie( testInput, testOutput ) = GENERATE( table< const wchar_t*, const char* >( { NARROWING_TEST_STR( "" ), @@ -38,7 +39,9 @@ TEST_CASE( "util: Narrowing wide string to std::string", "[util][narrow]" ) { } ) ); - REQUIRE( narrow( testInput, wcsnlen( testInput, 128 ) ) == testOutput ); + DYNAMIC_SECTION( "Converting L\"" << testOutput << "\" to narrow string" ) { + REQUIRE( narrow( testInput.c_str(), testInput.size() ) == testOutput ); + } } #define WIDENING_TEST_STR( str ) std::make_tuple( (str), L##str ) @@ -47,8 +50,8 @@ TEST_CASE( "util: Widening narrow string to std::wstring", "[util][widen]" ) { using bit7z::widen; using std::make_tuple; - const char* testInput = nullptr; - const wchar_t* testOutput = nullptr; + std::string testInput; + std::wstring testOutput; std::tie( testInput, testOutput ) = GENERATE( table< const char*, const wchar_t* >( { WIDENING_TEST_STR( "" ), @@ -60,7 +63,9 @@ TEST_CASE( "util: Widening narrow string to std::wstring", "[util][widen]" ) { } ) ); - REQUIRE( widen( testInput ) == testOutput ); + DYNAMIC_SECTION( "Converting \"" << testInput << "\" to wide string" ) { + REQUIRE( widen( testInput ) == testOutput ); + } } #endif From 602b0f61637a03d15acb6179df8ad518de3f2a99 Mon Sep 17 00:00:00 2001 From: Oz Date: Wed, 11 Oct 2023 19:38:46 +0200 Subject: [PATCH 15/18] Fix bit7z::narrow on Windows and add more tests for it Closes issue #172 --- src/internal/util.cpp | 8 +++---- tests/src/test_util.cpp | 50 +++++++++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/internal/util.cpp b/src/internal/util.cpp index 000e52c8..761136a3 100644 --- a/src/internal/util.cpp +++ b/src/internal/util.cpp @@ -17,10 +17,10 @@ #ifdef _WIN32 #ifdef BIT7Z_USE_SYSTEM_CODEPAGE #define CODEPAGE CP_ACP -#define CODEPAGE_FLAGS 0 +#define CODEPAGE_WC_FLAGS WC_NO_BEST_FIT_CHARS #else #define CODEPAGE CP_UTF8 -#define CODEPAGE_FLAGS WC_NO_BEST_FIT_CHARS +#define CODEPAGE_WC_FLAGS 0 #endif #else #ifndef BIT7Z_USE_STANDARD_FILESYSTEM @@ -46,7 +46,7 @@ auto narrow( const wchar_t* wideString, size_t size ) -> std::string { } #ifdef _WIN32 const int narrowStringSize = WideCharToMultiByte( CODEPAGE, - CODEPAGE_FLAGS, + CODEPAGE_WC_FLAGS, wideString, static_cast< int >( size ), nullptr, @@ -59,7 +59,7 @@ auto narrow( const wchar_t* wideString, size_t size ) -> std::string { std::string result( narrowStringSize, 0 ); WideCharToMultiByte( CODEPAGE, - CODEPAGE_FLAGS, + CODEPAGE_WC_FLAGS, wideString, -1, &result[ 0 ], // NOLINT(readability-container-data-pointer) diff --git a/tests/src/test_util.cpp b/tests/src/test_util.cpp index 6ac59c78..7bd44878 100644 --- a/tests/src/test_util.cpp +++ b/tests/src/test_util.cpp @@ -26,21 +26,43 @@ TEST_CASE( "util: Narrowing wide string to std::string", "[util][narrow]" ) { REQUIRE( narrow( nullptr, 42 ).empty() ); } - std::wstring testInput; - std::string testOutput; - std::tie( testInput, testOutput ) = GENERATE( table< const wchar_t*, const char* >( - { - NARROWING_TEST_STR( "" ), - NARROWING_TEST_STR( "h" ), - NARROWING_TEST_STR( "hello world!" ), - NARROWING_TEST_STR( "supercalifragilistichespiralidoso" ), - NARROWING_TEST_STR( "perché" ), - NARROWING_TEST_STR( "\u30e1\u30bf\u30eb\u30ac\u30eb\u30eb\u30e2\u30f3" ) // メタルガルルモン - } - ) ); +#if !defined( _LIBCPP_VERSION ) + SECTION( "Converting wide strings with unencodable UTF-8 chars" ) { + std::wstring testInput = L"\xDC80"; + std::string testOutput = narrow( testInput.c_str(), testInput.size() ); +#if defined( _MSC_VER ) || !defined( BIT7Z_USE_STANDARD_FILESYSTEM ) + REQUIRE( testOutput == "\uFFFD" ); +#else + REQUIRE( testOutput == "\xED\xB2\x80" ); +#endif - DYNAMIC_SECTION( "Converting L\"" << testOutput << "\" to narrow string" ) { - REQUIRE( narrow( testInput.c_str(), testInput.size() ) == testOutput ); + testInput = L"\xD843"; + testOutput = narrow( testInput.c_str(), testInput.size() ); +#if defined( _MSC_VER ) || !defined( BIT7Z_USE_STANDARD_FILESYSTEM ) + REQUIRE( testOutput == "\uFFFD" ); +#else + REQUIRE( testOutput == "\xED\xA1\x83" ); +#endif + } +#endif + + SECTION( "Converting wide strings without unencodable UTF-8 characters" ) { + std::wstring testInput; + std::string testOutput; + std::tie( testInput, testOutput ) = GENERATE( table< const wchar_t*, const char* >( + { + NARROWING_TEST_STR( "" ), + NARROWING_TEST_STR( "h" ), + NARROWING_TEST_STR( "hello world!" ), + NARROWING_TEST_STR( "supercalifragilistichespiralidoso" ), + NARROWING_TEST_STR( "perché" ), + NARROWING_TEST_STR( "\u30e1\u30bf\u30eb\u30ac\u30eb\u30eb\u30e2\u30f3" ) // メタルガルルモン + } + ) ); + + DYNAMIC_SECTION( "Converting L\"" << testOutput << "\" to narrow string" ) { + REQUIRE( narrow( testInput.c_str(), testInput.size() ) == testOutput ); + } } } From a20d5a171f025705eb85056714601d1ba22e22ac Mon Sep 17 00:00:00 2001 From: Oz Date: Wed, 11 Oct 2023 19:43:44 +0200 Subject: [PATCH 16/18] Fix building tests on old versions of Clang --- tests/src/test_bitarchivereader.cpp | 52 ++++++++++++++--------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/tests/src/test_bitarchivereader.cpp b/tests/src/test_bitarchivereader.cpp index 23330122..37067836 100644 --- a/tests/src/test_bitarchivereader.cpp +++ b/tests/src/test_bitarchivereader.cpp @@ -139,23 +139,23 @@ struct SingleFileArchive : public TestInputArchive { : TestInputArchive{ std::move( extension ), format, packedSize, single_file_content() } {} }; -template< typename T > -auto getInputArchive( const fs::path& path ) -> T = delete; +class InputArchiveProxy { + const fs::path& mPath; + public: + InputArchiveProxy( const fs::path& path ) : mPath{ path } {} -template<> -inline auto getInputArchive< tstring >( const fs::path& path ) -> tstring { - return path_to_tstring( path ); -} + operator tstring() const { + return path_to_tstring( mPath ); + } -template<> -inline auto getInputArchive< buffer_t >( const fs::path& path ) -> buffer_t { - return load_file( path ); -} + operator buffer_t() const { + return load_file( mPath ); + } -template<> -inline auto getInputArchive< std::ifstream >( const fs::path& path ) -> std::ifstream { - return fs::ifstream{ path, std::ios::binary }; -} + operator std::ifstream() const { + return std::ifstream{ mPath, std::ios::binary }; + } +}; template< typename T > using is_filesystem_archive = std::is_same< bit7z::tstring, std::decay_t< T > >; @@ -183,7 +183,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Reading archives containing only a single DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const auto arcFileName = fs::path{ clouds.name }.concat( "." + testArchive.extension() ); - TestType inputArchive = getInputArchive< TestType >( arcFileName ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; const BitArchiveReader info( lib, inputArchive, testArchive.format() ); if( is_filesystem_archive< TestType >::value ) { REQUIRE( info.archivePath() == arcFileName ); @@ -219,7 +219,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Reading archives containing multiple file DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "multiple_files." + testArchive.extension(); - TestType inputArchive = getInputArchive< TestType >( arcFileName ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; const BitArchiveReader info( lib, inputArchive, testArchive.format() ); if( is_filesystem_archive< TestType >::value ) { REQUIRE( info.archivePath() == arcFileName ); @@ -256,7 +256,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Reading archives containing multiple item DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "multiple_items." + testArchive.extension(); - TestType inputArchive = getInputArchive< TestType >( arcFileName ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; const BitArchiveReader info( lib, inputArchive, testArchive.format() ); if( is_filesystem_archive< TestType >::value ) { REQUIRE( info.archivePath() == arcFileName ); @@ -293,7 +293,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Reading archives containing encrypted ite DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "encrypted." + testArchive.extension(); - TestType inputArchive = getInputArchive< TestType >( arcFileName ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; SECTION( "BitArchiveReader::isHeaderEncrypted must return false" ){ REQUIRE_FALSE( BitArchiveReader::isHeaderEncrypted( lib, inputArchive, testArchive.format() ) ); @@ -338,7 +338,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Reading header-encrypted archives", DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "header_encrypted." + testArchive.extension(); - TestType inputArchive = getInputArchive< TestType >( arcFileName ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; SECTION( "BitArchiveReader::isHeaderEncrypted must return true" ){ REQUIRE( BitArchiveReader::isHeaderEncrypted( lib, inputArchive, testArchive.format() ) ); @@ -455,7 +455,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Reading an empty archive", DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "empty." + testArchive.extension(); - TestType inputArchive = getInputArchive< TestType >( arcFileName ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; const BitArchiveReader info( lib, inputArchive, testArchive.format() ); if( is_filesystem_archive< TestType >::value ) { REQUIRE( info.archivePath() == arcFileName ); @@ -561,7 +561,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Checking consistency between items() and DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "multiple_items." + testArchive.extension(); - TestType inputArchive = getInputArchive< TestType >( arcFileName ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; const BitArchiveReader info( lib, inputArchive, testArchive.format() ); const auto archiveItems = info.items(); @@ -595,7 +595,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Reading invalid archives", DYNAMIC_SECTION( "Archive format: " << testArchive.extension() ) { const fs::path arcFileName = "ko_test." + testArchive.extension(); - TestType inputArchive = getInputArchive< TestType >( arcFileName ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; const BitArchiveReader info( lib, inputArchive, testArchive.format() ); REQUIRE_THROWS( info.test() ); } @@ -640,7 +640,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Reading archives using the wrong format s if ( correctFormat.extension != wrongFormat.extension ) { DYNAMIC_SECTION( "Wrong format: " << wrongFormat.extension ) { - TestType inputArchive = getInputArchive< TestType >( arcFileName ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; REQUIRE_THROWS( BitArchiveReader( lib, inputArchive, wrongFormat.format ) ); } } @@ -771,7 +771,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Correctly reading file type inside archiv DYNAMIC_SECTION( "Archive format: " << testFormat.extension ) { const fs::path arcFileName = "file_type." + testFormat.extension; - TestType inputArchive = getInputArchive< TestType >( arcFileName ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; const BitArchiveReader info( lib, inputArchive, testFormat.format ); REQUIRE_ITEM_DIRECTORY( info, "dir" ); REQUIRE_ITEM_REGULAR( info, "regular" ); @@ -818,7 +818,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Correctly reading archive items with Unic DYNAMIC_SECTION( "Archive format: " << testFormat.extension ) { const fs::path arcFileName = "unicode." + testFormat.extension; - TestType inputArchive = getInputArchive< TestType >( arcFileName ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; const BitArchiveReader info( lib, inputArchive, testFormat.format ); REQUIRE_ITEM_UNICODE( info, "¡Porque sí!.doc" ); REQUIRE_ITEM_UNICODE( info, "σύννεφα.jpg" ); @@ -835,7 +835,7 @@ TEMPLATE_TEST_CASE( "BitArchiveReader: Reading an archive with a Unicode file na fs::path arcFileName{ BIT7Z_NATIVE_STRING( "αρχείο.7z" ) }; - TestType inputArchive = getInputArchive< TestType >( arcFileName ); + TestType inputArchive = InputArchiveProxy{ arcFileName }; const BitArchiveReader info( lib, inputArchive, BitFormat::SevenZip ); REQUIRE_ITEM_UNICODE( info, "¡Porque sí!.doc" ); REQUIRE_ITEM_UNICODE( info, "σύννεφα.jpg" ); From 94016f8323e138b8551d1e36ef249f3ba7776060 Mon Sep 17 00:00:00 2001 From: Oz Date: Wed, 11 Oct 2023 19:48:50 +0200 Subject: [PATCH 17/18] Fix clang warnings --- tests/src/test_bitarchivereader.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/src/test_bitarchivereader.cpp b/tests/src/test_bitarchivereader.cpp index 37067836..b9646610 100644 --- a/tests/src/test_bitarchivereader.cpp +++ b/tests/src/test_bitarchivereader.cpp @@ -142,17 +142,17 @@ struct SingleFileArchive : public TestInputArchive { class InputArchiveProxy { const fs::path& mPath; public: - InputArchiveProxy( const fs::path& path ) : mPath{ path } {} + explicit InputArchiveProxy( const fs::path& path ) : mPath{ path } {} - operator tstring() const { + operator tstring() const { // NOLINT(*-explicit-constructor) return path_to_tstring( mPath ); } - operator buffer_t() const { + operator buffer_t() const { // NOLINT(*-explicit-constructor) return load_file( mPath ); } - operator std::ifstream() const { + operator std::ifstream() const { // NOLINT(*-explicit-constructor) return std::ifstream{ mPath, std::ios::binary }; } }; From 485805a52733270f20ac37317117816af4685c0c Mon Sep 17 00:00:00 2001 From: Oz Date: Wed, 11 Oct 2023 20:48:27 +0200 Subject: [PATCH 18/18] Fix warnings in MSVC 2017 --- tests/CMakeLists.txt | 4 ---- tests/src/test_bitarchivereader.cpp | 6 +++++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d8000e65..e0ebbe9f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -129,10 +129,6 @@ endif() if( MSVC ) target_compile_options( ${TESTS_TARGET} PRIVATE /utf-8 ) target_compile_options( ${TESTS_TARGET_PUBLIC} PRIVATE /utf-8 ) - - # Needed by MSVC for defining the S_XXXX macros (used in BitArchiveReader tests) - target_compile_definitions( ${TESTS_TARGET} PRIVATE _CRT_INTERNAL_NONSTDC_NAMES=1 ) - target_compile_definitions( ${TESTS_TARGET_PUBLIC} PRIVATE _CRT_INTERNAL_NONSTDC_NAMES=1 ) endif() if( WIN32 ) diff --git a/tests/src/test_bitarchivereader.cpp b/tests/src/test_bitarchivereader.cpp index b9646610..2431e89b 100644 --- a/tests/src/test_bitarchivereader.cpp +++ b/tests/src/test_bitarchivereader.cpp @@ -22,8 +22,12 @@ #include #include +// Needed by MSVC for defining the S_XXXX macros. +#ifndef _CRT_INTERNAL_NONSTDC_NAMES +#define _CRT_INTERNAL_NONSTDC_NAMES 1 +#endif -// For checking posix file attributes +// For checking posix file attributes. #include // MSVC doesn't define these macros!