summaryrefslogtreecommitdiff
path: root/subplot/runcmd.py
blob: 631d8c0bfe330ae9975958c4f96debf313a99ec5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Some step implementations for running commands and capturing the result.

import logging
import os
import subprocess


# Run a command, capture its stdout, stderr, and exit code in context.
def runcmd(ctx, argv, **kwargs):
    logging.debug(f"Running program")
    logging.debug(f"  cwd={os.getcwd()}")
    logging.debug(f"  argv={argv}")
    logging.debug(f"  kwargs={argv}")
    p = subprocess.Popen(argv, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
    stdout, stderr = p.communicate("")
    ctx["argv"] = argv
    ctx["stdout"] = stdout.decode("utf-8")
    ctx["stderr"] = stderr.decode("utf-8")
    ctx["exit"] = p.returncode


# Check that latest exit code captured by runcmd was a specific one.
def exit_code_is(ctx, wanted):
    logging.debug(f"Verifying exit code is {wanted} (it is {ctx.get('exit')})")
    assert_eq(ctx.get("exit"), wanted)


# Check that latest exit code captured by runcmd was not a specific one.
def exit_code_is_not(ctx, unwanted):
    logging.debug(f"Verifying exit code is NOT {unwanted} (it is {ctx.get('exit')})")
    assert_ne(ctx.get("exit"), wanted)


# Check that latest exit code captured by runcmd was zero.
def exit_code_zero(ctx):
    exit_code_is(ctx, 0)


# Check that latest exit code captured by runcmd was not zero.
def exit_code_nonzero(ctx):
    exit_code_is_not(ctx, 0)


# Check that stdout of latest runcmd contains a specific string.
def stdout_contains(ctx, pattern=None):
    logging.debug(f"Verifying stdout contains {pattern}")
    logging.debug(f"  stdout is {ctx.get('stdout', '')})")
    stdout = ctx.get("stdout", "")
    assert_eq(pattern in stdout, True)


# Check that stdout of latest runcmd does not contain a specific string.
def stdout_does_not_contain(ctx, pattern=None):
    logging.debug(f"Verifying stdout does NOT contain {pattern}")
    logging.debug(f"  stdout is {ctx.get('stdout', '')})")
    stdout = ctx.get("stdout", "")
    assert_eq(pattern not in stdout, True)


# Check that stderr of latest runcmd does contains a specific string.
def stderr_contains(ctx, pattern=None):
    logging.debug(f"Verifying stderr contains {pattern}")
    logging.debug(f"  stderr is {ctx.get('stderr', '')})")
    stderr = ctx.get("stderr", "")
    assert_eq(pattern in stderr, True)


# Check that stderr of latest runcmd does not contain a specific string.
def stderr_does_not_contain(ctx, pattern=None):
    logging.debug(f"Verifying stderr does NOT contain {pattern}")
    logging.debug(f"  stderr is {ctx.get('stderr', '')})")
    stderr = ctx.get("stderr", "")
    assert_eq(pattern not in stderr, True)