Skip to content

Commit 57235d4

Browse files
committed
Move the frontend test servers to their own modules
1 parent 095fa66 commit 57235d4

4 files changed

Lines changed: 133 additions & 118 deletions

File tree

gambaterm/main.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import time
55
import argparse
66
from pathlib import Path
7-
from typing import ContextManager, Self
7+
from typing import ContextManager, TYPE_CHECKING
88
import dataclasses
99
from dataclasses import dataclass, field
1010

@@ -19,6 +19,10 @@
1919
from .controller_input import combine_console_input_from_controller_context
2020
from .file_input import console_input_from_file_context, write_input_context
2121

22+
# `typing.Self` is not available in python 3.10
23+
if TYPE_CHECKING:
24+
from typing import Self
25+
2226

2327
@dataclass
2428
class AppConfig:

tests/_frontend_ssh_server.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import sys
2+
import argparse
3+
import asyncio
4+
from concurrent.futures import ThreadPoolExecutor
5+
from pathlib import Path
6+
7+
import gambaterm.ssh as gambaterm_ssh
8+
from gambaterm.console import GameboyColor
9+
from gambaterm.main import AppConfig
10+
from gambaterm.remote_terminal import RemoteTerminal, KeyboardSupportDetection
11+
12+
13+
rom = Path(sys.argv[1])
14+
15+
16+
def test_frontend(
17+
terminal: RemoteTerminal,
18+
app_config: AppConfig,
19+
keyboard_support_detection: KeyboardSupportDetection,
20+
) -> AppConfig:
21+
terminal.stream.write("TEST_FRONTEND_ACTIVE\n")
22+
terminal.stream.flush()
23+
return app_config
24+
25+
26+
namespace = argparse.Namespace(
27+
romfile=rom,
28+
input_file=Path(sys.argv[2]),
29+
skip_inputs=188,
30+
color_mode=None,
31+
frame_advance=1,
32+
break_after=10,
33+
speed=1.0,
34+
cpr_sync=False,
35+
save_directory=None,
36+
force_gameboy=False,
37+
)
38+
39+
40+
async def main() -> None:
41+
with ThreadPoolExecutor(max_workers=4) as executor:
42+
async with gambaterm_ssh.run_ssh_server(
43+
bind="127.0.0.1",
44+
port=8022,
45+
authentication=gambaterm_ssh.NoAuthentication(),
46+
console_cls=GameboyColor,
47+
namespace=namespace,
48+
command_parser=lambda cmd, ns, write: ns,
49+
users_directory=Path("."),
50+
executor=executor,
51+
frontend=test_frontend,
52+
):
53+
await asyncio.Future()
54+
55+
56+
try:
57+
asyncio.run(main())
58+
except KeyboardInterrupt:
59+
pass

tests/_frontend_telnet_server.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import sys
2+
import argparse
3+
import asyncio
4+
from concurrent.futures import ThreadPoolExecutor
5+
from pathlib import Path
6+
7+
import gambaterm.telnet as gambaterm_telnet
8+
from gambaterm.console import GameboyColor
9+
from gambaterm.main import AppConfig
10+
from gambaterm.remote_terminal import RemoteTerminal, KeyboardSupportDetection
11+
12+
13+
rom = Path(sys.argv[1])
14+
15+
16+
def test_frontend(
17+
terminal: RemoteTerminal,
18+
app_config: AppConfig,
19+
keyboard_support_detection: KeyboardSupportDetection,
20+
) -> AppConfig:
21+
terminal.stream.write("TEST_FRONTEND_ACTIVE\n")
22+
terminal.stream.flush()
23+
return app_config
24+
25+
26+
namespace = argparse.Namespace(
27+
romfile=rom,
28+
input_file=Path(sys.argv[2]),
29+
skip_inputs=188,
30+
color_mode=None,
31+
frame_advance=1,
32+
break_after=10,
33+
speed=1.0,
34+
cpr_sync=False,
35+
save_directory=None,
36+
force_gameboy=False,
37+
)
38+
39+
40+
async def main() -> None:
41+
with ThreadPoolExecutor(max_workers=4) as executor:
42+
async with gambaterm_telnet.run_telnet_server(
43+
bind="127.0.0.1",
44+
port=8023,
45+
robot_check=False,
46+
max_players=0,
47+
idle_timeout=None,
48+
console_cls=GameboyColor,
49+
namespace=namespace,
50+
users_directory=Path("."),
51+
executor=executor,
52+
frontend=test_frontend,
53+
):
54+
await asyncio.Future()
55+
56+
57+
try:
58+
asyncio.run(main())
59+
except KeyboardInterrupt:
60+
pass

tests/test_gambaterm.py

Lines changed: 9 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -248,66 +248,12 @@ async def telnet_client() -> str:
248248
assert server.returncode == 0
249249

250250

251-
def run_frontend_ssh_server(rom_path_str: str, input_file_str: str) -> None:
252-
import argparse
253-
from concurrent.futures import ThreadPoolExecutor
254-
import gambaterm.ssh as gambaterm_ssh
255-
from gambaterm.console import GameboyColor
256-
from gambaterm.main import AppConfig
257-
from gambaterm.remote_terminal import RemoteTerminal, KeyboardSupportDetection
258-
259-
rom = Path(rom_path_str)
260-
261-
def test_frontend(
262-
terminal: RemoteTerminal,
263-
app_config: AppConfig,
264-
keyboard_support_detection: KeyboardSupportDetection,
265-
) -> AppConfig:
266-
terminal.stream.write("TEST_FRONTEND_ACTIVE\n")
267-
terminal.stream.flush()
268-
return app_config
269-
270-
namespace = argparse.Namespace(
271-
romfile=rom,
272-
input_file=Path(input_file_str),
273-
skip_inputs=188,
274-
color_mode=None,
275-
frame_advance=1,
276-
break_after=10,
277-
speed=1.0,
278-
cpr_sync=False,
279-
save_directory=None,
280-
force_gameboy=False,
281-
)
282-
283-
async def main() -> None:
284-
with ThreadPoolExecutor(max_workers=4) as executor:
285-
async with gambaterm_ssh.run_ssh_server(
286-
bind="127.0.0.1",
287-
port=8022,
288-
authentication=gambaterm_ssh.NoAuthentication(),
289-
console_cls=GameboyColor,
290-
namespace=namespace,
291-
command_parser=lambda cmd, ns, write: ns,
292-
users_directory=Path("."),
293-
executor=executor,
294-
frontend=test_frontend,
295-
):
296-
await asyncio.Future()
297-
298-
try:
299-
asyncio.run(main())
300-
except KeyboardInterrupt:
301-
pass
302-
303-
304251
def test_gambaterm_ssh_frontend(ssh_config: Path, gambaterm_config: Path) -> None:
305252
assert TEST_ROM.exists()
306253

307254
command = [
308255
sys.executable,
309-
"-c",
310-
"import sys; from tests.test_gambaterm import run_frontend_ssh_server; run_frontend_ssh_server(sys.argv[1], sys.argv[2])",
256+
str(Path(__file__).parent / "_frontend_ssh_server.py"),
311257
str(TEST_ROM),
312258
"/dev/null",
313259
]
@@ -336,12 +282,13 @@ async def ssh_client() -> str:
336282
known_hosts=None,
337283
username="user",
338284
) as conn:
339-
async with conn.create_process(
340-
term_type="xterm-256color", term_size=(80, 24)
341-
) as proc:
342-
stdout: str
343-
stdout, _ = await asyncio.wait_for(proc.communicate(), timeout=10.0)
344-
return stdout
285+
result = await conn.run(
286+
"",
287+
term_type="xterm-256color",
288+
term_size=(80, 24),
289+
)
290+
assert isinstance(result.stdout, str)
291+
return result.stdout
345292

346293
client_stdout = asyncio.run(ssh_client())
347294
assert "TEST_FRONTEND_ACTIVE" in client_stdout
@@ -358,67 +305,12 @@ async def ssh_client() -> str:
358305
assert server.returncode == 0
359306

360307

361-
def run_frontend_telnet_server(rom_path_str: str, input_file_str: str) -> None:
362-
import argparse
363-
from concurrent.futures import ThreadPoolExecutor
364-
import gambaterm.telnet as gambaterm_telnet
365-
from gambaterm.console import GameboyColor
366-
from gambaterm.main import AppConfig
367-
from gambaterm.remote_terminal import RemoteTerminal, KeyboardSupportDetection
368-
369-
rom = Path(rom_path_str)
370-
371-
def test_frontend(
372-
terminal: RemoteTerminal,
373-
app_config: AppConfig,
374-
keyboard_support_detection: KeyboardSupportDetection,
375-
) -> AppConfig:
376-
terminal.stream.write("TEST_FRONTEND_ACTIVE\n")
377-
terminal.stream.flush()
378-
return app_config
379-
380-
namespace = argparse.Namespace(
381-
romfile=rom,
382-
input_file=Path(input_file_str),
383-
skip_inputs=188,
384-
color_mode=None,
385-
frame_advance=1,
386-
break_after=10,
387-
speed=1.0,
388-
cpr_sync=False,
389-
save_directory=None,
390-
force_gameboy=False,
391-
)
392-
393-
async def main() -> None:
394-
with ThreadPoolExecutor(max_workers=4) as executor:
395-
async with gambaterm_telnet.run_telnet_server(
396-
bind="127.0.0.1",
397-
port=8023,
398-
robot_check=False,
399-
max_players=0,
400-
idle_timeout=None,
401-
console_cls=GameboyColor,
402-
namespace=namespace,
403-
users_directory=Path("."),
404-
executor=executor,
405-
frontend=test_frontend,
406-
):
407-
await asyncio.Future()
408-
409-
try:
410-
asyncio.run(main())
411-
except KeyboardInterrupt:
412-
pass
413-
414-
415308
def test_gambaterm_telnet_frontend(gambaterm_config: Path) -> None:
416309
assert TEST_ROM.exists()
417310

418311
command = [
419312
sys.executable,
420-
"-c",
421-
"import sys; from tests.test_gambaterm import run_frontend_telnet_server; run_frontend_telnet_server(sys.argv[1], sys.argv[2])",
313+
str(Path(__file__).parent / "_frontend_telnet_server.py"),
422314
str(TEST_ROM),
423315
"/dev/null",
424316
]

0 commit comments

Comments
 (0)