Skip to content

Commit

Permalink
Add command-line interface tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seanh committed Oct 24, 2014
1 parent 1eb6a7a commit e002509
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 7 deletions.
12 changes: 5 additions & 7 deletions losser/losser.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,10 @@ def _process_dict(pattern_path, dict_, case_sensitive=False, **kwargs):
return result


def main(args=None):
# This should:
# - Read input data from stdin (JSON, also support CSV?)
# - Print output data to stdout (CSV, JSON lines)
# - Print errors and diagnostics to stderr, not stdout
# - Exit with meaningful exit status
def main(args=None, table_function=None):

if table_function is None:
table_function = table

# Parse the command-line arguments.
parser = argparse.ArgumentParser(
Expand All @@ -260,7 +258,7 @@ def main(args=None):

dicts = json.loads(input_data)

csv_string = table(dicts, parsed_args.columns, csv=True)
csv_string = table_function(dicts, parsed_args.columns, csv=True)
sys.stdout.write(csv_string)


Expand Down
99 changes: 99 additions & 0 deletions losser/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""Tests for the command-line argument parsing."""
import mock
import losser
import os
import os.path
import StringIO
import inspect


devnull = open(os.devnull, 'w')


@mock.patch('sys.stderr', devnull)
def test_no_columns_argument():
"""It should exit with status 2 if there's no --columns argument."""
table_function = mock.Mock()
try:
losser.main(args=[''], table_function=table_function)
except SystemExit as err:
assert err.code == 2
assert not table_function.called


def test_help():
"""It should exit with status 0 if there's a -h argument."""
table_function = mock.Mock()
try:
losser.main(args=['-h'], table_function=table_function)
except SystemExit as err:
assert err.code == 0
assert not table_function.called


def test_long_help():
"""It should exit with status 0 if there's a --help argument."""
table_function = mock.Mock()
try:
losser.main(args=['--help'], table_function=table_function)
except SystemExit as err:
assert err.code == 0
assert not table_function.called


def test_help_and_other_args():
"""It should exit with status 0 if there's a -h argument, even if there are
other args as well."""
table_function = mock.Mock()
try:
losser.main(args=['-h', '--columns', 'test_columns.json'],
table_function=table_function)
except SystemExit as err:
assert err.code == 0
assert not table_function.called


@mock.patch('sys.stdin')
def test_columns(mock_stdin):
"""stdin, --columns and csv=True should be passed to table()."""
table_function = mock.Mock()

# Mock stdin with a file-like object with some JSON text input.
mock_stdin.read.return_value = '"foobar"'

losser.main(
args=['--columns', 'test_columns.json'], table_function=table_function)

table_function.assert_called_once_with(
"foobar", "test_columns.json", csv=True)


def test_unrecognized_argument():
"""It should exit with status 2 if given an unrecognized argument."""
table_function = mock.Mock()
try:
losser.main(args=['--columns', 'test_columns.json', '--foobar'],
table_function=table_function)
except SystemExit as err:
assert err.code == 2
assert not table_function.called


def _absolute_path(relative_path):
return os.path.join(os.path.dirname(os.path.abspath(
inspect.getfile(inspect.currentframe()))), relative_path)


@mock.patch('sys.stdin')
def test_input(mock_stdin):
"""If given the -i argument it should read from the file not stdin."""
for arg in ("-i", "--input"):
table_function = mock.Mock()
losser.main(
args=['--columns', 'test_columns.json',
arg, _absolute_path('test_input.json')],
table_function=table_function)

assert not mock_stdin.called
table_function.assert_called_once_with(
"foobar", "test_columns.json", csv=True)
1 change: 1 addition & 0 deletions losser/test_input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"foobar"

0 comments on commit e002509

Please sign in to comment.