Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #20 from prusse-martin/fb-fix-isort-skipped-files
Browse files Browse the repository at this point in the history
Fb fix isort skipped files
  • Loading branch information
nicoddemus authored Mar 21, 2018
2 parents bcb154d + e2ad3fb commit 7758ff4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
11 changes: 6 additions & 5 deletions esss_fix_format/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ def main(files_or_directories, check, stdin, commit):
errors.append(error_msg)

sorter = isort.SortImports(file_contents=new_contents, settings_path=settings_path)
# strangely, if the entire file is skipped by an "isort:skip_file"
# instruction in the docstring, SortImports doesn't even contain an
# "output" attribute
if hasattr(sorter, 'output'):
new_contents = sorter.output
# On older versions if the entire file is skipped (eg.: by an "isort:skip_file")
# instruction in the docstring, SortImports doesn't even contain an "output" attribute.
# In some recent versions it is `None`.
new_contents = getattr(sorter, 'output', None)
if new_contents is None:
new_contents = original_contents

new_contents = fix_whitespace(new_contents.splitlines(True), eol, ends_with_eol)
changed = new_contents != original_contents
Expand Down
22 changes: 14 additions & 8 deletions tests/test_esss_fix_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,20 @@ def test_empty_file(tmpdir, sort_cfg_to_tmpdir):
run([str(filename)], expected_exit=0)


def test_skip_entire_file(tmpdir, sort_cfg_to_tmpdir):
"""Check that a module-level isort:skip_file correctly skips that file"""
source = textwrap.dedent('''\
"""
isort:skip_file
"""
import sys
''')
@pytest.mark.parametrize(
'source',
[
'',
'"""\nisort:skip_file\n"""\nimport sys\nimport os\n',
'# isort:skip_file\nimport sys\nimport os\n',
],
ids=[
'empty file',
'module-level isort:skip_file docstring',
'module-level isort:skip_file comment',
]
)
def test_skip_entire_file(tmpdir, sort_cfg_to_tmpdir, source):
filename = tmpdir.join('test.py')
filename.write(source)
output = run([str(filename)], expected_exit=0)
Expand Down

0 comments on commit 7758ff4

Please sign in to comment.