Skip to content

Commit

Permalink
Don't rely on pty on Windows in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
webknjaz committed Mar 13, 2021
1 parent a9a3024 commit b780a84
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions magicbus/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@

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

try:
import pty
except ImportError:
pty = None

from magicbus.plugins import SimplePlugin


Expand All @@ -26,19 +30,23 @@ 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.
master_fd, self._pty_stdin = pty.openpty()
self.process = Popen(
self.args, env=env,
# Ref: https://stackoverflow.com/a/43012138/595220
preexec_fn=os.setsid, stdin=self._pty_stdin,
stdout=master_fd, stderr=master_fd, # both needed for TTY
)
os.close(master_fd) # Only needed to spawn the process
if pty is not None: # NOTE: It's probably Windows
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,
}
self.process = Popen(self.args, env=env, **popen_kwargs)
if pty is not None:
os.close(master_fd) # Only needed to spawn the process
self.process.poll() # W/o this the subprocess doesn't see a TTY

def stop(self):
Expand Down

0 comments on commit b780a84

Please sign in to comment.