Skip to content

Commit e8e8943

Browse files
committed
Fix testing for editable and non-editable installs alike
1 parent 504c30b commit e8e8943

File tree

15 files changed

+86
-46
lines changed

15 files changed

+86
-46
lines changed

example_pkg/example_pkg/conftest.py

Whitespace-only changes.

example_pkg/example_pkg/meson.build

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ py.extension_module(
66
)
77

88
python_sources = [
9-
'__init__.py'
9+
'__init__.py',
10+
'conftest.py'
1011
]
1112

1213
py.install_sources(
1314
python_sources,
1415
subdir: 'example_pkg'
1516
)
1617

17-
install_subdir('tests', install_dir: py.get_install_dir() / 'example_pkg/tests')
18+
install_subdir('submodule', install_dir: py.get_install_dir() / 'example_pkg')
19+
install_subdir('tests', install_dir: py.get_install_dir() / 'example_pkg')

example_pkg/example_pkg/submodule/__init__.py

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
install_subdir('tests', install_dir: py.get_install_dir() / 'example_pkg/submodule')

example_pkg/example_pkg/submodule/tests/__init__.py

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def test_something():
2+
pass

example_pkg/example_pkg/tests/__init__.py

Whitespace-only changes.

example_pkg/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ project(
33
'c',
44
version: '0.0.dev0',
55
license: 'BSD-3',
6-
meson_version: '>= 0.61.0',
6+
meson_version: '>= 0.64',
77
default_options: [
88
'buildtype=debugoptimized',
99
'c_std=c99',

spin/cmds/__init__.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
from . import meson
1+
__all__ = ["meson"]
22

3-
# Backward compatibility with older versions
4-
build = meson.build
5-
test = meson.test
6-
ipython = meson.ipython
7-
python = meson.python
8-
shell = meson.shell
3+
from . import meson

spin/cmds/meson.py

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,35 @@ def test(
425425
""" # noqa: E501
426426
cfg = get_config()
427427
distname = cfg.get("project.name", None)
428+
pytest_args = pytest_args or ()
428429

429-
if gcov and distname and _is_editable_install_of_same_source(distname):
430+
# User specified tests without -t flag
431+
# Rewrite arguments as though they specified using -t and proceed
432+
if (len(pytest_args) == 1) and (not tests):
433+
tests = pytest_args[0]
434+
pytest_args = ()
435+
436+
package = cfg.get("tool.spin.package", None)
437+
if package is None:
438+
print(
439+
"Please specify `package = packagename` under `tool.spin` section of `pyproject.toml`"
440+
)
441+
raise SystemExit(1)
442+
443+
# User did not specify what to test, so we test
444+
# the full package
445+
if not (pytest_args or tests):
446+
pytest_args = ("--pyargs", package)
447+
elif tests:
448+
if (os.path.sep in tests) or ("/" in tests):
449+
# Tests specified as file path
450+
pytest_args = pytest_args + (tests,)
451+
else:
452+
# Otherwise tests given as modules
453+
pytest_args = pytest_args + ("--pyargs", tests)
454+
455+
is_editable_install = distname and _is_editable_install_of_same_source(distname)
456+
if gcov and is_editable_install:
430457
click.secho(
431458
"Error: cannot generate coverage report for editable installs",
432459
fg="bright_red",
@@ -443,16 +470,6 @@ def test(
443470
else:
444471
ctx.invoke(build_cmd)
445472

446-
package = cfg.get("tool.spin.package", None)
447-
if package is None:
448-
print(
449-
"Please specify `package = packagename` under `tool.spin` section of `pyproject.toml`"
450-
)
451-
raise SystemExit(1)
452-
453-
if (not pytest_args) and (not tests):
454-
tests = package
455-
456473
site_path = _set_pythonpath()
457474
if site_path:
458475
print(f'$ export PYTHONPATH="{site_path}"')
@@ -476,8 +493,8 @@ def test(
476493
if (n_jobs != "1") and ("-n" not in pytest_args):
477494
pytest_args = ("-n", str(n_jobs)) + pytest_args
478495

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

482499
if verbose:
483500
pytest_args = ("-v",) + pytest_args
@@ -500,9 +517,12 @@ def test(
500517
else:
501518
cmd = ["pytest"]
502519

503-
pytest_p = _run(
504-
cmd + list(pytest_args),
505-
)
520+
if not os.path.exists(install_dir):
521+
os.mkdir(install_dir)
522+
523+
cwd = os.getcwd()
524+
pytest_p = _run(cmd + list(pytest_args), cwd=site_path)
525+
os.chdir(cwd)
506526

507527
if gcov:
508528
# Verify the tools are present

spin/cmds/util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def run(
4141
4242
Other arguments and keywords are passed directly to `subprocess.run`.
4343
44+
45+
Returns
46+
-------
47+
p : CompletedProcess
4448
"""
4549
if cwd:
4650
if echo:

spin/tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,10 @@ def pre_post_test():
1818
os.chdir(cwd)
1919
util.run(["git", "clean", "-xdf"], cwd="example_pkg")
2020
os.chdir(cwd)
21+
22+
23+
@pytest.fixture
24+
def editable_install():
25+
util.run(["pip", "install", "--quiet", "--no-build-isolation", "-e", "."])
26+
yield
27+
util.run(["pip", "uninstall", "--quiet", "-y", "example_pkg"])

spin/tests/test_build_cmds.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,6 @@ def test_recommend_run_python():
8888
), "Failed to recommend `python run python file.py`"
8989

9090

91-
def test_test():
92-
"""Does the test command run?"""
93-
spin("test")
94-
95-
96-
def test_test_with_pythonpath():
97-
"""Does `spin test` work when PYTHONPATH is set?"""
98-
spin("test", env={**os.environ, "PYTHONPATH": "/tmp"})
99-
100-
10191
def test_sdist():
10292
spin("sdist")
10393

spin/tests/test_editable.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
import pytest
21
from testutil import spin, stdout
32

4-
from spin.cmds.util import run
5-
6-
7-
@pytest.fixture
8-
def editable_install():
9-
run(["pip", "install", "--quiet", "--no-build-isolation", "-e", "."])
10-
yield
11-
run(["pip", "uninstall", "--quiet", "-y", "example_pkg"])
12-
133

144
def test_detect_editable(editable_install):
155
assert "Editable install of same source detected" in stdout(

spin/tests/test_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os
2+
3+
from testutil import spin
4+
5+
6+
def test_test():
7+
"""Does the test command run?"""
8+
spin("test")
9+
10+
11+
def test_test_with_pythonpath():
12+
"""Does `spin test` work when PYTHONPATH is set?"""
13+
spin("test", env={**os.environ, "PYTHONPATH": "/tmp"})
14+
15+
16+
def test_test_file_spec():
17+
spin("test", "example_pkg/submodule/tests/test_submodule.py")
18+
19+
20+
def test_test_module_spec():
21+
spin("test", "example_pkg.submodule")
22+
23+
24+
def test_test_editable_file_spec(editable_install):
25+
spin("test", "example_pkg/submodule/tests/test_submodule.py")
26+
27+
28+
def test_test_editable_module_spec(editable_install):
29+
spin("test", "example_pkg.submodule")

0 commit comments

Comments
 (0)