Repository: timothycrosley/isort
Pull request #1179 (2020-04-18 22:46:02)

More tests


Patch:
diff --git a/.coveragerc b/.coveragerc
index 2f5f9fad..31b649c8 100644
--- a/.coveragerc
+++ b/.coveragerc
@@ -2,6 +2,7 @@
 omit =
   isort/_future/*
   except ImportError:
+  tests/*
 
 include =
   isort/*
diff --git a/isort/api.py b/isort/api.py
index f88a95ce..770109dd 100644
--- a/isort/api.py
+++ b/isort/api.py
@@ -125,7 +125,7 @@ def sorted_imports(
         try:
             compile(output_stream.read(), content_source, "exec", 0, 1)
             output_stream.seek(0)
-        except SyntaxError:
+        except SyntaxError:  # pragma: no cover
             raise IntroducedSyntaxErrors(content_source)
 
     return changed
@@ -344,7 +344,7 @@ def sort_imports(
                 contains_imports = True
                 add_imports = []
 
-            if next_import_section and not import_section:
+            if next_import_section and not import_section:  # pragma: no cover
                 import_section = next_import_section
                 next_import_section = ""
 
@@ -486,7 +486,6 @@ def sort_file(
                             disregard_skip=disregard_skip,
                             **config_kwargs,
                         )
-                    source_file.stream.close()
                     if changed:
                         if show_diff or ask_to_apply:
                             source_file.stream.seek(0)
@@ -499,6 +498,7 @@ def sort_file(
                                 str(source_file.path)
                             ):
                                 return
+                        source_file.stream.close()
                         tmp_file.replace(source_file.path)
                         if not config.quiet:
                             print(f"Fixing {source_file.path}")
@@ -509,5 +509,5 @@ def sort_file(
                         pass
         except ExistingSyntaxErrors:
             warn("{file_path} unable to sort due to existing syntax errors")
-        except IntroducedSyntaxErrors:
+        except IntroducedSyntaxErrors:  # pragma: no cover
             warn("{file_path} unable to sort as isort introduces new syntax errors")
diff --git a/isort/wrap.py b/isort/wrap.py
index 37bf6ec2..a6dbeee1 100644
--- a/isort/wrap.py
+++ b/isort/wrap.py
@@ -10,8 +10,8 @@ from .wrap_modes import formatter_from_string
 def import_statement(
     import_start: str,
     from_imports: List[str],
-    comments: Sequence[str],
-    line_separator: str,
+    comments: Sequence[str] = (),
+    line_separator: str = "\n",
     config: Config = DEFAULT_CONFIG,
     multi_line_output: Optional[Modes] = None,
 ) -> str:


Test patch:
diff --git a/tests/test_api.py b/tests/test_api.py
index 88b1fcc6..6e0795f6 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -1,12 +1,43 @@
 """Tests the isort API module"""
+from unittest.mock import MagicMock, patch
+
 import pytest
 
 from isort import api, exceptions
+from isort.settings import Config
 
 
-def test_sort_file_invalid_syntax(tmpdir) -> None:
-    """Test to ensure file encoding is respected"""
+def test_sort_file(tmpdir) -> None:
     tmp_file = tmpdir.join(f"test_bad_syntax.py")
     tmp_file.write_text("""print('mismathing quotes")""", "utf8")
     with pytest.warns(UserWarning):
         api.sort_file(tmp_file, atomic=True)
+    with pytest.warns(UserWarning):
+        api.sort_file(tmp_file, atomic=True, write_to_stdout=True)
+
+    imperfect = tmpdir.join(f"test_needs_changes.py")
+    imperfect.write_text("import b\nimport a\n", "utf8")
+    api.sort_file(imperfect, write_to_stdout=True, show_diff=True)
+
+    # First show diff, but ensure change wont get written by asking to apply
+    # and ensuring answer is no.
+    with patch("isort.format.input", MagicMock(return_value="n")):
+        api.sort_file(imperfect, show_diff=True, ask_to_apply=True)
+
+    # Then run again, but apply the change without asking
+    api.sort_file(imperfect, show_diff=True)
+
+
+def test_check_file(tmpdir) -> None:
+    perfect = tmpdir.join(f"test_no_changes.py")
+    perfect.write_text("import a\nimport b\n", "utf8")
+    assert api.check_file(perfect, show_diff=True)
+
+    imperfect = tmpdir.join(f"test_needs_changes.py")
+    imperfect.write_text("import b\nimport a\n", "utf8")
+    assert not api.check_file(imperfect, show_diff=True)
+
+
+def test_sorted_imports_multiple_configs() -> None:
+    with pytest.raises(ValueError):
+        api.sort_code_string("import os", config=Config(line_length=80), line_length=80)
diff --git a/tests/test_isort.py b/tests/test_isort.py
index 0b933606..83290de2 100644
--- a/tests/test_isort.py
+++ b/tests/test_isort.py
@@ -4416,6 +4416,8 @@ ELIF UNAME_SYSNAME == "Linux":
 from cef_time cimport *
 from cef_drag cimport *
 
+import os
+
 IF CEF_VERSION == 1:
     from cef_v8 cimport *
     cimport cef_v8_static
@@ -4592,6 +4594,8 @@ ELIF UNAME_SYSNAME == "Linux":
 from cef_drag cimport *
 from cef_time cimport *
 
+import os
+
 IF CEF_VERSION == 1:
     cimport cef_cookie_manager_namespace
     cimport cef_stream_static
diff --git a/tests/test_wrap.py b/tests/test_wrap.py
new file mode 100644
index 00000000..2b8ec6fa
--- /dev/null
+++ b/tests/test_wrap.py
@@ -0,0 +1,15 @@
+from isort import wrap
+from isort.settings import Config
+
+
+def test_import_statement():
+    assert wrap.import_statement("", [], []) == ""
+    assert (
+        wrap.import_statement("from x import ", ["y"], [], config=Config(balanced_wrapping=True))
+        == "from x import (y)"
+    )
+    assert (
+        wrap.import_statement("from long_import ", ["verylong"] * 10, [])
+        == """from long_import (verylong, verylong, verylong, verylong, verylong, verylong,
+                  verylong, verylong, verylong, verylong)"""
+    )