Пример #1
0
def test_additional_directories_integration():
    if '' in sys.path:  # pragma: no cover (depends on run environment)
        sys.path.remove('')

    # Intentionally avoiding 'tests' and 'testing' because those would clash
    # with the names of this project
    os.makedirs('nottests/nottesting')
    io.open('nottests/nottesting/__init__.py', 'w').close()

    with io.open('foo.py', 'w') as foo_file:
        foo_file.write(
            'import thirdparty\n'
            'import nottests\n'
            'import nottesting\n'
        )

    # Without the new option
    main(('foo.py',))
    assert io.open('foo.py').read() == (
        'import nottesting\n'
        'import thirdparty\n'
        '\n'
        'import nottests\n'
    )

    # With the new option
    main(('foo.py', '--application-directories', '.:nottests'))
    assert io.open('foo.py').read() == (
        'import thirdparty\n'
        '\n'
        'import nottesting\n'
        'import nottests\n'
    )
def test_additional_directories_integration():
    if '' in sys.path:  # pragma: no cover (depends on run environment)
        sys.path.remove('')

    # Intentionally avoiding 'tests' and 'testing' because those would clash
    # with the names of this project
    os.makedirs('nottests/nottesting')
    io.open('nottests/nottesting/__init__.py', 'w').close()

    with io.open('foo.py', 'w') as foo_file:
        foo_file.write('import thirdparty\n'
                       'import nottests\n'
                       'import nottesting\n')

    # Without the new option
    main(('foo.py', ))
    assert io.open('foo.py').read() == ('import nottesting\n'
                                        'import thirdparty\n'
                                        '\n'
                                        'import nottests\n')

    # With the new option
    main(('foo.py', '--application-directories', '.:nottests'))
    assert io.open('foo.py').read() == ('import thirdparty\n'
                                        '\n'
                                        'import nottesting\n'
                                        'import nottests\n')
def test_separate_from_import_integration():
    os.makedirs('foo/bar')
    io.open('foo/__init__.py', 'w').close()
    io.open('foo/bar/__init__.py', 'w').close()

    with io.open('foo/foo.py', 'w') as foo_file:
        foo_file.write('import thirdparty\n'
                       'import foo.bar\n'
                       'from foo import bar\n'
                       'from . import bar\n')

    main(('foo/foo.py', ))
    assert io.open('foo/foo.py').read() == ('import thirdparty\n'
                                            '\n'
                                            'import foo.bar\n'
                                            'from . import bar\n'
                                            'from foo import bar\n')

    main(('foo/foo.py', '--separate-from-import'))
    assert io.open('foo/foo.py').read() == ('import thirdparty\n'
                                            '\n'
                                            'import foo.bar\n'
                                            '\n'
                                            'from . import bar\n'
                                            'from foo import bar\n')
def test_separate_from_import_integration():
    os.makedirs('foo/bar')
    io.open('foo/__init__.py', 'w').close()
    io.open('foo/bar/__init__.py', 'w').close()

    with io.open('foo/foo.py', 'w') as foo_file:
        foo_file.write(
            'import thirdparty\n'
            'import foo.bar\n'
            'from foo import bar\n'
            'from . import bar\n'
        )

    main(('foo/foo.py',))
    assert io.open('foo/foo.py').read() == (
        'import thirdparty\n'
        '\n'
        'import foo.bar\n'
        'from . import bar\n'
        'from foo import bar\n'
    )

    main(('foo/foo.py', '--separate-from-import'))
    assert io.open('foo/foo.py').read() == (
        'import thirdparty\n'
        '\n'
        'import foo.bar\n'
        '\n'
        'from . import bar\n'
        'from foo import bar\n'
    )
Пример #5
0
def test_integration_main(filename, tmpdir):
    input_contents = io.open(os.path.join('test_data/inputs', filename)).read()
    expected = io.open(os.path.join('test_data/outputs', filename)).read()

    test_file_path = tmpdir.join('test.py').strpath
    with io.open(test_file_path, 'w') as f:
        f.write(input_contents)

    # Check return value with --diff-only
    retv_diff = main((test_file_path, '--diff-only'))
    assert retv_diff == int(input_contents != expected)

    retv = main((test_file_path,))
    # Check return value
    assert retv == int(input_contents != expected)

    # Check the contents rewritten
    assert io.open(test_file_path).read() == expected
def test_integration_main(filename, tmpdir):
    input_contents = io.open(os.path.join('test_data/inputs', filename)).read()
    expected = io.open(os.path.join('test_data/outputs', filename)).read()

    test_file_path = tmpdir.join('test.py').strpath
    with io.open(test_file_path, 'w') as f:
        f.write(input_contents)

    # Check return value with --diff-only
    retv_diff = main((test_file_path, '--diff-only'))
    assert retv_diff == int(input_contents != expected)

    retv = main((test_file_path, ))
    # Check return value
    assert retv == int(input_contents != expected)

    # Check the contents rewritten
    assert io.open(test_file_path).read() == expected
Пример #7
0
def test_does_not_reorder_with_diff_only(in_tmpdir, capsys):
    test_file_path = in_tmpdir.join('test.py').strpath
    original_contents = 'import sys\nimport os\n'
    with io.open(test_file_path, 'w') as test_file:
        test_file.write(original_contents)

    retv = main((test_file_path, '--diff-only'))
    assert retv == 1
    assert io.open(test_file_path).read() == original_contents
    patch, _ = capsys.readouterr()
    _apply_patch(patch)
    assert io.open(test_file_path).read() == 'import os\nimport sys\n'
def test_does_not_reorder_with_diff_only(in_tmpdir, capsys):
    test_file_path = in_tmpdir.join('test.py').strpath
    original_contents = 'import sys\nimport os\n'
    with io.open(test_file_path, 'w') as test_file:
        test_file.write(original_contents)

    retv = main((test_file_path, '--diff-only'))
    assert retv == 1
    assert io.open(test_file_path).read() == original_contents
    patch, _ = capsys.readouterr()
    _apply_patch(patch)
    assert io.open(test_file_path).read() == 'import os\nimport sys\n'
Пример #9
0
def test_patch_multiple_files_no_eol(in_tmpdir, capsys):
    test1filename = in_tmpdir.join('test1.py').strpath
    test2filename = in_tmpdir.join('test2.py').strpath
    with io.open(test1filename, 'w') as test1:
        # Intentionally no EOL
        test1.write('import sys\nimport os')

    with io.open(test2filename, 'w') as test2:
        test2.write('import sys\nimport os\n')

    ret = main((test1filename, test2filename, '--diff-only'))
    assert ret == 1
    patch, _ = capsys.readouterr()
    _apply_patch(patch)
    assert io.open(test1filename).read() == 'import os\nimport sys\n'
    assert io.open(test2filename).read() == 'import os\nimport sys\n'
def test_patch_multiple_files_no_eol(in_tmpdir, capsys):
    test1filename = in_tmpdir.join('test1.py').strpath
    test2filename = in_tmpdir.join('test2.py').strpath
    with io.open(test1filename, 'w') as test1:
        # Intentionally no EOL
        test1.write('import sys\nimport os')

    with io.open(test2filename, 'w') as test2:
        test2.write('import sys\nimport os\n')

    ret = main((test1filename, test2filename, '--diff-only'))
    assert ret == 1
    patch, _ = capsys.readouterr()
    _apply_patch(patch)
    assert io.open(test1filename).read() == 'import os\nimport sys\n'
    assert io.open(test2filename).read() == 'import os\nimport sys\n'
Пример #11
0
def test_integration_main_stdout(capsys):
    ret = main(('--print-only', 'test_data/inputs/needs_reordering.py'))
    assert ret == 1
    out, err = capsys.readouterr()
    assert out == 'import os\n\nimport six\n\nimport reorder_python_imports\n'
    assert err == '==> test_data/inputs/needs_reordering.py <==\n'
def test_integration_main_stdout(capsys):
    ret = main(('--print-only', 'test_data/inputs/needs_reordering.py'))
    assert ret == 1
    out, err = capsys.readouterr()
    assert out == 'import os\n\nimport six\n\nimport reorder_python_imports\n'
    assert err == '==> test_data/inputs/needs_reordering.py <==\n'