Skip to content

Commit

Permalink
tests/python: improve reporting when a command fails
Browse files Browse the repository at this point in the history
* represent the command accurately using shell quoting;
* show the exit code;
* show both stdout and stderr.
  • Loading branch information
SpecLad committed Aug 21, 2023
1 parent afefe22 commit a0dfd50
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions tests/python/shared/fixtures/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import pytest
import requests
import shlex
import yaml

from shared.utils.config import ASSETS_DIR, get_server_url
Expand Down Expand Up @@ -103,12 +104,13 @@ def _run(command, capture_output=True):
proc = run(_command) # nosec
return stdout, stderr
except CalledProcessError as exc:
stderr = exc.stderr.decode() or exc.stdout.decode() if capture_output else "see above"
pytest.exit(
f"Command failed: {command}.\n"
f"Error message: {stderr}.\n"
"Add `-s` option to see more details"
)
message = f"Command failed: {' '.join(map(shlex.quote, _command))}."
message += f"\nExit code: {exc.returncode}"
if capture_output:
message += f"\nStandard output:\n{exc.stdout.decode()}"
message += f"\nStandard error:\n{exc.stderr.decode()}"

pytest.exit(message)


def _kube_get_server_pod_name():
Expand Down

0 comments on commit a0dfd50

Please sign in to comment.