15
15
from common import assert_equal
16
16
17
17
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 = ' ' ):
19
28
table = "\n " .join ([
20
29
sep .join (['Sun' , '696000' , '1.9891e9' ]),
21
30
sep .join (['Earth' , '6371' , '5973.6' ]),
22
31
sep .join (['Moon' , '1737' , '73.5' ]),
23
32
sep .join (['Mars' , '3390' , '641.85' ])])
24
- tmpfile .write (table )
25
- tmpfile .file .seek (0 )
33
+ return table
26
34
27
35
28
- def run_and_capture_stdout (cmd ):
36
+ def run_and_capture_stdout (cmd , input = None ):
29
37
x = subprocess .Popen (cmd ,
38
+ stdin = subprocess .PIPE ,
30
39
stdout = subprocess .PIPE ,
31
40
stderr = subprocess .PIPE )
32
- out , err = x .communicate ()
41
+ out , err = x .communicate (input = input )
33
42
out = out .decode ("utf-8" )
34
43
if x .returncode != 0 :
35
44
raise IOError (err )
@@ -48,37 +57,59 @@ def __exit__(self, exc_type, exc_val, exc_tb):
48
57
os .unlink (self .tmpfile .name )
49
58
50
59
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
+
51
70
def test_script_from_file_to_stdout ():
52
71
"""Command line utility: read from file, print to stdout"""
53
72
with TemporaryTextFile () as tmpfile :
54
- write_sample_input (tmpfile )
73
+ tmpfile .write (sample_input ())
74
+ tmpfile .seek (0 )
55
75
cmd = ["python" , "tabulate.py" , tmpfile .name ]
56
76
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
64
78
print ("got: " ,repr (out ))
65
79
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 ())
67
103
68
104
69
105
def test_script_sep_option ():
70
106
"""Command line utility: --sep option"""
71
107
with TemporaryTextFile () as tmpfile :
72
- write_sample_input (tmpfile , sep = "," )
108
+ tmpfile .write (sample_input (sep = "," ))
109
+ tmpfile .seek (0 )
73
110
cmd = ["python" , "tabulate.py" , "--sep" , "," , tmpfile .name ]
74
111
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
82
113
print ("got: " ,repr (out ))
83
114
print ("expected:" ,repr (expected ))
84
- assert_equal (out .rstrip (). splitlines (), expected . rstrip () .splitlines ())
115
+ assert_equal (out .splitlines (), expected .splitlines ())
0 commit comments