|
5 | 5 |
|
6 | 6 | from __future__ import print_function
|
7 | 7 | from __future__ import unicode_literals
|
| 8 | +import os |
8 | 9 |
|
9 | 10 |
|
10 | 11 | import subprocess
|
@@ -33,19 +34,31 @@ def get_stdout_and_stderr(cmd):
|
33 | 34 | return out
|
34 | 35 |
|
35 | 36 |
|
| 37 | +class TemporaryTextFile(object): |
| 38 | + def __init__(self): |
| 39 | + self.tmpfile = None |
| 40 | + def __enter__(self): |
| 41 | + self.tmpfile = tempfile.NamedTemporaryFile("w+", prefix="tabulate-test-tmp-", delete=False) |
| 42 | + return self.tmpfile |
| 43 | + def __exit__(self, exc_type, exc_val, exc_tb): |
| 44 | + if self.tmpfile: |
| 45 | + self.tmpfile.close() |
| 46 | + os.unlink(self.tmpfile.name) |
| 47 | + |
| 48 | + |
36 | 49 | def test_script_from_file_to_stdout():
|
37 | 50 | """CLI: read from file, print to stdout"""
|
38 |
| - with tempfile.NamedTemporaryFile("w+") as tmpfile: |
| 51 | + with TemporaryTextFile() as tmpfile: |
39 | 52 | write_sample_input(tmpfile)
|
40 | 53 | cmd = ["python", "tabulate.py", tmpfile.name]
|
41 | 54 | out = get_stdout_and_stderr(cmd)
|
42 | 55 | expected = "\n".join([
|
43 |
| - '----- ------ -------------', |
44 |
| - 'Sun 696000 1.9891e+09', |
45 |
| - 'Earth 6371 5973.6', |
46 |
| - 'Moon 1737 73.5', |
47 |
| - 'Mars 3390 641.85', |
48 |
| - '----- ------ -------------']) |
| 56 | + '----- ------ -------------', |
| 57 | + 'Sun 696000 1.9891e+09', |
| 58 | + 'Earth 6371 5973.6', |
| 59 | + 'Moon 1737 73.5', |
| 60 | + 'Mars 3390 641.85', |
| 61 | + '----- ------ -------------']) |
49 | 62 | print("got: ",repr(out))
|
50 | 63 | print("expected:",repr(expected))
|
51 |
| - assert_equal(out.rstrip(), expected.rstrip()) |
| 64 | + assert_equal(out.rstrip().splitlines(), expected.rstrip().splitlines()) |
0 commit comments