Skip to content

Commit

Permalink
Fix testing for editable and non-editable installs alike (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanv authored Jun 28, 2024
2 parents 91517b6 + e8e8943 commit 7e0c6b3
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 46 deletions.
Empty file.
6 changes: 4 additions & 2 deletions example_pkg/example_pkg/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ py.extension_module(
)

python_sources = [
'__init__.py'
'__init__.py',
'conftest.py'
]

py.install_sources(
python_sources,
subdir: 'example_pkg'
)

install_subdir('tests', install_dir: py.get_install_dir() / 'example_pkg/tests')
install_subdir('submodule', install_dir: py.get_install_dir() / 'example_pkg')
install_subdir('tests', install_dir: py.get_install_dir() / 'example_pkg')
Empty file.
1 change: 1 addition & 0 deletions example_pkg/example_pkg/submodule/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
install_subdir('tests', install_dir: py.get_install_dir() / 'example_pkg/submodule')
Empty file.
2 changes: 2 additions & 0 deletions example_pkg/example_pkg/submodule/tests/test_submodule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test_something():
pass
Empty file.
2 changes: 1 addition & 1 deletion example_pkg/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project(
'c',
version: '0.0.dev0',
license: 'BSD-3',
meson_version: '>= 0.61.0',
meson_version: '>= 0.64',
default_options: [
'buildtype=debugoptimized',
'c_std=c99',
Expand Down
9 changes: 2 additions & 7 deletions spin/cmds/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
from . import meson
__all__ = ["meson"]

# Backward compatibility with older versions
build = meson.build
test = meson.test
ipython = meson.ipython
python = meson.python
shell = meson.shell
from . import meson
52 changes: 36 additions & 16 deletions spin/cmds/meson.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,35 @@ def test(
""" # noqa: E501
cfg = get_config()
distname = cfg.get("project.name", None)
pytest_args = pytest_args or ()

if gcov and distname and _is_editable_install_of_same_source(distname):
# User specified tests without -t flag
# Rewrite arguments as though they specified using -t and proceed
if (len(pytest_args) == 1) and (not tests):
tests = pytest_args[0]
pytest_args = ()

package = cfg.get("tool.spin.package", None)
if package is None:
print(
"Please specify `package = packagename` under `tool.spin` section of `pyproject.toml`"
)
raise SystemExit(1)

# User did not specify what to test, so we test
# the full package
if not (pytest_args or tests):
pytest_args = ("--pyargs", package)
elif tests:
if (os.path.sep in tests) or ("/" in tests):
# Tests specified as file path
pytest_args = pytest_args + (tests,)
else:
# Otherwise tests given as modules
pytest_args = pytest_args + ("--pyargs", tests)

is_editable_install = distname and _is_editable_install_of_same_source(distname)
if gcov and is_editable_install:
click.secho(
"Error: cannot generate coverage report for editable installs",
fg="bright_red",
Expand All @@ -446,16 +473,6 @@ def test(
else:
ctx.invoke(build_cmd)

package = cfg.get("tool.spin.package", None)
if package is None:
print(
"Please specify `package = packagename` under `tool.spin` section of `pyproject.toml`"
)
raise SystemExit(1)

if (not pytest_args) and (not tests):
tests = package

site_path = _set_pythonpath()
if site_path:
print(f'$ export PYTHONPATH="{site_path}"')
Expand All @@ -479,8 +496,8 @@ def test(
if (n_jobs != "1") and ("-n" not in pytest_args):
pytest_args = ("-n", str(n_jobs)) + pytest_args

if tests and "--pyargs" not in pytest_args:
pytest_args = ("--pyargs", tests) + pytest_args
if not any("--import-mode" in arg for arg in pytest_args):
pytest_args = ("--import-mode=importlib",) + pytest_args

if verbose:
pytest_args = ("-v",) + pytest_args
Expand All @@ -503,9 +520,12 @@ def test(
else:
cmd = ["pytest"]

pytest_p = _run(
cmd + list(pytest_args),
)
if not os.path.exists(install_dir):
os.mkdir(install_dir)

cwd = os.getcwd()
pytest_p = _run(cmd + list(pytest_args), cwd=site_path)
os.chdir(cwd)

if gcov:
# Verify the tools are present
Expand Down
4 changes: 4 additions & 0 deletions spin/cmds/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def run(
Other arguments and keywords are passed directly to `subprocess.run`.
Returns
-------
p : CompletedProcess
"""
if cwd:
if echo:
Expand Down
7 changes: 7 additions & 0 deletions spin/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ def pre_post_test():
os.chdir(cwd)
util.run(["git", "clean", "-xdf"], cwd="example_pkg")
os.chdir(cwd)


@pytest.fixture
def editable_install():
util.run(["pip", "install", "--quiet", "--no-build-isolation", "-e", "."])
yield
util.run(["pip", "uninstall", "--quiet", "-y", "example_pkg"])
10 changes: 0 additions & 10 deletions spin/tests/test_build_cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,6 @@ def test_recommend_run_python():
), "Failed to recommend `python run python file.py`"


def test_test():
"""Does the test command run?"""
spin("test")


def test_test_with_pythonpath():
"""Does `spin test` work when PYTHONPATH is set?"""
spin("test", env={**os.environ, "PYTHONPATH": "/tmp"})


def test_sdist():
spin("sdist")

Expand Down
10 changes: 0 additions & 10 deletions spin/tests/test_editable.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import pytest
from testutil import spin, stdout

from spin.cmds.util import run


@pytest.fixture
def editable_install():
run(["pip", "install", "--quiet", "--no-build-isolation", "-e", "."])
yield
run(["pip", "uninstall", "--quiet", "-y", "example_pkg"])


def test_detect_editable(editable_install):
assert "Editable install of same source detected" in stdout(
Expand Down
29 changes: 29 additions & 0 deletions spin/tests/test_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os

from testutil import spin


def test_test():
"""Does the test command run?"""
spin("test")


def test_test_with_pythonpath():
"""Does `spin test` work when PYTHONPATH is set?"""
spin("test", env={**os.environ, "PYTHONPATH": "/tmp"})


def test_test_file_spec():
spin("test", "example_pkg/submodule/tests/test_submodule.py")


def test_test_module_spec():
spin("test", "example_pkg.submodule")


def test_test_editable_file_spec(editable_install):
spin("test", "example_pkg/submodule/tests/test_submodule.py")


def test_test_editable_module_spec(editable_install):
spin("test", "example_pkg.submodule")

0 comments on commit 7e0c6b3

Please sign in to comment.