summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Edmund Crosley <timothy.crosley@gmail.com>2016-03-28 03:52:47 -0700
committerTimothy Edmund Crosley <timothy.crosley@gmail.com>2016-03-28 03:52:47 -0700
commit9469519ab6bca7b39a2db82bdc361cdb952c9d5d (patch)
treeec71f68073ff5e5668e50232bc186f175d0c937a
parent6bedf1c2486a4bc7f5f89f33b60a899f04cb217f (diff)
parenta5b830d2d63182f5eb254b6e4b51443a0a6b710b (diff)
downloadisort-9469519ab6bca7b39a2db82bdc361cdb952c9d5d.tar.gz
Merge pull request #406 from timothycrosley/feature/fix-issue-358
Feature/fix issue 358
-rw-r--r--isort/isort.py3
-rw-r--r--isort/settings.py1
-rw-r--r--test_isort.py57
3 files changed, 60 insertions, 1 deletions
diff --git a/isort/isort.py b/isort/isort.py
index 44900b4c..20cc96f5 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -699,6 +699,7 @@ class SortImports(object):
self._in_top_comment = False
while not self._at_end():
line = self._get_line()
+ statement_index = self.index
skip_line = self._skip_line(line)
if line in self._section_comments and not skip_line:
@@ -801,7 +802,7 @@ class SortImports(object):
last = self.out_lines[-1].rstrip()
else:
last = ""
- if self.index - 1 == self.import_index:
+ if statement_index - 1 == self.import_index:
self.import_index -= len(self.comments['above']['from'].get(import_from, []))
if root.get(import_from, False):
diff --git a/isort/settings.py b/isort/settings.py
index 75718d95..df5552eb 100644
--- a/isort/settings.py
+++ b/isort/settings.py
@@ -203,6 +203,7 @@ def should_skip(filename, config, path='/'):
"""Returns True if the file should be skipped based on the passed in settings."""
for skip_path in config['skip']:
if os.path.join(path, filename).endswith('/' + skip_path.lstrip('/')):
+ print(skip_path)
return True
position = os.path.split(filename)
diff --git a/test_isort.py b/test_isort.py
index 2fb1879d..b15d46ae 100644
--- a/test_isort.py
+++ b/test_isort.py
@@ -1681,3 +1681,60 @@ def test_forced_sepatate_globs():
'\n'
'from myproject.bar.models import Bar\n'
'from myproject.foo.models import Foo\n')
+
+
+def test_no_additional_lines_issue_358():
+ """Test to ensure issue 358 is resovled and running isort multiple times does not add extra newlines"""
+ test_input = ('"""This is a docstring"""\n'
+ '# This is a comment\n'
+ 'from __future__ import (\n'
+ ' absolute_import,\n'
+ ' division,\n'
+ ' print_function,\n'
+ ' unicode_literals\n'
+ ')\n')
+ expected_output = ('"""This is a docstring"""\n'
+ '# This is a comment\n'
+ 'from __future__ import (\n'
+ ' absolute_import,\n'
+ ' division,\n'
+ ' print_function,\n'
+ ' unicode_literals\n'
+ ')\n')
+ test_output = SortImports(file_contents=test_input, multi_line_output=3, line_length=20).output
+ assert test_output == expected_output
+
+ test_output = SortImports(file_contents=test_output, multi_line_output=3, line_length=20).output
+ assert test_output == expected_output
+
+ for attempt in range(5):
+ test_output = SortImports(file_contents=test_output, multi_line_output=3, line_length=20).output
+ assert test_output == expected_output
+
+ test_input = ('"""This is a docstring"""\n'
+ '\n'
+ '# This is a comment\n'
+ 'from __future__ import (\n'
+ ' absolute_import,\n'
+ ' division,\n'
+ ' print_function,\n'
+ ' unicode_literals\n'
+ ')\n')
+ expected_output = ('"""This is a docstring"""\n'
+ '\n'
+ '# This is a comment\n'
+ 'from __future__ import (\n'
+ ' absolute_import,\n'
+ ' division,\n'
+ ' print_function,\n'
+ ' unicode_literals\n'
+ ')\n')
+ test_output = SortImports(file_contents=test_input, multi_line_output=3, line_length=20).output
+ assert test_output == expected_output
+
+ test_output = SortImports(file_contents=test_output, multi_line_output=3, line_length=20).output
+ assert test_output == expected_output
+
+ for attempt in range(5):
+ test_output = SortImports(file_contents=test_output, multi_line_output=3, line_length=20).output
+ assert test_output == expected_output