Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pre_commit_hooks/requirements_txt_fixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ def name(self) -> bytes:

return name[:m.start()]

@property
def sort_key(self) -> tuple[int, bytes]:
if self.name == b'--index-url':
return (0, self.name)
elif self.name == b'--extra-index-url':
return (1, self.name)
else:
return (2, self.name)

def __lt__(self, requirement: Requirement) -> bool:
# \n means top of file comment, so always return True,
# otherwise just do a string comparison with value.
Expand All @@ -50,7 +59,7 @@ def __lt__(self, requirement: Requirement) -> bool:
# with comments is kept)
if self.name == requirement.name:
return bool(self.comments) > bool(requirement.comments)
return self.name < requirement.name
return self.sort_key < requirement.sort_key

def is_complete(self) -> bool:
return (
Expand Down
10 changes: 10 additions & 0 deletions tests/requirements_txt_fixer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@
PASS,
b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n',
),

(
b'--extra-index-url https://example-extra/simple\n'
b'--index-url https://example-main/simple\n'
b'requests==2.31.0\n',
FAIL,
b'--index-url https://example-main/simple\n'
b'--extra-index-url https://example-extra/simple\n'
b'requests==2.31.0\n',
),
),
)
def test_integration(input_s, expected_retval, output, tmpdir):
Expand Down