| 1 |
import os |
| 2 |
from pathlib import Path |
| 3 |
from subprocess import CompletedProcess |
| 4 |
from unittest.mock import MagicMock, patch |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from isort import exceptions, hooks |
| 9 |
from isort._version import _IS_COMPILED |
| 10 |
|
| 11 |
|
| 12 |
def test_git_hook(src_dir: str) -> None: |
| 13 |
"""Simple smoke level testing of git hooks""" |
| 14 |
|
| 15 |
# Ensure correct subprocess command is called |
| 16 |
with patch( |
| 17 |
"subprocess.run", return_value=CompletedProcess("command", returncode=0, stdout=b"") |
| 18 |
) as run_mock: |
| 19 |
hooks.git_hook() |
| 20 |
run_mock.assert_called_once() |
| 21 |
assert run_mock.call_args[0][0] == [ |
| 22 |
"git", |
| 23 |
"diff-index", |
| 24 |
"--cached", |
| 25 |
"--name-only", |
| 26 |
"--diff-filter=ACMRTUXB", |
| 27 |
"HEAD", |
| 28 |
] |
| 29 |
|
| 30 |
with patch( |
| 31 |
"subprocess.run", return_value=CompletedProcess("command", returncode=0, stdout=b"") |
| 32 |
) as run_mock: |
| 33 |
hooks.git_hook(lazy=True) |
| 34 |
run_mock.assert_called_once() |
| 35 |
assert run_mock.call_args[0][0] == [ |
| 36 |
"git", |
| 37 |
"diff-index", |
| 38 |
"--name-only", |
| 39 |
"--diff-filter=ACMRTUXB", |
| 40 |
"HEAD", |
| 41 |
] |
| 42 |
|
| 43 |
# Test that non python files aren't processed |
| 44 |
with patch( |
| 45 |
"subprocess.run", |
| 46 |
return_value=CompletedProcess( |
| 47 |
"command", returncode=0, stdout=b"README.md\nsetup.cfg\nLICDENSE\nmkdocs.yml\ntest" |
| 48 |
), |
| 49 |
) as run_mock: |
| 50 |
hooks.git_hook(modify=True) |
| 51 |
run_mock.assert_called_once() |
| 52 |
|
| 53 |
# Test with incorrectly sorted file returned from git |
| 54 |
file_name = os.path.join(src_dir, "main.py") |
| 55 |
with patch( |
| 56 |
"subprocess.run", |
| 57 |
side_effect=[ |
| 58 |
CompletedProcess("command", returncode=0, stdout=file_name.encode()), |
| 59 |
CompletedProcess("command", returncode=0, stdout=b"import b,a"), |
| 60 |
], |
| 61 |
) as run_mock: |
| 62 |
errors = hooks.git_hook(modify=True, strict=True) |
| 63 |
assert run_mock.call_count == 2 |
| 64 |
assert errors == 1 |
| 65 |
|
| 66 |
|
| 67 |
@pytest.mark.skipif(reason="Can't use these mocks in mypyc-compiled code.", condition=_IS_COMPILED) |
| 68 |
def test_git_hook_with_mocks(src_dir: str) -> None: |
| 69 |
""" |
| 70 |
Additional tests for the git hook, split off to allow running some when compiled. |
| 71 |
""" |
| 72 |
mock_main_py = MagicMock(return_value=[os.path.join(src_dir, "main.py")]) |
| 73 |
|
| 74 |
mock_imperfect = MagicMock() |
| 75 |
mock_imperfect.return_value.stdout = b"import b\nimport a" |
| 76 |
|
| 77 |
# Test with sorted file returned from git and modify=False |
| 78 |
with patch("isort.hooks.get_lines", mock_main_py): |
| 79 |
with patch("subprocess.run", mock_imperfect): |
| 80 |
with patch("isort.api.sort_file", MagicMock(return_value=False)) as api_mock: |
| 81 |
hooks.git_hook(modify=False) |
| 82 |
api_mock.assert_not_called() |
| 83 |
|
| 84 |
# Test with skipped file returned from git |
| 85 |
with patch("isort.hooks.get_lines", MagicMock(return_value=[os.path.join(src_dir, "main.py")])): |
| 86 |
|
| 87 |
class FakeProcessResponse: |
| 88 |
stdout = b"# isort: skip-file\nimport b\nimport a\n" |
| 89 |
|
| 90 |
with patch("subprocess.run", MagicMock(return_value=FakeProcessResponse())): |
| 91 |
with patch("isort.api", MagicMock(side_effect=exceptions.FileSkipped("", ""))): |
| 92 |
hooks.git_hook(modify=True) |
| 93 |
|
| 94 |
|
| 95 |
def test_git_hook_lazy(tmp_path: Path) -> None: |
| 96 |
# Write an actual unsorted file to disk & check that `lazy=True` spots it |
| 97 |
|
| 98 |
has_problems = tmp_path / "test_has_problems.py" |
| 99 |
has_problems.write_text("import b\nimport a\n", "utf8") |
| 100 |
|
| 101 |
with patch( |
| 102 |
"subprocess.run", |
| 103 |
side_effect=[ |
| 104 |
CompletedProcess("command", returncode=0, stdout=str(has_problems).encode()), |
| 105 |
# Second call to subprocess.run should not be made when lazy=True |
| 106 |
RuntimeError("donotcall"), |
| 107 |
], |
| 108 |
) as run_mock: |
| 109 |
found_errors = hooks.git_hook(lazy=True, strict=True) |
| 110 |
# subprocess.run (via get_output) should not be called when lazy=True |
| 111 |
# because we're not asking git for the staged content |
| 112 |
assert run_mock.call_count == 1 |
| 113 |
|
| 114 |
assert found_errors == 1 |
| 115 |
|
| 116 |
|
| 117 |
@pytest.mark.skipif(reason="Can't use these mocks in mypyc-compiled code.", condition=_IS_COMPILED) |
| 118 |
def test_git_hook_uses_the_configuration_file_specified_in_settings_path(tmp_path: Path) -> None: |
| 119 |
subdirectory_path = tmp_path / "subdirectory" |
| 120 |
configuration_file_path = subdirectory_path / ".isort.cfg" |
| 121 |
|
| 122 |
# Inserting the modified file in the parent directory of the configuration file ensures that it |
| 123 |
# will not be found by the normal search routine |
| 124 |
modified_file_path = configuration_file_path.parent.parent / "somefile.py" |
| 125 |
|
| 126 |
# This section will be used to check that the configuration file was indeed loaded |
| 127 |
section = "testsection" |
| 128 |
|
| 129 |
os.mkdir(subdirectory_path) |
| 130 |
with open(configuration_file_path, "w") as fd: |
| 131 |
fd.write("[isort]\n") |
| 132 |
fd.write(f"sections={section}") |
| 133 |
|
| 134 |
with open(modified_file_path, "w") as fd: |
| 135 |
pass |
| 136 |
|
| 137 |
files_modified = [str(modified_file_path.absolute())] |
| 138 |
with patch("isort.hooks.get_lines", MagicMock(return_value=files_modified)): |
| 139 |
with patch("isort.hooks.get_output", MagicMock(return_value="")): |
| 140 |
with patch("isort.api.check_code_string", MagicMock()) as run_mock: |
| 141 |
hooks.git_hook(settings_file=str(configuration_file_path)) |
| 142 |
|
| 143 |
assert run_mock.call_args[1]["config"].sections == (section,) |