|
| 1 | +from os import listdir |
| 2 | +from pathlib import Path |
| 3 | +from shutil import rmtree |
| 4 | +from subprocess import check_call |
| 5 | +from sys import modules, path, platform |
| 6 | + |
| 7 | + |
| 8 | +class TestHatchBuild: |
| 9 | + def test_hatch_build(self): |
| 10 | + project = "test_project_hatch_build" |
| 11 | + |
| 12 | + rmtree(f"hatch_cpp/tests/{project}/project/extension.so", ignore_errors=True) |
| 13 | + rmtree(f"hatch_cpp/tests/{project}/project/extension.pyd", ignore_errors=True) |
| 14 | + modules.pop("project", None) |
| 15 | + modules.pop("project.extension", None) |
| 16 | + |
| 17 | + # compile |
| 18 | + check_call( |
| 19 | + [ |
| 20 | + "hatch-build", |
| 21 | + "--hooks-only", |
| 22 | + "--", |
| 23 | + "--libraries.0.name=project/extension", |
| 24 | + "--libraries.0.sources=cpp/project/basic.cpp", |
| 25 | + "--libraries.0.include-dirs=cpp", |
| 26 | + "--libraries.0.binding=nanobind", |
| 27 | + ], |
| 28 | + cwd=f"hatch_cpp/tests/{project}", |
| 29 | + ) |
| 30 | + |
| 31 | + # assert built |
| 32 | + |
| 33 | + if project == "test_project_limited_api" and platform != "win32": |
| 34 | + assert "extension.abi3.so" in listdir(f"hatch_cpp/tests/{project}/project") |
| 35 | + else: |
| 36 | + if platform == "win32": |
| 37 | + assert "extension.pyd" in listdir(f"hatch_cpp/tests/{project}/project") |
| 38 | + else: |
| 39 | + assert "extension.so" in listdir(f"hatch_cpp/tests/{project}/project") |
| 40 | + |
| 41 | + # import |
| 42 | + here = Path(__file__).parent / project |
| 43 | + path.insert(0, str(here)) |
| 44 | + import project.extension |
| 45 | + |
| 46 | + assert project.extension.hello() == "A string" |
0 commit comments