From c358fa1c8f9c9928be9e544257bb09bfbb1497db Mon Sep 17 00:00:00 2001 From: Jordan Gillard Date: Sun, 30 Jun 2024 11:32:39 -0400 Subject: [PATCH] test: Create test to run executable --- .github/workflows/tox.yaml | 2 +- .gitignore | 2 ++ pyproject.toml | 2 +- src/shadowfinder/__init__.py | 3 ++- src/shadowfinder/__main__.py | 6 +++--- tests/test_executable.py | 33 +++++++++++++++++++++++++++++++++ 6 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 tests/test_executable.py diff --git a/.github/workflows/tox.yaml b/.github/workflows/tox.yaml index c58c9b3..f0a6795 100644 --- a/.github/workflows/tox.yaml +++ b/.github/workflows/tox.yaml @@ -1,4 +1,4 @@ -name: pytest +name: tox on: push: diff --git a/.gitignore b/.gitignore index df56374..0a7d4ae 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ __pycache__/ *.py[cod] *$py.class +dist/ # Environments .env @@ -11,6 +12,7 @@ venv/ ENV/ env.bak/ venv.bak/ +.tox/ # Output files *.png diff --git a/pyproject.toml b/pyproject.toml index bdb2688..7af22fa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,7 @@ keywords=["shadow", "finder", "locator", "map"] "Bug Tracker" = "https://github.com/bellingcat/ShadowFinder/issues" [tool.poetry.scripts] -shadowfinder = "shadowfinder:main_entrypoint" +shadowfinder = "shadowfinder.__main__:main" [tool.poetry.dependencies] python = ">=3.9,<3.13" diff --git a/src/shadowfinder/__init__.py b/src/shadowfinder/__init__.py index 2d5fc29..df20885 100644 --- a/src/shadowfinder/__init__.py +++ b/src/shadowfinder/__init__.py @@ -1,3 +1,4 @@ from .shadowfinder import ShadowFinder +from .cli import ShadowFinderCli -__all__ = ["ShadowFinder"] +__all__ = ["ShadowFinder", "ShadowFinderCli"] diff --git a/src/shadowfinder/__main__.py b/src/shadowfinder/__main__.py index 0816e58..5ef7a8a 100644 --- a/src/shadowfinder/__main__.py +++ b/src/shadowfinder/__main__.py @@ -1,10 +1,10 @@ -from cli import ShadowFinderCli +from . import ShadowFinderCli import fire -def main_entrypoint(): +def main(): fire.Fire(ShadowFinderCli) if __name__ == "__main__": - main_entrypoint() + main() diff --git a/tests/test_executable.py b/tests/test_executable.py new file mode 100644 index 0000000..5adc9ce --- /dev/null +++ b/tests/test_executable.py @@ -0,0 +1,33 @@ +""" +The tests in this file test that running shadowfinder as an executable behaves +as expected. +""" +import subprocess + + +def test_executable_without_args(): + """Tests that running shadowfinder without any arguments returns the CLI's help string and 0 exit code.""" + # GIVEN + expected = """ +NAME + shadowfinder + +SYNOPSIS + shadowfinder COMMAND + +COMMANDS + COMMAND is one of the following: + + find + Find the shadow length of an object given its height and the date and time. + + find_sun + Locate a shadow based on the solar altitude angle and the date and time. +""" + + # WHEN + result = subprocess.run(['shadowfinder'], capture_output=True, text=True) + + # THEN + assert result.returncode == 0 + assert result.stdout.strip() == expected.strip()