|
7 | 7 |
|
8 | 8 | import isort |
9 | 9 | import isort.sections |
10 | | -from isort.core import STRING_PREFIXES |
| 10 | +from isort.core import STRING_PREFIXES, _float_to_top |
11 | 11 | from isort.main import main |
12 | 12 | from isort.wrap_modes import WrapModes |
13 | 13 |
|
@@ -919,6 +919,61 @@ def test_isort_float_to_top_respects_isort_off_with_crlf_issue_2528(): |
919 | 919 | ) |
920 | 920 |
|
921 | 921 |
|
| 922 | +@pytest.mark.parametrize("line_ending", ["\n", "\r\n"]) |
| 923 | +@pytest.mark.parametrize( |
| 924 | + "boundary", |
| 925 | + [ |
| 926 | + "# isort: split\n", |
| 927 | + "import z # isort: split\n", |
| 928 | + "# isort: off\nimport z\nimport a\n# isort: on\n", |
| 929 | + ], |
| 930 | +) |
| 931 | +def test_float_to_top_check_accepts_unchanged_sections_issue_2681(line_ending, boundary): |
| 932 | + source = ("import os\n\n" + boundary + "\nimport sys\n").replace("\n", line_ending) |
| 933 | + |
| 934 | + assert isort.code(source, float_to_top=True) == source |
| 935 | + assert isort.check_code(source, float_to_top=True, show_diff=True) |
| 936 | + |
| 937 | + |
| 938 | +@pytest.mark.parametrize("line_ending", ["\n", "\r\n"]) |
| 939 | +@pytest.mark.parametrize("blank_lines", [0, 1, 3]) |
| 940 | +def test_float_to_top_preserves_boundary_spacing_issue_2681(line_ending, blank_lines): |
| 941 | + source = ("import os\n" + "\n" * blank_lines + "# isort: split\n").replace("\n", line_ending) |
| 942 | + result = _float_to_top(StringIO(source), [], isort.Config(float_to_top=True), "py") |
| 943 | + |
| 944 | + assert result.input_stream.read() == source |
| 945 | + assert not result.made_changes |
| 946 | + |
| 947 | + |
| 948 | +@pytest.mark.parametrize("line_ending", ["\n", "\r\n"]) |
| 949 | +def test_float_to_top_check_detects_unsorted_sections_issue_2681(line_ending): |
| 950 | + source = "import sys\nimport os\n\n# isort: split\n\nimport a\n".replace("\n", line_ending) |
| 951 | + expected = "import os\nimport sys\n\n# isort: split\n\nimport a\n".replace("\n", line_ending) |
| 952 | + |
| 953 | + assert not isort.check_code(source, float_to_top=True) |
| 954 | + assert isort.code(source, float_to_top=True) == expected |
| 955 | + assert isort.check_code(expected, float_to_top=True) |
| 956 | + |
| 957 | + |
| 958 | +@pytest.mark.parametrize("configured_ending", ["\n", "\r\n"]) |
| 959 | +def test_float_to_top_configured_line_ending_issue_2681(configured_ending): |
| 960 | + source = "import os\r\n\r\n# isort: split\r\n\r\nimport sys\r\n" |
| 961 | + expected = source.replace("\r\n", configured_ending) |
| 962 | + result = isort.code(source, float_to_top=True, line_ending=configured_ending) |
| 963 | + |
| 964 | + assert result == expected |
| 965 | + assert isort.check_code(result, float_to_top=True, line_ending=configured_ending) |
| 966 | + |
| 967 | + |
| 968 | +def test_float_to_top_cli_check_accepts_crlf_issue_2681(tmp_path): |
| 969 | + path = tmp_path / "example.py" |
| 970 | + source = b"import os\r\n\r\n# isort: split\r\n\r\nimport sys\r\n" |
| 971 | + path.write_bytes(source) |
| 972 | + |
| 973 | + main(["--float-to-top", "--check-only", str(path)]) |
| 974 | + assert path.read_bytes() == source |
| 975 | + |
| 976 | + |
922 | 977 | def test_isort_doesnt_float_to_top_correctly_when_imports_not_at_top_issue_1382(): |
923 | 978 | """isort should float existing imports to the top, if they are currently below the top. |
924 | 979 | See: https://github.com/PyCQA/isort/issues/1382 |
|
0 commit comments