Skip to content

Commit 7e0c6b3

Browse files
authored
Fix testing for editable and non-editable installs alike (#213)
Closes #209 #203 #204 #208
2 parents 91517b6 + e8e8943 commit 7e0c6b3

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
@@ -428,8 +428,35 @@ def test(
428428
""" # noqa: E501
429429
cfg = get_config()
430430
distname = cfg.get("project.name", None)
431+
pytest_args = pytest_args or ()
431432

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

449-
package = cfg.get("tool.spin.package", None)
450-
if package is None:
451-
print(
452-
"Please specify `package = packagename` under `tool.spin` section of `pyproject.toml`"
453-
)
454-
raise SystemExit(1)
455-
456-
if (not pytest_args) and (not tests):
457-
tests = package
458-
459476
site_path = _set_pythonpath()
460477
if site_path:
461478
print(f'$ export PYTHONPATH="{site_path}"')
@@ -479,8 +496,8 @@ def test(
479496
if (n_jobs != "1") and ("-n" not in pytest_args):
480497
pytest_args = ("-n", str(n_jobs)) + pytest_args
481498

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

485502
if verbose:
486503
pytest_args = ("-v",) + pytest_args
@@ -503,9 +520,12 @@ def test(
503520
else:
504521
cmd = ["pytest"]
505522

506-
pytest_p = _run(
507-
cmd + list(pytest_args),
508-
)
523+
if not os.path.exists(install_dir):
524+
os.mkdir(install_dir)
525+
526+
cwd = os.getcwd()
527+
pytest_p = _run(cmd + list(pytest_args), cwd=site_path)
528+
os.chdir(cwd)
509529

510530
if gcov:
511531
# Verify the tools are present

0 commit comments

Comments
 (0)