Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --unique arg to file_contents_sorter #524

Merged
merged 1 commit into from Oct 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 additions & 4 deletions pre_commit_hooks/file_contents_sorter.py
Expand Up @@ -13,6 +13,7 @@
from typing import Any
from typing import Callable
from typing import IO
from typing import Iterable
from typing import Optional
from typing import Sequence

Expand All @@ -23,12 +24,16 @@
def sort_file_contents(
f: IO[bytes],
key: Optional[Callable[[bytes], Any]],
*,
unique: bool = False,
) -> int:
before = list(f)
after = sorted(
(line.strip(b'\n\r') for line in before if line.strip()),
key=key,
lines: Iterable[bytes] = (
line.rstrip(b'\n\r') for line in before if line.strip()
)
if unique:
lines = set(lines)
after = sorted(lines, key=key)

before_string = b''.join(before)
after_string = b'\n'.join(after) + b'\n'
Expand All @@ -52,13 +57,20 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
default=None,
help='fold lower case to upper case characters',
)
parser.add_argument(
'--unique',
action='store_true',
help='ensure each line is unique',
)
args = parser.parse_args(argv)

retv = PASS

for arg in args.filenames:
with open(arg, 'rb+') as file_obj:
ret_for_file = sort_file_contents(file_obj, key=args.ignore_case)
ret_for_file = sort_file_contents(
file_obj, key=args.ignore_case, unique=args.unique,
)

if ret_for_file:
print(f'Sorting {arg}')
Expand Down
30 changes: 30 additions & 0 deletions tests/file_contents_sorter_test.py
Expand Up @@ -45,6 +45,36 @@
FAIL,
b'fee\nFie\nFoe\nfum\n',
),
(
b'Fie\nFoe\nfee\nfee\nfum\n',
['--ignore-case'],
FAIL,
b'fee\nfee\nFie\nFoe\nfum\n',
),
(
b'Fie\nFoe\nfee\nfum\n',
['--unique'],
PASS,
b'Fie\nFoe\nfee\nfum\n',
),
(
b'Fie\nFie\nFoe\nfee\nfum\n',
['--unique'],
FAIL,
b'Fie\nFoe\nfee\nfum\n',
),
(
b'fee\nFie\nFoe\nfum\n',
['--unique', '--ignore-case'],
PASS,
b'fee\nFie\nFoe\nfum\n',
),
(
b'fee\nfee\nFie\nFoe\nfum\n',
['--unique', '--ignore-case'],
FAIL,
b'fee\nFie\nFoe\nfum\n',
),
),
)
def test_integration(input_s, argv, expected_retval, output, tmpdir):
Expand Down