Skip to content

Commit 0440d0e

Browse files
committed
Add tests
1 parent 00edac4 commit 0440d0e

File tree

1 file changed

+51
-56
lines changed

1 file changed

+51
-56
lines changed

Lib/test/test_repl.py

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -188,65 +188,60 @@ def foo(x):
188188
]
189189
self.assertEqual(traceback_lines, expected_lines)
190190

191-
def test_pythonstartup_error_reporting(self):
191+
def test_pythonstartup_success(self):
192192
# errors based on https://github.com/python/cpython/issues/137576
193-
194-
def make_repl(env):
195-
return subprocess.Popen(
196-
[os.path.join(os.path.dirname(sys.executable), '<stdin>'), "-i"],
197-
executable=sys.executable,
198-
text=True,
199-
stdin=subprocess.PIPE,
200-
stdout=subprocess.PIPE,
201-
stderr=subprocess.STDOUT,
202-
env=env,
203-
)
204-
205193
# case 1: error in user input, but PYTHONSTARTUP is fine
206-
with os_helper.temp_dir() as tmpdir:
207-
script = os.path.join(tmpdir, "pythonstartup.py")
208-
with open(script, "w") as f:
209-
f.write("print('from pythonstartup')" + os.linesep)
210-
211-
env = os.environ.copy()
212-
env['PYTHONSTARTUP'] = script
213-
env["PYTHON_HISTORY"] = os.path.join(tmpdir, ".pythonhist")
214-
p = make_repl(env)
215-
p.stdin.write("1/0")
216-
output = kill_python(p)
217-
expected = dedent("""
218-
Traceback (most recent call last):
219-
File "<stdin>", line 1, in <module>
220-
1/0
221-
~^~
222-
ZeroDivisionError: division by zero
223-
""")
224-
self.assertIn("from pythonstartup", output)
225-
self.assertIn(expected, output)
226-
194+
for repl_name, repl in ("REPL", spawn_repl), ("asyncio REPL", spawn_asyncio_repl):
195+
with os_helper.temp_dir() as tmpdir:
196+
script = os.path.join(tmpdir, "pythonstartup.py")
197+
with open(script, "w") as f:
198+
f.write("print('from pythonstartup')" + os.linesep)
199+
200+
env = os.environ.copy()
201+
env['PYTHONSTARTUP'] = script
202+
env["PYTHON_HISTORY"] = os.path.join(tmpdir, ".pythonhist")
203+
p = repl(env=env)
204+
p.stdin.write("1/0")
205+
output = kill_python(p)
206+
207+
with self.subTest(repl_name):
208+
self.assertIn("Traceback (most recent call last):", output)
209+
expected = dedent("""
210+
File "<stdin>", line 1, in <module>
211+
1/0
212+
~^~
213+
ZeroDivisionError: division by zero
214+
""")
215+
self.assertIn("from pythonstartup", output)
216+
self.assertIn(expected, output)
217+
218+
def test_pythonstartup_failure(self):
227219
# case 2: error in PYTHONSTARTUP triggered by user input
228-
with os_helper.temp_dir() as tmpdir:
229-
script = os.path.join(tmpdir, "pythonstartup.py")
230-
with open(script, "w") as f:
231-
f.write("def foo():\n 1/0\n")
232-
233-
env = os.environ.copy()
234-
env['PYTHONSTARTUP'] = script
235-
env["PYTHON_HISTORY"] = os.path.join(tmpdir, ".pythonhist")
236-
p = make_repl(env)
237-
p.stdin.write('foo()')
238-
output = kill_python(p)
239-
expected = dedent("""
240-
Traceback (most recent call last):
241-
File "<stdin>", line 1, in <module>
242-
foo()
243-
~~~^^
244-
File "%s", line 2, in foo
245-
1/0
246-
~^~
247-
ZeroDivisionError: division by zero
248-
""") % script
249-
self.assertIn(expected, output)
220+
for repl_name, repl in ("REPL", spawn_repl), ("asyncio REPL", spawn_asyncio_repl):
221+
with os_helper.temp_dir() as tmpdir:
222+
script = os.path.join(tmpdir, "pythonstartup.py")
223+
with open(script, "w") as f:
224+
f.write("def foo():\n 1/0\n")
225+
226+
env = os.environ.copy()
227+
env['PYTHONSTARTUP'] = script
228+
env["PYTHON_HISTORY"] = os.path.join(tmpdir, ".pythonhist")
229+
p = repl(env=env)
230+
p.stdin.write('foo()')
231+
output = kill_python(p)
232+
233+
with self.subTest(repl_name):
234+
self.assertIn("Traceback (most recent call last):", output)
235+
expected = dedent("""
236+
File "<stdin>", line 1, in <module>
237+
foo()
238+
~~~^^
239+
File "%s", line 2, in foo
240+
1/0
241+
~^~
242+
ZeroDivisionError: division by zero
243+
""") % script
244+
self.assertIn(expected, output)
250245

251246

252247

0 commit comments

Comments
 (0)