Ejemplo n.º 1
0
    def test_conda_and_virtual_env(self, tmpdir):
        python3lib = tmpdir.mkdir("lib").mkdir("python3")
        python3lib.mkdir("site-packages").mkdir("y")
        python3lib.mkdir("n").mkdir("site-packages").mkdir("x")
        tmpdir.mkdir("z").join("__init__.py").write("__version__ = '1.0.0'")
        tmpdir.chdir()

        conda = self.kind(settings.Config(conda_env=str(tmpdir)), str(tmpdir))
        venv = self.kind(settings.Config(virtual_env=str(tmpdir)), str(tmpdir))
        assert conda.find("y") == venv.find("y") == "THIRDPARTY"
        assert conda.find("x") == venv.find("x") == "THIRDPARTY"
        assert conda.find("z") == "THIRDPARTY"
        assert conda.find("os") == venv.find("os") == "STDLIB"
Ejemplo n.º 2
0
def _get_config(args: tuple[str, ...], src: list[str]):
    kwargs = {}
    if args:
        kwargs["settings_file"] = _write_temp_config(args)
    if src:
        kwargs["src_paths"] = src
    return settings.Config(**kwargs)
Ejemplo n.º 3
0
    def test_find_broken_finder(self):
        class ExceptionOnFind(finders.BaseFinder):
            def find(*args, **kwargs):
                raise ValueError("test")

        assert (FindersManager(settings.Config(verbose=True),
                               [ExceptionOnFind]).find("isort") is None)
Ejemplo n.º 4
0
 def _get_config(self, args: tuple[str, ...], src: list[str]):
     with tempfile.NamedTemporaryFile('w', suffix='.toml',
                                      delete=False) as tmp:
         tmp.write('\n'.join(('[tool.isort]', *args)))
         file_path = tmp.name
     kwargs = {}
     if src:
         kwargs['src_paths'] = src
     return settings.Config(settings_file=file_path, **kwargs)
Ejemplo n.º 5
0
def sort(document, override=None):
    source = override or document.source
    isort_config = settings.Config(settings_path=os.path.dirname(document.path))
    sorted_source = code(source, config=isort_config)
    if source == sorted_source:
        return
    change_range = {
        "start": {"line": 0, "character": 0},
        "end": {"line": len(document.lines), "character": 0},
    }
    return [{"range": change_range, "newText": sorted_source}]
Ejemplo n.º 6
0
    def test_init(self):
        assert FindersManager(settings.DEFAULT_CONFIG)

        class ExceptionOnInit(finders.BaseFinder):
            def __init__(*args, **kwargs):
                super().__init__(*args, **kwargs)
                raise ValueError("test")

        with patch(
                "isort.deprecated.finders.FindersManager._default_finders_classes",
                FindersManager._default_finders_classes + (ExceptionOnInit, ),
        ):
            assert FindersManager(settings.Config(verbose=True))
Ejemplo n.º 7
0
 def test_src_paths(self, tmpdir):
     tmpdir.join("file.py").write("import b\nimport a\n")
     assert (self.kind(
         settings.Config(src_paths=[Path(str(tmpdir))]),
         tmpdir).find("file") == settings.DEFAULT_CONFIG.default_section)
Ejemplo n.º 8
0
 def test_default_section(self, tmpdir):
     tmpdir.join("file.py").write("import b\nimport a\n")
     assert self.kind(settings.Config(default_section="CUSTOM"),
                      tmpdir).find("file") == "CUSTOM"