Skip to content

Commit 5074542

Browse files
Consolidate duplicate and nested imports in test files (#45)
* Initial plan * Consolidate duplicate and nested imports in test files Agent-Logs-Url: https://github.com/NetApp/recline/sessions/7a2eaa6c-36b6-4202-a8a1-aae1977a52f9 Co-authored-by: RobertBlackhart <5414318+RobertBlackhart@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: RobertBlackhart <5414318+RobertBlackhart@users.noreply.github.com>
1 parent 957310a commit 5074542

3 files changed

Lines changed: 23 additions & 93 deletions

File tree

tests/test_commands/test_async_command.py

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@
55
"""
66

77
import asyncio
8+
import os
9+
import sys
10+
import termios
11+
import threading
12+
import time
813

914
import pytest
1015

1116
import recline
12-
from recline.commands.async_command import AsyncCommand
17+
from recline.commands.async_command import (
18+
AsyncCommand,
19+
CommandBackgrounded,
20+
CommandCancelled,
21+
set_terminal_echo,
22+
)
1323

1424

1525
@pytest.mark.usefixtures("clean_jobs")
@@ -111,8 +121,6 @@ async def test_coro():
111121
def test_command_backgrounded_exception():
112122
"""Verify CommandBackgrounded carries its job_pid attribute."""
113123

114-
from recline.commands.async_command import CommandBackgrounded
115-
116124
exc = CommandBackgrounded(42)
117125
assert exc.job_pid == 42
118126

@@ -121,8 +129,6 @@ def test_command_backgrounded_exception():
121129
def test_command_cancelled_exception():
122130
"""Verify CommandCancelled carries its job_pid attribute."""
123131

124-
from recline.commands.async_command import CommandCancelled
125-
126132
exc = CommandCancelled(99)
127133
assert exc.job_pid == 99
128134

@@ -159,8 +165,6 @@ async def test_coro():
159165
def test_async_command_foreground_killed():
160166
"""Verify that foreground() raises CommandCancelled when the thread was killed."""
161167

162-
from recline.commands.async_command import CommandCancelled
163-
164168
i_was_started = False
165169

166170
@recline.command(name="test")
@@ -190,9 +194,6 @@ def test_async_command_foreground_backgrounded():
190194
to the background while foreground() is waiting.
191195
"""
192196

193-
import time
194-
from recline.commands.async_command import CommandBackgrounded
195-
196197
ready = [False]
197198

198199
@recline.command(name="test")
@@ -211,7 +212,6 @@ def do_background():
211212
time.sleep(0.05)
212213
thread.background()
213214

214-
import threading
215215
t = threading.Thread(target=do_background)
216216
t.start()
217217

@@ -227,9 +227,6 @@ def do_background():
227227
def test_set_terminal_echo_no_termios(monkeypatch):
228228
"""Verify set_terminal_echo gracefully yields when termios is unavailable (e.g. Windows)."""
229229

230-
import sys
231-
from recline.commands.async_command import set_terminal_echo
232-
233230
# Setting termios to None in sys.modules causes 'import termios' to raise ImportError
234231
monkeypatch.setitem(sys.modules, "termios", None)
235232

@@ -242,11 +239,6 @@ def test_set_terminal_echo_no_termios(monkeypatch):
242239
def test_set_terminal_echo_with_tty(monkeypatch):
243240
"""Verify set_terminal_echo modifies echo when stdin is a real TTY."""
244241

245-
import os
246-
import sys
247-
import termios
248-
from recline.commands.async_command import set_terminal_echo
249-
250242
fake_attrs = [0, 0, 0, termios.ECHO, 0, 0, [b'\x00'] * 19]
251243
set_calls = []
252244

@@ -265,11 +257,6 @@ def test_set_terminal_echo_with_tty(monkeypatch):
265257
def test_set_terminal_echo_enabled_true(monkeypatch):
266258
"""Verify set_terminal_echo correctly enables ECHO (covering the enabled=True branch)."""
267259

268-
import os
269-
import sys
270-
import termios
271-
from recline.commands.async_command import set_terminal_echo
272-
273260
# Start with ECHO disabled (bit not set)
274261
fake_attrs = [0, 0, 0, 0, 0, 0, [b'\x00'] * 19]
275262
set_calls = []
@@ -288,10 +275,6 @@ def test_set_terminal_echo_enabled_true(monkeypatch):
288275
def test_set_terminal_echo_non_tty(monkeypatch):
289276
"""Verify set_terminal_echo yields without changes when stdin is not a TTY."""
290277

291-
import os
292-
import sys
293-
from recline.commands.async_command import set_terminal_echo
294-
295278
monkeypatch.setattr(sys.stdin, "fileno", lambda: 0)
296279
monkeypatch.setattr(os, "isatty", lambda fd: False)
297280

tests/test_commands/test_builtin_commands.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
A test module for the recline.commands.builtin_commands module
55
"""
66

7+
import curses
78
import pdb
89
import pudb
910
import pytest
1011

1112
import recline
12-
from recline.commands import builtin_commands
13+
from recline.commands import ReclineCommandError, builtin_commands
1314
from recline.commands.cli_command import CLICommand
1415

1516

@@ -124,8 +125,6 @@ def test_man_commands_returns_non_aliases():
124125
def test_exit_command_with_jobs_decline(monkeypatch):
125126
"""Verify exit_command() with backgrounded jobs does NOT exit if user declines."""
126127

127-
import recline
128-
129128
monkeypatch.setattr(recline, "JOBS", {1: object()})
130129
monkeypatch.setattr("builtins.input", lambda _: "n")
131130
# Should return without raising SystemExit
@@ -135,8 +134,6 @@ def test_exit_command_with_jobs_decline(monkeypatch):
135134
def test_exit_command_with_jobs_accept(monkeypatch):
136135
"""Verify exit_command() with backgrounded jobs exits when user confirms."""
137136

138-
import recline
139-
140137
class _FakeJob:
141138
def stop(self, dont_delete=False):
142139
pass
@@ -151,8 +148,6 @@ def stop(self, dont_delete=False):
151148
def test_exit_command_abort_jobs(monkeypatch):
152149
"""Verify exit_command() with abort_jobs=True cleans up jobs and exits."""
153150

154-
import recline
155-
156151
stopped = []
157152

158153
class _FakeJob:
@@ -168,9 +163,6 @@ def stop(self, dont_delete=False):
168163
def test_fg_no_jobs(monkeypatch):
169164
"""Verify fg() raises ReclineCommandError when there are no jobs."""
170165

171-
import recline
172-
from recline.commands import ReclineCommandError
173-
174166
monkeypatch.setattr(recline, "JOBS", {})
175167
with pytest.raises(ReclineCommandError, match="No running jobs"):
176168
builtin_commands.fg()
@@ -179,9 +171,6 @@ def test_fg_no_jobs(monkeypatch):
179171
def test_fg_invalid_job(monkeypatch):
180172
"""Verify fg() raises ReclineCommandError for an unknown job ID."""
181173

182-
import recline
183-
from recline.commands import ReclineCommandError
184-
185174
monkeypatch.setattr(recline, "JOBS", {1: object()})
186175
with pytest.raises(ReclineCommandError, match="Could not find"):
187176
builtin_commands.fg(job=999)
@@ -190,8 +179,6 @@ def test_fg_invalid_job(monkeypatch):
190179
def test_fg_with_job_no_formatter(monkeypatch):
191180
"""Verify fg() completes silently when the job's command has no output formatter."""
192181

193-
import recline
194-
195182
class _FakeCommand:
196183
output_formatter = None
197184

@@ -241,8 +228,6 @@ def test_debug_interrupt_handler_callable(monkeypatch):
241228
def test_fg_with_valid_job(monkeypatch):
242229
"""Verify fg() calls foreground() and formats output for a known job."""
243230

244-
import recline
245-
246231
formatted = []
247232

248233
class _FakeFormatter:
@@ -274,8 +259,6 @@ def test_man_unknown_command(capsys):
274259
def test_man_command_large_window(monkeypatch):
275260
"""Test man command with a large window so all text fits and (END) is displayed."""
276261

277-
import curses
278-
279262
@recline.command(name="man large cmd")
280263
def _man_large():
281264
"""Short description for large-window man test."""
@@ -313,8 +296,6 @@ def getch(self):
313296
def test_man_command_scrolling(monkeypatch):
314297
"""Test man command with a small window to exercise KEY_DOWN and KEY_UP scrolling."""
315298

316-
import curses
317-
318299
@recline.command(name="man scroll cmd")
319300
def _man_scroll():
320301
"""Short desc for scroll test.

0 commit comments

Comments
 (0)