Skip to content

Commit

Permalink
behave: better sub-project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
praiskup committed Sep 30, 2024
1 parent f3a128d commit 8a922f7
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 60 deletions.
14 changes: 14 additions & 0 deletions behave/README.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 2 additions & 1 deletion behave/features/environment.py → behave/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions behave/testlib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
""" Helper library for Mock's BDD """
61 changes: 61 additions & 0 deletions behave/testlib/commands.py
Original file line number Diff line number Diff line change
@@ -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
65 changes: 6 additions & 59 deletions behave/testlib.py → behave/testlib/mock.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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}")

0 comments on commit 8a922f7

Please sign in to comment.