Skip to content

Commit e8e8943

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

15 files changed

Lines changed: 86 additions & 46 deletions

File tree

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

0 commit comments

Comments
 (0)