Skip to content
This repository was archived by the owner on Nov 3, 2020. It is now read-only.

Commit d044ff2

Browse files
committed
Merge pull request #12 from teeberg/show_diff
Add --diff flag to show diff of expected imports
2 parents ec01d99 + fe7d5f1 commit d044ff2

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Available flags:
2424

2525
* ``--silent-overwrite``: The hook won't fail if it has to change files. It will
2626
just do it.
27+
* ``--check-only``: The hook will not change any files.
28+
* ``--diff``: If imports are not ordered correctly, print a diff of required
29+
changes to fix the import order.
2730

2831
The hook supports [isort's configuration files](https://github.com/timothycrosley/isort#configuring-isort) - Please refer to the isort documentation for reference
2932

pre_commit_hook/sort.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from isort import isort
77

88

9-
def imports_incorrect(filename):
10-
return isort.SortImports(filename, check=True).incorrectly_sorted
9+
def imports_incorrect(filename, show_diff=False):
10+
return isort.SortImports(filename, check=True, show_diff=show_diff).incorrectly_sorted
1111

1212

1313
def main(argv=None):
@@ -16,12 +16,13 @@ def main(argv=None):
1616
parser.add_argument('filenames', nargs='*', help='Filenames to run')
1717
parser.add_argument('--silent-overwrite', action='store_true', dest='silent', default=False)
1818
parser.add_argument('--check-only', action='store_true', dest='check_only', default=False)
19+
parser.add_argument('--diff', action='store_true', dest='show_diff', default=False)
1920
args = parser.parse_args(argv)
2021

2122
return_value = 0
2223

2324
for filename in args.filenames:
24-
if imports_incorrect(filename):
25+
if imports_incorrect(filename, show_diff=args.show_diff):
2526
if args.check_only:
2627
return_value = 1
2728
elif args.silent:

tests/sort_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from isort.isort import stdout
23

34
from pre_commit_hook.sort import main
45

@@ -21,3 +22,20 @@ def test_sort(tmpfiles):
2122
assert main([tmpfiles.join('incorrect_1.py').strpath]) == 1
2223
assert main([tmpfiles.join('incorrect_2.py').strpath, '--check-only']) == 1
2324
assert main([tmpfiles.join('incorrect_2.py').strpath, '--silent-overwrite']) == 0
25+
26+
27+
def test_sort_with_diff(tmpfiles):
28+
filename = tmpfiles.join('incorrect_1.py').strpath
29+
main(['--diff', '--check-only', filename])
30+
31+
stdout.seek(0)
32+
lines = stdout.read().splitlines()
33+
# Skip diff header
34+
lines = lines[4:]
35+
assert lines == [
36+
'+import json',
37+
' import sys',
38+
'-',
39+
'-',
40+
'-import json',
41+
]

0 commit comments

Comments
 (0)