1from subprocess import Popen, PIPE
2import sys
3
4
5def get_output_error_code(cmd):
6    """Get stdout, stderr, and exit code from running a command"""
7    p = Popen(cmd, stdout=PIPE, stderr=PIPE)
8    out, err = p.communicate()
9    out = out.decode('utf8', 'replace')
10    err = err.decode('utf8', 'replace')
11    return out, err, p.returncode
12
13
14def check_help_output(pkg, subcommand=None):
15    """test that `python -m PKG [subcommand] -h` works"""
16    cmd = [sys.executable, '-m', pkg]
17    if subcommand:
18        cmd.extend(subcommand)
19    cmd.append('-h')
20    out, err, rc = get_output_error_code(cmd)
21    assert rc == 0, err
22    assert "Traceback" not in err
23    assert "Options" in out
24    assert "--help-all" in out
25    return out, err
26
27
28def check_help_all_output(pkg, subcommand=None):
29    """test that `python -m PKG --help-all` works"""
30    cmd = [sys.executable, '-m', pkg]
31    if subcommand:
32        cmd.extend(subcommand)
33    cmd.append('--help-all')
34    out, err, rc = get_output_error_code(cmd)
35    assert rc == 0, err
36    assert "Traceback" not in err
37    assert "Options" in out
38    assert "Class options" in out
39    return out, err
40