Exemple #1
0
def test_fuzz_sort_imports(file_name, config, check, ask_to_apply, write_to_stdout):
    main.sort_imports(
        file_name=file_name,
        config=config,
        check=check,
        ask_to_apply=ask_to_apply,
        write_to_stdout=write_to_stdout,
    )
Exemple #2
0
def test_sort_imports_error_handling(tmpdir, mocker, capsys):
    tmp_file = tmpdir.join("file.py")
    tmp_file.write("import os, sys\n")
    mocker.patch("isort.core.process").side_effect = IndexError("Example unhandled exception")
    with pytest.raises(IndexError):
        main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted  # type: ignore # noqa

    out, error = capsys.readouterr()
    assert "Unrecoverable exception thrown when parsing" in error
Exemple #3
0
def isort(check):  # sorting headers -the imports
    """
    Run isort on all the files in app and tests directory
    """
    exit_code = 0
    config = from_path(os.getcwd()).copy()
    config["check"] = check
    file_names = iter_source_code(["app", "tests", "manage.py"], config, [])
    for file_name in file_names:
        sort_attempt = sort_imports(file_name, **config)
        if sort_attempt:
            if sort_attempt.incorrectly_sorted:
                exit_code = 1
    raise sys.exit(exit_code)
Exemple #4
0
def test_sort_imports(tmpdir):
    tmp_file = tmpdir.join("file.py")
    tmp_file.write("import os, sys\n")
    assert main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted
    main.sort_imports(str(tmp_file), DEFAULT_CONFIG)
    assert not main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted

    skip_config = Config(skip=["file.py"])
    assert main.sort_imports(
        str(tmp_file), config=skip_config, check=True, disregard_skip=False
    ).skipped
    assert main.sort_imports(str(tmp_file), config=skip_config, disregard_skip=False).skipped