From 1f5423afec09cb88b133a3824dbd5183e8a01ed3 Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Mon, 30 Sep 2024 14:40:13 +0200 Subject: [PATCH] behave: make linters happy --- behave/environment.py | 4 ++-- behave/pylintrc | 4 +++- behave/steps/other.py | 22 +++++++++++----------- behave/testlib/commands.py | 25 +++++++++++++------------ 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/behave/environment.py b/behave/environment.py index abcb0023a..0884517de 100644 --- a/behave/environment.py +++ b/behave/environment.py @@ -21,8 +21,8 @@ def _random_string(length): def _download(context, url): - print('Downloading {}'.format(url)) - req = requests.get(url) + print(f'Downloading {url}') + req = requests.get(url, timeout=60) filename = os.path.join(context.workdir, os.path.basename(url)) with open(filename, 'wb') as dfd: dfd.write(req.content) diff --git a/behave/pylintrc b/behave/pylintrc index d0be57849..e5283377f 100644 --- a/behave/pylintrc +++ b/behave/pylintrc @@ -10,4 +10,6 @@ # cyclic-import # Seems like cyclic-import is just a style check which is not going to be # fixed: https://github.com/PyCQA/pylint/issues/6983 -disable=import-error,cyclic-import +# function-redefined +# This is a Behave's policy to create all step methods as `step_impl()`. +disable=import-error,cyclic-import,function-redefined diff --git a/behave/steps/other.py b/behave/steps/other.py index b0557c98d..3cd92e382 100644 --- a/behave/steps/other.py +++ b/behave/steps/other.py @@ -36,11 +36,11 @@ def _first_int(string, max_lines=20): first_word = line.split()[0] if first_word.isdigit(): return first_word - raise Exception("unexpected dnf history output") + raise RuntimeError("unexpected dnf history output") def add_cleanup_last_transaction(context): - # TODO: DNF5 support https://github.com/rpm-software-management/dnf5/issues/140 + # DNF5 support https://github.com/rpm-software-management/dnf5/issues/140 dnf = ["sudo", "/usr/bin/dnf-3", "-y", "history"] _, out, _ = run(dnf + ["list"]) transaction_id = _first_int(out) @@ -52,13 +52,13 @@ def _revert_transaction(_context): context.add_cleanup(_revert_transaction, context) -@given(u'an unique mock namespace') +@given('an unique mock namespace') def step_impl(context): - print("using uniqueext {}".format(context.uniqueext)) + print(f"using uniqueext {context.uniqueext}") context.uniqueext_used = True -@given(u'the {package} package {state} installed on host') +@given('the {package} package {state} installed on host') def step_impl(context, package, state): """ Install the package, and uninstall in post- action. If state is "not", then @@ -93,12 +93,12 @@ def _uninstall_pkg(_context): context.add_cleanup(_uninstall_pkg, context) -@given(u'pre-intitialized chroot') +@given('pre-intitialized chroot') def step_impl(context): context.mock.init() -@given(u'a custom third-party repository is used for builds') +@given('a custom third-party repository is used for builds') def step_impl(context): context.add_repos.append( "https://raw.githubusercontent.com/rpm-software-management/" @@ -112,7 +112,7 @@ def step_impl(context): run(["createrepo_c", context.local_repo]) -@given(u'the local repo contains a "{rpm}" RPM') +@given('the local repo contains a "{rpm}" RPM') def step_impl(context, rpm): rpm = context.download_rpm(rpm) shutil.move(rpm, context.local_repo) @@ -124,14 +124,14 @@ def step_impl(context): context.add_repos.append(context.local_repo) -@when(u'a build is depending on third-party repo requested') -@when(u'a build which requires the "always-installable" RPM is requested') +@when('a build is depending on third-party repo requested') +@when('a build which requires the "always-installable" RPM is requested') def step_impl(context): local_file = context.download_rpm("buildrequires-always-installable") context.mock.rebuild([local_file]) -@then(u'the build succeeds') +@then('the build succeeds') def step_impl(context): assert os.path.exists(context.mock.resultdir) rpms = glob.glob(os.path.join(context.mock.resultdir, "*.rpm")) diff --git a/behave/testlib/commands.py b/behave/testlib/commands.py index 54304b900..d2e74d769 100644 --- a/behave/testlib/commands.py +++ b/behave/testlib/commands.py @@ -33,21 +33,21 @@ def run(cmd): directly. """ try: - process = subprocess.Popen( + with 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 + ) as process: + stdout, stderr = process.communicate() + print(f"Exit code: {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) @@ -57,5 +57,6 @@ 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}") + raise RuntimeError(f"Command failed with return code {retcode}: " + f"{quoted_cmd(cmd)}\n{stderr}") return stdout, stderr