2
Tests the Grip command-line interface.
5
from __future__ import print_function, unicode_literals
8
from subprocess import PIPE, STDOUT, CalledProcessError, Popen
11
from grip.command import usage, version
14
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
15
class CalledProcessError(CalledProcessError):
16
def __init__(self, returncode, cmd, output):
17
super(CalledProcessError, self).__init__(returncode, cmd)
21
def run(*args, **kwargs):
22
command = kwargs.pop('command', 'grip')
23
stdin = kwargs.pop('stdin', None)
25
cmd = [command] + list(args)
26
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT,
27
universal_newlines=True)
28
# Sent input as STDIN then close it
29
output, _ = p.communicate(input=stdin)
31
# Wait for process to terminate
33
# Capture any more output that occurred during shutdown
35
output += p.communicate()[0]
38
# Raise exception on failed process calls
40
raise CalledProcessError(returncode, cmd, output=output)
45
assert run('-h') == usage
46
assert run('--help') == usage
50
assert run('-V') == version + '\n'
51
assert run('--version') == version + '\n'
54
def test_bad_command():
55
simple_usage = '\n\n'.join(usage.split('\n\n')[:1])
56
with pytest.raises(CalledProcessError) as excinfo:
57
run('--does-not-exist')
58
assert excinfo.value.output == simple_usage + '\n'
61
# TODO: Figure out how to run the CLI and still capture requests
62
# TODO: Test all Grip CLI commands and arguments
63
# TODO: Test settings wire-up (settings.py, settings_local.py, ~/.grip)
65
# TODO: Test `cat README.md | ~/.local/bin/grip - --export -` (#152)