Skip to content

Commit

Permalink
Fix issue #938 - add support for checking file list against skip config
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed May 1, 2019
1 parent 807943e commit e63757d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
12 changes: 12 additions & 0 deletions isort/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ def parse_args(argv=None):
'where it may not be safe to operate in')
parser.add_argument('--case-sensitive', dest='case_sensitive', action='store_true',
help='Tells isort to include casing when sorting module names')
parser.add_argument('--filter-files', dest='filter_files', action='store_true',
help='Tells isort to filter files even when they are explicitly passed in as part of the command')
parser.add_argument('files', nargs='*', help='One or more Python source files that need their imports sorted.')

arguments = {key: value for key, value in vars(parser.parse_args(argv)).items() if value}
Expand Down Expand Up @@ -350,6 +352,16 @@ def main(argv=None):
config.update(arguments)
wrong_sorted_files = False
skipped = []

if config.get('filter_files'):
filtered_files = []
for file_name in file_names:
if should_skip(file_name, config):
skipped.append(file_name)
else:
filtered_files.append(file_name)
file_names = filtered_files

if arguments.get('recursive', False):
file_names = iter_source_code(file_names, config, skipped)
num_skipped = 0
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pytest==2.9.1
pytest==4.4.1
ipython==4.1.2
appdirs
38 changes: 38 additions & 0 deletions test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -2917,6 +2917,44 @@ def test_settings_path_skip_issue_909(tmpdir):
assert b'skipped 2' in results.lower()


def test_skip_paths_issue_938(tmpdir):
base_dir = tmpdir.mkdir('project')
config_dir = base_dir.mkdir('conf')
config_dir.join('.isort.cfg').write('[isort]\n'
'line_length = 88\n'
'multi_line_output = 4\n'
'lines_after_imports = 2\n'
'skip_glob =\n'
' migrations/**.py\n')
base_dir.join('dont_skip.py').write('import os\n'
'\n'
'print("Hello World")'
'\n'
'import sys\n')

migrations_dir = base_dir.mkdir('migrations')
migrations_dir.join('file_glob_skip.py').write('import os\n'
'\n'
'print("Hello World")\n'
'\n'
'import sys\n')

test_run_directory = os.getcwd()
os.chdir(str(base_dir))
results = check_output(['isort', 'dont_skip.py', 'migrations/file_glob_skip.py'])
os.chdir(str(test_run_directory))

assert not b'skipped' in results.lower()

os.chdir(str(base_dir))
results = check_output(['isort', '--filter-files', '--settings-path=conf/.isort.cfg', 'dont_skip.py', 'migrations/file_glob_skip.py'])
os.chdir(str(test_run_directory))

assert b'skipped 1' in results.lower()




def test_standard_library_deprecates_user_issue_778():
test_input = ('import os\n'
'\n'
Expand Down

0 comments on commit e63757d

Please sign in to comment.