Skip to content
This repository has been archived by the owner on Nov 29, 2022. It is now read-only.

Commit

Permalink
Exit if child process creation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
hSaria committed May 29, 2022
1 parent 8a85cd0 commit 67214bb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
10 changes: 7 additions & 3 deletions chromaterm/platform/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,13 @@ class STARTUPINFOEX(ctypes.Structure):

# EXTENDED_STARTUPINFO_PRESENT = 0x80000
process_info = PROCESS_INFORMATION()
K32.CreateProcessW(None, subprocess.list2cmdline(program_args), None, None,
False, 0x80000, None, None,
byref(startup_info_ex.StartupInfo), byref(process_info))
if not K32.CreateProcessW(None, subprocess.list2cmdline(program_args),
None, None, False, 0x80000, None, None,
byref(startup_info_ex.StartupInfo),
byref(process_info)):
message = ctypes.FormatError()
K32.ClosePseudoConsole(console)
sys.exit(message)

# Create pipe and necessary forwarder threads
master, slave = create_socket_pipe()
Expand Down
15 changes: 12 additions & 3 deletions tests/platform/test_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,8 @@ def test_run_program_incompatible_windows_version(monkeypatch):
patch_functions(monkeypatch)
platform.WINDOWS_BUILD_CURRENT = platform.WINDOWS_BUILD_MINIMUM - 1

try:
with pytest.raises(SystemExit, match='Windows version not supported'):
platform.run_program([])
except SystemExit:
pass


def test_run_program_close_program_handles(monkeypatch):
Expand All @@ -175,6 +173,17 @@ def test_run_program_escape_program_args(monkeypatch):
assert platform.K32.CreateProcessW.mock_calls[0][1][1] == '"foo bar" 1 2'


def test_run_program_create_process_error(monkeypatch):
'''If an error is encountered during process creation, ChromaTerm should exit
with the relevant message from `GetLastError`.'''
patch_functions(monkeypatch)
monkeypatch.setattr(ctypes, 'FormatError', lambda: 'Hello', raising=False)
platform.K32.CreateProcessW.return_value = False

with pytest.raises(SystemExit, match='Hello'):
platform.run_program([])


def test_run_program_read(monkeypatch):
'''Confirm ChromaTerm _reads_ the program's _output_ (output_r).'''
patch_functions(monkeypatch)
Expand Down

0 comments on commit 67214bb

Please sign in to comment.