Skip to content

Commit 812d22f

Browse files
committed
Simplify tests by a gazillion procent πŸš€πŸš€πŸš€
1 parent cc624d1 commit 812d22f

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

β€ŽLib/test/test_repl.pyβ€Ž

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -483,57 +483,63 @@ def test_quiet_mode(self):
483483
self.assertEqual(output[:3], ">>>")
484484

485485
def test_pythonstartup_success(self):
486-
startup_code = "import sys\nprint('notice from pythonstartup in asyncio repl', file=sys.stderr)"
486+
startup_code = dedent("""\
487+
import sys
488+
print('notice from pythonstartup in asyncio repl', file=sys.stderr)
489+
""")
487490
startup_env = self.enterContext(
488491
new_pythonstartup_env(code=startup_code, histfile=".asyncio_history"))
489492

490-
p = spawn_asyncio_repl(env=os.environ | startup_env, stderr=subprocess.PIPE, isolated=False)
493+
p = spawn_repl(
494+
"-qm", "asyncio",
495+
env=os.environ | startup_env,
496+
stderr=subprocess.PIPE,
497+
isolated=False,
498+
custom=True)
491499
p.stdin.write("1/0")
492500
kill_python(p)
493-
output_lines = p.stderr.read().splitlines()
501+
output = p.stderr.read()
494502
p.stderr.close()
503+
self.assertStartsWith(output, 'notice from pythonstartup in asyncio repl')
495504

496-
self.assertEqual(output_lines[3], 'notice from pythonstartup in asyncio repl')
505+
expected = dedent("""\
506+
File "<stdin>", line 1, in <module>
507+
1/0
508+
~^~
509+
ZeroDivisionError: division by zero
497510
498-
tb_start_lines = output_lines[5:6]
499-
tb_final_lines = output_lines[13:]
500-
expected_lines = [
501-
'Traceback (most recent call last):',
502-
' File "<stdin>", line 1, in <module>',
503-
' 1/0',
504-
' ~^~',
505-
'ZeroDivisionError: division by zero',
506-
'',
507-
'exiting asyncio REPL...',
508-
]
509-
self.assertEqual(tb_start_lines + tb_final_lines, expected_lines)
511+
exiting asyncio REPL...
512+
""")
513+
self.assertEndsWith(output, expected)
510514

511515
def test_pythonstartup_failure(self):
512516
startup_code = "def foo():\n 1/0\n"
513517
startup_env = self.enterContext(
514518
new_pythonstartup_env(code=startup_code, histfile=".asyncio_history"))
515519

516-
p = spawn_asyncio_repl(env=os.environ | startup_env, stderr=subprocess.PIPE, isolated=False)
520+
p = spawn_repl(
521+
"-qm", "asyncio",
522+
env=os.environ | startup_env,
523+
stderr=subprocess.PIPE,
524+
isolated=False,
525+
custom=True)
517526
p.stdin.write("foo()")
518527
kill_python(p)
519-
output_lines = p.stderr.read().splitlines()
528+
output = p.stderr.read()
520529
p.stderr.close()
521530

522-
tb_start_lines = output_lines[4:5]
523-
tb_final_lines = output_lines[12:]
524-
expected_lines = [
525-
'Traceback (most recent call last):',
526-
' File "<stdin>", line 1, in <module>',
527-
' foo()',
528-
' ~~~^^',
529-
f' File "{startup_env['PYTHONSTARTUP']}", line 2, in foo',
530-
' 1/0',
531-
' ~^~',
532-
'ZeroDivisionError: division by zero',
533-
'',
534-
'exiting asyncio REPL...',
535-
]
536-
self.assertEqual(tb_start_lines + tb_final_lines, expected_lines)
531+
expected = dedent(f"""\
532+
File "<stdin>", line 1, in <module>
533+
foo()
534+
~~~^^
535+
File "{startup_env['PYTHONSTARTUP']}", line 2, in foo
536+
1/0
537+
~^~
538+
ZeroDivisionError: division by zero
539+
540+
exiting asyncio REPL...
541+
""")
542+
self.assertEndsWith(output, expected)
537543

538544
if __name__ == "__main__":
539545
unittest.main()

0 commit comments

Comments
Β (0)