Skip to content

Commit

Permalink
Add a PTY fallback for Windows in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
webknjaz committed Mar 13, 2021
1 parent b780a84 commit 4c9e222
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions magicbus/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from magicbus.compat import HTTPServer, HTTPConnection, HTTPHandler
import os
from subprocess import Popen
from subprocess import DEVNULL, Popen
import threading
import time

Expand All @@ -30,20 +30,21 @@ def start(self):
cwd = os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))
env = os.environ.copy()
env['PYTHONPATH'] = cwd
popen_kwargs = {}
# NOTE: Openning a pseudo-terminal to interact with subprocess
# NOTE: is necessary because `pytest` captures stdin unless `-s`
# NOTE: is used and it's impossible to disable this with
# NOTE: `capsys` because it only unpatches stdout+stderr but not
# NOTE: stdin.
if pty is not None: # NOTE: It's probably Windows
if pty is not None: # NOTE: Something UNIXy
master_fd, self._pty_stdin = pty.openpty()
popen_kwargs = {
# Ref: https://stackoverflow.com/a/43012138/595220
'preexec_fn': os.setsid, 'stdin': self._pty_stdin,
# stdout and stderr are both needed for TTY
'stdout': master_fd, 'stderr': master_fd,
}
else: # NOTE: It's probably Windows
popen_kwargs = {'stdin': DEVNULL}
self.process = Popen(self.args, env=env, **popen_kwargs)
if pty is not None:
os.close(master_fd) # Only needed to spawn the process
Expand Down

0 comments on commit 4c9e222

Please sign in to comment.