diff --git a/.github/workflows/python-unittests.yml b/.github/workflows/python-unittests.yml index 1b5f427..6359849 100644 --- a/.github/workflows/python-unittests.yml +++ b/.github/workflows/python-unittests.yml @@ -39,7 +39,7 @@ jobs: pip cache purge python -m pip install --upgrade pip pip install .[dev] - python tests/compile.py + python tools/compile_tests.py - name: Test with unittest run: python -m unittest discover -v -s tests diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..5f52fd9 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,41 @@ +cmake_minimum_required(VERSION 3.13) + +project(amulet_test_utils LANGUAGES CXX) + +# Set C++20 +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +# Set platform variables +if (WIN32) + # set windows 7 as the minimum version + add_definitions(-D_WIN32_WINNT=0x0601) +elseif(APPLE) + set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15") +else() + set(CMAKE_POSITION_INDEPENDENT_CODE ON) +endif() + +if (MSVC) + add_definitions("/MP") +endif() + +# Find C++ files +file(REAL_PATH src SOURCE_PATH) +file(GLOB_RECURSE HEADERS LIST_DIRECTORIES false ${SOURCE_PATH}/amulet/*.hpp) + +# Add implementation +add_library(amulet_test_utils INTERFACE) +target_include_directories(amulet_test_utils INTERFACE "src") +target_sources(amulet_test_utils PRIVATE ${HEADERS}) +foreach(FILE ${HEADERS}) + file(RELATIVE_PATH REL_PATH ${SOURCE_PATH} ${FILE}) + get_filename_component(GROUP ${REL_PATH} DIRECTORY) + string(REPLACE "/" "\\" GROUP ${GROUP}) + source_group(${GROUP} FILES ${FILE}) +endforeach() + +if (TEST_AMULET_TEST_UTILS_DIR) + add_subdirectory(tests) +endif() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 12a34e6..3450c59 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -21,19 +21,9 @@ endif() find_package(pybind11 CONFIG REQUIRED) find_package(amulet_test_utils CONFIG REQUIRED) -# Find sources -file(GLOB_RECURSE SOURCES LIST_DIRECTORIES false "${CMAKE_CURRENT_LIST_DIR}/*.py.cpp") - -pybind11_add_module(_test_test_utils) +pybind11_add_module(_test_test_utils "test_amulet_test_utils/_test_test_utils.py.cpp") target_compile_definitions(_test_test_utils PRIVATE PYBIND11_DETAILED_ERROR_MESSAGES) target_link_libraries(_test_test_utils PRIVATE amulet_test_utils) -target_sources(_test_test_utils PRIVATE ${SOURCES}) -foreach(FILE ${SOURCES}) - file(RELATIVE_PATH REL_PATH ${CMAKE_CURRENT_LIST_DIR} ${FILE}) - get_filename_component(GROUP ${REL_PATH} DIRECTORY) - string(REPLACE "/" "\\" GROUP "${GROUP}") - source_group(${GROUP} FILES ${FILE}) -endforeach() # Install -install(TARGETS _test_test_utils DESTINATION ".") +install(TARGETS _test_test_utils DESTINATION ${TEST_AMULET_TEST_UTILS_DIR}) diff --git a/tests/compile.py b/tests/compile.py deleted file mode 100644 index 11d9931..0000000 --- a/tests/compile.py +++ /dev/null @@ -1,65 +0,0 @@ -import subprocess -import sys -import shutil -import os -import importlib.util - -import pybind11 - - -def get_package_path(name: str) -> str: - try: - spec = importlib.util.find_spec(name) - if spec is None: - raise RuntimeError(f"Could not find {name}") - module_path = spec.origin - if module_path is None: - raise RuntimeError(f"Could not find {name}") - if not module_path.endswith("__init__.py"): - raise RuntimeError(f"{name} is not a package.") - return os.path.dirname(module_path).replace(os.sep, "/") - except: - print(f"Failed finding {name}. Falling back to importing.") - return importlib.import_module(name).__path__[0].replace(os.sep, "/") - - -def main() -> None: - os.chdir(os.path.dirname(__file__)) - - if os.path.isdir("build/CMakeFiles"): - shutil.rmtree("build/CMakeFiles") - - platform_args = [] - if sys.platform == "win32": - platform_args.extend(["-G", "Visual Studio 17 2022"]) - if sys.maxsize > 2**32: - platform_args.extend(["-A", "x64"]) - else: - platform_args.extend(["-A", "Win32"]) - platform_args.extend(["-T", "v143"]) - - if subprocess.run( - [ - "cmake", - *platform_args, - f"-DPYTHON_EXECUTABLE={sys.executable}", - f"-Dpybind11_DIR={pybind11.get_cmake_dir().replace(os.sep, '/')}", - f"-Damulet_test_utils_DIR={get_package_path('amulet.test_utils')}", - f"-DCMAKE_INSTALL_PREFIX={os.path.dirname(__file__).replace(os.sep, '/')}", - "-B", - "build", - ] - ).returncode: - raise RuntimeError("Error configuring test_test_utils") - if subprocess.run( - ["cmake", "--build", "build", "--config", "RelWithDebInfo"] - ).returncode: - raise RuntimeError("Error installing test_test_utils") - if subprocess.run( - ["cmake", "--install", "build", "--config", "RelWithDebInfo"] - ).returncode: - raise RuntimeError("Error installing test_test_utils") - - -if __name__ == "__main__": - main() diff --git a/tests/test_amulet_test_utils/__init__.py b/tests/test_amulet_test_utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/_test_test_utils.py.cpp b/tests/test_amulet_test_utils/_test_test_utils.py.cpp similarity index 100% rename from tests/_test_test_utils.py.cpp rename to tests/test_amulet_test_utils/_test_test_utils.py.cpp diff --git a/tests/_test_test_utils.pyi b/tests/test_amulet_test_utils/_test_test_utils.pyi similarity index 100% rename from tests/_test_test_utils.pyi rename to tests/test_amulet_test_utils/_test_test_utils.pyi diff --git a/tests/test_test_utils.py b/tests/test_amulet_test_utils/test_test_utils.py similarity index 77% rename from tests/test_test_utils.py rename to tests/test_amulet_test_utils/test_test_utils.py index 342cd3e..ccbb1b5 100644 --- a/tests/test_test_utils.py +++ b/tests/test_amulet_test_utils/test_test_utils.py @@ -3,7 +3,7 @@ class TestUtilsTestCase(TestCase): def test_assert_equal(self) -> None: - from _test_test_utils import ( + from test_amulet_test_utils._test_test_utils import ( test_assert_equal_1, test_assert_equal_2, test_assert_equal_3, @@ -23,7 +23,11 @@ def test_assert_equal(self) -> None: test_assert_equal_6() def test_assert_raises(self) -> None: - from _test_test_utils import test_assert_raises_1, test_assert_raises_2, test_assert_raises_3 + from test_amulet_test_utils._test_test_utils import ( + test_assert_raises_1, + test_assert_raises_2, + test_assert_raises_3, + ) test_assert_raises_1() test_assert_raises_2() diff --git a/tools/cmake_generate.py b/tools/cmake_generate.py new file mode 100644 index 0000000..88ac891 --- /dev/null +++ b/tools/cmake_generate.py @@ -0,0 +1,47 @@ +import sys +import subprocess +import os +import shutil + +import pybind11 + + +def fix_path(path: str) -> str: + return os.path.realpath(path).replace(os.sep, "/") + + +RootDir = fix_path(os.path.dirname(os.path.dirname(__file__))) + + +def main(): + platform_args = [] + if sys.platform == "win32": + platform_args.extend(["-G", "Visual Studio 17 2022"]) + if sys.maxsize > 2**32: + platform_args.extend(["-A", "x64"]) + else: + platform_args.extend(["-A", "Win32"]) + platform_args.extend(["-T", "v143"]) + + os.chdir(RootDir) + shutil.rmtree(os.path.join(RootDir, "build", "CMakeFiles"), ignore_errors=True) + + if subprocess.run( + [ + "cmake", + *platform_args, + f"-DPYTHON_EXECUTABLE={sys.executable}", + f"-Dpybind11_DIR={fix_path(pybind11.get_cmake_dir())}", + f"-DCMAKE_INSTALL_PREFIX=install", + # test args + f"-Damulet_test_utils_DIR={os.path.join(RootDir, 'src', 'amulet', 'test_utils')}", + f"-DTEST_AMULET_TEST_UTILS_DIR={os.path.join(RootDir, 'tests', 'test_amulet_test_utils')}", + "-B", + "build", + ] + ).returncode: + raise RuntimeError("Error configuring amulet_utils") + + +if __name__ == "__main__": + main() diff --git a/tools/compile_tests.py b/tools/compile_tests.py new file mode 100644 index 0000000..447c620 --- /dev/null +++ b/tools/compile_tests.py @@ -0,0 +1,57 @@ +import subprocess +import sys +import shutil +import os + +import pybind11 +import amulet.test_utils + + +def fix_path(path: str) -> str: + return os.path.realpath(path).replace(os.sep, "/") + + +RootDir = os.path.dirname(os.path.dirname(__file__)) +TestsDir = os.path.join(RootDir, "tests") + + +def main() -> None: + platform_args = [] + if sys.platform == "win32": + platform_args.extend(["-G", "Visual Studio 17 2022"]) + if sys.maxsize > 2**32: + platform_args.extend(["-A", "x64"]) + else: + platform_args.extend(["-A", "Win32"]) + platform_args.extend(["-T", "v143"]) + + os.chdir(TestsDir) + shutil.rmtree(os.path.join(TestsDir, "build", "CMakeFiles"), ignore_errors=True) + + if subprocess.run( + [ + "cmake", + *platform_args, + f"-DPYTHON_EXECUTABLE={sys.executable}", + f"-Dpybind11_DIR={fix_path(pybind11.get_cmake_dir())}", + f"-DCMAKE_INSTALL_PREFIX=install", + # test args + f"-Damulet_test_utils_DIR={fix_path(amulet.test_utils.__path__[0])}", + f"-DTEST_AMULET_TEST_UTILS_DIR={fix_path(os.path.join(TestsDir, 'test_amulet_test_utils'))}", + "-B", + "build", + ] + ).returncode: + raise RuntimeError("Error configuring test_amulet_test_utils") + if subprocess.run( + ["cmake", "--build", "build", "--config", "RelWithDebInfo"] + ).returncode: + raise RuntimeError("Error installing test_amulet_test_utils") + if subprocess.run( + ["cmake", "--install", "build", "--config", "RelWithDebInfo"] + ).returncode: + raise RuntimeError("Error installing test_amulet_test_utils") + + +if __name__ == "__main__": + main()