summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2019-03-07 22:08:39 -0800
committerTimothy Crosley <timothy.crosley@gmail.com>2019-03-07 22:08:39 -0800
commit4f4064b82260e08f1d07f2f95d31d4e0b8488290 (patch)
treedf5a5a6e9d7b9ece851fa01880f661e2a67a99ac
parent3a856aa26fc5563e68f87c5739b5aae4e02c65d3 (diff)
downloadisort-4f4064b82260e08f1d07f2f95d31d4e0b8488290.tar.gz
Fix file skipping in particular with globs
-rw-r--r--isort/isort.py22
-rw-r--r--isort/main.py10
-rw-r--r--isort/settings.py2
-rw-r--r--test_isort.py14
4 files changed, 35 insertions, 13 deletions
diff --git a/isort/isort.py b/isort/isort.py
index 7b054edf..c16259f3 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -47,7 +47,8 @@ class SortImports(object):
skipped = False
def __init__(self, file_path=None, file_contents=None, write_to_stdout=False, check=False,
- show_diff=False, settings_path=None, ask_to_apply=False, check_skip=True, **setting_overrides):
+ show_diff=False, settings_path=None, ask_to_apply=False, run_path='', check_skip=True,
+ **setting_overrides):
if not settings_path and file_path:
settings_path = os.path.dirname(os.path.abspath(file_path))
settings_path = settings_path or os.getcwd()
@@ -93,12 +94,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.should_skip(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 = file_path.replace(run_path, '', 1)
+ else:
+ file_name = file_path
+ run_path = ''
+
+ if settings.should_skip(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 io.open(file_path, encoding=file_encoding, newline='') as file_to_import_sort:
diff --git a/isort/main.py b/isort/main.py
index 29049ac6..299562ed 100644
--- a/isort/main.py
+++ b/isort/main.py
@@ -85,7 +85,7 @@ class SortAttempt(object):
def sort_imports(file_name, **arguments):
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))
@@ -99,9 +99,7 @@ def iter_source_code(paths, config, skipped):
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 should_skip(dirname, config, dirpath):
skipped.append(dirname)
@@ -109,7 +107,8 @@ def iter_source_code(paths, config, skipped):
for filename in filenames:
filepath = os.path.join(dirpath, filename)
if is_python_file(filepath):
- if should_skip(filename, config, dirpath):
+ relative_file = os.path.relpath(filepath, path)
+ if should_skip(relative_file, config, path):
skipped.append(filename)
else:
yield filepath
@@ -314,6 +313,7 @@ def main(argv=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 56ee223c..c796d966 100644
--- a/isort/settings.py
+++ b/isort/settings.py
@@ -325,7 +325,7 @@ def should_skip(filename, config, path=''):
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 86d5ae24..fb095fc7 100644
--- a/test_isort.py
+++ b/test_isort.py
@@ -2782,6 +2782,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'