From ce3786f086184bfd1894ded4f2d562010890fc66 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 5 Jan 2025 00:13:46 +0000 Subject: [PATCH] chore: check exact paths of test executable and provide temp dir --- premake5.lua | 2 ++ test/Catch2Common.lua | 51 ++++++++++++++++++++++++++++++ test/Catch2Common/OatTestPaths.cpp | 27 ++++++++++++++++ test/Catch2Common/OatTestPaths.h | 10 ++++++ test/Catch2Common/main.cpp | 48 ++++++++++++++++++++++++++++ test/ObjCommonTests.lua | 2 ++ test/ObjCompilingTests.lua | 2 ++ test/ObjLoadingTests.lua | 2 ++ test/ParserTests.lua | 2 ++ test/ZoneCodeGeneratorLibTests.lua | 2 ++ test/ZoneCommonTests.lua | 2 ++ thirdparty/catch2.lua | 1 + 12 files changed, 151 insertions(+) create mode 100644 test/Catch2Common.lua create mode 100644 test/Catch2Common/OatTestPaths.cpp create mode 100644 test/Catch2Common/OatTestPaths.h create mode 100644 test/Catch2Common/main.cpp diff --git a/premake5.lua b/premake5.lua index 7ff24f46..b3962cbf 100644 --- a/premake5.lua +++ b/premake5.lua @@ -170,6 +170,7 @@ group "" -- ======================== -- Tests -- ======================== +include "test/Catch2Common.lua" include "test/ObjCommonTestUtils.lua" include "test/ObjCommonTests.lua" include "test/ObjCompilingTests.lua" @@ -181,6 +182,7 @@ include "test/ZoneCommonTests.lua" -- Tests group: Unit test and other tests projects group "Tests" + Catch2Common:project() ObjCommonTestUtils:project() ObjCommonTests:project() ObjCompilingTests:project() diff --git a/test/Catch2Common.lua b/test/Catch2Common.lua new file mode 100644 index 00000000..514a4ca6 --- /dev/null +++ b/test/Catch2Common.lua @@ -0,0 +1,51 @@ +Catch2Common = {} + +function Catch2Common:include(includes) + if includes:handle(self:name()) then + includedirs { + path.join(TestFolder(), "Catch2Common") + } + end +end + +function Catch2Common:link(links) + links:add(self:name()) + links:linkto(catch2) +end + +function Catch2Common:use() + +end + +function Catch2Common:name() + return "Catch2Common" +end + +function Catch2Common:project() + local folder = TestFolder() + local includes = Includes:create() + local links = Links:create() + + project(self:name()) + targetdir(TargetDirectoryTest) + location "%{wks.location}/test/%{prj.name}" + kind "StaticLib" + language "C++" + + files { + path.join(folder, "Catch2Common/**.h"), + path.join(folder, "Catch2Common/**.cpp") + } + + vpaths { + ["*"] = { + path.join(folder, "Catch2Common") + } + } + + self:include(includes) + catch2:include(includes) + + links:linkto(catch2) + links:linkall() +end diff --git a/test/Catch2Common/OatTestPaths.cpp b/test/Catch2Common/OatTestPaths.cpp new file mode 100644 index 00000000..0370a1ba --- /dev/null +++ b/test/Catch2Common/OatTestPaths.cpp @@ -0,0 +1,27 @@ +#include "OatTestPaths.h" + +#include + +namespace fs = std::filesystem; + +namespace oat::paths +{ + std::filesystem::path GetSourceDirectory() + { + return fs::current_path() / "src"; + } + + std::filesystem::path GetTestDirectory() + { + return fs::current_path() / "test"; + } + + std::filesystem::path GetTempDirectory() + { + auto result = fs::current_path() / "build" / ".tmp"; + if (!fs::is_directory(result)) + fs::create_directories(result); + + return result; + } +} // namespace oat::paths diff --git a/test/Catch2Common/OatTestPaths.h b/test/Catch2Common/OatTestPaths.h new file mode 100644 index 00000000..c63783cf --- /dev/null +++ b/test/Catch2Common/OatTestPaths.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +namespace oat::paths +{ + std::filesystem::path GetSourceDirectory(); + std::filesystem::path GetTestDirectory(); + std::filesystem::path GetTempDirectory(); +} // namespace oat::paths diff --git a/test/Catch2Common/main.cpp b/test/Catch2Common/main.cpp new file mode 100644 index 00000000..05cb7ccb --- /dev/null +++ b/test/Catch2Common/main.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include + +namespace fs = std::filesystem; + +int main(const int argc, char* argv[]) +{ + const fs::path absoluteBinDir(fs::absolute(argv[0]).parent_path()); + + const auto expectedLibDir = absoluteBinDir.parent_path().parent_path(); + const auto expectedBuildDir = expectedLibDir.parent_path(); + const auto expectedRootDir = expectedBuildDir.parent_path(); + + if (absoluteBinDir.filename() != "tests" || expectedLibDir.filename() != "lib" || expectedBuildDir.filename() != "build") + { + std::cerr << std::format("Expected test binary to be in the folder it was compiled into (build/lib/?/tests) but was {}\n", absoluteBinDir.string()); + std::cerr << "Please do not move test executable out of compilation folder\n"; + return 1; + } + + const auto expectedSrcDir = expectedRootDir / "src"; + if (!fs::is_directory(expectedSrcDir)) + { + std::cerr << std::format("Expected source directory to exist in {}, but it did not\n", expectedSrcDir.string()); + std::cerr << "Please do not move test executable out of compilation folder\n"; + return 1; + } + + const auto expectedTestDir = expectedRootDir / "test"; + if (!fs::is_directory(expectedTestDir)) + { + std::cerr << std::format("Expected test directory to exist in {}, but it did not\n", expectedTestDir.string()); + std::cerr << "Please do not move test executable out of compilation folder\n"; + return 1; + } + + fs::current_path(expectedRootDir); + + const auto result = Catch::Session().run(argc, argv); + + const auto tempDir = expectedBuildDir / ".tmp"; + if (fs::is_directory(tempDir)) + fs::remove_all(tempDir); + + return result; +} diff --git a/test/ObjCommonTests.lua b/test/ObjCommonTests.lua index a1360e05..68607a16 100644 --- a/test/ObjCommonTests.lua +++ b/test/ObjCommonTests.lua @@ -44,6 +44,7 @@ function ObjCommonTests:project() } self:include(includes) + Catch2Common:include(includes) ObjCommon:include(includes) ObjImage:include(includes) catch2:include(includes) @@ -51,5 +52,6 @@ function ObjCommonTests:project() links:linkto(ObjCommon) links:linkto(ObjImage) links:linkto(catch2) + links:linkto(Catch2Common) links:linkall() end diff --git a/test/ObjCompilingTests.lua b/test/ObjCompilingTests.lua index fc08d7a7..e521a743 100644 --- a/test/ObjCompilingTests.lua +++ b/test/ObjCompilingTests.lua @@ -43,6 +43,7 @@ function ObjCompilingTests:project() } self:include(includes) + Catch2Common:include(includes) ObjCommonTestUtils:include(includes) ParserTestUtils:include(includes) ObjLoading:include(includes) @@ -54,5 +55,6 @@ function ObjCompilingTests:project() links:linkto(ObjLoading) links:linkto(ObjCompiling) links:linkto(catch2) + links:linkto(Catch2Common) links:linkall() end diff --git a/test/ObjLoadingTests.lua b/test/ObjLoadingTests.lua index 2fc39089..339ca606 100644 --- a/test/ObjLoadingTests.lua +++ b/test/ObjLoadingTests.lua @@ -43,6 +43,7 @@ function ObjLoadingTests:project() } self:include(includes) + Catch2Common:include(includes) ObjCommonTestUtils:include(includes) ParserTestUtils:include(includes) ObjLoading:include(includes) @@ -52,5 +53,6 @@ function ObjLoadingTests:project() links:linkto(ParserTestUtils) links:linkto(ObjLoading) links:linkto(catch2) + links:linkto(Catch2Common) links:linkall() end diff --git a/test/ParserTests.lua b/test/ParserTests.lua index bc19f2de..1233c4bc 100644 --- a/test/ParserTests.lua +++ b/test/ParserTests.lua @@ -43,6 +43,7 @@ function ParserTests:project() } self:include(includes) + Catch2Common:include(includes) ParserTestUtils:include(includes) Parser:include(includes) catch2:include(includes) @@ -50,5 +51,6 @@ function ParserTests:project() links:linkto(ParserTestUtils) links:linkto(Parser) links:linkto(catch2) + links:linkto(Catch2Common) links:linkall() end diff --git a/test/ZoneCodeGeneratorLibTests.lua b/test/ZoneCodeGeneratorLibTests.lua index bb8679a1..3be5ae66 100644 --- a/test/ZoneCodeGeneratorLibTests.lua +++ b/test/ZoneCodeGeneratorLibTests.lua @@ -43,6 +43,7 @@ function ZoneCodeGeneratorLibTests:project() } self:include(includes) + Catch2Common:include(includes) ZoneCodeGeneratorLib:include(includes) ParserTestUtils:include(includes) catch2:include(includes) @@ -50,5 +51,6 @@ function ZoneCodeGeneratorLibTests:project() links:linkto(ZoneCodeGeneratorLib) links:linkto(ParserTestUtils) links:linkto(catch2) + links:linkto(Catch2Common) links:linkall() end diff --git a/test/ZoneCommonTests.lua b/test/ZoneCommonTests.lua index cec3edd9..4b237e01 100644 --- a/test/ZoneCommonTests.lua +++ b/test/ZoneCommonTests.lua @@ -45,6 +45,7 @@ function ZoneCommonTests:project() } self:include(includes) + Catch2Common:include(includes) ObjCommonTestUtils:include(includes) ZoneCommon:include(includes) catch2:include(includes) @@ -52,6 +53,7 @@ function ZoneCommonTests:project() links:linkto(ObjCommonTestUtils) links:linkto(ZoneCommon) links:linkto(catch2) + links:linkto(Catch2Common) links:linkall() ZoneCode:use() diff --git a/thirdparty/catch2.lua b/thirdparty/catch2.lua index f43d7205..0b7cdeaf 100644 --- a/thirdparty/catch2.lua +++ b/thirdparty/catch2.lua @@ -49,6 +49,7 @@ function catch2:project() defines { "DO_NOT_USE_WMAIN", + "CATCH_AMALGAMATED_CUSTOM_MAIN", "_CRT_SECURE_NO_WARNINGS" }