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

Adds a detection for index urls similar to how comments are captured. #825

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions pre_commit_hooks/requirements_txt_fixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Requirement:
def __init__(self) -> None:
self.value: bytes | None = None
self.comments: list[bytes] = []
self.index_urls: list[bytes] = []

@property
def name(self) -> bytes:
Expand Down Expand Up @@ -64,6 +65,8 @@ def fix_requirements(f: IO[bytes]) -> int:
requirements: list[Requirement] = []
before = list(f)
after: list[bytes] = []
index_options: list[bytes] = []
extra_index_options: list[bytes] = []

before_string = b''.join(before)

Expand Down Expand Up @@ -97,6 +100,13 @@ def fix_requirements(f: IO[bytes]) -> int:
requirement.comments.append(line)
elif line.lstrip().startswith(b'#') or line.strip() == b'':
requirement.comments.append(line)
elif (
line.lstrip().startswith(b'-i ') or
line.lstrip().startswith(b'--index-url')
):
index_options.append(line)
elif line.lstrip().startswith(b'--extra-index-url'):
extra_index_options.append(line)
else:
requirement.append_value(line)

Expand All @@ -113,6 +123,8 @@ def fix_requirements(f: IO[bytes]) -> int:
if req.value != b'pkg-resources==0.0.0\n'
]

after.extend(index_options)
after.extend(extra_index_options)
for requirement in sorted(requirements):
after.extend(requirement.comments)
assert requirement.value, requirement.value
Expand Down
21 changes: 21 additions & 0 deletions tests/requirements_txt_fixer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,27 @@
PASS,
b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n',
),
(
b'# Bar\n'
b'--extra-index-url http://dist.repoze.org/zope2/2.10/simple\n'
b'zopelib2\n'
b'# Foo\n'
b'-i http://dist.repoze.org/zope2/2.10/simple\n'
b'zopelib1\n'
b'# Baz\n'
b'--index-url http://dist.repoze.org/zope2/2.10/simple\n'
b'zopelib3',
FAIL,
b'-i http://dist.repoze.org/zope2/2.10/simple\n'
b'--index-url http://dist.repoze.org/zope2/2.10/simple\n'
b'--extra-index-url http://dist.repoze.org/zope2/2.10/simple\n'
b'# Foo\n'
b'zopelib1\n'
b'# Bar\n'
b'zopelib2\n'
b'# Baz\n'
b'zopelib3\n',
),
),
)
def test_integration(input_s, expected_retval, output, tmpdir):
Expand Down