Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cross-platfrom issues with test suite #529

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pycodestyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -1317,9 +1317,9 @@ def normalize_paths(value, parent=os.curdir):
paths = []
for path in value.split(','):
path = path.strip()
if '/' in path:
if os.path.sep in path:
path = os.path.abspath(os.path.join(parent, path))
paths.append(path.rstrip('/'))
paths.append(path.rstrip(os.path.sep))
return paths


Expand Down
46 changes: 43 additions & 3 deletions testsuite/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,46 @@
from testsuite.support import ROOT_DIR, PseudoFile


def safe_line_split(line):
r"""Parses output lines of the form

[location & file]:[row]:[column]:[message]

and returns a tuple of the form

(path, row, column, message)

This function handles the OS-issues related to a ":" appearing in some
Windows paths.

In Windows, the location is usually denoted as "[DriveLetter]:[location]".
On all other platforms, there is no colon in the location.

The majority of time on windows, the line will look like:
C:\projects\pycodestyle\testsuite\E11.py:3:3: E111 indentation is ...

Or if somebody is using a networked location
\\projects\pycodestyle\testsuite\E11.py:3:3: E111 indentation is ...

On non-windows, the same line would looke like:
/home/projects/pycodestyle/testsuite/E11.py:3:3: E111 indentation is ...
"""

split = line.split(':')

if sys.platform == 'win32': # Note, even 64 bit platforms return 'win32'
if len(split) == 5: # This will be the most common
drive, path, x, y, msg = split
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you leave comments explaining what kind of situation would result in having 5 items? (Likewise for the elif checking for a length of 4) Bonus points for an example in the comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, that will be helpful as we continue to build up the windows support and verify that use case.

path = drive + ':' + path
elif len(split) == 4: # If the location is a network share
path, x, y, msg = split
else:
raise Exception("Unhandled edge case parsing message: " + line)
else:
path, x, y, msg = split
return path, x, y, msg


class ShellTestCase(unittest.TestCase):
"""Test the usual CLI options and output."""

Expand Down Expand Up @@ -77,7 +117,7 @@ def test_check_simple(self):
self.assertFalse(stderr)
self.assertEqual(len(stdout), 17)
for line, num, col in zip(stdout, (3, 6, 9, 12), (3, 6, 1, 5)):
path, x, y, msg = line.split(':')
path, x, y, msg = safe_line_split(line)
self.assertTrue(path.endswith(E11))
self.assertEqual(x, str(num))
self.assertEqual(y, str(col))
Expand Down Expand Up @@ -141,7 +181,7 @@ def test_check_diff(self):
self.assertEqual(errcode, 1)
self.assertFalse(stderr)
for line, num, col in zip(stdout, (3, 6), (3, 6)):
path, x, y, msg = line.split(':')
path, x, y, msg = safe_line_split(line)
self.assertEqual(x, str(num))
self.assertEqual(y, str(col))
self.assertTrue(msg.startswith(' E11'))
Expand All @@ -154,7 +194,7 @@ def test_check_diff(self):
self.assertEqual(errcode, 1)
self.assertFalse(stderr)
for line, num, col in zip(stdout, (3, 6), (3, 6)):
path, x, y, msg = line.split(':')
path, x, y, msg = safe_line_split(line)
self.assertEqual(x, str(num))
self.assertEqual(y, str(col))
self.assertTrue(msg.startswith(' E11'))
Expand Down
16 changes: 11 additions & 5 deletions testsuite/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
import os
import unittest

import sys
from pycodestyle import normalize_paths


Expand All @@ -17,7 +17,13 @@ def test_normalize_paths(self):
self.assertEqual(normalize_paths('foo'), ['foo'])
self.assertEqual(normalize_paths('foo,bar'), ['foo', 'bar'])
self.assertEqual(normalize_paths('foo, bar '), ['foo', 'bar'])
self.assertEqual(normalize_paths('/foo/bar,baz/../bat'),
['/foo/bar', cwd + '/bat'])
self.assertEqual(normalize_paths(".pyc,\n build/*"),
['.pyc', cwd + '/build/*'])

if 'win' in sys.platform:
self.assertEqual(normalize_paths(r'C:\foo\bar,baz\..\bat'),
[r'C:\foo\bar', cwd + r'\bat'])
self.assertEqual(normalize_paths(".pyc"), ['.pyc'])
else:
self.assertEqual(normalize_paths('/foo/bar,baz/../bat'),
['/foo/bar', cwd + '/bat'])
self.assertEqual(normalize_paths(".pyc,\n build/*"),
['.pyc', cwd + '/build/*'])