Skip to content

Commit

Permalink
Fix TestMain tests on Windows (#75)
Browse files Browse the repository at this point in the history
Currently the API test module has failures for TestMain class,
added in f008459, on Windows as SysStreamCapturing is in
universal newlines mode while its super class IntegrationTests
is using a native console stream with newline=os.linesep.

Add Appveyor CI script as .appveyor.yml,
which can be selected in the Appveyor settings.
  • Loading branch information
jayvdb authored and sigmavirus24 committed Jul 21, 2016
1 parent b161e50 commit 2ab47d7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
8 changes: 8 additions & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# To activate, change the Appveyor settings to use `.appveyor.yml`.
install:
- python -m pip install tox

build: off

test_script:
- python -m tox
32 changes: 28 additions & 4 deletions pyflakes/test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,50 @@ def __init__(self, lineno, col_offset=0):

class SysStreamCapturing(object):

"""Replaces sys.stdin, sys.stdout and sys.stderr with StringIO objects."""
"""
Context manager capturing sys.stdin, sys.stdout and sys.stderr.
The file handles are replaced with a StringIO object.
On environments that support it, the StringIO object uses newlines
set to os.linesep. Otherwise newlines are converted from \\n to
os.linesep during __exit__.
"""

def _create_StringIO(self, buffer=None):
# Python 3 has a newline argument
try:
return StringIO(buffer, newline=os.linesep)
except TypeError:
self._newline = True
# Python 2 creates an input only stream when buffer is not None
if buffer is None:
return StringIO()
else:
return StringIO(buffer)

def __init__(self, stdin):
self._stdin = StringIO(stdin or '')
self._newline = False
self._stdin = self._create_StringIO(stdin or '')

def __enter__(self):
self._orig_stdin = sys.stdin
self._orig_stdout = sys.stdout
self._orig_stderr = sys.stderr

sys.stdin = self._stdin
sys.stdout = self._stdout_stringio = StringIO()
sys.stderr = self._stderr_stringio = StringIO()
sys.stdout = self._stdout_stringio = self._create_StringIO()
sys.stderr = self._stderr_stringio = self._create_StringIO()

return self

def __exit__(self, *args):
self.output = self._stdout_stringio.getvalue()
self.error = self._stderr_stringio.getvalue()

if self._newline and os.linesep != '\n':
self.output = self.output.replace('\n', os.linesep)
self.error = self.error.replace('\n', os.linesep)

sys.stdin = self._orig_stdin
sys.stdout = self._orig_stdout
sys.stderr = self._orig_stderr
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[tox]
skip_missing_interpreters = True
envlist =
py26,py27,py32,py33,py34,py35,pypy,pypy3

Expand Down

0 comments on commit 2ab47d7

Please sign in to comment.