Skip to content

Commit cd45b9b

Browse files
committed
tests: Add Ctrl-C interrupt tests for REPL and execution.
Tests paste mode Ctrl-C cancellation and Ctrl-C interruption during code execution to validate terminal mode switching behavior. Signed-off-by: Andrew Leech <[email protected]>
1 parent fa301f6 commit cd45b9b

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ exclude = [ # Ruff finds Python SyntaxError in these files
3434
"tests/cmdline/repl_autoindent.py",
3535
"tests/cmdline/repl_basic.py",
3636
"tests/cmdline/repl_cont.py",
37+
"tests/cmdline/repl_ctrl_c_interrupt.py",
3738
"tests/cmdline/repl_emacs_keys.py",
3839
"tests/cmdline/repl_paste.py",
3940
"tests/cmdline/repl_words_move.py",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Test that Ctrl-C interrupts running code
2+
3+
# Test paste mode Ctrl-C cancel
4+
{\x05}
5+
print('paste mode')
6+
{\x03}
7+
8+
# Verify REPL works
9+
print('REPL works')
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
MicroPython \.\+ version
2+
Type "help()" for more information.
3+
>>> # Test that Ctrl-C interrupts running code
4+
>>>
5+
>>> # Test paste mode Ctrl-C cancel
6+
>>>
7+
paste mode; Ctrl-C to cancel, Ctrl-D to finish
8+
===
9+
=== print('paste mode')
10+
===
11+
>>>
12+
>>>
13+
>>> # Verify REPL works
14+
>>> print('REPL works')
15+
REPL works
16+
>>>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
Test that Ctrl-C interrupts running code in the REPL.
3+
4+
This test is run by run-tests.py which sets up a PTY with ISIG enabled
5+
and spawns MicroPython with the PTY as its controlling terminal.
6+
7+
Globals provided by run-tests.py:
8+
- master: PTY master file descriptor
9+
- read_until_quiet: Function to read from PTY until no data for timeout seconds
10+
"""
11+
12+
import os
13+
import time
14+
15+
16+
# Wait for banner
17+
time.sleep(0.2)
18+
banner = read_until_quiet(master)
19+
20+
if b"MicroPython" not in banner:
21+
print("SKIP")
22+
raise SystemExit
23+
24+
# Check if time module is available
25+
os.write(master, b"import time\n")
26+
time.sleep(0.05)
27+
check_output = read_until_quiet(master, timeout=0.05)
28+
if b"ImportError" in check_output:
29+
print("SKIP")
30+
raise SystemExit
31+
32+
# Start a long-running operation
33+
os.write(master, b"time.sleep(30)\n")
34+
time.sleep(0.1) # Give it time to start sleeping
35+
36+
# Send Ctrl-C byte (0x03) through the terminal
37+
# This tests that the terminal mode allows SIGINT generation
38+
os.write(master, b"\x03")
39+
40+
# Read output
41+
time.sleep(0.2)
42+
output = read_until_quiet(master, timeout=0.1)
43+
44+
# Check for KeyboardInterrupt
45+
if b"KeyboardInterrupt" in output:
46+
print("PASS: Ctrl-C interrupted execution")
47+
else:
48+
print("FAIL: No KeyboardInterrupt found")
49+
50+
# Verify REPL still works
51+
os.write(master, b"print('OK')\n")
52+
time.sleep(0.1)
53+
output = read_until_quiet(master)
54+
55+
if b"OK" in output:
56+
print("PASS: REPL functional after interrupt")
57+
else:
58+
print("FAIL: REPL not functional")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PASS: Ctrl-C interrupted execution
2+
PASS: REPL functional after interrupt

0 commit comments

Comments
 (0)