Skip to content

Commit ace538a

Browse files
committed
test tabulate CLI: stdin-to-stdout, file-to-file
1 parent 05601ef commit ace538a

File tree

1 file changed

+54
-23
lines changed

1 file changed

+54
-23
lines changed

test/test_cli.py

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,30 @@
1515
from common import assert_equal
1616

1717

18-
def write_sample_input(tmpfile, sep=' '):
18+
SAMPLE_SIMPLE_FORMAT = "\n".join(
19+
['----- ------ -------------',
20+
'Sun 696000 1.9891e+09',
21+
'Earth 6371 5973.6',
22+
'Moon 1737 73.5',
23+
'Mars 3390 641.85',
24+
'----- ------ -------------'])
25+
26+
27+
def sample_input(sep=' '):
1928
table = "\n".join([
2029
sep.join(['Sun', '696000', '1.9891e9']),
2130
sep.join(['Earth', '6371', '5973.6']),
2231
sep.join(['Moon', '1737', '73.5']),
2332
sep.join(['Mars', '3390', '641.85'])])
24-
tmpfile.write(table)
25-
tmpfile.file.seek(0)
33+
return table
2634

2735

28-
def run_and_capture_stdout(cmd):
36+
def run_and_capture_stdout(cmd, input=None):
2937
x = subprocess.Popen(cmd,
38+
stdin=subprocess.PIPE,
3039
stdout=subprocess.PIPE,
3140
stderr=subprocess.PIPE)
32-
out, err = x.communicate()
41+
out, err = x.communicate(input=input)
3342
out = out.decode("utf-8")
3443
if x.returncode != 0:
3544
raise IOError(err)
@@ -48,37 +57,59 @@ def __exit__(self, exc_type, exc_val, exc_tb):
4857
os.unlink(self.tmpfile.name)
4958

5059

60+
def test_script_from_stdin_to_stdout():
61+
"""Command line utility: read from stdin, print to stdout"""
62+
cmd = ["python", "tabulate.py"]
63+
out = run_and_capture_stdout(cmd, input=sample_input())
64+
expected = SAMPLE_SIMPLE_FORMAT
65+
print("got: ",repr(out))
66+
print("expected:",repr(expected))
67+
assert_equal(out.splitlines(), expected.splitlines())
68+
69+
5170
def test_script_from_file_to_stdout():
5271
"""Command line utility: read from file, print to stdout"""
5372
with TemporaryTextFile() as tmpfile:
54-
write_sample_input(tmpfile)
73+
tmpfile.write(sample_input())
74+
tmpfile.seek(0)
5575
cmd = ["python", "tabulate.py", tmpfile.name]
5676
out = run_and_capture_stdout(cmd)
57-
expected = "\n".join([
58-
'----- ------ -------------',
59-
'Sun 696000 1.9891e+09',
60-
'Earth 6371 5973.6',
61-
'Moon 1737 73.5',
62-
'Mars 3390 641.85',
63-
'----- ------ -------------'])
77+
expected = SAMPLE_SIMPLE_FORMAT
6478
print("got: ",repr(out))
6579
print("expected:",repr(expected))
66-
assert_equal(out.rstrip().splitlines(), expected.rstrip().splitlines())
80+
assert_equal(out.splitlines(), expected.splitlines())
81+
82+
83+
def test_script_from_file_to_file():
84+
"""Command line utility: read from file, write to file"""
85+
with TemporaryTextFile() as input_file:
86+
with TemporaryTextFile() as output_file:
87+
input_file.write(sample_input())
88+
input_file.seek(0)
89+
cmd = ["python", "tabulate.py", "-o", output_file.name, input_file.name]
90+
out = run_and_capture_stdout(cmd)
91+
# check that nothing is printed to stdout
92+
expected = ""
93+
print("got: ", repr(out))
94+
print("expected:", repr(expected))
95+
assert_equal(out.splitlines(), expected.splitlines())
96+
# check that the output was written to file
97+
output_file.seek(0)
98+
out = output_file.file.read()
99+
expected = SAMPLE_SIMPLE_FORMAT
100+
print("got: ", repr(out))
101+
print("expected:", repr(expected))
102+
assert_equal(out.splitlines(), expected.splitlines())
67103

68104

69105
def test_script_sep_option():
70106
"""Command line utility: --sep option"""
71107
with TemporaryTextFile() as tmpfile:
72-
write_sample_input(tmpfile, sep=",")
108+
tmpfile.write(sample_input(sep=","))
109+
tmpfile.seek(0)
73110
cmd = ["python", "tabulate.py", "--sep", ",", tmpfile.name]
74111
out = run_and_capture_stdout(cmd)
75-
expected = "\n".join([
76-
'----- ------ -------------',
77-
'Sun 696000 1.9891e+09',
78-
'Earth 6371 5973.6',
79-
'Moon 1737 73.5',
80-
'Mars 3390 641.85',
81-
'----- ------ -------------'])
112+
expected = SAMPLE_SIMPLE_FORMAT
82113
print("got: ",repr(out))
83114
print("expected:",repr(expected))
84-
assert_equal(out.rstrip().splitlines(), expected.rstrip().splitlines())
115+
assert_equal(out.splitlines(), expected.splitlines())

0 commit comments

Comments
 (0)