~ubuntu-branches/debian/sid/grip/sid

« back to all changes in this revision

Viewing changes to tests/test_cli.py

  • Committer: Package Import Robot
  • Author(s): Tiago Ilieve
  • Date: 2016-04-04 14:26:23 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20160404142623-zw16rooy7z3xadvu
Tags: 4.1.0-1
Initial release (Closes: #790611)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Tests the Grip command-line interface.
 
3
"""
 
4
 
 
5
from __future__ import print_function, unicode_literals
 
6
 
 
7
import sys
 
8
from subprocess import PIPE, STDOUT, CalledProcessError, Popen
 
9
 
 
10
import pytest
 
11
from grip.command import usage, version
 
12
 
 
13
 
 
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)
 
18
            self.output = output
 
19
 
 
20
 
 
21
def run(*args, **kwargs):
 
22
    command = kwargs.pop('command', 'grip')
 
23
    stdin = kwargs.pop('stdin', None)
 
24
 
 
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)
 
30
    p.stdin.close()
 
31
    # Wait for process to terminate
 
32
    returncode = p.wait()
 
33
    # Capture any more output that occurred during shutdown
 
34
    try:
 
35
        output += p.communicate()[0]
 
36
    except ValueError:
 
37
        pass
 
38
    # Raise exception on failed process calls
 
39
    if returncode != 0:
 
40
        raise CalledProcessError(returncode, cmd, output=output)
 
41
    return output
 
42
 
 
43
 
 
44
def test_help():
 
45
    assert run('-h') == usage
 
46
    assert run('--help') == usage
 
47
 
 
48
 
 
49
def test_version():
 
50
    assert run('-V') == version + '\n'
 
51
    assert run('--version') == version + '\n'
 
52
 
 
53
 
 
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'
 
59
 
 
60
 
 
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)
 
64
 
 
65
# TODO: Test `cat README.md | ~/.local/bin/grip - --export -` (#152)