Skip to content

Commit df46f71

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent fb0f6fa commit df46f71

File tree

3 files changed

+142
-140
lines changed

3 files changed

+142
-140
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ the following commandline options:
186186
- `--top-keys comma,separated,keys` - Keys to keep at the top of mappings.
187187

188188
#### `requirements-txt-fixer`
189-
Sorts entries in requirements.txt and constraints.txt and removes incorrect entry for `pkg-resources==0.0.0`
189+
Sorts entries in requirements.txt and constraints.txt and removes incorrect entry for `pkg-resources==0.0.0`
190190
Provides also an optional check if a version is specified for each requirement. You can configure this with
191191
the following commandline options:
192192
- `--fail-without-version` - Fails when no version is specified for a requirement

pre_commit_hooks/requirements_txt_fixer.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def fix_requirements(f: IO[bytes], fail_without_version: bool) -> int:
151151
if before_string == after_string:
152152
return PASS
153153
else:
154-
print("Sorting requirements")
154+
print('Sorting requirements')
155155
f.seek(0)
156156
f.write(after_string)
157157
f.truncate()
@@ -161,8 +161,10 @@ def fix_requirements(f: IO[bytes], fail_without_version: bool) -> int:
161161
def main(argv: Sequence[str] | None = None) -> int:
162162
parser = argparse.ArgumentParser()
163163
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
164-
parser.add_argument("--fail-without-version", action="store_true",
165-
help="Fail if a requirement is missing a version")
164+
parser.add_argument(
165+
'--fail-without-version', action='store_true',
166+
help='Fail if a requirement is missing a version',
167+
)
166168
args = parser.parse_args(argv)
167169

168170
retv = PASS

tests/requirements_txt_fixer_test.py

Lines changed: 136 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -12,152 +12,152 @@
1212
('input_s', 'argv', 'expected_retval', 'output'),
1313
(
1414
15-
(b'', [], PASS, b''),
16-
(b'\n', [], PASS, b'\n'),
17-
(b'# intentionally empty\n', [], PASS, b'# intentionally empty\n'),
18-
(b'foo\n# comment at end\n', [], PASS, b'foo\n# comment at end\n'),
19-
(b'foo\nbar\n', [], FAIL, b'bar\nfoo\n'),
20-
(b'bar\nfoo\n', [], PASS, b'bar\nfoo\n'),
21-
(b'a\nc\nb\n', [], FAIL, b'a\nb\nc\n'),
22-
(b'a\nc\nb', [], FAIL, b'a\nb\nc\n'),
23-
(b'a\nb\nc', [], FAIL, b'a\nb\nc\n'),
24-
(
25-
b'#comment1\nfoo\n#comment2\nbar\n',
26-
[],
27-
FAIL,
28-
b'#comment2\nbar\n#comment1\nfoo\n',
29-
),
30-
(
31-
b'#comment1\nbar\n#comment2\nfoo\n',
32-
[],
33-
PASS,
34-
b'#comment1\nbar\n#comment2\nfoo\n',
35-
),
36-
(b'#comment\n\nfoo\nbar\n', [], FAIL, b'#comment\n\nbar\nfoo\n'),
37-
(b'#comment\n\nbar\nfoo\n', [], PASS, b'#comment\n\nbar\nfoo\n'),
38-
(
39-
b'foo\n\t#comment with indent\nbar\n',
40-
[],
41-
FAIL,
42-
b'\t#comment with indent\nbar\nfoo\n',
43-
),
44-
(
45-
b'bar\n\t#comment with indent\nfoo\n',
46-
[],
47-
PASS,
48-
b'bar\n\t#comment with indent\nfoo\n',
49-
),
50-
(b'\nfoo\nbar\n', [], FAIL, b'bar\n\nfoo\n'),
51-
(b'\nbar\nfoo\n', [], PASS, b'\nbar\nfoo\n'),
52-
(
53-
b'pyramid-foo==1\npyramid>=2\n',
54-
[],
55-
FAIL,
56-
b'pyramid>=2\npyramid-foo==1\n',
57-
),
58-
(
59-
b'a==1\n'
60-
b'c>=1\n'
61-
b'bbbb!=1\n'
62-
b'c-a>=1;python_version>="3.6"\n'
63-
b'e>=2\n'
64-
b'd>2\n'
65-
b'g<2\n'
66-
b'f<=2\n',
67-
[],
68-
FAIL,
69-
b'a==1\n'
70-
b'bbbb!=1\n'
71-
b'c>=1\n'
72-
b'c-a>=1;python_version>="3.6"\n'
73-
b'd>2\n'
74-
b'e>=2\n'
75-
b'f<=2\n'
76-
b'g<2\n',
77-
),
78-
(b'a==1\nb==1\na==1\n', [], FAIL, b'a==1\nb==1\n'),
79-
(
80-
b'a==1\nb==1\n#comment about a\na==1\n',
81-
[],
82-
FAIL,
83-
b'#comment about a\na==1\nb==1\n',
84-
),
85-
(b'ocflib\nDjango\nPyMySQL\n', [], FAIL, b'Django\nocflib\nPyMySQL\n'),
86-
(
87-
b'-e git+ssh://git_url@tag#egg=ocflib\nDjango\nPyMySQL\n',
88-
[],
89-
FAIL,
90-
b'Django\n-e git+ssh://git_url@tag#egg=ocflib\nPyMySQL\n',
91-
),
92-
(b'bar\npkg-resources==0.0.0\nfoo\n', [], FAIL, b'bar\nfoo\n'),
93-
(b'foo\npkg-resources==0.0.0\nbar\n', [], FAIL, b'bar\nfoo\n'),
94-
(b'bar\npkg_resources==0.0.0\nfoo\n', [], FAIL, b'bar\nfoo\n'),
95-
(b'foo\npkg_resources==0.0.0\nbar\n', [], FAIL, b'bar\nfoo\n'),
96-
(
97-
b'git+ssh://git_url@tag#egg=ocflib\nDjango\nijk\n',
98-
[],
99-
FAIL,
100-
b'Django\nijk\ngit+ssh://git_url@tag#egg=ocflib\n',
101-
),
102-
(
103-
b'b==1.0.0\n'
104-
b'c=2.0.0 \\\n'
105-
b' --hash=sha256:abcd\n'
106-
b'a=3.0.0 \\\n'
107-
b' --hash=sha256:a1b1c1d1',
108-
[],
109-
FAIL,
110-
b'a=3.0.0 \\\n'
111-
b' --hash=sha256:a1b1c1d1\n'
112-
b'b==1.0.0\n'
113-
b'c=2.0.0 \\\n'
114-
b' --hash=sha256:abcd\n',
115-
),
116-
(
117-
b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n',
118-
[],
119-
PASS,
120-
b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n',
121-
),
122-
(b'bar\nfoo\n', ["--fail-without-version"], FAIL, b'bar\nfoo\n'),
123-
(b'bar==1.0\nfoo==1.1a\n', ["--fail-without-version"], PASS, b'bar==1.0\nfoo==1.1a\n'),
124-
(b'#test\nbar==1.0\nfoo==1.1a\n', ["--fail-without-version"], PASS, b'#test\nbar==1.0\nfoo==1.1a\n'),
125-
(b'bar==1.0\n#test\nfoo==1.1a\n', ["--fail-without-version"], PASS, b'bar==1.0\n#test\nfoo==1.1a\n'),
15+
(b'', [], PASS, b''),
16+
(b'\n', [], PASS, b'\n'),
17+
(b'# intentionally empty\n', [], PASS, b'# intentionally empty\n'),
18+
(b'foo\n# comment at end\n', [], PASS, b'foo\n# comment at end\n'),
19+
(b'foo\nbar\n', [], FAIL, b'bar\nfoo\n'),
20+
(b'bar\nfoo\n', [], PASS, b'bar\nfoo\n'),
21+
(b'a\nc\nb\n', [], FAIL, b'a\nb\nc\n'),
22+
(b'a\nc\nb', [], FAIL, b'a\nb\nc\n'),
23+
(b'a\nb\nc', [], FAIL, b'a\nb\nc\n'),
24+
(
25+
b'#comment1\nfoo\n#comment2\nbar\n',
26+
[],
27+
FAIL,
28+
b'#comment2\nbar\n#comment1\nfoo\n',
29+
),
30+
(
31+
b'#comment1\nbar\n#comment2\nfoo\n',
32+
[],
33+
PASS,
34+
b'#comment1\nbar\n#comment2\nfoo\n',
35+
),
36+
(b'#comment\n\nfoo\nbar\n', [], FAIL, b'#comment\n\nbar\nfoo\n'),
37+
(b'#comment\n\nbar\nfoo\n', [], PASS, b'#comment\n\nbar\nfoo\n'),
38+
(
39+
b'foo\n\t#comment with indent\nbar\n',
40+
[],
41+
FAIL,
42+
b'\t#comment with indent\nbar\nfoo\n',
43+
),
44+
(
45+
b'bar\n\t#comment with indent\nfoo\n',
46+
[],
47+
PASS,
48+
b'bar\n\t#comment with indent\nfoo\n',
49+
),
50+
(b'\nfoo\nbar\n', [], FAIL, b'bar\n\nfoo\n'),
51+
(b'\nbar\nfoo\n', [], PASS, b'\nbar\nfoo\n'),
52+
(
53+
b'pyramid-foo==1\npyramid>=2\n',
54+
[],
55+
FAIL,
56+
b'pyramid>=2\npyramid-foo==1\n',
57+
),
58+
(
59+
b'a==1\n'
60+
b'c>=1\n'
61+
b'bbbb!=1\n'
62+
b'c-a>=1;python_version>="3.6"\n'
63+
b'e>=2\n'
64+
b'd>2\n'
65+
b'g<2\n'
66+
b'f<=2\n',
67+
[],
68+
FAIL,
69+
b'a==1\n'
70+
b'bbbb!=1\n'
71+
b'c>=1\n'
72+
b'c-a>=1;python_version>="3.6"\n'
73+
b'd>2\n'
74+
b'e>=2\n'
75+
b'f<=2\n'
76+
b'g<2\n',
77+
),
78+
(b'a==1\nb==1\na==1\n', [], FAIL, b'a==1\nb==1\n'),
79+
(
80+
b'a==1\nb==1\n#comment about a\na==1\n',
81+
[],
82+
FAIL,
83+
b'#comment about a\na==1\nb==1\n',
84+
),
85+
(b'ocflib\nDjango\nPyMySQL\n', [], FAIL, b'Django\nocflib\nPyMySQL\n'),
86+
(
87+
b'-e git+ssh://git_url@tag#egg=ocflib\nDjango\nPyMySQL\n',
88+
[],
89+
FAIL,
90+
b'Django\n-e git+ssh://git_url@tag#egg=ocflib\nPyMySQL\n',
91+
),
92+
(b'bar\npkg-resources==0.0.0\nfoo\n', [], FAIL, b'bar\nfoo\n'),
93+
(b'foo\npkg-resources==0.0.0\nbar\n', [], FAIL, b'bar\nfoo\n'),
94+
(b'bar\npkg_resources==0.0.0\nfoo\n', [], FAIL, b'bar\nfoo\n'),
95+
(b'foo\npkg_resources==0.0.0\nbar\n', [], FAIL, b'bar\nfoo\n'),
96+
(
97+
b'git+ssh://git_url@tag#egg=ocflib\nDjango\nijk\n',
98+
[],
99+
FAIL,
100+
b'Django\nijk\ngit+ssh://git_url@tag#egg=ocflib\n',
101+
),
102+
(
103+
b'b==1.0.0\n'
104+
b'c=2.0.0 \\\n'
105+
b' --hash=sha256:abcd\n'
106+
b'a=3.0.0 \\\n'
107+
b' --hash=sha256:a1b1c1d1',
108+
[],
109+
FAIL,
110+
b'a=3.0.0 \\\n'
111+
b' --hash=sha256:a1b1c1d1\n'
112+
b'b==1.0.0\n'
113+
b'c=2.0.0 \\\n'
114+
b' --hash=sha256:abcd\n',
115+
),
116+
(
117+
b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n',
118+
[],
119+
PASS,
120+
b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n',
121+
),
122+
(b'bar\nfoo\n', ['--fail-without-version'], FAIL, b'bar\nfoo\n'),
123+
(b'bar==1.0\nfoo==1.1a\n', ['--fail-without-version'], PASS, b'bar==1.0\nfoo==1.1a\n'),
124+
(b'#test\nbar==1.0\nfoo==1.1a\n', ['--fail-without-version'], PASS, b'#test\nbar==1.0\nfoo==1.1a\n'),
125+
(b'bar==1.0\n#test\nfoo==1.1a\n', ['--fail-without-version'], PASS, b'bar==1.0\n#test\nfoo==1.1a\n'),
126126
),
127127
)
128128
def test_integration(input_s, argv, expected_retval, output, tmpdir):
129-
path = tmpdir.join('file.txt')
130-
path.write_binary(input_s)
129+
path = tmpdir.join('file.txt')
130+
path.write_binary(input_s)
131131

132-
output_retval = main([str(path)] + argv)
132+
output_retval = main([str(path)] + argv)
133133

134-
assert path.read_binary() == output
135-
assert output_retval == expected_retval
134+
assert path.read_binary() == output
135+
assert output_retval == expected_retval
136136

137137

138138
def test_requirement_object():
139-
top_of_file = Requirement()
140-
top_of_file.comments.append(b'#foo')
141-
top_of_file.value = b'\n'
139+
top_of_file = Requirement()
140+
top_of_file.comments.append(b'#foo')
141+
top_of_file.value = b'\n'
142142

143-
requirement_foo = Requirement()
144-
requirement_foo.value = b'foo'
143+
requirement_foo = Requirement()
144+
requirement_foo.value = b'foo'
145145

146-
requirement_bar = Requirement()
147-
requirement_bar.value = b'bar'
146+
requirement_bar = Requirement()
147+
requirement_bar.value = b'bar'
148148

149-
requirements_bar_versioned = Requirement()
150-
requirements_bar_versioned.value = b'bar==1.0'
149+
requirements_bar_versioned = Requirement()
150+
requirements_bar_versioned.value = b'bar==1.0'
151151

152-
# check for version specification
153-
assert top_of_file.contains_version_specifier() is False
154-
assert requirement_foo.contains_version_specifier() is False
155-
assert requirement_bar.contains_version_specifier() is False
156-
assert requirements_bar_versioned.contains_version_specifier() is True
152+
# check for version specification
153+
assert top_of_file.contains_version_specifier() is False
154+
assert requirement_foo.contains_version_specifier() is False
155+
assert requirement_bar.contains_version_specifier() is False
156+
assert requirements_bar_versioned.contains_version_specifier() is True
157157

158-
# This may look redundant, but we need to test both foo.__lt__(bar) and
159-
# bar.__lt__(foo)
160-
assert requirement_foo > top_of_file
161-
assert top_of_file < requirement_foo
162-
assert requirement_foo > requirement_bar
163-
assert requirement_bar < requirement_foo
158+
# This may look redundant, but we need to test both foo.__lt__(bar) and
159+
# bar.__lt__(foo)
160+
assert requirement_foo > top_of_file
161+
assert top_of_file < requirement_foo
162+
assert requirement_foo > requirement_bar
163+
assert requirement_bar < requirement_foo

0 commit comments

Comments
 (0)