Skip to content

Commit

Permalink
test: Run examples in CI
Browse files Browse the repository at this point in the history
Create `example/test_examples.py` so `pytest` runs all the examples and
checks their output.
  • Loading branch information
jmgate committed Oct 3, 2023
1 parent 2ceb579 commit 6642a17
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 4 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ jobs:
-r example/requirements.txt \
-r test/requirements.txt
- name: Test install
run: python3 -m pip install .

- name: Test with pytest
run: python3 -m pytest --cov=reverse_argparse test/
run: python3 -m pytest --cov=reverse_argparse example/ test/

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

- name: Test install
run: python3 -m pip install .

- name: Check documentation spelling
run: make spelling
working-directory: ./doc
Expand Down
Empty file modified example/basic.py
100644 → 100755
Empty file.
Empty file modified example/default_values.py
100644 → 100755
Empty file.
Empty file modified example/post_processing.py
100644 → 100755
Empty file.
Empty file modified example/pretty_printing.py
100644 → 100755
Empty file.
Empty file modified example/relative_references.py
100644 → 100755
Empty file.
Empty file modified example/subparsers.py
100644 → 100755
Empty file.
129 changes: 129 additions & 0 deletions example/test_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/usr/bin/env python3
"""Run all the examples and ensure their output is correct."""
import re
import shlex
import subprocess # nosec B404
from datetime import datetime, timedelta
from pathlib import Path


def test_basic() -> None:
example = Path(__file__).parent / "basic.py"
result = subprocess.run(
[example, "--foo", "bar"],
capture_output=True,
check=True,
text=True,
) # nosec B603
assert (
result.stdout
== """
The effective command line invocation was:
basic.py --foo bar
""".lstrip()
)


def test_default_values() -> None:
example = Path(__file__).parent / "default_values.py"
result = subprocess.run(
[example, "--foo", "bar"],
capture_output=True,
check=True,
text=True,
) # nosec B603
assert (
result.stdout
== """
The effective command line invocation was:
default_values.py --foo bar --bar spam --baz 42
""".lstrip()
)


def test_relative_references() -> None:
example = Path(__file__).parent / "relative_references.py"
result = subprocess.run(
[example, "--src", "bar.txt"],
capture_output=True,
check=True,
text=True,
) # nosec B603
assert (
"""
The effective command line invocation was:
relative_references.py --bar spam --baz 42 --src
""".strip()
in result.stdout
)
assert re.search(r"--src /\S+/bar\.txt", result.stdout)


def test_post_processing() -> None:
example = Path(__file__).parent / "post_processing.py"
result = subprocess.run(
[example, "--before", "'30 minutes ago'"],
capture_output=True,
check=True,
text=True,
) # nosec B603
assert (
"""
The effective command line invocation was:
post_processing.py --bar spam --baz 42 --before
""".strip()
in result.stdout
)
thirty_miutes_ago = datetime.now() - timedelta(minutes=30)
time_from_example = datetime.strptime(
shlex.split(result.stdout)[-1], "%Y-%m-%d %H:%M:%S.%f"
)
assert thirty_miutes_ago - time_from_example < timedelta(seconds=1)


def test_pretty_printing() -> None:
example = Path(__file__).parent / "pretty_printing.py"
result = subprocess.run(
[example, "--foo", "eggs", "--src", "file.txt", "--before", "'today'"],
capture_output=True,
check=True,
text=True,
) # nosec B603
assert (
"""
The effective command line invocation was:
pretty_printing.py \\
--foo eggs \\
--bar spam \\
--baz 42 \\
""".strip()
in result.stdout
)
assert re.search(r"--src /\S+/file\.txt", result.stdout)
today = datetime.now()
time_from_example = datetime.strptime(
shlex.split(result.stdout.splitlines()[-1])[-1],
"%Y-%m-%d %H:%M:%S.%f",
)
assert today - time_from_example < timedelta(seconds=1)


def test_subparsers() -> None:
example = Path(__file__).parent / "subparsers.py"
result = subprocess.run(
[example, "foo", "--one", "eggs"],
capture_output=True,
check=True,
text=True,
) # nosec B603
assert (
result.stdout
== """
The effective command line invocation was:
subparsers.py \\
foo \\
--one eggs \\
--two spam \\
--three 42
""".lstrip()
)
1 change: 1 addition & 0 deletions test/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

bandit
black
dateparser
flake8
flake8-bugbear
mypy
Expand Down

0 comments on commit 6642a17

Please sign in to comment.