blob: c400be1f0618d18a5b30e6969cbbb4056be772e0 [file] [log] [blame]
Anthony Sottile8f615292022-01-15 19:24:05 -05001from __future__ import annotations
2
Cameron Paulb83ea592014-12-16 12:22:37 -08003import pytest
4
Daniel Gallagher844d9832017-06-25 10:14:58 -07005from pre_commit_hooks.requirements_txt_fixer import FAIL
Anthony Sottile030bfac2019-01-31 19:19:10 -08006from pre_commit_hooks.requirements_txt_fixer import main
Daniel Gallagher844d9832017-06-25 10:14:58 -07007from pre_commit_hooks.requirements_txt_fixer import PASS
Cameron Paulb83ea592014-12-16 12:22:37 -08008from pre_commit_hooks.requirements_txt_fixer import Requirement
9
Daniel Gallagher844d9832017-06-25 10:14:58 -070010
11@pytest.mark.parametrize(
12 ('input_s', 'expected_retval', 'output'),
13 (
14 (b'', PASS, b''),
15 (b'\n', PASS, b'\n'),
Anthony Sottile86691ed2017-10-09 10:59:17 -070016 (b'# intentionally empty\n', PASS, b'# intentionally empty\n'),
17 (b'foo\n# comment at end\n', PASS, b'foo\n# comment at end\n'),
Daniel Gallagher844d9832017-06-25 10:14:58 -070018 (b'foo\nbar\n', FAIL, b'bar\nfoo\n'),
19 (b'bar\nfoo\n', PASS, b'bar\nfoo\n'),
Barak Y. Reifda2ea3f2019-09-28 21:40:09 +030020 (b'a\nc\nb\n', FAIL, b'a\nb\nc\n'),
21 (b'a\nc\nb', FAIL, b'a\nb\nc\n'),
Barak Y. Reifd4b544d2019-09-28 21:59:41 +030022 (b'a\nb\nc', FAIL, b'a\nb\nc\n'),
Anthony Sottile45756522019-02-11 19:56:15 -080023 (
24 b'#comment1\nfoo\n#comment2\nbar\n',
25 FAIL,
26 b'#comment2\nbar\n#comment1\nfoo\n',
27 ),
28 (
29 b'#comment1\nbar\n#comment2\nfoo\n',
30 PASS,
31 b'#comment1\nbar\n#comment2\nfoo\n',
32 ),
Daniel Gallagher844d9832017-06-25 10:14:58 -070033 (b'#comment\n\nfoo\nbar\n', FAIL, b'#comment\n\nbar\nfoo\n'),
34 (b'#comment\n\nbar\nfoo\n', PASS, b'#comment\n\nbar\nfoo\n'),
Viacheslav Greshilov28b2c8e2021-01-17 21:15:05 +020035 (
36 b'foo\n\t#comment with indent\nbar\n',
37 FAIL,
38 b'\t#comment with indent\nbar\nfoo\n',
39 ),
40 (
41 b'bar\n\t#comment with indent\nfoo\n',
42 PASS,
43 b'bar\n\t#comment with indent\nfoo\n',
44 ),
Daniel Gallagher844d9832017-06-25 10:14:58 -070045 (b'\nfoo\nbar\n', FAIL, b'bar\n\nfoo\n'),
46 (b'\nbar\nfoo\n', PASS, b'\nbar\nfoo\n'),
Anthony Sottile45756522019-02-11 19:56:15 -080047 (
Max Rozentsveyg7ebd4202020-05-16 23:58:27 -040048 b'pyramid-foo==1\npyramid>=2\n',
49 FAIL,
50 b'pyramid>=2\npyramid-foo==1\n',
51 ),
52 (
53 b'a==1\n'
54 b'c>=1\n'
55 b'bbbb!=1\n'
56 b'c-a>=1;python_version>="3.6"\n'
57 b'e>=2\n'
58 b'd>2\n'
59 b'g<2\n'
60 b'f<=2\n',
61 FAIL,
62 b'a==1\n'
63 b'bbbb!=1\n'
64 b'c>=1\n'
65 b'c-a>=1;python_version>="3.6"\n'
66 b'd>2\n'
67 b'e>=2\n'
68 b'f<=2\n'
69 b'g<2\n',
Anthony Sottile45756522019-02-11 19:56:15 -080070 ),
Vincent Houlbrèque0d6d0a72024-02-21 14:22:08 +010071 (b'a==1\nb==1\na==1\n', FAIL, b'a==1\nb==1\n'),
72 (
73 b'a==1\nb==1\n#comment about a\na==1\n',
74 FAIL,
75 b'#comment about a\na==1\nb==1\n',
76 ),
Daniel Gallagher844d9832017-06-25 10:14:58 -070077 (b'ocflib\nDjango\nPyMySQL\n', FAIL, b'Django\nocflib\nPyMySQL\n'),
78 (
79 b'-e git+ssh://git_url@tag#egg=ocflib\nDjango\nPyMySQL\n',
80 FAIL,
Anthony Sottile2a902e02017-07-12 18:35:24 -070081 b'Django\n-e git+ssh://git_url@tag#egg=ocflib\nPyMySQL\n',
Daniel Gallagher844d9832017-06-25 10:14:58 -070082 ),
Michał Sochoń1d6ad0d2018-03-25 23:28:04 +020083 (b'bar\npkg-resources==0.0.0\nfoo\n', FAIL, b'bar\nfoo\n'),
84 (b'foo\npkg-resources==0.0.0\nbar\n', FAIL, b'bar\nfoo\n'),
Vinay Karanam189e33e2019-11-14 02:22:07 +053085 (
86 b'git+ssh://git_url@tag#egg=ocflib\nDjango\nijk\n',
87 FAIL,
88 b'Django\nijk\ngit+ssh://git_url@tag#egg=ocflib\n',
89 ),
Aniket Bhatnagarbbcd31e2020-05-07 21:02:12 +010090 (
91 b'b==1.0.0\n'
92 b'c=2.0.0 \\\n'
93 b' --hash=sha256:abcd\n'
94 b'a=3.0.0 \\\n'
95 b' --hash=sha256:a1b1c1d1',
96 FAIL,
97 b'a=3.0.0 \\\n'
98 b' --hash=sha256:a1b1c1d1\n'
99 b'b==1.0.0\n'
100 b'c=2.0.0 \\\n'
101 b' --hash=sha256:abcd\n',
102 ),
103 (
104 b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n',
105 PASS,
106 b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n',
107 ),
Anthony Sottile2a902e02017-07-12 18:35:24 -0700108 ),
Cameron Paulb83ea592014-12-16 12:22:37 -0800109)
Anthony Sottile2f1d2bb2015-01-04 11:08:53 -0800110def test_integration(input_s, expected_retval, output, tmpdir):
Anthony Sottilea99475a2016-05-27 14:09:50 -0700111 path = tmpdir.join('file.txt')
112 path.write_binary(input_s)
Cameron Paulb83ea592014-12-16 12:22:37 -0800113
Max Rozentsveygf35bfed2020-05-20 12:07:45 -0400114 output_retval = main([str(path)])
Daniel Gallagher844d9832017-06-25 10:14:58 -0700115
Anthony Sottilea99475a2016-05-27 14:09:50 -0700116 assert path.read_binary() == output
Daniel Gallagher844d9832017-06-25 10:14:58 -0700117 assert output_retval == expected_retval
Cameron Paulb83ea592014-12-16 12:22:37 -0800118
119
120def test_requirement_object():
121 top_of_file = Requirement()
Anthony Sottile030bfac2019-01-31 19:19:10 -0800122 top_of_file.comments.append(b'#foo')
Cameron Paulb83ea592014-12-16 12:22:37 -0800123 top_of_file.value = b'\n'
124
125 requirement_foo = Requirement()
126 requirement_foo.value = b'foo'
127
128 requirement_bar = Requirement()
129 requirement_bar.value = b'bar'
130
131 # This may look redundant, but we need to test both foo.__lt__(bar) and
132 # bar.__lt__(foo)
133 assert requirement_foo > top_of_file
134 assert top_of_file < requirement_foo
135 assert requirement_foo > requirement_bar
136 assert requirement_bar < requirement_foo