Skip to content

Commit

Permalink
Merge pull request #69 from maerteijn/master
Browse files Browse the repository at this point in the history
Fixes an issue with isort output when pipes (stdin) are uses
  • Loading branch information
gforcada committed Mar 19, 2019
2 parents bb4c723 + 5a847aa commit 88d6ed0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ python:
- 'pypy3'
cache: pip
install:
- pip install -e .[test]
- pip install .[test]
script:
- flake8 *.py
- pytest run_tests.py
Expand Down
15 changes: 10 additions & 5 deletions flake8_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Flake8Isort(object):

config_file = None
show_traceback = False
stdin_display_name = None

def __init__(self, tree, filename, lines, search_current=True):
self.filename = filename
Expand Down Expand Up @@ -63,22 +64,26 @@ def parse_options(cls, options):
else:
cls.config_file = False

cls.stdin_display_name = options.stdin_display_name
cls.show_traceback = options.isort_show_traceback

def run(self):
settings_file = self.search_isort_config()
if self.config_file and not settings_file:
yield 0, 0, self.no_config_msg, type(self)
else:
if self.filename is not self.stdin_display_name:
file_path = self.filename
else:
file_path = None
with OutputCapture() as buffer:
sort_result = SortImports(
file_path=self.filename,
file_path=file_path,
file_contents=''.join(self.lines),
check=True,
settings_path=settings_file,
show_diff=True,
)

traceback = self._format_isort_output(buffer)

for line_num, message in self.sortimports_linenum_msg(sort_result):
Expand All @@ -87,7 +92,7 @@ def run(self):
yield line_num, 0, message, type(self)

def search_isort_config(self):
# type: () -> Optional[str]
# type: () -> Optional[str] # noqa: F821
"""Search for isort configuration all the way up to the root folder
Looks for ``.isort.cfg``, ``.editorconfig`` or ``[isort]`` section in
Expand All @@ -113,12 +118,12 @@ def search_isort_config(self):
return None

def search_isort_config_at_current(self):
# type: () -> Optional[str]
# type: () -> Optional[str] # noqa: F821
"""Search for isort configuration at current directory"""
return self._search_config_on_path(os.path.realpath('.'))

def _search_config_on_path(self, path):
# type: (str) -> Optional[str]
# type: (str) -> Optional[str] # noqa: F821
"""Search for isort configuration files at the specifed path.
Args:
Expand Down
8 changes: 6 additions & 2 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,11 @@ def test_config_file(self):

def test_isort_formatted_output(self):
options = collections.namedtuple(
'Options', ['no_isort_config', 'isort_show_traceback']
'Options', [
'no_isort_config',
'isort_show_traceback',
'stdin_display_name'
]
)

(file_path, lines) = self._given_a_file_in_test_dir(
Expand All @@ -283,7 +287,7 @@ def test_isort_formatted_output(self):

with OutputCapture():
checker = Flake8Isort(None, file_path, lines)
checker.parse_options(options(None, True))
checker.parse_options(options(None, True, 'stdin'))
ret = list(checker.run())
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0][0], 2)
Expand Down

0 comments on commit 88d6ed0

Please sign in to comment.