diff --git a/build-requirements.txt b/build-requirements.txt index ea058e3..352bf79 100644 --- a/build-requirements.txt +++ b/build-requirements.txt @@ -5,6 +5,7 @@ pytest pytest-cov requests setuptools +sh tox types-requests wheel diff --git a/tasks/test.py b/tasks/test.py index 480012f..a238f22 100644 --- a/tasks/test.py +++ b/tasks/test.py @@ -1,6 +1,7 @@ import subprocess from pathlib import Path from time import sleep +import sys from invoke import task @@ -12,11 +13,46 @@ DOCKER_TAG = "test-anybadge:latest" def local(c): """Run local tests.""" print("Running local tests...") + + print("Ensuring pip is installed") subprocess.run( - "pytest --doctest-modules --cov=anybadge --cov-report html:htmlcov anybadge tests", + f"{sys.executable} -m ensurepip", shell=True, ) + print("Ensuring anybagde command is not already installed") + result = subprocess.run( + "which anybadge", + shell=True, + ) + if result.returncode == 0: + raise RuntimeError("anybadge command is already installed. Uninstall it first.") + + print("Installing local package to current virtual environment") + subprocess.run( + f"{sys.executable} -m pip install .", + cwd=str(PROJECT_DIR), + shell=True, + ) + + retval = 0 + try: + subprocess.run( + f"{sys.executable} -m pytest --doctest-modules --cov=anybadge --cov-report html:htmlcov anybadge tests", + shell=True, + ) + except Exception as e: + print(f"Error running tests: {e}") + retval = 1 + + print("Uninstalling local package from current virtual environment") + subprocess.run( + f"{sys.executable} -m pip uninstall anybadge -y", + cwd=str(PROJECT_DIR), + shell=True, + ) + sys.exit(retval) + def build_test_docker_image(): print("Building test docker image... ") diff --git a/tests/test_anybadge.py b/tests/test_anybadge.py index 47ad8f9..0ec4ac4 100644 --- a/tests/test_anybadge.py +++ b/tests/test_anybadge.py @@ -3,9 +3,12 @@ from pathlib import Path from unittest import TestCase from anybadge import Badge from anybadge.cli import main, parse_args +import sys +import sh TESTS_DIR = Path(__file__).parent +PROJECT_ROOT = TESTS_DIR.parent class TestAnybadge(TestCase): @@ -401,8 +404,20 @@ class TestAnybadge(TestCase): def test_module_same_output_as_main_cli(self): """Test that `python -m anybadge` is equivalent to calling `anybadge` directly.""" - output_module = subprocess.check_output(["python", "-m", "anybadge", "--help"]) - output_script = subprocess.check_output(["anybadge", "--help"]) + + python_executable = sys.executable + anybadge_executable = Path(python_executable).parent / "anybadge" + + python_cmd = sh.Command(sys.executable) + + env = { + "PYTHONPATH": str(PROJECT_ROOT), + } + output_module = python_cmd("-m", "anybadge", "--help", _env=env) + + anybadge_executable = Path(python_executable).parent / "anybadge" + output_script = sh.Command(anybadge_executable)("--help") + self.assertEqual(output_module, output_script) def test_badge_with_no_label(self):