diff options
author | Timothy Edmund Crosley <timothy.crosley@gmail.com> | 2019-03-01 18:32:58 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-01 18:32:58 -0800 |
commit | a7992050c0721139c7dd40abd7f7d746576abd12 (patch) | |
tree | b298c4bf5b5dd1cfd4a22b5dee633d088114a6e9 | |
parent | 0c39fa476966f279862abf634c5d1ea1800a887c (diff) | |
parent | 36e64f73d1435535719382be97204c7e0d020be0 (diff) | |
download | isort-a7992050c0721139c7dd40abd7f7d746576abd12.tar.gz |
Merge pull request #868 from timothycrosley/feature/fix-windows-support
Feature/fix windows support
-rw-r--r-- | isort/settings.py | 10 | ||||
-rw-r--r-- | test_isort.py | 87 | ||||
-rw-r--r-- | tox.ini | 2 |
3 files changed, 61 insertions, 38 deletions
diff --git a/isort/settings.py b/isort/settings.py index 1a907e61..2801e780 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -29,7 +29,6 @@ import io import os import posixpath import re -import stat import sys import warnings from collections import namedtuple @@ -316,9 +315,12 @@ def _get_config_data(file_path, sections): return settings -def should_skip(filename, config, path='/'): +def should_skip(filename, config, path=''): """Returns True if the file should be skipped based on the passed in settings.""" - normalized_path = posixpath.join(path.replace('\\', '/'), filename) + os_path = os.path.join(path, filename) + normalized_path = os_path.replace('\\', '/') + if normalized_path[1:2] == ':': + normalized_path = normalized_path[2:] if config['safety_excludes'] and safety_exclude_re.search(normalized_path): return True @@ -337,7 +339,7 @@ def should_skip(filename, config, path='/'): if fnmatch.fnmatch(filename, glob): return True - if stat.S_ISFIFO(os.stat(normalized_path).st_mode): + if not (os.path.isfile(os_path) or os.path.isdir(os_path) or os.path.islink(os_path)): return True return False diff --git a/test_isort.py b/test_isort.py index e1503e8e..27282290 100644 --- a/test_isort.py +++ b/test_isort.py @@ -27,6 +27,7 @@ from tempfile import NamedTemporaryFile import io import os import os.path +import posixpath import sys import sysconfig @@ -55,7 +56,6 @@ skip = build,.tox,venv balanced_wrapping = true not_skip = __init__.py """ - SHORT_IMPORT = "from third_party import lib1, lib2, lib3, lib4" SINGLE_FROM_IMPORT = "from third_party import lib1" SINGLE_LINE_LONG_IMPORT = "from third_party import lib1, lib2, lib3, lib4, lib5, lib5ab" @@ -569,7 +569,7 @@ def test_skip_with_file_name(): test_input = ("import django\n" "import myproject\n") - sort_imports = SortImports(file_path='/baz.py', file_contents=test_input, known_third_party=['django'], + sort_imports = SortImports(file_path='/baz.py', file_contents=test_input, settings_path=os.getcwd(), skip=['baz.py']) assert sort_imports.skipped assert sort_imports.output is None @@ -1725,7 +1725,7 @@ def test_other_file_encodings(tmpdir): tmp_fname = tmpdir.join('test_{0}.py'.format(encoding)) file_contents = "# coding: {0}\n\ns = u'ã'\n".format(encoding) tmp_fname.write_binary(file_contents.encode(encoding)) - assert SortImports(file_path=str(tmp_fname)).output == file_contents + assert SortImports(file_path=str(tmp_fname), settings_path=os.getcwd()).output == file_contents def test_comment_at_top_of_file(): @@ -2513,32 +2513,49 @@ def test_to_ensure_tabs_dont_become_space_issue_665(): def test_new_lines_are_preserved(): - with NamedTemporaryFile('w', suffix='py') as rn_newline: + with NamedTemporaryFile('w', suffix='py', delete=False) as rn_newline: + pass + + try: with io.open(rn_newline.name, mode='w', newline='') as rn_newline_input: rn_newline_input.write('import sys\r\nimport os\r\n') - rn_newline_input.flush() - SortImports(rn_newline.name) - with io.open(rn_newline.name, newline='') as rn_newline_file: - rn_newline_contents = rn_newline_file.read() - assert rn_newline_contents == 'import os\r\nimport sys\r\n' - with NamedTemporaryFile('w', suffix='py') as r_newline: + SortImports(rn_newline.name, settings_path=os.getcwd()) + with io.open(rn_newline.name) as new_line_file: + print(new_line_file.read()) + with io.open(rn_newline.name, newline='') as rn_newline_file: + rn_newline_contents = rn_newline_file.read() + assert rn_newline_contents == 'import os\r\nimport sys\r\n' + finally: + os.remove(rn_newline.name) + + with NamedTemporaryFile('w', suffix='py', delete=False) as r_newline: + pass + + try: with io.open(r_newline.name, mode='w', newline='') as r_newline_input: r_newline_input.write('import sys\rimport os\r') - r_newline_input.flush() - SortImports(r_newline.name) - with io.open(r_newline.name, newline='') as r_newline_file: - r_newline_contents = r_newline_file.read() - assert r_newline_contents == 'import os\rimport sys\r' - with NamedTemporaryFile('w', suffix='py') as n_newline: + SortImports(r_newline.name, settings_path=os.getcwd()) + with io.open(r_newline.name, newline='') as r_newline_file: + r_newline_contents = r_newline_file.read() + assert r_newline_contents == 'import os\rimport sys\r' + finally: + os.remove(r_newline.name) + + with NamedTemporaryFile('w', suffix='py', delete=False) as n_newline: + pass + + try: with io.open(n_newline.name, mode='w', newline='') as n_newline_input: n_newline_input.write('import sys\nimport os\n') - n_newline_input.flush() - SortImports(n_newline.name) - with io.open(n_newline.name, newline='') as n_newline_file: - n_newline_contents = n_newline_file.read() - assert n_newline_contents == 'import os\nimport sys\n' + + SortImports(n_newline.name, settings_path=os.getcwd()) + with io.open(n_newline.name, newline='') as n_newline_file: + n_newline_contents = n_newline_file.read() + assert n_newline_contents == 'import os\nimport sys\n' + finally: + os.remove(n_newline.name) @pytest.mark.skipif(not finders.RequirementsFinder.enabled, reason='RequirementsFinder not enabled (too old version of pip?)') @@ -2664,11 +2681,11 @@ def test_path_finder(monkeypatch): third_party_prefix = next(path for path in finder.paths if "site-packages" in path) ext_suffix = sysconfig.get_config_var("EXT_SUFFIX") or ".so" imaginary_paths = set([ - os.path.join(finder.stdlib_lib_prefix, "example_1.py"), - os.path.join(third_party_prefix, "example_2.py"), - os.path.join(third_party_prefix, "example_3.so"), - os.path.join(third_party_prefix, "example_4" + ext_suffix), - os.path.join(os.getcwd(), "example_5.py"), + posixpath.join(finder.stdlib_lib_prefix, "example_1.py"), + posixpath.join(third_party_prefix, "example_2.py"), + posixpath.join(third_party_prefix, "example_3.so"), + posixpath.join(third_party_prefix, "example_4" + ext_suffix), + posixpath.join(os.getcwd(), "example_5.py"), ]) monkeypatch.setattr("isort.finders.exists_case_sensitive", lambda p: p in imaginary_paths) assert finder.find("example_1") == finder.sections.STDLIB @@ -2692,17 +2709,18 @@ def test_command_line(tmpdir, capfd, multiprocess): from isort.main import main tmpdir.join("file1.py").write("import re\nimport os\n\nimport contextlib\n\n\nimport isort") tmpdir.join("file2.py").write("import collections\nimport time\n\nimport abc\n\n\nimport isort") - arguments = ["-rc", str(tmpdir)] + arguments = ["-rc", str(tmpdir), '--settings-path', os.getcwd()] if multiprocess: arguments.extend(['--jobs', '2']) main(arguments) assert tmpdir.join("file1.py").read() == "import contextlib\nimport os\nimport re\n\nimport isort\n" assert tmpdir.join("file2.py").read() == "import abc\nimport collections\nimport time\n\nimport isort\n" - out, err = capfd.readouterr() - assert not err - # it informs us about fixing the files: - assert str(tmpdir.join("file1.py")) in out - assert str(tmpdir.join("file2.py")) in out + if not sys.platform.startswith('win'): + out, err = capfd.readouterr() + assert not err + # it informs us about fixing the files: + assert str(tmpdir.join("file1.py")) in out + assert str(tmpdir.join("file2.py")) in out @pytest.mark.parametrize('enabled', (False, True)) @@ -2712,12 +2730,15 @@ def test_safety_excludes(tmpdir, enabled): tmpdir.mkdir("lib").mkdir("python3.7").join("importantsystemlibrary.py").write("# ...") config = dict(settings.default.copy(), safety_excludes=enabled) skipped = [] + codes = [str(tmpdir)], + main.iter_source_code(codes, config, 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'} assert len(skipped) == 2 else: - assert file_names == {'.tox/verysafe.py', 'lib/python3.7/importantsystemlibrary.py', 'victim.py'} + assert file_names == {os.sep.join(('.tox', 'verysafe.py')), + os.sep.join(('lib', 'python3.7', 'importantsystemlibrary.py')), 'victim.py'} assert not skipped @@ -12,7 +12,7 @@ extras = pipfile pyproject requirements -commands = py.test test_isort.py {posargs} +commands = py.test -vv -s test_isort.py {posargs} [testenv:isort-check] commands = python setup.py isort |