def test_sort_file_with_bad_syntax(tmpdir) -> None: tmp_file = tmpdir.join("test_bad_syntax.py") tmp_file.write_text("""print('mismatching 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)
def git_hook(strict: bool = False, modify: bool = False) -> int: """ Git pre-commit hook to check staged files for isort errors :param bool strict - if True, return number of errors on exit, causing the hook to fail. If False, return zero so it will just act as a warning. :param bool modify - if True, fix the sources if they are not sorted properly. If False, only report result without modifying anything. :return number of errors if in strict mode, 0 otherwise. """ # Get list of files modified and staged diff_cmd = [ "git", "diff-index", "--cached", "--name-only", "--diff-filter=ACMRTUXB", "HEAD" ] files_modified = get_lines(diff_cmd) errors = 0 for filename in files_modified: if filename.endswith(".py"): # Get the staged contents of the file staged_cmd = ["git", "show", f":{filename}"] staged_contents = get_output(staged_cmd) if not api.check_code_string(staged_contents, file_path=Path(filename)): errors += 1 if modify: api.sort_file(filename) return errors if strict else 0
def git_hook(strict: bool = False, modify: bool = False, lazy: bool = False, settings_file: str = "") -> int: """ Git pre-commit hook to check staged files for isort errors :param bool strict - if True, return number of errors on exit, causing the hook to fail. If False, return zero so it will just act as a warning. :param bool modify - if True, fix the sources if they are not sorted properly. If False, only report result without modifying anything. :param bool lazy - if True, also check/fix unstaged files. This is useful if you frequently use ``git commit -a`` for example. If False, ony check/fix the staged files for isort errors. :param str settings_file - A path to a file to be used as the configuration file for this run. When settings_file is the empty string, the configuration file will be searched starting at the directory containing the first staged file, if any, and going upward in the directory structure. :return number of errors if in strict mode, 0 otherwise. """ # Get list of files modified and staged diff_cmd = [ "git", "diff-index", "--cached", "--name-only", "--diff-filter=ACMRTUXB", "HEAD" ] if lazy: diff_cmd.remove("--cached") files_modified = get_lines(diff_cmd) if not files_modified: return 0 errors = 0 config = Config( settings_file=settings_file, settings_path=os.path.dirname(os.path.abspath(files_modified[0])), ) for filename in files_modified: if filename.endswith(".py"): # Get the staged contents of the file staged_cmd = ["git", "show", f":{filename}"] staged_contents = get_output(staged_cmd) try: if not api.check_code_string(staged_contents, file_path=Path(filename), config=config): errors += 1 if modify: api.sort_file(filename, config=config) except exceptions.FileSkipped: # pragma: no cover pass return errors if strict else 0
def test_other_ask_to_apply(imperfect) -> None: # 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")): assert not api.sort_file(imperfect, ask_to_apply=True) assert imperfect.read() == imperfect_content # Then run again, but apply the change (answer is yes) with patch("isort.format.input", MagicMock(return_value="y")): assert api.sort_file(imperfect, ask_to_apply=True) assert imperfect.read() == fixed_content
def test_sort_file(tmpdir) -> None: tmp_file = tmpdir.join("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("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_sort_file_to_stdout(capsys, imperfect) -> None: assert api.sort_file(imperfect, write_to_stdout=True) out, _ = capsys.readouterr() assert out == fixed_content.replace("\n", os.linesep)
def test_sort_file_in_place(imperfect) -> None: assert api.sort_file(imperfect, overwrite_in_place=True) assert imperfect.read() == fixed_content
def test_sort_file(imperfect) -> None: assert api.sort_file(imperfect) assert imperfect.read() == fixed_content
def sort_file(): api.sort_file(imperfect, overwrite_in_place=True)
def sort_file(): api.sort_file(imperfect)
def _sort_imports(output_file_name): sort_file(filename=output_file_name, ask_to_apply=False)
def process(self, file_path: Path): """Process the given file through isort.""" sort_file( filename=str(file_path), **self._settings, )