diff --git a/pre_commit_hooks/requirements_txt_fixer.py b/pre_commit_hooks/requirements_txt_fixer.py index 8e97bcf..04590e4 100644 --- a/pre_commit_hooks/requirements_txt_fixer.py +++ b/pre_commit_hooks/requirements_txt_fixer.py @@ -60,7 +60,10 @@ class Requirement: ) def contains_version_specifier(self) -> bool: - return bool(self.VERSION_SPECIFIED.match(self.value)) + return ( + self.value is not None and + bool(self.VERSION_SPECIFIED.match(self.value)) + ) def append_value(self, value: bytes) -> None: if self.value is not None: @@ -171,7 +174,9 @@ def main(argv: Sequence[str] | None = None) -> int: for arg in args.filenames: with open(arg, 'rb+') as file_obj: - ret_for_file = fix_requirements(file_obj, args.fail_without_version) + ret_for_file = fix_requirements( + file_obj, args.fail_without_version, + ) if ret_for_file: print(f'Error in file {arg}') diff --git a/tests/requirements_txt_fixer_test.py b/tests/requirements_txt_fixer_test.py index b12f2f3..b35585c 100644 --- a/tests/requirements_txt_fixer_test.py +++ b/tests/requirements_txt_fixer_test.py @@ -120,9 +120,24 @@ from pre_commit_hooks.requirements_txt_fixer import Requirement b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n', ), (b'bar\nfoo\n', ['--fail-without-version'], FAIL, b'bar\nfoo\n'), - (b'bar==1.0\nfoo==1.1a\n', ['--fail-without-version'], PASS, b'bar==1.0\nfoo==1.1a\n'), - (b'#test\nbar==1.0\nfoo==1.1a\n', ['--fail-without-version'], PASS, b'#test\nbar==1.0\nfoo==1.1a\n'), - (b'bar==1.0\n#test\nfoo==1.1a\n', ['--fail-without-version'], PASS, b'bar==1.0\n#test\nfoo==1.1a\n'), + ( + b'bar==1.0\nfoo==1.1a\n', + ['--fail-without-version'], + PASS, + b'bar==1.0\nfoo==1.1a\n', + ), + ( + b'#test\nbar==1.0\nfoo==1.1a\n', + ['--fail-without-version'], + PASS, + b'#test\nbar==1.0\nfoo==1.1a\n', + ), + ( + b'bar==1.0\n#test\nfoo==1.1a\n', + ['--fail-without-version'], + PASS, + b'bar==1.0\n#test\nfoo==1.1a\n', + ), ), ) def test_integration(input_s, argv, expected_retval, output, tmpdir):