mirror of
https://github.com/jongracecox/anybadge.git
synced 2025-07-21 04:11:05 +02:00
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:
@@ -5,6 +5,7 @@ pytest
|
|||||||
pytest-cov
|
pytest-cov
|
||||||
requests
|
requests
|
||||||
setuptools
|
setuptools
|
||||||
|
sh
|
||||||
tox
|
tox
|
||||||
types-requests
|
types-requests
|
||||||
wheel
|
wheel
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
import subprocess
|
import subprocess
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
import sys
|
||||||
|
|
||||||
from invoke import task
|
from invoke import task
|
||||||
|
|
||||||
@@ -12,11 +13,46 @@ DOCKER_TAG = "test-anybadge:latest"
|
|||||||
def local(c):
|
def local(c):
|
||||||
"""Run local tests."""
|
"""Run local tests."""
|
||||||
print("Running local tests...")
|
print("Running local tests...")
|
||||||
|
|
||||||
|
print("Ensuring pip is installed")
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
"pytest --doctest-modules --cov=anybadge --cov-report html:htmlcov anybadge tests",
|
f"{sys.executable} -m ensurepip",
|
||||||
shell=True,
|
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():
|
def build_test_docker_image():
|
||||||
print("Building test docker image... ")
|
print("Building test docker image... ")
|
||||||
|
@@ -3,9 +3,12 @@ from pathlib import Path
|
|||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
from anybadge import Badge
|
from anybadge import Badge
|
||||||
from anybadge.cli import main, parse_args
|
from anybadge.cli import main, parse_args
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import sh
|
||||||
|
|
||||||
TESTS_DIR = Path(__file__).parent
|
TESTS_DIR = Path(__file__).parent
|
||||||
|
PROJECT_ROOT = TESTS_DIR.parent
|
||||||
|
|
||||||
|
|
||||||
class TestAnybadge(TestCase):
|
class TestAnybadge(TestCase):
|
||||||
@@ -401,8 +404,20 @@ class TestAnybadge(TestCase):
|
|||||||
|
|
||||||
def test_module_same_output_as_main_cli(self):
|
def test_module_same_output_as_main_cli(self):
|
||||||
"""Test that `python -m anybadge` is equivalent to calling `anybadge` directly."""
|
"""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)
|
self.assertEqual(output_module, output_script)
|
||||||
|
|
||||||
def test_badge_with_no_label(self):
|
def test_badge_with_no_label(self):
|
||||||
|
Reference in New Issue
Block a user