test: Ensure tests run against correct version

Update the `inv --test.local` command to ensure that tests are run against
the correct version of the package. The invoke command has been updated to:

- Ensure the `anybadge` command is not available on the system to start with

- Ensure `pip` is available

- Pip install the local project

- Run tests

- Pip uninstall `anybadge`

The net result is that invoke will be more robust in terms of making sure the
installed version is in sync when running CLI tests.
This commit is contained in:
Jon Grace-Cox
2025-01-11 17:19:59 -05:00
parent 6199d65ee7
commit 575833bf19
3 changed files with 55 additions and 3 deletions

View File

@@ -5,6 +5,7 @@ pytest
pytest-cov
requests
setuptools
sh
tox
types-requests
wheel

View File

@@ -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... ")

View File

@@ -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):