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"
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)
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)
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)
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}]
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))
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)
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"