def test_airflow(tmpdir): check_call([ "git", "clone", "--depth", "1", "https://github.com/apache/airflow.git", str(tmpdir) ]) main(["--check-only", "--diff", str(tmpdir)])
def test_unsupported_encodings(tmpdir, capsys): tmp_file = tmpdir.join("file.py") # fmt: off tmp_file.write_text(''' # [syntax-error]\ # -*- coding: IBO-8859-1 -*- """ check correct unknown encoding declaration """ __revision__ = 'יייי' ''', encoding="utf8") # fmt: on # should throw an error if only unsupported encoding provided with pytest.raises(SystemExit): main.main([str(tmp_file)]) out, error = capsys.readouterr() assert "No valid encodings." in error # should not throw an error if at least one valid encoding found normal_file = tmpdir.join("file1.py") normal_file.write("import os\nimport sys") main.main([str(tmp_file), str(normal_file), "--verbose"]) out, error = capsys.readouterr()
def test_fastapi(tmpdir): check_call([ "git", "clone", "--depth", "1", "https://github.com/tiangolo/fastapi.git", str(tmpdir) ]) main(["--check-only", "--diff", str(tmpdir / "fastapi")])
def test_tmuxp(tmpdir): check_call([ "git", "clone", "--depth", "1", "https://github.com/tmux-python/tmuxp.git", str(tmpdir) ]) main(["--check-only", "--diff", str(tmpdir)])
def test_pillow(tmpdir): check_call([ "git", "clone", "--depth", "1", "https://github.com/python-pillow/Pillow.git", str(tmpdir) ]) main(["--check-only", "--diff", str(tmpdir), "--skip", "tests"])
def test_hypothesis(tmpdir): check_call([ "git", "clone", "--depth", "1", "https://github.com/HypothesisWorks/hypothesis.git", str(tmpdir), ]) main(["--check-only", "--diff", str(tmpdir), "--skip", "tests"])
def test_pandas(tmpdir): check_call([ "git", "clone", "--depth", "1", "https://github.com/pandas-dev/pandas.git", str(tmpdir) ]) main([ "--check-only", "--diff", str(tmpdir / "pandas"), "--skip", "__init__.py" ])
def test_django(tmpdir): check_call([ "git", "clone", "--depth", "1", "https://github.com/django/django.git", str(tmpdir) ]) isort_target_dirs = [ str(target_dir) for target_dir in (tmpdir / "django", tmpdir / "tests", tmpdir / "scripts") ] main(["--check-only", "--diff", *isort_target_dirs])
def test_habitat_lab(tmpdir): check_call([ "git", "clone", "--depth", "1", "https://github.com/facebookresearch/habitat-lab.git", str(tmpdir), ]) main(["--check-only", "--diff", str(tmpdir)])
def test_plone(tmpdir): check_call([ "git", "clone", "--depth", "1", "https://github.com/plone/plone.app.multilingualindexes.git", str(tmpdir), ]) main(["--check-only", "--diff", str(tmpdir / "src")])
def isort(path): # Potential ImportErrors are caught upstream import mock from isort.main import main return # Disable isort pending resolution of https://github.com/timothycrosley/isort/issues/606 with mock.patch.object(sys, 'argv', ['isort', '-y', '-ac', '-vb']): oldcwd = os.getcwd() try: os.chdir(path) main() finally: os.chdir(oldcwd)
def test_missing_default_section(tmpdir): config_file = tmpdir.join(".isort.cfg") config_file.write(""" [settings] sections=MADEUP """) python_file = tmpdir.join("file.py") python_file.write("import os") with pytest.raises(SystemExit): main.main([str(python_file)])
def test_ascii_art(capsys): main.main(["--version"]) out, error = capsys.readouterr() assert (out == f""" _ _ (_) ___ ___ _ __| |_ | |/ _/ / _ \\/ '__ _/ | |\\__ \\/\\_\\/| | | |_ |_|\\___/\\___/\\_/ \\_/ isort your imports, so you don't have to. VERSION {__version__} """) assert error == ""
def test_typeshed(tmpdir): check_call([ "git", "clone", "--depth", "1", "https://github.com/python/typeshed.git", str(tmpdir) ]) main([ "--check-only", "--diff", str(tmpdir), "--skip", "tests", "--skip", "scripts", "--skip", f"{tmpdir}/third_party/2and3/yaml/__init__.pyi", ])
def test_attrs(tmpdir): check_call([ "git", "clone", "--depth", "1", "https://github.com/python-attrs/attrs.git", str(tmpdir) ]) main([ "--check-only", "--diff", str(tmpdir), "--skip", "tests", "--ext", "py", "--skip", "_compat.py", ])
def test_websockets(tmpdir): check_call([ "git", "clone", "--depth", "1", "https://github.com/aaugustin/websockets.git", str(tmpdir) ]) main([ "--check-only", "--diff", str(tmpdir), "--skip", "example", "--skip", "docs", "--skip", "compliance", ])
def test_isort_filename_overrides(tmpdir, capsys): """Tests isorts available approaches for overriding filename and extension based behavior""" input_text = """ import b import a def function(): pass """ def build_input_content(): return as_stream(input_text) main.main(["-"], stdin=build_input_content()) out, error = capsys.readouterr() assert not error assert out == (""" import a import b def function(): pass """) main.main(["-", "--ext-format", "pyi"], stdin=build_input_content()) out, error = capsys.readouterr() assert not error assert out == (""" import a import b def function(): pass """) tmp_file = tmpdir.join("tmp.pyi") tmp_file.write_text(input_text, encoding="utf8") main.main(["-", "--filename", str(tmp_file)], stdin=build_input_content()) out, error = capsys.readouterr() assert not error assert out == (""" import a import b def function(): pass """) # setting a filename override when file is passed in as non-stream is not supported. with pytest.raises(SystemExit): main.main([str(tmp_file), "--filename", str(tmp_file)], stdin=build_input_content())
def test_multiple_src_paths(tmpdir, capsys): """ Ensure that isort has consistent behavior with multiple source paths """ tests_module = tmpdir / "tests" app_module = tmpdir / "app" tests_module.mkdir() app_module.mkdir() pyproject_toml = tmpdir / "pyproject.toml" pyproject_toml.write_text( """ [tool.isort] profile = "black" src_paths = ["app", "tests"] auto_identify_namespace_packages = false """, "utf-8", ) file = tmpdir / "file.py" file.write_text( """ from app.something import something from tests.something import something_else """, "utf-8", ) for _ in range(10): # To ensure isort has consistent results in multiple runs main.main([str(tmpdir), "--verbose"]) out, _ = capsys.readouterr() assert ( file.read() == """ from app.something import something from tests.something import something_else """ ) assert "from-type place_module for tests.something returned FIRSTPARTY" in out
def test_isort_float_to_top_overrides(tmpdir, capsys): """Tests isorts supports overriding float to top from CLI""" test_input = """ import b def function(): pass import a """ config_file = tmpdir.join(".isort.cfg") config_file.write( """ [settings] float_to_top=True """ ) python_file = tmpdir.join("file.py") python_file.write(test_input) main.main([str(python_file)]) out, error = capsys.readouterr() assert not error assert "Fixing" in out assert python_file.read_text(encoding="utf8") == ( """ import a import b def function(): pass """ ) python_file.write(test_input) main.main([str(python_file), "--dont-float-to-top"]) _, error = capsys.readouterr() assert not error assert python_file.read_text(encoding="utf8") == test_input with pytest.raises(SystemExit): main.main([str(python_file), "--float-to-top", "--dont-float-to-top"])
def test_show_files(capsys, tmpdir): tmpdir.join("a.py").write("import a") tmpdir.join("b.py").write("import b") # show files should list the files isort would sort main.main([str(tmpdir), "--show-files"]) out, error = capsys.readouterr() assert "a.py" in out assert "b.py" in out assert not error # can not be used for stream with pytest.raises(SystemExit): main.main(["-", "--show-files"]) # can not be used with show-config with pytest.raises(SystemExit): main.main([str(tmpdir), "--show-files", "--show-config"])
def test_stream_skip_file(tmpdir, capsys): input_with_skip = """ # isort: skip_file import b import a """ stream_with_skip = as_stream(input_with_skip) main.main(["-"], stdin=stream_with_skip) out, error = capsys.readouterr() assert out == input_with_skip input_without_skip = input_with_skip.replace("isort: skip_file", "generic comment") stream_without_skip = as_stream(input_without_skip) main.main(["-"], stdin=stream_without_skip) out, error = capsys.readouterr() assert ( out == """ # generic comment import a import b """ ) atomic_input_without_skip = input_with_skip.replace("isort: skip_file", "generic comment") stream_without_skip = as_stream(atomic_input_without_skip) main.main(["-", "--atomic"], stdin=stream_without_skip) out, error = capsys.readouterr() assert ( out == """ # generic comment import a import b """ )
#!/usr/bin/env python # -*- coding: utf-8 -*- import os import re import sys sys.path.insert( 0, os.path.join(os.path.abspath(os.path.dirname(__file__)), 'deps')) if sys.version_info[0] == 2: sys.path.insert( 0, os.path.join(os.path.abspath(os.path.dirname(__file__)), 'backports')) from isort.main import main if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) sys.exit(main())
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 file4 = tmpdir.join("file4.py") file4.write(""" import sys import os """) with pytest.raises(SystemExit): main.main([ str(file2), str(file4), "--check-only", "--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
def test_isort_with_stdin(capsys): # ensures that isort sorts stdin without any flags input_content = UnseekableTextIOWrapper(BytesIO(b""" import b import a """)) main.main(["-"], stdin=input_content) out, error = capsys.readouterr() assert out == (""" import a import b """) input_content_from = UnseekableTextIOWrapper( BytesIO(b""" import c import b from a import z, y, x """)) main.main(["-"], stdin=input_content_from) out, error = capsys.readouterr() assert out == (""" import b import c from a import x, y, z """) # ensures that isort correctly sorts stdin with --fas flag input_content = UnseekableTextIOWrapper( BytesIO(b""" import sys import pandas from z import abc from a import xyz """)) main.main(["-", "--fas"], stdin=input_content) out, error = capsys.readouterr() assert out == (""" from a import xyz from z import abc import pandas import sys """) # ensures that isort correctly sorts stdin with --fass flag input_content = UnseekableTextIOWrapper( BytesIO(b""" from a import Path, abc """)) main.main(["-", "--fass"], stdin=input_content) out, error = capsys.readouterr() assert out == (""" from a import abc, Path """) # ensures that isort correctly sorts stdin with --ff flag input_content = UnseekableTextIOWrapper( BytesIO(b""" import b from c import x from a import y """)) main.main(["-", "--ff", "FROM_FIRST"], stdin=input_content) out, error = capsys.readouterr() assert out == (""" from a import y from c import x import b """) # ensures that isort correctly sorts stdin with -fss flag input_content = UnseekableTextIOWrapper( BytesIO(b""" import b from a import a """)) main.main(["-", "--fss"], stdin=input_content) out, error = capsys.readouterr() assert out == (""" from a import a import b """) input_content = UnseekableTextIOWrapper( BytesIO(b""" import a from b import c """)) main.main(["-", "--fss"], stdin=input_content) out, error = capsys.readouterr() assert out == (""" import a from b import c """) # ensures that isort correctly sorts stdin with --ds flag input_content = UnseekableTextIOWrapper( BytesIO(b""" import sys import pandas import a """)) main.main(["-", "--ds"], stdin=input_content) out, error = capsys.readouterr() assert out == (""" import a import pandas import sys """) # ensures that isort correctly sorts stdin with --cs flag input_content = UnseekableTextIOWrapper( BytesIO(b""" from a import b from a import * """)) main.main(["-", "--cs"], stdin=input_content) out, error = capsys.readouterr() assert out == (""" from a import * """) # ensures that isort correctly sorts stdin with --ca flag input_content = UnseekableTextIOWrapper( BytesIO(b""" from a import x as X from a import y as Y """)) main.main(["-", "--ca"], stdin=input_content) out, error = capsys.readouterr() assert out == (""" from a import x as X, y as Y """) # ensures that isort works consistently with check and ws flags input_content = UnseekableTextIOWrapper( BytesIO(b""" import os import a import b """)) main.main(["-", "--check-only", "--ws"], stdin=input_content) out, error = capsys.readouterr() assert not error # ensures that isort works consistently with check and diff flags input_content = UnseekableTextIOWrapper(BytesIO(b""" import b import a """)) with pytest.raises(SystemExit): main.main(["-", "--check", "--diff"], stdin=input_content) out, error = capsys.readouterr() assert error assert "underlying stream is not seekable" not in error assert "underlying stream is not seekable" not in error # ensures that isort correctly sorts stdin with --ls flag input_content = UnseekableTextIOWrapper( BytesIO(b""" import abcdef import x """)) main.main(["-", "--ls"], stdin=input_content) out, error = capsys.readouterr() assert out == (""" import x import abcdef """) # ensures that isort correctly sorts stdin with --nis flag input_content = UnseekableTextIOWrapper( BytesIO(b""" from z import b, c, a """)) main.main(["-", "--nis"], stdin=input_content) out, error = capsys.readouterr() assert out == (""" from z import b, c, a """) # ensures that isort correctly sorts stdin with --sl flag input_content = UnseekableTextIOWrapper( BytesIO(b""" from z import b, c, a """)) main.main(["-", "--sl"], stdin=input_content) out, error = capsys.readouterr() assert out == (""" from z import a from z import b from z import c """) # ensures that isort correctly sorts stdin with --top flag input_content = UnseekableTextIOWrapper( BytesIO(b""" import os import sys """)) main.main(["-", "--top", "sys"], stdin=input_content) out, error = capsys.readouterr() assert out == (""" import sys import os """) # ensure that isort correctly sorts stdin with --os flag input_content = UnseekableTextIOWrapper( BytesIO(b""" import sys import os import z from a import b, e, c """)) main.main(["-", "--os"], stdin=input_content) out, error = capsys.readouterr() assert out == (""" import sys import os import z from a import b, e, c """) # ensures that isort warns with deprecated flags with stdin input_content = UnseekableTextIOWrapper( BytesIO(b""" import sys import os """)) with pytest.warns(UserWarning): main.main(["-", "-ns"], stdin=input_content) out, error = capsys.readouterr() assert out == (""" import os import sys """) input_content = UnseekableTextIOWrapper( BytesIO(b""" import sys import os """)) with pytest.warns(UserWarning): main.main(["-", "-k"], stdin=input_content) out, error = capsys.readouterr() assert out == (""" import os import sys """) # ensures that only-modified flag works with stdin input_content = UnseekableTextIOWrapper(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
# Copyright 2021 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """isort""" import sys from isort.main import main if __name__ == "__main__": print("isort: ", sys.argv) main(sys.argv[1:])
def test_zulip(tmpdir): check_call([ "git", "clone", "--depth", "1", "https://github.com/zulip/zulip.git", str(tmpdir) ]) main(["--check-only", "--diff", str(tmpdir), "--skip", "__init__.pyi"])
def run_isort(arguments: Sequence[str]): """Runs isort in diff and check mode with the given arguments""" main(["--check-only", "--diff", *arguments])
def test_pylint(tmpdir): check_call([ "git", "clone", "--depth", "1", "https://github.com/PyCQA/pylint.git", str(tmpdir) ]) main(["--check-only", "--diff", str(tmpdir)])
from isort.main import main main()
from __future__ import absolute_import from isort.pie_slice import apply_changes_to_python_environment apply_changes_to_python_environment() from isort.main import main # noqa: E402 isort:skip main()
def test_main(capsys, tmpdir): base_args = [ "-sp", str(tmpdir), "--virtual-env", str(tmpdir), "--src-path", str(tmpdir), ] tmpdir.mkdir(".git") # If nothing is passed in the quick guide is returned without erroring main.main([]) out, error = capsys.readouterr() assert main.QUICK_GUIDE in out assert not error # If no files are passed in but arguments are the quick guide is returned, alongside an error. with pytest.raises(SystemExit): main.main(base_args) out, error = capsys.readouterr() assert main.QUICK_GUIDE in out # Unless the config is requested, in which case it will be returned alone as JSON main.main(base_args + ["--show-config"]) out, error = capsys.readouterr() returned_config = json.loads(out) assert returned_config assert returned_config["virtual_env"] == str(tmpdir) # This should work even if settings path is not provided main.main(base_args[2:] + ["--show-config"]) out, error = capsys.readouterr() assert json.loads(out)["virtual_env"] == str(tmpdir) # This should raise an error if an invalid settings path is provided with pytest.raises(InvalidSettingsPath): main.main( base_args[2:] + ["--show-config"] + ["--settings-path", "/random-root-folder-that-cant-exist-right?"] ) # Should be able to set settings path to a file config_file = tmpdir.join(".isort.cfg") config_file.write( """ [settings] profile=hug verbose=true """ ) config_args = ["--settings-path", str(config_file)] main.main( config_args + ["--virtual-env", "/random-root-folder-that-cant-exist-right?"] + ["--show-config"] ) out, error = capsys.readouterr() assert json.loads(out)["profile"] == "hug" # Should be able to stream in content to sort input_content = TextIOWrapper( BytesIO( b""" import b import a """ ) ) main.main(config_args + ["-"], stdin=input_content) out, error = capsys.readouterr() assert ( out == f""" else-type place_module for b returned {DEFAULT_CONFIG.default_section} else-type place_module for a returned {DEFAULT_CONFIG.default_section} import a import b """ ) # Should be able to run with just a file python_file = tmpdir.join("has_imports.py") python_file.write( """ import b import a """ ) main.main([str(python_file), "--filter-files", "--verbose"]) assert python_file.read().lstrip() == "import a\nimport b\n" # Add a file to skip should_skip = tmpdir.join("should_skip.py") should_skip.write("import nothing") main.main( [ str(python_file), str(should_skip), "--filter-files", "--verbose", "--skip", str(should_skip), ] ) # Should raise a system exit if check only, with broken file python_file.write( """ import b import a """ ) with pytest.raises(SystemExit): main.main( [ str(python_file), str(should_skip), "--filter-files", "--verbose", "--check-only", "--skip", str(should_skip), ] ) # Should have same behavior if full directory is skipped with pytest.raises(SystemExit): main.main( [str(tmpdir), "--filter-files", "--verbose", "--check-only", "--skip", str(should_skip)] ) # Nested files should be skipped without needing --filter-files nested_file = tmpdir.mkdir("nested_dir").join("skip.py") nested_file.write("import b;import a") python_file.write( """ import a import b """ ) main.main([str(tmpdir), "--skip", "skip.py", "--check"]) # without filter options passed in should successfully sort files main.main([str(python_file), str(should_skip), "--verbose", "--atomic"]) # should respect gitignore if requested. out, error = capsys.readouterr() # clear sysoutput before tests subprocess.run(["git", "init", str(tmpdir)]) main.main([str(python_file), "--skip-gitignore", "--filter-files"]) out, error = capsys.readouterr() assert "Skipped" not in out tmpdir.join(".gitignore").write("has_imports.py") main.main([str(python_file)]) out, error = capsys.readouterr() assert "Skipped" not in out main.main([str(python_file), "--skip-gitignore", "--filter-files"]) out, error = capsys.readouterr() assert "Skipped" in out # warnings should be displayed if old flags are used with pytest.warns(UserWarning): main.main([str(python_file), "--recursive", "-fss"])
# coding=utf-8 # Copyright 2016 Pants project contributors (see CONTRIBUTORS.md). # Licensed under the Apache License, Version 2.0 (see LICENSE). from __future__ import (absolute_import, division, generators, nested_scopes, print_function, unicode_literals, with_statement) import re import sys from isort.main import main if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) sys.exit(main())