From e7bd43738e75a3cc7a7396ac70f4557f9220bfd2 Mon Sep 17 00:00:00 2001 From: "d.kovalenko" Date: Tue, 12 May 2026 23:38:57 +0300 Subject: [PATCH] New command line option "--version" --- README.md | 1 + src/pgpro_pytest_html_merger/__main__.py | 15 ++++++++++++--- tests/integration/e2eworkspace.py | 2 +- tests/integration/test_set001__cli.py | 20 +++++++++++++++++++- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3c2a6dd..f683856 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ pgpro-pytest-html-merger -i ./unit-tests -i ./e2e-tests extra-report.html -o ful | Argument | Shorthand | Description | Default | | :--- | :--- | :--- | :--- | +| `--version` | | Show program's version number and exit | None | | `--input-dir` | `-i` | Directory containing HTML reports (can be used multiple times) | None | | `--out` | `-o` | Name of the output HTML report | `merged.html` | | `--title` | `-t` | Title of the output HTML report | None | diff --git a/src/pgpro_pytest_html_merger/__main__.py b/src/pgpro_pytest_html_merger/__main__.py index b08ea80..3845497 100644 --- a/src/pgpro_pytest_html_merger/__main__.py +++ b/src/pgpro_pytest_html_merger/__main__.py @@ -14,6 +14,8 @@ from packaging.version import Version +from . import __version__ as current_version + log = logging.getLogger(__name__) @@ -23,6 +25,13 @@ def parse_arguments(): epilog="Example: pgpro-pytest-html-merger -i ./reports -o summary.html --title 'Nightly Build'", # noqa: E501 ) + command_parser.add_argument( + "--version", + action="version", + version=f"%(prog)s {current_version}", + help="Show program's version number and exit", + ) + command_parser.add_argument( "--out", "-o", @@ -591,10 +600,10 @@ def _process_test(self, test: typing.Dict[str, typing.Any]) -> None: def process_report(self, report_path): self._report_count += 1 - html_doc = "" with open(report_path, "r") as f: - html_doc = f.read() - soup = bs4.BeautifulSoup(html_doc, features="html.parser") + soup = bs4.BeautifulSoup(f, features="html.parser") + + assert isinstance(soup, bs4.BeautifulSoup) html_version = __class__._extract_pytest_html_version( report_path, diff --git a/tests/integration/e2eworkspace.py b/tests/integration/e2eworkspace.py index 15818d2..406ad3b 100644 --- a/tests/integration/e2eworkspace.py +++ b/tests/integration/e2eworkspace.py @@ -82,7 +82,7 @@ def generate_report( f"but failed to generate report at: {report_path}" ) - return report_path + return str(report_path) def run_merger(self, args: list): env = os.environ.copy() diff --git a/tests/integration/test_set001__cli.py b/tests/integration/test_set001__cli.py index 6de6383..14a9f64 100644 --- a/tests/integration/test_set001__cli.py +++ b/tests/integration/test_set001__cli.py @@ -1,6 +1,10 @@ import subprocess import os import typing +import sys +import re + +from src.pgpro_pytest_html_merger import __version__ as prog_version # ////////////////////////////////////////////////////////////////////////////// @@ -14,7 +18,7 @@ def run_cli(args: typing.List[str]) -> subprocess.CompletedProcess: env["PYTHONPATH"] = "src" + os.pathsep + env.get("PYTHONPATH", "") return subprocess.run( - ["python3", "-m", "pgpro_pytest_html_merger"] + args, + [sys.executable, "-m", "pgpro_pytest_html_merger"] + args, capture_output=True, text=True, env=env, @@ -69,6 +73,7 @@ def test_cli_help(): assert "merge multiple pytest-html reports" in output assert "--input-dir" in output assert "--out" in output + assert "--version" in output return @@ -160,4 +165,17 @@ def test_cli_error_on_duplicate_files(tmp_path): return +# ------------------------------------------------------------------------ +def test_cli_version(): + """ + Check that --version prints the version and exits with code 0. + """ + result = run_cli(["--version"]) + + assert result.returncode == 0 + # We check that the output contains the version of our utility. + assert re.search(re.escape(" " + prog_version) + "$", result.stdout) is not None + return + + # //////////////////////////////////////////////////////////////////////////////