Skip to content

Commit 10d1581

Browse files
Harden feature teardown cleanup
1 parent c9cb6f8 commit 10d1581

4 files changed

Lines changed: 19 additions & 81 deletions

File tree

test/features/environment.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,11 @@ def after_scenario(context, _):
133133
dbname = context.currentdb
134134
context.cli.expect_exact(f"{user}@{host}:{dbname}>", timeout=5)
135135
context.cli.sendcontrol("c")
136-
user = context.conf["user"]
137-
host = context.conf["host"]
138-
dbname = context.currentdb
139-
context.cli.expect_exact(f"{user}@{host}:{dbname}>", timeout=5)
140136
context.cli.sendcontrol("d")
141-
context.cli.expect_exact(pexpect.EOF, timeout=5)
137+
try:
138+
context.cli.expect_exact(pexpect.EOF, timeout=5)
139+
except pexpect.TIMEOUT:
140+
context.cli.terminate(force=True)
142141

143142
if os.path.exists(MY_CNF_BACKUP_PATH):
144143
shutil.move(MY_CNF_BACKUP_PATH, MY_CNF_PATH)

test/features/steps/iocommands.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ def step_edit_done_sql(context, query):
3838
wrappers.expect_exact(context, match, timeout=5)
3939
# Cleanup the command line.
4040
context.cli.sendcontrol("c")
41-
wrappers.wait_prompt(context)
4241
# Cleanup the edited file.
4342
if context.editor_file_name and os.path.exists(context.editor_file_name):
4443
os.remove(context.editor_file_name)

test/pytests/test_feature_environment.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,40 @@
1717
environment = cast(Any, environment_module)
1818

1919

20-
class PromptRaceCli:
20+
class TimeoutOnEofCli:
2121
def __init__(self) -> None:
22-
self.interrupt_prompt_seen = False
23-
self.closed = False
22+
self.sent_controls: list[str] = []
23+
self.terminated_force: bool | None = None
24+
25+
def sendcontrol(self, key: str) -> None:
26+
self.sent_controls.append(key)
2427

2528
def expect_exact(self, expected: object, timeout: int) -> None:
2629
if expected == pexpect.EOF:
27-
if not self.closed:
28-
raise pexpect.TIMEOUT('process still running')
29-
return
30-
31-
if expected == 'root@127.0.0.1:mycli_behave_tests>':
32-
self.interrupt_prompt_seen = True
33-
return
30+
raise pexpect.TIMEOUT('process still running')
3431

3532
raise AssertionError(f'unexpected expectation: {expected!r}')
3633

37-
def sendcontrol(self, key: str) -> None:
38-
if key == 'c':
39-
return
40-
if key == 'd':
41-
self.closed = self.interrupt_prompt_seen
34+
def terminate(self, force: bool = False) -> None:
35+
self.terminated_force = force
4236

4337

44-
def test_after_scenario_waits_for_prompt_after_interrupt(monkeypatch, tmp_path) -> None:
38+
def test_after_scenario_terminates_when_teardown_eof_times_out(monkeypatch, tmp_path) -> None:
4539
log_file = tmp_path / 'mycli.test.log'
4640
log_file.write_text('')
4741
monkeypatch.setattr(environment, 'test_log_file', str(log_file))
4842
monkeypatch.setattr(environment, 'MY_CNF_BACKUP_PATH', str(tmp_path / '.my.cnf.backup'))
4943
monkeypatch.setattr(environment, 'MYLOGIN_CNF_BACKUP_PATH', str(tmp_path / '.mylogin.cnf.backup'))
5044
monkeypatch.setattr(environment, 'MYLOGIN_CNF_PATH', str(tmp_path / '.mylogin.cnf'))
5145

46+
cli = TimeoutOnEofCli()
5247
context = SimpleNamespace(
5348
atprompt=True,
54-
cli=PromptRaceCli(),
55-
conf={
56-
'user': 'root',
57-
'host': '127.0.0.1',
58-
'dbname': 'mycli_behave_tests',
59-
},
60-
currentdb='mycli_behave_tests',
49+
cli=cli,
6150
exit_sent=False,
6251
)
6352

6453
environment.after_scenario(context, SimpleNamespace())
54+
55+
assert cli.sent_controls == ['c', 'd']
56+
assert cli.terminated_force is True

test/pytests/test_iocommands_steps.py

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)