Skip to content

Commit 2ab47d7

Browse files
jayvdbsigmavirus24
authored andcommitted
Fix TestMain tests on Windows (#75)
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.
1 parent b161e50 commit 2ab47d7

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

.appveyor.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# To activate, change the Appveyor settings to use `.appveyor.yml`.
2+
install:
3+
- python -m pip install tox
4+
5+
build: off
6+
7+
test_script:
8+
- python -m tox

pyflakes/test/test_api.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,50 @@ def __init__(self, lineno, col_offset=0):
5555

5656
class SysStreamCapturing(object):
5757

58-
"""Replaces sys.stdin, sys.stdout and sys.stderr with StringIO objects."""
58+
"""
59+
Context manager capturing sys.stdin, sys.stdout and sys.stderr.
60+
61+
The file handles are replaced with a StringIO object.
62+
On environments that support it, the StringIO object uses newlines
63+
set to os.linesep. Otherwise newlines are converted from \\n to
64+
os.linesep during __exit__.
65+
"""
66+
67+
def _create_StringIO(self, buffer=None):
68+
# Python 3 has a newline argument
69+
try:
70+
return StringIO(buffer, newline=os.linesep)
71+
except TypeError:
72+
self._newline = True
73+
# Python 2 creates an input only stream when buffer is not None
74+
if buffer is None:
75+
return StringIO()
76+
else:
77+
return StringIO(buffer)
5978

6079
def __init__(self, stdin):
61-
self._stdin = StringIO(stdin or '')
80+
self._newline = False
81+
self._stdin = self._create_StringIO(stdin or '')
6282

6383
def __enter__(self):
6484
self._orig_stdin = sys.stdin
6585
self._orig_stdout = sys.stdout
6686
self._orig_stderr = sys.stderr
6787

6888
sys.stdin = self._stdin
69-
sys.stdout = self._stdout_stringio = StringIO()
70-
sys.stderr = self._stderr_stringio = StringIO()
89+
sys.stdout = self._stdout_stringio = self._create_StringIO()
90+
sys.stderr = self._stderr_stringio = self._create_StringIO()
7191

7292
return self
7393

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

98+
if self._newline and os.linesep != '\n':
99+
self.output = self.output.replace('\n', os.linesep)
100+
self.error = self.error.replace('\n', os.linesep)
101+
78102
sys.stdin = self._orig_stdin
79103
sys.stdout = self._orig_stdout
80104
sys.stderr = self._orig_stderr

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[tox]
2+
skip_missing_interpreters = True
23
envlist =
34
py26,py27,py32,py33,py34,py35,pypy,pypy3
45

0 commit comments

Comments
 (0)