From 8a922f7cfc48890a00803b93fe0d32fbe4a7857b Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Mon, 30 Sep 2024 14:24:48 +0200 Subject: [PATCH] behave: better sub-project structure --- behave/README.md | 14 ++++++ behave/{features => }/environment.py | 3 +- behave/{features => }/steps/other.py | 0 behave/testlib/__init__.py | 1 + behave/testlib/commands.py | 61 ++++++++++++++++++++++++ behave/{testlib.py => testlib/mock.py} | 65 +++----------------------- 6 files changed, 84 insertions(+), 60 deletions(-) create mode 100644 behave/README.md rename behave/{features => }/environment.py (96%) rename behave/{features => }/steps/other.py (100%) create mode 100644 behave/testlib/__init__.py create mode 100644 behave/testlib/commands.py rename behave/{testlib.py => testlib/mock.py} (72%) diff --git a/behave/README.md b/behave/README.md new file mode 100644 index 000000000..f078414d8 --- /dev/null +++ b/behave/README.md @@ -0,0 +1,14 @@ +BDD for Mock +============ + +This test-suite can destroy your system! Not intentionally, but some steps +require us to use root (e.g. install or remove packages). **Never** execute +this test suite on your host system, allocate some disposable machine. + +How to run the tests +-------------------- + +1. Install the Mock RPM that you want to test. + +2. Run `$ behave` command in this directory, with `--tags tagname` if you want + to test only subset of all provided scenarios. diff --git a/behave/features/environment.py b/behave/environment.py similarity index 96% rename from behave/features/environment.py rename to behave/environment.py index 01bc23666..abcb0023a 100644 --- a/behave/features/environment.py +++ b/behave/environment.py @@ -11,7 +11,8 @@ import requests -from testlib import Mock, no_output +from testlib.mock import Mock +from testlib.commands import no_output def _random_string(length): diff --git a/behave/features/steps/other.py b/behave/steps/other.py similarity index 100% rename from behave/features/steps/other.py rename to behave/steps/other.py diff --git a/behave/testlib/__init__.py b/behave/testlib/__init__.py new file mode 100644 index 000000000..a54a12ca0 --- /dev/null +++ b/behave/testlib/__init__.py @@ -0,0 +1 @@ +""" Helper library for Mock's BDD """ diff --git a/behave/testlib/commands.py b/behave/testlib/commands.py new file mode 100644 index 000000000..54304b900 --- /dev/null +++ b/behave/testlib/commands.py @@ -0,0 +1,61 @@ +""" +Executing commands in Mock's behave test suite. +""" + +from contextlib import contextmanager +import io +import shlex +import subprocess +import sys + + +@contextmanager +def no_output(): + """ + Suppress stdout/stderr when it is not captured by behave + https://github.com/behave/behave/issues/863 + """ + real_out = sys.stdout, sys.stderr + sys.stdout = io.StringIO() + sys.stderr = io.StringIO() + yield + sys.stdout, sys.stderr = real_out + + +def quoted_cmd(cmd): + """ shell quoted cmd array as string """ + return " ".join(shlex.quote(arg) for arg in cmd) + + +def run(cmd): + """ + Return exitcode, stdout, stderr. It's bad there's no such thing in behave + directly. + """ + try: + process = subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + ) + stdout, stderr = process.communicate() + print(f"Command exit status {process.returncode} in: {quoted_cmd(cmd)}") + if stdout: + print("stdout:") + print(stdout) + if stderr: + print("stderr:") + print(stderr) + return process.returncode, stdout, stderr + except (FileNotFoundError, PermissionError) as e: + print(f"Error running command {quoted_cmd(cmd)}: {e}") + return -1, "", str(e) + + +def run_check(cmd): + """ run, but check nonzero exit status """ + retcode, stdout, stderr = run(cmd) + if retcode != 0: + raise Exception(f"Command failed with return code {retcode}: {quoted_cmd(cmd)}\n{stderr}") + return stdout, stderr diff --git a/behave/testlib.py b/behave/testlib/mock.py similarity index 72% rename from behave/testlib.py rename to behave/testlib/mock.py index 2a4322c79..0bdb28268 100644 --- a/behave/testlib.py +++ b/behave/testlib/mock.py @@ -1,64 +1,11 @@ -""" helpers for Copr BDD tests """ +""" +Stateful "Mock" command object. +""" -from contextlib import contextmanager -import io from pathlib import Path -import shlex import os -import subprocess -import sys - - -@contextmanager -def no_output(): - """ - Suppress stdout/stderr when it is not captured by behave - https://github.com/behave/behave/issues/863 - """ - real_out = sys.stdout, sys.stderr - sys.stdout = io.StringIO() - sys.stderr = io.StringIO() - yield - sys.stdout, sys.stderr = real_out - - -def quoted_cmd(cmd): - """ shell quoted cmd array as string """ - return " ".join(shlex.quote(arg) for arg in cmd) - - -def run(cmd): - """ - Return exitcode, stdout, stderr. It's bad there's no such thing in behave - directly. - """ - try: - process = subprocess.Popen( - cmd, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - universal_newlines=True, - ) - stdout, stderr = process.communicate() - print(f"Command exit status {process.returncode} in: {quoted_cmd(cmd)}") - if stdout: - print("stdout:") - print(stdout) - if stderr: - print("stderr:") - print(stderr) - return process.returncode, stdout, stderr - except (FileNotFoundError, PermissionError) as e: - print(f"Error running command {quoted_cmd(cmd)}: {e}") - return -1, "", str(e) - - -def run_check(cmd): - """ run, but check nonzero exit status """ - retcode, stdout, stderr = run(cmd) - if retcode != 0: - raise Exception(f"Command failed with return code {retcode}: {quoted_cmd(cmd)}\n{stderr}") - return stdout, stderr + +from testlib.commands import run_check class Mock: @@ -187,4 +134,4 @@ def assert_is_subset(set_a, set_b): """ assert that SET_A is subset of SET_B """ if set_a.issubset(set_b): return - raise AssertionError("Set {} is not a subset of {}".format(set_a, set_b)) + raise AssertionError(f"Set {set_a} is not a subset of {set_b}")