Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 0 additions & 122 deletions .github/workflows/test.sh

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install -y gdb
- name: Tests
- name: Tests PyTest
run: |
pipx run nox --forcecolor -s tests
pipx run nox --forcecolor -s test
8 changes: 2 additions & 6 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@


@nox.session
def tests(session: nox.Session) -> None:
"""
Run the unit and regular tests.
"""
session.install(".", "pytest", "build")
def test(session: nox.Session) -> None:
session.install(".", "pytest", "build", "meson-python", "ninja")
session.run("pytest", "spin", *session.posargs)
session.run("bash", ".github/workflows/test.sh", external=True)
20 changes: 20 additions & 0 deletions spin/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os

import pytest

from spin import util


@pytest.fixture(autouse=True)
def pre_post_test():
# Pre-test code
cwd = os.getcwd()
os.chdir("example_pkg")

try:
yield
finally:
# Post test code
os.chdir(cwd)
util.run(["git", "clean", "-xdf"], cwd="example_pkg")
os.chdir(cwd)
159 changes: 159 additions & 0 deletions spin/tests/test_build_cmds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import os
import subprocess
import sys
import tempfile
from pathlib import Path

import pytest

import spin as libspin
from spin.cmds.util import run

skip_on_windows = pytest.mark.skipif(
sys.platform.startswith("win"), reason="Skipped; platform is Windows"
)

on_linux = pytest.mark.skipif(
not sys.platform.startswith("linux"), reason="Skipped; platform not Linux"
)


def spin(*args, **user_kwargs):
default_kwargs = {
"stdout": subprocess.PIPE,
"stderr": subprocess.PIPE,
"sys_exit": True,
}
return run(["spin"] + list(args), **{**default_kwargs, **user_kwargs})


def stdout(p):
return p.stdout.decode("utf-8").strip()


def stderr(p):
return p.stderr.decode("utf-8").strip()


def test_get_version():
p = spin("--version")
assert stdout(p) == f"spin {libspin.__version__}"


def test_basic_build():
"""Does the package build?"""
spin("build")

assert Path("build").exists(), "`build` folder not created after `spin build`"
assert Path(
"build-install"
).exists(), "`build-install` folder not created after `spin build`"


def test_debug_builds():
"""Does spin generate gcov debug output files?"""
spin("build", "--gcov")

debug_files = Path(".").rglob("*.gcno")
assert len(list(debug_files)) != 0, "debug files not generated for gcov build"


def test_expand_pythonpath():
"""Does an $ENV_VAR get expanded in `spin run`?"""
output = spin("run", "echo $PYTHONPATH")
assert any(
p in stdout(output) for p in ("site-packages", "dist-packages")
), f"Expected value of $PYTHONPATH, got {stdout(output)} instead"


def test_run_stdout():
"""Ensure `spin run` only includes command output on stdout."""
p = spin(
"run",
sys.executable,
"-c",
"import sys; del sys.path[0]; import example_pkg; print(example_pkg.__version__)",
)
assert (
stdout(p) == "0.0.0dev0"
), f"`spin run` stdout did not yield version, but {stdout(p)}"


def test_editable_conflict():
"""Do we warn when a conflicting editable install is present?"""
try:
run(["pip", "install", "--quiet", "-e", "."])
assert "Warning! An editable installation" in stdout(
spin("run", "ls")
), "Failed to detect and warn about editable install"
finally:
run(["pip", "uninstall", "--quiet", "-y", "example_pkg"])


# Detecting whether a file is executable is not that easy on Windows,
# as it seems to take into consideration whether that file is associated as an executable.
@skip_on_windows
def test_recommend_run_python():
"""If `spin run file.py` is called, is `spin run python file.py` recommended?"""
with tempfile.NamedTemporaryFile(suffix=".py") as f:
p = spin("run", f.name, sys_exit=False)
assert "Did you mean to call" in stdout(
p
), "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")


def test_example():
spin("example")


def test_docs():
run(["pip", "install", "--quiet", "sphinx"])
spin("docs")


def test_spin_install():
cwd = os.getcwd()
spin("install")
with tempfile.TemporaryDirectory() as d:
try:
os.chdir(d)
p = run(
[
sys.executable,
"-c",
"import example_pkg; print(example_pkg.__version__)",
],
stdout=subprocess.PIPE,
)
assert stdout(p) == "0.0.0dev0"
finally:
os.chdir(cwd)
run(["pip", "uninstall", "-y", "--quiet", "example_pkg"])


@on_linux
def test_gdb():
p = spin(
"gdb",
"-c",
'import example_pkg; example_pkg.echo("hi")',
"--",
"--eval",
"run",
"--batch",
)
assert "hi" in stdout(p)
28 changes: 28 additions & 0 deletions spin/tests/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
import subprocess

from spin.cmds import util

PKG_NAME = "example_pkg"


def assert_cmd(cmd, *args, **kwargs) -> str:
cwd = os.getcwd()
p = util.run(
cmd,
*args,
cwd=PKG_NAME,
replace=False,
sys_exit=False,
output=False,
echo=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
**kwargs,
)
os.chdir(cwd)
stdout = p.stdout.decode("utf-8").strip()
if not p.returncode == 0:
print(stdout)
raise AssertionError(f"[{cmd}] failed with exit code {p.returncode}")
return p