From aac3f68a4ca141d1c39a84729f930d033dff07e4 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Wed, 15 Jan 2025 17:58:32 +0000 Subject: [PATCH] Fix running tests using ctest --- cmake/tests.cmake | 4 +- src/tests/api/internals/bsa_test.h | 27 +++++------ src/tests/api/internals/main.cpp | 19 ++++++-- src/tests/api/internals/plugin_test.h | 70 +++++++++++++++++---------- src/tests/common_game_test_fixture.h | 19 +------- src/tests/test_helpers.h | 32 ++++++++++++ 6 files changed, 107 insertions(+), 64 deletions(-) diff --git a/cmake/tests.cmake b/cmake/tests.cmake index 86336ce9..06cc2d1e 100644 --- a/cmake/tests.cmake +++ b/cmake/tests.cmake @@ -226,9 +226,9 @@ endif() add_custom_command(TARGET libloot_internals_tests POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${testing-plugins_SOURCE_DIR} - $) + ${CMAKE_CURRENT_BINARY_DIR}/testing-plugins) add_custom_command(TARGET libloot_tests POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${testing-plugins_SOURCE_DIR} - $) + ${CMAKE_CURRENT_BINARY_DIR}/testing-plugins) diff --git a/src/tests/api/internals/bsa_test.h b/src/tests/api/internals/bsa_test.h index 848ef298..5197e080 100644 --- a/src/tests/api/internals/bsa_test.h +++ b/src/tests/api/internals/bsa_test.h @@ -34,7 +34,7 @@ along with LOOT. If not, see namespace loot::test { TEST(GetAssetsInBethesdaArchive, shouldSupportV103BSAs) { - const auto path = std::filesystem::u8path("./Oblivion/Data/Blank.bsa"); + const auto path = getSourceArchivesPath(GameType::tes4) / "Blank.bsa"; const auto assets = GetAssetsInBethesdaArchive(path); @@ -51,7 +51,7 @@ TEST(GetAssetsInBethesdaArchive, shouldSupportV103BSAs) { } TEST(GetAssetsInBethesdaArchive, shouldSupportV104BSAs) { - const auto path = std::filesystem::u8path("./Skyrim/Data/Blank.bsa"); + const auto path = getSourceArchivesPath(GameType::tes5) / "Blank.bsa"; const auto assets = GetAssetsInBethesdaArchive(path); @@ -68,7 +68,7 @@ TEST(GetAssetsInBethesdaArchive, shouldSupportV104BSAs) { } TEST(GetAssetsInBethesdaArchive, shouldSupportV105BSAs) { - const auto path = std::filesystem::u8path("./SkyrimSE/Data/Blank.bsa"); + const auto path = getSourceArchivesPath(GameType::tes5se) / "Blank.bsa"; const auto assets = GetAssetsInBethesdaArchive(path); @@ -91,8 +91,7 @@ TEST(GetAssetsInBethesdaArchive, shouldThrowIfFileCannotBeOpened) { } TEST(GetAssetsInBethesdaArchive, shouldSupportGeneralBA2s) { - const auto path = - std::filesystem::u8path("./Fallout 4/Data/Blank - Main.ba2"); + const auto path = getSourceArchivesPath(GameType::fo4) / "Blank - Main.ba2"; const uint64_t folderHash = std::hash{}("dev\\git\\testing-plugins"); const uint64_t fileHash = std::hash{}("license.txt"); @@ -115,7 +114,7 @@ TEST(GetAssetsInBethesdaArchive, shouldSupportGeneralBA2s) { TEST(GetAssetsInBethesdaArchive, shouldSupportTextureBA2s) { const auto path = - std::filesystem::u8path("./Fallout 4/Data/Blank - Textures.ba2"); + getSourceArchivesPath(GameType::fo4) / "Blank - Textures.ba2"; const uint64_t folderHash = std::hash{}("dev\\git\\testing-plugins"); const uint64_t fileHash = std::hash{}("blank.dds"); @@ -143,7 +142,7 @@ class GetAssetsInBethesdaArchive_BA2Version std::filesystem::create_directories(path.parent_path()); const auto sourcePath = - std::filesystem::u8path("./Fallout 4/Data/Blank - Main.ba2"); + getSourceArchivesPath(GameType::fo4) / "Blank - Main.ba2"; std::filesystem::copy(sourcePath, path); std::fstream stream( @@ -178,7 +177,7 @@ TEST_P(GetAssetsInBethesdaArchive_BA2Version, shouldSupportBA2Version) { TEST(GetAssetsInBethesdaArchives, shouldSkipFilesThatCannotBeRead) { std::vector paths( {std::filesystem::u8path("invalid.bsa"), - std::filesystem::u8path("./Skyrim/Data/Blank.bsa")}); + getSourceArchivesPath(GameType::tes5) / "Blank.bsa"}); const auto assets = GetAssetsInBethesdaArchives(paths); @@ -196,9 +195,9 @@ TEST(GetAssetsInBethesdaArchives, shouldSkipFilesThatCannotBeRead) { TEST(GetAssetsInBethesdaArchives, shouldCombineAssetsFromEachLoadedArchive) { std::vector paths( - {std::filesystem::u8path("./Oblivion/Data/Blank.bsa"), - std::filesystem::u8path("./Skyrim/Data/Blank.bsa"), - std::filesystem::u8path("./SkyrimSE/Data/Blank.bsa")}); + {getSourceArchivesPath(GameType::tes4) / "Blank.bsa", + getSourceArchivesPath(GameType::tes5) / "Blank.bsa", + getSourceArchivesPath(GameType::tes5se) / "Blank.bsa"}); const auto assets = GetAssetsInBethesdaArchives(paths); @@ -221,7 +220,7 @@ TEST(GetAssetsInBethesdaArchives, shouldCombineAssetsFromEachLoadedArchive) { } TEST(DoAssetsIntersect, shouldReturnTrueIfTheSameFileExistsInTheSameFolder) { - const auto path = std::filesystem::u8path("./Oblivion/Data/Blank.bsa"); + const auto path = getSourceArchivesPath(GameType::tes4) / "Blank.bsa"; const auto assets = GetAssetsInBethesdaArchive(path); @@ -230,10 +229,10 @@ TEST(DoAssetsIntersect, shouldReturnTrueIfTheSameFileExistsInTheSameFolder) { TEST(DoAssetsIntersect, shouldReturnFalseIfTheSameFileExistsInDifferentFolders) { - const auto path1 = std::filesystem::u8path("./Oblivion/Data/Blank.bsa"); + const auto path1 = getSourceArchivesPath(GameType::tes4) / "Blank.bsa"; const auto assets1 = GetAssetsInBethesdaArchive(path1); - const auto path2 = std::filesystem::u8path("./Skyrim/Data/Blank.bsa"); + const auto path2 = getSourceArchivesPath(GameType::tes5) / "Blank.bsa"; const auto assets2 = GetAssetsInBethesdaArchive(path2); EXPECT_EQ(*assets2.at(0x2E01002E).begin(), *assets1.at(0).begin()); diff --git a/src/tests/api/internals/main.cpp b/src/tests/api/internals/main.cpp index 3b8a67cb..ea14ba7b 100644 --- a/src/tests/api/internals/main.cpp +++ b/src/tests/api/internals/main.cpp @@ -162,6 +162,8 @@ TEST(Filesystem, equalityShouldBeCaseSensitive) { auto upper = std::filesystem::path("LICENSE"); auto lower = std::filesystem::path("license"); + ASSERT_NE(lower.u8string(), upper.u8string()); + EXPECT_NE(lower, upper); } @@ -169,14 +171,20 @@ TEST(Filesystem, equivalentShouldRequireThatBothPathsExist) { auto upper = std::filesystem::path("LICENSE"); auto lower = std::filesystem::path("license2"); + ASSERT_FALSE(std::filesystem::exists(upper)); + ASSERT_FALSE(std::filesystem::exists(lower)); + EXPECT_THROW(std::ignore = std::filesystem::equivalent(lower, upper), std::filesystem::filesystem_error); } #ifdef _WIN32 TEST(Filesystem, equivalentShouldBeCaseInsensitive) { - auto upper = std::filesystem::path("LICENSE"); - auto lower = std::filesystem::path("license"); + auto upper = std::filesystem::path("./testing-plugins/LICENSE"); + auto lower = std::filesystem::path("./testing-plugins/license"); + + ASSERT_TRUE(std::filesystem::exists(upper)); + ASSERT_TRUE(std::filesystem::exists(lower)); EXPECT_TRUE(std::filesystem::equivalent(lower, upper)); } @@ -194,12 +202,15 @@ TEST( } #else TEST(Filesystem, equivalentShouldBeCaseSensitive) { - auto upper = std::filesystem::path("LICENSE"); - auto lower = std::filesystem::path("license"); + auto upper = std::filesystem::path("./testing-plugins/LICENSE"); + auto lower = std::filesystem::path("./testing-plugins/license"); std::ofstream out(lower); out.close(); + ASSERT_TRUE(std::filesystem::exists(upper)); + ASSERT_TRUE(std::filesystem::exists(lower)); + EXPECT_FALSE(std::filesystem::equivalent(lower, upper)); } #endif diff --git a/src/tests/api/internals/plugin_test.h b/src/tests/api/internals/plugin_test.h index 561503cb..c088f086 100644 --- a/src/tests/api/internals/plugin_test.h +++ b/src/tests/api/internals/plugin_test.h @@ -76,12 +76,13 @@ class PluginTest : public CommonGameTestFixture { std::filesystem::path blankMasterDependentArchive; if (GetParam() == GameType::fo4 || GetParam() == GameType::fo4vr || GetParam() == GameType::starfield) { - copyPlugin("./Fallout 4/Data", "Blank - Main.ba2"); - copyPlugin("./Fallout 4/Data", "Blank - Textures.ba2"); + copyPlugin(getSourceArchivesPath(GetParam()), "Blank - Main.ba2"); + copyPlugin(getSourceArchivesPath(GetParam()), "Blank - Textures.ba2"); blankMasterDependentArchive = "Blank - Master Dependent - Main.ba2"; - std::filesystem::copy_file("./Fallout 4/Data/Blank - Main.ba2", - dataPath / blankMasterDependentArchive); + std::filesystem::copy_file( + getSourceArchivesPath(GetParam()) / "Blank - Main.ba2", + dataPath / blankMasterDependentArchive); ASSERT_TRUE( std::filesystem::exists(dataPath / blankMasterDependentArchive)); } else if (GetParam() == GameType::tes3) { @@ -785,8 +786,11 @@ TEST_P( } TEST(equivalent, shouldReturnTrueIfGivenEqualPathsThatExist) { - auto path1 = std::filesystem::path("LICENSE"); - auto path2 = std::filesystem::path("LICENSE"); + auto path1 = std::filesystem::path("./testing-plugins/LICENSE"); + auto path2 = std::filesystem::path("./testing-plugins/LICENSE"); + + ASSERT_EQ(path1, path2); + ASSERT_TRUE(std::filesystem::exists(path1)); EXPECT_TRUE(loot::equivalent(path1, path2)); } @@ -795,30 +799,45 @@ TEST(equivalent, shouldReturnTrueIfGivenEqualPathsThatDoNotExist) { auto path1 = std::filesystem::path("LICENSE2"); auto path2 = std::filesystem::path("LICENSE2"); + ASSERT_EQ(path1, path2); + ASSERT_FALSE(std::filesystem::exists(path1)); + EXPECT_TRUE(loot::equivalent(path1, path2)); } -TEST(equivalent, shouldReturnFalseIfPathsAreNotCaseInsensitivelyEqual) { - auto upper = std::filesystem::path("LICENSE"); +TEST(equivalent, + shouldReturnFalseIfGivenCaseInsensitivelyEqualPathsThatDoNotExist) { + auto upper = std::filesystem::path("LICENSE2"); auto lower = std::filesystem::path("license2"); + ASSERT_TRUE(boost::iequals(upper.u8string(), lower.u8string())); + ASSERT_FALSE(std::filesystem::exists(upper)); + ASSERT_FALSE(std::filesystem::exists(lower)); + EXPECT_FALSE(loot::equivalent(lower, upper)); } -#ifdef _WIN32 -TEST(equivalent, shouldReturnTrueIfGivenCaseInsensitivelyEqualPathsThatExist) { - auto upper = std::filesystem::path("LICENSE"); - auto lower = std::filesystem::path("license"); +TEST(equivalent, shouldReturnFalseIfGivenCaseInsensitivelyUnequalThatExist) { + auto path1 = std::filesystem::path("./testing-plugins/LICENSE"); + auto path2 = std::filesystem::path("./testing-plugins/README.md"); - EXPECT_TRUE(loot::equivalent(lower, upper)); + ASSERT_FALSE(boost::iequals(path1.u8string(), path2.u8string())); + ASSERT_TRUE(std::filesystem::exists(path1)); + ASSERT_TRUE(std::filesystem::exists(path2)); + + EXPECT_FALSE(loot::equivalent(path1, path2)); } -TEST(equivalent, - shouldReturnFalseIfGivenCaseInsensitivelyEqualPathsThatDoNotExist) { - auto upper = std::filesystem::path("LICENSE2"); - auto lower = std::filesystem::path("license2"); +#ifdef _WIN32 +TEST(equivalent, shouldReturnTrueIfGivenCaseInsensitivelyEqualPathsThatExist) { + auto upper = std::filesystem::path("./testing-plugins/LICENSE"); + auto lower = std::filesystem::path("./testing-plugins/license"); - EXPECT_FALSE(loot::equivalent(lower, upper)); + ASSERT_TRUE(boost::iequals(upper.u8string(), lower.u8string())); + ASSERT_TRUE(std::filesystem::exists(upper)); + ASSERT_TRUE(std::filesystem::exists(lower)); + + EXPECT_TRUE(loot::equivalent(lower, upper)); } TEST( @@ -844,16 +863,15 @@ TEST( } #else TEST(equivalent, shouldReturnFalseIfGivenCaseInsensitivelyEqualPathsThatExist) { - auto upper = std::filesystem::path("LICENSE"); - auto lower = std::filesystem::path("license"); + auto upper = std::filesystem::path("./testing-plugins/LICENSE"); + auto lower = std::filesystem::path("./testing-plugins/license"); - EXPECT_FALSE(loot::equivalent(lower, upper)); -} + std::ofstream out(lower); + out.close(); -TEST(equivalent, - shouldReturnFalseIfGivenCaseInsensitivelyEqualPathsThatDoNotExist) { - auto upper = std::filesystem::path("LICENSE2"); - auto lower = std::filesystem::path("license2"); + ASSERT_TRUE(boost::iequals(upper.u8string(), lower.u8string())); + ASSERT_TRUE(std::filesystem::exists(upper)); + ASSERT_TRUE(std::filesystem::exists(lower)); EXPECT_FALSE(loot::equivalent(lower, upper)); } diff --git a/src/tests/common_game_test_fixture.h b/src/tests/common_game_test_fixture.h index 4ee35cde..2fb02bf4 100644 --- a/src/tests/common_game_test_fixture.h +++ b/src/tests/common_game_test_fixture.h @@ -34,7 +34,6 @@ along with LOOT. If not, see #include #include "loot/enum/game_type.h" - #include "tests/test_helpers.h" namespace loot { @@ -278,17 +277,7 @@ class CommonGameTestFixture : public ::testing::TestWithParam { } std::filesystem::path getSourcePluginsPath() const { - using std::filesystem::absolute; - if (GetParam() == GameType::tes3) - return absolute("./Morrowind/Data Files"); - else if (GetParam() == GameType::tes4) - return absolute("./Oblivion/Data"); - else if (GetParam() == GameType::starfield) - return absolute("./Starfield/Data"); - else if (supportsLightPlugins(GetParam())) - return absolute("./SkyrimSE/Data"); - else - return absolute("./Skyrim/Data"); + return loot::test::getSourcePluginsPath(GetParam()); } void touch(const std::filesystem::path& path) { @@ -473,12 +462,6 @@ class CommonGameTestFixture : public ::testing::TestWithParam { return gameType == GameType::tes3 || gameType == GameType::tes4 || gameType == GameType::fo3 || gameType == GameType::fonv; } - - static bool supportsLightPlugins(GameType gameType) { - return gameType == GameType::tes5se || gameType == GameType::tes5vr || - gameType == GameType::fo4 || gameType == GameType::fo4vr || - gameType == GameType::starfield; - } }; } } diff --git a/src/tests/test_helpers.h b/src/tests/test_helpers.h index 4f64a614..ea1d8277 100644 --- a/src/tests/test_helpers.h +++ b/src/tests/test_helpers.h @@ -29,7 +29,39 @@ along with LOOT. If not, see #include #include +#include "loot/enum/game_type.h" + namespace loot::test { +bool supportsLightPlugins(GameType gameType) { + return gameType == GameType::tes5se || gameType == GameType::tes5vr || + gameType == GameType::fo4 || gameType == GameType::fo4vr || + gameType == GameType::starfield; +} + +std::filesystem::path getSourcePluginsPath(GameType gameType) { + using std::filesystem::absolute; + if (gameType == GameType::tes3) { + return absolute("./testing-plugins/Morrowind/Data Files"); + } else if (gameType == GameType::tes4) { + return absolute("./testing-plugins/Oblivion/Data"); + } else if (gameType == GameType::starfield) { + return absolute("./testing-plugins/Starfield/Data"); + } else if (supportsLightPlugins(gameType)) { + return absolute("./testing-plugins/SkyrimSE/Data"); + } else { + return absolute("./testing-plugins/Skyrim/Data"); + } +} + +std::filesystem::path getSourceArchivesPath(GameType gameType) { + if (gameType == GameType::fo4 || gameType == GameType::fo4vr || + gameType == GameType::starfield) { + return "./testing-plugins/Fallout 4/Data"; + } else { + return getSourcePluginsPath(gameType); + } +} + std::filesystem::path getRootTestPath() { std::random_device randomDevice; std::default_random_engine prng(randomDevice());