diff options
| author | Timothy Crosley <timothy.crosley@gmail.com> | 2020-10-03 01:24:35 -0700 |
|---|---|---|
| committer | Timothy Crosley <timothy.crosley@gmail.com> | 2020-10-03 01:24:35 -0700 |
| commit | 5fad3537b0e4eed1f21454f33f7387ac79b2caa8 (patch) | |
| tree | 8dac26526289cc83a9579623c8795d8545de47e8 /tests/unit | |
| parent | 82d76ef81b90ed213f9ce3f46d3c97e1322025be (diff) | |
| parent | fc956a7e01ab9f24488a5a18954ea23ed0e649c2 (diff) | |
| download | isort-5fad3537b0e4eed1f21454f33f7387ac79b2caa8.tar.gz | |
Merge branch 'develop' of https://github.com/timothycrosley/isort into issue/1487/improve-handling-of-encoding-errors
Diffstat (limited to 'tests/unit')
| -rw-r--r-- | tests/unit/test_main.py | 101 | ||||
| -rw-r--r-- | tests/unit/test_parse.py | 1 | ||||
| -rw-r--r-- | tests/unit/test_regressions.py | 75 |
3 files changed, 177 insertions, 0 deletions
diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index 9c2da466..3fb75ed6 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -70,6 +70,8 @@ def test_parse_args(): assert main.parse_args(["--dt"]) == {"order_by_type": False} assert main.parse_args(["--only-sections"]) == {"only_sections": True} assert main.parse_args(["--os"]) == {"only_sections": True} + assert main.parse_args(["--om"]) == {"only_modified": True} + assert main.parse_args(["--only-modified"]) == {"only_modified": True} def test_ascii_art(capsys): @@ -750,4 +752,103 @@ __revision__ = 'יייי' main.main([str(tmp_file), str(normal_file), "--verbose"]) out, error = capsys.readouterr() + # ensures that only-modified flag works with stdin + input_content = TextIOWrapper( + BytesIO( + b""" +import a +import b +""" + ) + ) + + main.main(["-", "--verbose", "--only-modified"], stdin=input_content) + out, error = capsys.readouterr() + + assert "else-type place_module for a returned THIRDPARTY" not in out + assert "else-type place_module for b returned THIRDPARTY" not in out + + +def test_only_modified_flag(tmpdir, capsys): + # ensures there is no verbose output for correct files with only-modified flag + + file1 = tmpdir.join("file1.py") + file1.write( + """ +import a +import b +""" + ) + + file2 = tmpdir.join("file2.py") + file2.write( + """ +import math + +import pandas as pd +""" + ) + + main.main([str(file1), str(file2), "--verbose", "--only-modified"]) + out, error = capsys.readouterr() + + assert ( + out + == f""" + _ _ + (_) ___ ___ _ __| |_ + | |/ _/ / _ \\/ '__ _/ + | |\\__ \\/\\_\\/| | | |_ + |_|\\___/\\___/\\_/ \\_/ + + isort your imports, so you don't have to. + + VERSION {__version__} + +""" + ) + + assert not error + + # ensures that verbose output is only for modified file(s) with only-modified flag + + file3 = tmpdir.join("file3.py") + file3.write( + """ +import sys +import os +""" + ) + + main.main([str(file1), str(file2), str(file3), "--verbose", "--only-modified"]) + out, error = capsys.readouterr() + + assert "else-type place_module for sys returned STDLIB" in out + assert "else-type place_module for os returned STDLIB" in out + assert "else-type place_module for math returned STDLIB" not in out + assert "else-type place_module for pandas returned THIRDPARTY" not in out + + assert not error + + # ensures that the behaviour is consistent for check flag with only-modified flag + + main.main([str(file1), str(file2), "--check-only", "--verbose", "--only-modified"]) + out, error = capsys.readouterr() + + assert ( + out + == f""" + _ _ + (_) ___ ___ _ __| |_ + | |/ _/ / _ \\/ '__ _/ + | |\\__ \\/\\_\\/| | | |_ + |_|\\___/\\___/\\_/ \\_/ + + isort your imports, so you don't have to. + + VERSION {__version__} + +""" + ) + assert not error diff --git a/tests/unit/test_parse.py b/tests/unit/test_parse.py index 98183617..0becac90 100644 --- a/tests/unit/test_parse.py +++ b/tests/unit/test_parse.py @@ -37,6 +37,7 @@ def test_file_contents(): original_line_count, _, _, + _, ) = parse.file_contents(TEST_CONTENTS, config=Config(default_section="")) assert "\n".join(in_lines) == TEST_CONTENTS assert "import" not in "\n".join(out_lines) diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index 5dc2fc81..75759522 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -716,6 +716,81 @@ import os ) +def test_isort_float_to_top_with_sort_on_off_tests(): + """Characterization test for current behaviour of float-to-top on isort: on/off sections. + - imports in isort:off sections stay where they are + - imports in isort:on sections float up, but to the top of the isort:on section (not the + top of the file)""" + assert ( + isort.code( + """ +def foo(): + pass + +import a + +# isort: off +import stays_in_section + +x = 1 + +import stays_in_place + +# isort: on + +def bar(): + pass + +import floats_to_top_of_section + +def baz(): + pass +""", + float_to_top=True, + ) + == """import a + + +def foo(): + pass + +# isort: off +import stays_in_section + +x = 1 + +import stays_in_place + +# isort: on +import floats_to_top_of_section + + +def bar(): + pass + + +def baz(): + pass +""" + ) + + to_sort = """# isort: off + +def foo(): + pass + +import stays_in_place +import no_float_to_to_top +import no_ordering + +def bar(): + pass +""" + + # No changes if isort is off + assert isort.code(to_sort, float_to_top=True) == to_sort + + def test_isort_doesnt_float_to_top_correctly_when_imports_not_at_top_issue_1382(): """isort should float existing imports to the top, if they are currently below the top. See: https://github.com/PyCQA/isort/issues/1382 |
