88import tempfile
99from unittest .mock import patch
1010
11- from common import assert_equal
1211from tabulate .cli import _main
1312
13+ from common import assert_equal
14+
1415
1516class _UnclosableStringIO (io .StringIO ):
1617 """StringIO that ignores close() so getvalue() works after a 'with' block."""
18+
1719 def close (self ):
1820 pass # _main does `with sys.stdout as out:`, which would close a plain StringIO
1921
@@ -22,12 +24,15 @@ def run_main_in_process(args, input_text=None):
2224 """Call _main() in-process, capturing stdout. Returns the captured output."""
2325 stdin = io .StringIO (input_text ) if input_text is not None else sys .stdin
2426 stdout = _UnclosableStringIO ()
25- with patch ("sys.argv" , ["tabulate" ] + args ), \
26- patch ("sys.stdin" , stdin ), \
27- contextlib .redirect_stdout (stdout ):
27+ with (
28+ patch ("sys.argv" , ["tabulate" ] + args ),
29+ patch ("sys.stdin" , stdin ),
30+ contextlib .redirect_stdout (stdout ),
31+ ):
2832 _main ()
2933 return stdout .getvalue ()
3034
35+
3136SAMPLE_SIMPLE_FORMAT = "\n " .join (
3237 [
3338 "----- ------ -------------" ,
@@ -281,7 +286,7 @@ def test_module_jsonl_from_stdin():
281286SAMPLE_CSV_FORMAT = "\n " .join (
282287 [
283288 "-- ----- ----------------- ----------------" ,
284- " id name email \ " favorite\ " fruit" ,
289+ ' id name email "favorite" fruit' ,
285290 "1 Alice alice@example.com apple, kiwi" ,
286291 "2 Bob bob@example.com banana," ,
287292 " orange," ,
@@ -291,6 +296,7 @@ def test_module_jsonl_from_stdin():
291296 ]
292297)
293298
299+
294300def test_module_csv_from_stdin ():
295301 """Command line utility: python -m tabulate with CSV input from stdin"""
296302 cmd = [sys .executable , "-m" , "tabulate" , "-r" , "csv" ]
@@ -304,9 +310,13 @@ def test_module_csv_from_stdin():
304310def test_module_jsonl_remapped_headers ():
305311 """Command line utility: --headers with key:header remapping for JSONL input"""
306312 cmd = [
307- sys .executable , "-m" , "tabulate" ,
308- "-r" , "jsonl" ,
309- "--headers" , "id:ID,name:First Name,email:Email" ,
313+ sys .executable ,
314+ "-m" ,
315+ "tabulate" ,
316+ "-r" ,
317+ "jsonl" ,
318+ "--headers" ,
319+ "id:ID,name:First Name,email:Email" ,
310320 ]
311321 out = run_and_capture_stdout (cmd , input = SAMPLE_INPUT_JSONL )
312322 expected = SAMPLE_REMAPPED_HEADERS
@@ -320,6 +330,7 @@ def test_module_jsonl_remapped_headers():
320330# that coverage.py can instrument the code in tabulate/cli.py.
321331# ---------------------------------------------------------------------------
322332
333+
323334def test_inprocess_stdin_to_stdout ():
324335 """In-process: read RSV from stdin, print to stdout"""
325336 out = run_main_in_process ([], input_text = sample_input ())
@@ -390,6 +401,7 @@ def test_inprocess_csv_from_stdin():
390401def test_inprocess_invalid_option ():
391402 """In-process: unrecognised option exits with code 2"""
392403 import pytest
404+
393405 with pytest .raises (SystemExit ) as exc_info :
394406 run_main_in_process (["--no-such-option" ], input_text = "a b\n 1 2\n " )
395407 assert exc_info .value .code == 2
@@ -398,6 +410,7 @@ def test_inprocess_invalid_option():
398410def test_inprocess_help_option ():
399411 """In-process: --help / -h exits with code 0"""
400412 import pytest
413+
401414 for opt in ["-h" , "--help" ]:
402415 with pytest .raises (SystemExit ) as exc_info :
403416 run_main_in_process ([opt ], input_text = "" )
@@ -407,6 +420,7 @@ def test_inprocess_help_option():
407420def test_inprocess_invalid_format ():
408421 """In-process: unknown --format value exits with code 3"""
409422 import pytest
423+
410424 with pytest .raises (SystemExit ) as exc_info :
411425 run_main_in_process (["-f" , "nosuchformat" ], input_text = "a b\n 1 2\n " )
412426 assert exc_info .value .code == 3
@@ -415,6 +429,7 @@ def test_inprocess_invalid_format():
415429def test_inprocess_invalid_fileformat ():
416430 """In-process: unknown --read value exits with code 3"""
417431 import pytest
432+
418433 with pytest .raises (SystemExit ) as exc_info :
419434 run_main_in_process (["-r" , "xml" ], input_text = "" )
420435 assert exc_info .value .code == 3
@@ -424,9 +439,7 @@ def test_inprocess_int_option():
424439 """In-process: -I / --int option"""
425440 jsonl_ints = '{"n": 1000000}\n {"n": 2000000}\n '
426441 for opt in ["-I" , "--int" ]:
427- out = run_main_in_process (
428- ["-r" , "jsonl" , opt , "_" ], input_text = jsonl_ints
429- )
442+ out = run_main_in_process (["-r" , "jsonl" , opt , "_" ], input_text = jsonl_ints )
430443 assert "1_000_000" in out
431444
432445
@@ -441,9 +454,7 @@ def test_inprocess_colalign_option():
441454
442455def test_inprocess_rsv_custom_headers ():
443456 """In-process: --headers with custom column names for RSV input"""
444- out = run_main_in_process (
445- ["--headers" , "Planet,Radius,Mass" ], input_text = sample_input ()
446- )
457+ out = run_main_in_process (["--headers" , "Planet,Radius,Mass" ], input_text = sample_input ())
447458 assert_equal (out .splitlines (), SAMPLE_SIMPLE_FORMAT_WITH_HEADERS .splitlines ())
448459
449460
0 commit comments