mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 20:16:53 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
fb0f6fac81
commit
df46f71980
3 changed files with 142 additions and 140 deletions
|
|
@ -186,7 +186,7 @@ the following commandline options:
|
||||||
- `--top-keys comma,separated,keys` - Keys to keep at the top of mappings.
|
- `--top-keys comma,separated,keys` - Keys to keep at the top of mappings.
|
||||||
|
|
||||||
#### `requirements-txt-fixer`
|
#### `requirements-txt-fixer`
|
||||||
Sorts entries in requirements.txt and constraints.txt and removes incorrect entry for `pkg-resources==0.0.0`
|
Sorts entries in requirements.txt and constraints.txt and removes incorrect entry for `pkg-resources==0.0.0`
|
||||||
Provides also an optional check if a version is specified for each requirement. You can configure this with
|
Provides also an optional check if a version is specified for each requirement. You can configure this with
|
||||||
the following commandline options:
|
the following commandline options:
|
||||||
- `--fail-without-version` - Fails when no version is specified for a requirement
|
- `--fail-without-version` - Fails when no version is specified for a requirement
|
||||||
|
|
|
||||||
|
|
@ -151,7 +151,7 @@ def fix_requirements(f: IO[bytes], fail_without_version: bool) -> int:
|
||||||
if before_string == after_string:
|
if before_string == after_string:
|
||||||
return PASS
|
return PASS
|
||||||
else:
|
else:
|
||||||
print("Sorting requirements")
|
print('Sorting requirements')
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
f.write(after_string)
|
f.write(after_string)
|
||||||
f.truncate()
|
f.truncate()
|
||||||
|
|
@ -161,8 +161,10 @@ def fix_requirements(f: IO[bytes], fail_without_version: bool) -> int:
|
||||||
def main(argv: Sequence[str] | None = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
||||||
parser.add_argument("--fail-without-version", action="store_true",
|
parser.add_argument(
|
||||||
help="Fail if a requirement is missing a version")
|
'--fail-without-version', action='store_true',
|
||||||
|
help='Fail if a requirement is missing a version',
|
||||||
|
)
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
retv = PASS
|
retv = PASS
|
||||||
|
|
|
||||||
|
|
@ -12,152 +12,152 @@ from pre_commit_hooks.requirements_txt_fixer import Requirement
|
||||||
('input_s', 'argv', 'expected_retval', 'output'),
|
('input_s', 'argv', 'expected_retval', 'output'),
|
||||||
(
|
(
|
||||||
|
|
||||||
(b'', [], PASS, b''),
|
(b'', [], PASS, b''),
|
||||||
(b'\n', [], PASS, b'\n'),
|
(b'\n', [], PASS, b'\n'),
|
||||||
(b'# intentionally empty\n', [], PASS, b'# intentionally empty\n'),
|
(b'# intentionally empty\n', [], PASS, b'# intentionally empty\n'),
|
||||||
(b'foo\n# comment at end\n', [], PASS, b'foo\n# comment at end\n'),
|
(b'foo\n# comment at end\n', [], PASS, b'foo\n# comment at end\n'),
|
||||||
(b'foo\nbar\n', [], FAIL, b'bar\nfoo\n'),
|
(b'foo\nbar\n', [], FAIL, b'bar\nfoo\n'),
|
||||||
(b'bar\nfoo\n', [], PASS, b'bar\nfoo\n'),
|
(b'bar\nfoo\n', [], PASS, b'bar\nfoo\n'),
|
||||||
(b'a\nc\nb\n', [], FAIL, b'a\nb\nc\n'),
|
(b'a\nc\nb\n', [], FAIL, b'a\nb\nc\n'),
|
||||||
(b'a\nc\nb', [], FAIL, b'a\nb\nc\n'),
|
(b'a\nc\nb', [], FAIL, b'a\nb\nc\n'),
|
||||||
(b'a\nb\nc', [], FAIL, b'a\nb\nc\n'),
|
(b'a\nb\nc', [], FAIL, b'a\nb\nc\n'),
|
||||||
(
|
(
|
||||||
b'#comment1\nfoo\n#comment2\nbar\n',
|
b'#comment1\nfoo\n#comment2\nbar\n',
|
||||||
[],
|
[],
|
||||||
FAIL,
|
FAIL,
|
||||||
b'#comment2\nbar\n#comment1\nfoo\n',
|
b'#comment2\nbar\n#comment1\nfoo\n',
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
b'#comment1\nbar\n#comment2\nfoo\n',
|
b'#comment1\nbar\n#comment2\nfoo\n',
|
||||||
[],
|
[],
|
||||||
PASS,
|
PASS,
|
||||||
b'#comment1\nbar\n#comment2\nfoo\n',
|
b'#comment1\nbar\n#comment2\nfoo\n',
|
||||||
),
|
),
|
||||||
(b'#comment\n\nfoo\nbar\n', [], FAIL, b'#comment\n\nbar\nfoo\n'),
|
(b'#comment\n\nfoo\nbar\n', [], FAIL, b'#comment\n\nbar\nfoo\n'),
|
||||||
(b'#comment\n\nbar\nfoo\n', [], PASS, b'#comment\n\nbar\nfoo\n'),
|
(b'#comment\n\nbar\nfoo\n', [], PASS, b'#comment\n\nbar\nfoo\n'),
|
||||||
(
|
(
|
||||||
b'foo\n\t#comment with indent\nbar\n',
|
b'foo\n\t#comment with indent\nbar\n',
|
||||||
[],
|
[],
|
||||||
FAIL,
|
FAIL,
|
||||||
b'\t#comment with indent\nbar\nfoo\n',
|
b'\t#comment with indent\nbar\nfoo\n',
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
b'bar\n\t#comment with indent\nfoo\n',
|
b'bar\n\t#comment with indent\nfoo\n',
|
||||||
[],
|
[],
|
||||||
PASS,
|
PASS,
|
||||||
b'bar\n\t#comment with indent\nfoo\n',
|
b'bar\n\t#comment with indent\nfoo\n',
|
||||||
),
|
),
|
||||||
(b'\nfoo\nbar\n', [], FAIL, b'bar\n\nfoo\n'),
|
(b'\nfoo\nbar\n', [], FAIL, b'bar\n\nfoo\n'),
|
||||||
(b'\nbar\nfoo\n', [], PASS, b'\nbar\nfoo\n'),
|
(b'\nbar\nfoo\n', [], PASS, b'\nbar\nfoo\n'),
|
||||||
(
|
(
|
||||||
b'pyramid-foo==1\npyramid>=2\n',
|
b'pyramid-foo==1\npyramid>=2\n',
|
||||||
[],
|
[],
|
||||||
FAIL,
|
FAIL,
|
||||||
b'pyramid>=2\npyramid-foo==1\n',
|
b'pyramid>=2\npyramid-foo==1\n',
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
b'a==1\n'
|
b'a==1\n'
|
||||||
b'c>=1\n'
|
b'c>=1\n'
|
||||||
b'bbbb!=1\n'
|
b'bbbb!=1\n'
|
||||||
b'c-a>=1;python_version>="3.6"\n'
|
b'c-a>=1;python_version>="3.6"\n'
|
||||||
b'e>=2\n'
|
b'e>=2\n'
|
||||||
b'd>2\n'
|
b'd>2\n'
|
||||||
b'g<2\n'
|
b'g<2\n'
|
||||||
b'f<=2\n',
|
b'f<=2\n',
|
||||||
[],
|
[],
|
||||||
FAIL,
|
FAIL,
|
||||||
b'a==1\n'
|
b'a==1\n'
|
||||||
b'bbbb!=1\n'
|
b'bbbb!=1\n'
|
||||||
b'c>=1\n'
|
b'c>=1\n'
|
||||||
b'c-a>=1;python_version>="3.6"\n'
|
b'c-a>=1;python_version>="3.6"\n'
|
||||||
b'd>2\n'
|
b'd>2\n'
|
||||||
b'e>=2\n'
|
b'e>=2\n'
|
||||||
b'f<=2\n'
|
b'f<=2\n'
|
||||||
b'g<2\n',
|
b'g<2\n',
|
||||||
),
|
),
|
||||||
(b'a==1\nb==1\na==1\n', [], FAIL, b'a==1\nb==1\n'),
|
(b'a==1\nb==1\na==1\n', [], FAIL, b'a==1\nb==1\n'),
|
||||||
(
|
(
|
||||||
b'a==1\nb==1\n#comment about a\na==1\n',
|
b'a==1\nb==1\n#comment about a\na==1\n',
|
||||||
[],
|
[],
|
||||||
FAIL,
|
FAIL,
|
||||||
b'#comment about a\na==1\nb==1\n',
|
b'#comment about a\na==1\nb==1\n',
|
||||||
),
|
),
|
||||||
(b'ocflib\nDjango\nPyMySQL\n', [], FAIL, b'Django\nocflib\nPyMySQL\n'),
|
(b'ocflib\nDjango\nPyMySQL\n', [], FAIL, b'Django\nocflib\nPyMySQL\n'),
|
||||||
(
|
(
|
||||||
b'-e git+ssh://git_url@tag#egg=ocflib\nDjango\nPyMySQL\n',
|
b'-e git+ssh://git_url@tag#egg=ocflib\nDjango\nPyMySQL\n',
|
||||||
[],
|
[],
|
||||||
FAIL,
|
FAIL,
|
||||||
b'Django\n-e git+ssh://git_url@tag#egg=ocflib\nPyMySQL\n',
|
b'Django\n-e git+ssh://git_url@tag#egg=ocflib\nPyMySQL\n',
|
||||||
),
|
),
|
||||||
(b'bar\npkg-resources==0.0.0\nfoo\n', [], FAIL, b'bar\nfoo\n'),
|
(b'bar\npkg-resources==0.0.0\nfoo\n', [], FAIL, b'bar\nfoo\n'),
|
||||||
(b'foo\npkg-resources==0.0.0\nbar\n', [], FAIL, b'bar\nfoo\n'),
|
(b'foo\npkg-resources==0.0.0\nbar\n', [], FAIL, b'bar\nfoo\n'),
|
||||||
(b'bar\npkg_resources==0.0.0\nfoo\n', [], FAIL, b'bar\nfoo\n'),
|
(b'bar\npkg_resources==0.0.0\nfoo\n', [], FAIL, b'bar\nfoo\n'),
|
||||||
(b'foo\npkg_resources==0.0.0\nbar\n', [], FAIL, b'bar\nfoo\n'),
|
(b'foo\npkg_resources==0.0.0\nbar\n', [], FAIL, b'bar\nfoo\n'),
|
||||||
(
|
(
|
||||||
b'git+ssh://git_url@tag#egg=ocflib\nDjango\nijk\n',
|
b'git+ssh://git_url@tag#egg=ocflib\nDjango\nijk\n',
|
||||||
[],
|
[],
|
||||||
FAIL,
|
FAIL,
|
||||||
b'Django\nijk\ngit+ssh://git_url@tag#egg=ocflib\n',
|
b'Django\nijk\ngit+ssh://git_url@tag#egg=ocflib\n',
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
b'b==1.0.0\n'
|
b'b==1.0.0\n'
|
||||||
b'c=2.0.0 \\\n'
|
b'c=2.0.0 \\\n'
|
||||||
b' --hash=sha256:abcd\n'
|
b' --hash=sha256:abcd\n'
|
||||||
b'a=3.0.0 \\\n'
|
b'a=3.0.0 \\\n'
|
||||||
b' --hash=sha256:a1b1c1d1',
|
b' --hash=sha256:a1b1c1d1',
|
||||||
[],
|
[],
|
||||||
FAIL,
|
FAIL,
|
||||||
b'a=3.0.0 \\\n'
|
b'a=3.0.0 \\\n'
|
||||||
b' --hash=sha256:a1b1c1d1\n'
|
b' --hash=sha256:a1b1c1d1\n'
|
||||||
b'b==1.0.0\n'
|
b'b==1.0.0\n'
|
||||||
b'c=2.0.0 \\\n'
|
b'c=2.0.0 \\\n'
|
||||||
b' --hash=sha256:abcd\n',
|
b' --hash=sha256:abcd\n',
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n',
|
b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n',
|
||||||
[],
|
[],
|
||||||
PASS,
|
PASS,
|
||||||
b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n',
|
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\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'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'#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\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):
|
def test_integration(input_s, argv, expected_retval, output, tmpdir):
|
||||||
path = tmpdir.join('file.txt')
|
path = tmpdir.join('file.txt')
|
||||||
path.write_binary(input_s)
|
path.write_binary(input_s)
|
||||||
|
|
||||||
output_retval = main([str(path)] + argv)
|
output_retval = main([str(path)] + argv)
|
||||||
|
|
||||||
assert path.read_binary() == output
|
assert path.read_binary() == output
|
||||||
assert output_retval == expected_retval
|
assert output_retval == expected_retval
|
||||||
|
|
||||||
|
|
||||||
def test_requirement_object():
|
def test_requirement_object():
|
||||||
top_of_file = Requirement()
|
top_of_file = Requirement()
|
||||||
top_of_file.comments.append(b'#foo')
|
top_of_file.comments.append(b'#foo')
|
||||||
top_of_file.value = b'\n'
|
top_of_file.value = b'\n'
|
||||||
|
|
||||||
requirement_foo = Requirement()
|
requirement_foo = Requirement()
|
||||||
requirement_foo.value = b'foo'
|
requirement_foo.value = b'foo'
|
||||||
|
|
||||||
requirement_bar = Requirement()
|
requirement_bar = Requirement()
|
||||||
requirement_bar.value = b'bar'
|
requirement_bar.value = b'bar'
|
||||||
|
|
||||||
requirements_bar_versioned = Requirement()
|
requirements_bar_versioned = Requirement()
|
||||||
requirements_bar_versioned.value = b'bar==1.0'
|
requirements_bar_versioned.value = b'bar==1.0'
|
||||||
|
|
||||||
# check for version specification
|
# check for version specification
|
||||||
assert top_of_file.contains_version_specifier() is False
|
assert top_of_file.contains_version_specifier() is False
|
||||||
assert requirement_foo.contains_version_specifier() is False
|
assert requirement_foo.contains_version_specifier() is False
|
||||||
assert requirement_bar.contains_version_specifier() is False
|
assert requirement_bar.contains_version_specifier() is False
|
||||||
assert requirements_bar_versioned.contains_version_specifier() is True
|
assert requirements_bar_versioned.contains_version_specifier() is True
|
||||||
|
|
||||||
# This may look redundant, but we need to test both foo.__lt__(bar) and
|
# This may look redundant, but we need to test both foo.__lt__(bar) and
|
||||||
# bar.__lt__(foo)
|
# bar.__lt__(foo)
|
||||||
assert requirement_foo > top_of_file
|
assert requirement_foo > top_of_file
|
||||||
assert top_of_file < requirement_foo
|
assert top_of_file < requirement_foo
|
||||||
assert requirement_foo > requirement_bar
|
assert requirement_foo > requirement_bar
|
||||||
assert requirement_bar < requirement_foo
|
assert requirement_bar < requirement_foo
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue