summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--isort/isort.py22
-rw-r--r--isort/main.py10
-rw-r--r--isort/settings.py2
-rw-r--r--test_isort.py23
4 files changed, 43 insertions, 14 deletions
diff --git a/isort/isort.py b/isort/isort.py
index 0d8d6f09..6b8a781e 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,13 +91,20 @@ 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
- elif not file_contents:
+ 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
+ if not self.skipped and not file_contents:
file_encoding = coding_check(file_path)
with open(file_path, encoding=file_encoding, newline='') as file_to_import_sort:
try:
diff --git a/isort/main.py b/isort/main.py
index 1eea5b12..767927d4 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
@@ -313,6 +312,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 bfc89ff9..9b3c1e42 100644
--- a/test_isort.py
+++ b/test_isort.py
@@ -2759,13 +2759,16 @@ def test_quiet(tmpdir, capfd, quiet):
@pytest.mark.parametrize('enabled', (False, True))
def test_safety_excludes(tmpdir, enabled):
tmpdir.join("victim.py").write("# ...")
- tmpdir.mkdir(".tox").join("verysafe.py").write("# ...")
+ toxdir = tmpdir.mkdir(".tox")
+ toxdir.join("verysafe.py").write("# ...")
tmpdir.mkdir("lib").mkdir("python3.7").join("importantsystemlibrary.py").write("# ...")
tmpdir.mkdir(".pants.d").join("pants.py").write("import os")
config = dict(settings.default.copy(), safety_excludes=enabled)
skipped = []
codes = [str(tmpdir)],
main.iter_source_code(codes, config, skipped)
+
+ # if enabled files within nested unsafe directories should be skipped
file_names = set(os.path.relpath(f, str(tmpdir)) for f in main.iter_source_code([str(tmpdir)], config, skipped))
if enabled:
assert file_names == {'victim.py'}
@@ -2777,6 +2780,24 @@ def test_safety_excludes(tmpdir, enabled):
'victim.py'}
assert not skipped
+ # directly pointing to files within unsafe directories shouldn't skip them either way
+ file_names = set(os.path.relpath(f, str(toxdir)) for f in main.iter_source_code([str(toxdir)], config, skipped))
+ 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'