From e1773beb351bdbeb6104ae0db4b58a65ce6c7299 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Thu, 7 Mar 2019 22:08:39 -0800 Subject: Fix file skipping in particular with globs --- isort/isort.py | 20 ++++++++++++++------ isort/main.py | 10 +++++----- isort/settings.py | 2 +- test_isort.py | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/isort/isort.py b/isort/isort.py index 73363e83..67cade39 100644 --- a/isort/isort.py +++ b/isort/isort.py @@ -69,6 +69,7 @@ class SortImports(object): show_diff: bool = False, settings_path: Optional[str] = None, ask_to_apply: bool = False, + run_path: str='', check_skip: bool = True, **setting_overrides: Any ) -> None: @@ -90,12 +91,19 @@ class SortImports(object): self.file_path = file_path or "" if file_path: file_path = os.path.abspath(file_path) - if check_skip and settings.file_should_be_skipped(file_path, self.config): - self.skipped = True - if self.config['verbose']: - print("WARNING: {0} was skipped as it's listed in 'skip' setting" - " or matches a glob in 'skip_glob' setting".format(file_path)) - file_contents = None + if check_skip: + if run_path and file_path.startswith(run_path): + file_name = os.path.relpath(file_path, run_path) + else: + file_name = file_path + run_path = '' + + if settings.file_should_be_skipped(file_name, self.config, run_path): + self.skipped = True + if self.config['verbose']: + print("WARNING: {0} was skipped as it's listed in 'skip' setting" + " or matches a glob in 'skip_glob' setting".format(file_path)) + file_contents = None elif not file_contents: file_encoding = coding_check(file_path) with open(file_path, encoding=file_encoding, newline='') as file_to_import_sort: diff --git a/isort/main.py b/isort/main.py index 8842a318..3e7cbc6a 100644 --- a/isort/main.py +++ b/isort/main.py @@ -84,7 +84,7 @@ class SortAttempt(object): def sort_imports(file_name: str, **arguments: Any) -> Optional[SortAttempt]: try: - result = SortImports(file_name, check_skip=False, **arguments) + result = SortImports(file_name, **arguments) return SortAttempt(result.incorrectly_sorted, result.skipped) except IOError as e: print("WARNING: Unable to parse file {0} due to {1}".format(file_name, e)) @@ -98,9 +98,7 @@ def iter_source_code(paths: Iterable[str], config: MutableMapping[str, Any], ski for path in paths: if os.path.isdir(path): - for dirpath, dirnames, filenames in os.walk( - path, topdown=True, followlinks=True - ): + for dirpath, dirnames, filenames in os.walk(path, topdown=True, followlinks=True): for dirname in list(dirnames): if file_should_be_skipped(dirname, config, dirpath): skipped.append(dirname) @@ -108,7 +106,8 @@ def iter_source_code(paths: Iterable[str], config: MutableMapping[str, Any], ski for filename in filenames: filepath = os.path.join(dirpath, filename) if is_python_file(filepath): - if file_should_be_skipped(filename, config, dirpath): + relative_file = os.path.relpath(filepath, path) + if file_should_be_skipped(relative_file, config, path): skipped.append(filename) else: yield filepath @@ -311,6 +310,7 @@ def main(argv: Optional[Sequence[str]] = None) -> None: '-rc for recursive') sys.exit(1) + arguments['check_skip'] = False if 'settings_path' in arguments: sp = arguments['settings_path'] arguments['settings_path'] = os.path.abspath(sp) if os.path.isdir(sp) else os.path.dirname(os.path.abspath(sp)) diff --git a/isort/settings.py b/isort/settings.py index b11d2e6b..43a0f2a4 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -383,7 +383,7 @@ def file_should_be_skipped( if normalized_path[1:2] == ':': normalized_path = normalized_path[2:] - if config['safety_excludes']: + if path and config['safety_excludes']: check_exclude = '/' + filename.replace('\\', '/') + '/' if path and os.path.basename(path) in ('lib', ): check_exclude = '/' + os.path.basename(path) + check_exclude diff --git a/test_isort.py b/test_isort.py index 00e7e82e..570a7c50 100644 --- a/test_isort.py +++ b/test_isort.py @@ -2769,6 +2769,20 @@ def test_safety_excludes(tmpdir, enabled): assert file_names == {'verysafe.py'} +@pytest.mark.parametrize('skip_glob_assert', (([], 0, {os.sep.join(('code', 'file.py'))}), (['**/*.py'], 1, {}))) +def test_skip_glob(tmpdir, skip_glob_assert): + skip_glob, skipped_count, file_names = skip_glob_assert + base_dir = tmpdir.mkdir('build') + code_dir = base_dir.mkdir('code') + code_dir.join('file.py').write('import os') + + config = dict(settings.default.copy(), skip_glob=skip_glob) + skipped = [] + file_names = set(os.path.relpath(f, str(base_dir)) for f in main.iter_source_code([str(base_dir)], config, skipped)) + assert len(skipped) == skipped_count + assert file_names == file_names + + def test_comments_not_removed_issue_576(): test_input = ('import distutils\n' '# this comment is important and should not be removed\n' -- cgit v1.2.1