|
| 1 | +# ==================================================================================================================== # |
| 2 | +# _____ _ _ _ _ _ # |
| 3 | +# _ __ _ |_ _|__ ___ | (_)_ __ __ _ / \ ___| |_(_) ___ _ __ ___ # |
| 4 | +# | '_ \| | | || |/ _ \ / _ \| | | '_ \ / _` | / _ \ / __| __| |/ _ \| '_ \/ __| # |
| 5 | +# | |_) | |_| || | (_) | (_) | | | | | | (_| |_ / ___ \ (__| |_| | (_) | | | \__ \ # |
| 6 | +# | .__/ \__, ||_|\___/ \___/|_|_|_| |_|\__, (_)_/ \_\___|\__|_|\___/|_| |_|___/ # |
| 7 | +# |_| |___/ |___/ # |
| 8 | +# ==================================================================================================================== # |
| 9 | +# Authors: # |
| 10 | +# Patrick Lehmann # |
| 11 | +# # |
| 12 | +# License: # |
| 13 | +# ==================================================================================================================== # |
| 14 | +# Copyright 2017-2026 Patrick Lehmann - Bötzingen, Germany # |
| 15 | +# # |
| 16 | +# Licensed under the Apache License, Version 2.0 (the "License"); # |
| 17 | +# you may not use this file except in compliance with the License. # |
| 18 | +# You may obtain a copy of the License at # |
| 19 | +# # |
| 20 | +# http://www.apache.org/licenses/LICENSE-2.0 # |
| 21 | +# # |
| 22 | +# Unless required by applicable law or agreed to in writing, software # |
| 23 | +# distributed under the License is distributed on an "AS IS" BASIS, # |
| 24 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # |
| 25 | +# See the License for the specific language governing permissions and # |
| 26 | +# limitations under the License. # |
| 27 | +# # |
| 28 | +# SPDX-License-Identifier: Apache-2.0 # |
| 29 | +# ==================================================================================================================== # |
| 30 | +# |
| 31 | +from importlib.metadata import version as get_version |
| 32 | +from json import loads as json_loads, JSONDecodeError |
| 33 | +from re import IGNORECASE, MULTILINE |
| 34 | +from shutil import which |
| 35 | +from subprocess import CompletedProcess, run as subprocess_run, TimeoutExpired |
| 36 | +from sys import executable as PYTHON_EXECUTABLE |
| 37 | +from tempfile import TemporaryDirectory |
| 38 | +from typing import ClassVar |
| 39 | +from unittest import TestCase |
| 40 | + |
| 41 | + |
| 42 | +class Testcase(TestCase): |
| 43 | + """ |
| 44 | + Shared base class: resolves the installed console_scripts executable |
| 45 | + once per test class and provides a subprocess-invocation helper. |
| 46 | + """ |
| 47 | + |
| 48 | + ENTRY_POINT_NAME: ClassVar[str] = "myPackage" |
| 49 | + executable: str |
| 50 | + |
| 51 | + @classmethod |
| 52 | + def setUpClass(cls) -> None: |
| 53 | + if (resolved := which(cls.ENTRY_POINT_NAME)) is None: |
| 54 | + raise RuntimeError( |
| 55 | + f"'{cls.ENTRY_POINT_NAME}' not found on PATH. Verify the wheel " |
| 56 | + f"was installed in this environment and that setup.py's " |
| 57 | + f"entry_points {{'console_scripts': " |
| 58 | + f"['{cls.ENTRY_POINT_NAME}=myPackage.CLI:main']}} was picked " |
| 59 | + f"up correctly." |
| 60 | + ) |
| 61 | + cls.executable = resolved |
| 62 | + |
| 63 | + def RunEntrypoint( |
| 64 | + self, |
| 65 | + *args: str, |
| 66 | + timeout: float = 10.0, |
| 67 | + input_text: str | None = None, |
| 68 | + env: dict | None = None, |
| 69 | + cwd: str | None = None, |
| 70 | + ) -> CompletedProcess: |
| 71 | + return subprocess_run( |
| 72 | + [self.executable, *args], |
| 73 | + capture_output=True, |
| 74 | + text=True, |
| 75 | + timeout=timeout, |
| 76 | + input=input_text, |
| 77 | + env=env, |
| 78 | + cwd=cwd, |
| 79 | + ) |
| 80 | + |
| 81 | + def RunModule(self, *args: str, timeout: float = 10.0) -> CompletedProcess: |
| 82 | + """ |
| 83 | + Invokes `python -m myPackage.CLI` directly, bypassing the |
| 84 | + console_scripts shim. Use to isolate whether a failure originates |
| 85 | + in the entry-point wiring vs. the CLI logic itself. |
| 86 | + """ |
| 87 | + return subprocess_run( |
| 88 | + [PYTHON_EXECUTABLE, "-m", "myPackage.CLI", *args], |
| 89 | + capture_output=True, |
| 90 | + text=True, |
| 91 | + timeout=timeout, |
| 92 | + ) |
| 93 | + |
| 94 | + def assertExitCode(self, result: CompletedProcess, expected: int) -> None: |
| 95 | + self.assertEqual( |
| 96 | + expected, |
| 97 | + result.returncode, |
| 98 | + msg=( |
| 99 | + f"args={result.args!r}\n" |
| 100 | + f"--- stdout ---\n{result.stdout}\n" |
| 101 | + f"--- stderr ---\n{result.stderr}" |
| 102 | + ), |
| 103 | + ) |
| 104 | + |
| 105 | + |
| 106 | +class Basic(Testcase): |
| 107 | + def test_NoArguments(self) -> None: |
| 108 | + result = self.RunEntrypoint() |
| 109 | + self.assertExitCode(result, 0) |
0 commit comments