def test_add_import_not_there(): assert fix_file_contents( 'import os', imports_to_add=('from __future__ import absolute_import', ), ) == ('from __future__ import absolute_import\n' '\n' 'import os\n')
def test_replace_imports_specific_attribute_name(): ret = fix_file_contents( 'from foo import bar\n' 'from foo import baz\n', imports_to_replace=[(['foo'], ['aaa'], 'bar')], ) assert ret == ('from aaa import bar\n' 'from foo import baz\n')
def test_replace_imports_noop(): ret = fix_file_contents( 'import os\n' 'import sys\n', # import imports are not rewritten imports_to_replace=[(['os'], ['fail'], '')], ) assert ret == 'import os\nimport sys\n'
def test_does_not_put_before_leading_comment(): assert fix_file_contents( '# -*- coding: UTF-8 -*-', imports_to_add=('from __future__ import absolute_import',), ) == ( '# -*- coding: UTF-8 -*-\n' 'from __future__ import absolute_import\n' )
def test_fix_cr(): s = ( '"""foo"""\r' 'import os\r' 'import sys\r' 'x = 1\r' ) assert fix_file_contents(s) == s
def test_separate_relative_and_separate_from_next_to_import_import(): ret = fix_file_contents( 'import thirdparty\n' 'from . import bar\n', separate_from_import=True, separate_relative=True, ) assert ret == ('import thirdparty\n' '\n' 'from . import bar\n')
def test_separate_relative_and_separate_from_next_to_from_import(): ret = fix_file_contents( 'from reorder_python_imports import y\n' 'from . import z\n', separate_from_import=True, separate_relative=True, ) assert ret == ('from reorder_python_imports import y\n' '\n' 'from . import z\n')
def main() -> int: parser = argparse.ArgumentParser( description="Sorts imports in Python 3.7+ source files.") parser.add_argument("source", nargs="*") parser.add_argument( "--check", action="store_true", help="Check if sorti would like to make changes.", ) parser.add_argument("--version", action="version", version=f"%(prog)s {get_version()}") args = parser.parse_args() if not args.source: print("No sources given, doing nothing.") return 0 sources = tuple(args.source) num_would_change = 0 for path in get_source_files(sources): with path.open("r") as file: contents = file.read() new_contents = fix_file_contents(contents) if contents == new_contents: continue if args.check: print(f"Would reformat {path}") num_would_change += 1 continue print(f"Reordering imports in {path}") with path.open("w") as file: file.write(new_contents) if num_would_change and args.check: print(f"sorti would sort imports in {num_would_change} " f"file{'s' if num_would_change != 1 else ''} ") return 1 elif args.check: print("sorti would make no changes, all imports are sorted") return 0
def test_separate_relative_when_only_relative_imports_are_present(): src = ('from . import bar\n' 'from . import foo\n') assert fix_file_contents(src, separate_relative=True) == src
def test_fix_file_contents(s, expected): assert fix_file_contents(s) == expected
def test_replace_module_imported_asname(): ret = fix_file_contents( 'from six.moves import queue as Queue\n', imports_to_replace=[(['six', 'moves', 'queue'], ['queue'], '')], ) assert ret == 'import queue as Queue\n'
def test_fix_file_contents(filename): with io.open(os.path.join('test_data/inputs', filename)) as f: input_contents = f.read() with io.open(os.path.join('test_data/outputs', filename)) as f: expected = f.read() assert fix_file_contents(input_contents) == expected
def test_replace_imports_from_asname(): ret = fix_file_contents( 'from foo import bar as baz\n', imports_to_replace=[(['foo'], ['baz'], '')], ) assert ret == 'from baz import bar as baz\n'
def test_replace_imports_from_does_not_replace_name(): ret = fix_file_contents( 'from foo import bar\n', imports_to_replace=[(['foo', 'bar'], ['baz', 'hi'], '')], ) assert ret == 'from foo import bar\n'
def test_replace_imports_basic_from(): ret = fix_file_contents( 'from foo import bar\n', imports_to_replace=[(['foo'], ['baz'], '')], ) assert ret == 'from baz import bar\n'
def test_remove_imports_actually_removes(): assert fix_file_contents( 'from __future__ import with_statement\n\n' 'import os\n', imports_to_remove=('from __future__ import with_statement', ), ) == 'import os\n'
def test_remove_import_import_not_there(): assert fix_file_contents( 'import os\n', imports_to_remove=('from __future__ import with_statement', ), ) == 'import os\n'
def test_remove_import_trivial(): assert fix_file_contents( '', imports_to_remove=('from __future__ import with_statement', ), ) == ''
def test_add_import_import_already_there(): assert fix_file_contents( 'from __future__ import absolute_import\n', imports_to_add=('from __future__ import absolute_import', ), ) == 'from __future__ import absolute_import\n'
def test_add_import_trivial(): assert fix_file_contents( '', imports_to_add=('from __future__ import absolute_import', ), ) == ''