Repository: timothycrosley/isort
Pull request #406 (2016-03-28 10:49:14)

Feature/fix issue 358


Patch:
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)


Test patch:
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