-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
test_pre_commit.py
525 lines (486 loc) · 13.9 KB
/
test_pre_commit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
"""Pre-commit tests."""
from textwrap import dedent
import pytest
from testfixtures import compare
from nitpick.constants import PRE_COMMIT_CONFIG_YAML, SETUP_CFG
from nitpick.plugins.pre_commit import PreCommitHook
from nitpick.violations import Fuss
from tests.helpers import NBSP, ProjectMock
def test_pre_commit_has_no_configuration(tmp_path):
"""No errors should be raised if pre-commit is not referenced in any style file.
Also the file should not be deleted unless explicitly asked.
"""
ProjectMock(tmp_path).style("").pre_commit("").api_check_then_fix()
def test_pre_commit_referenced_in_style(tmp_path):
"""Only check files if they have configured styles."""
ProjectMock(tmp_path).style(
"""
[".pre-commit-config.yaml"]
fail_fast = true
"""
).pre_commit("").api_check_then_fix(Fuss(False, PRE_COMMIT_CONFIG_YAML, 331, " doesn't have the 'repos' root key"))
def test_suggest_initial_contents(tmp_path):
"""Suggest initial contents for missing pre-commit config file."""
ProjectMock(tmp_path).load_styles("isort", "black").pyproject_toml(
"""
[tool.nitpick]
style = ["isort", "black"]
"""
).api_check_then_fix(
Fuss(
False,
PRE_COMMIT_CONFIG_YAML,
331,
" was not found. Create it with this content:",
"""
repos:
- repo: https://github.com/PyCQA/isort
rev: 5.8.0
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 21.5b1
hooks:
- id: black
args: [--safe, --quiet]
- repo: https://github.com/asottile/blacken-docs
rev: v1.10.0
hooks:
- id: blacken-docs
additional_dependencies: [black==21.5b1]
""",
),
partial_names=[PRE_COMMIT_CONFIG_YAML],
)
def test_no_yaml_key(tmp_path):
"""Test an invalid repo config."""
ProjectMock(tmp_path).style(
'''
[[".pre-commit-config.yaml".repos]]
missing_yaml_key = """
- repo: https://github.com/PyCQA/isort
rev: 5.8.0
hooks:
- id: isort
"""
'''
).api_check_then_fix(
Fuss(
False,
PRE_COMMIT_CONFIG_YAML,
331,
" was not found. Create it with this content:",
"""
repos: []
""",
)
)
def test_root_values_on_missing_file(tmp_path):
"""Test values on the root of the config file when it's missing."""
ProjectMock(tmp_path).style(
"""
[".pre-commit-config.yaml"]
bla_bla = "oh yeah"
fail_fast = true
whatever = "1"
"""
).api_check_then_fix(
Fuss(
False,
PRE_COMMIT_CONFIG_YAML,
331,
" was not found. Create it with this content:",
"""
bla_bla: oh yeah
fail_fast: true
whatever: '1'
""",
)
)
def test_root_values_on_existing_file(tmp_path):
"""Test values on the root of the config file when there is a file."""
ProjectMock(tmp_path).style(
"""
[".pre-commit-config.yaml"]
fail_fast = true
blabla = "what"
something = true
another_thing = "yep"
"""
).pre_commit(
"""
repos:
- hooks:
- id: whatever
something: false
another_thing: "nope"
"""
).api_check_then_fix(
Fuss(
False,
PRE_COMMIT_CONFIG_YAML,
338,
" has missing values:",
"""
blabla: what
fail_fast: true
""",
),
Fuss(
False,
PRE_COMMIT_CONFIG_YAML,
339,
" has different values. Use this:",
"""
another_thing: yep
something: true
""",
),
)
def test_missing_repos(tmp_path):
"""Test missing repos on file."""
ProjectMock(tmp_path).style(
"""
[".pre-commit-config.yaml"]
fail_fast = true
"""
).pre_commit(
"""
grepos:
- hooks:
- id: whatever
"""
).api_check_then_fix(
Fuss(False, PRE_COMMIT_CONFIG_YAML, 331, " doesn't have the 'repos' root key")
)
def test_missing_repo_key(tmp_path):
"""Test missing repo key on the style file."""
ProjectMock(tmp_path).style(
"""
[[".pre-commit-config.yaml".repos]]
grepo = "glocal"
"""
).pre_commit(
"""
repos:
- hooks:
- id: whatever
"""
).api_check_then_fix(
Fuss(False, PRE_COMMIT_CONFIG_YAML, 332, ": style file is missing 'repo' key in repo #0")
)
def test_repo_does_not_exist(tmp_path):
"""Test repo does not exist on the pre-commit file."""
ProjectMock(tmp_path).style(
"""
[[".pre-commit-config.yaml".repos]]
repo = "local"
"""
).pre_commit(
"""
repos:
- hooks:
- id: whatever
"""
).api_check_then_fix(
Fuss(False, PRE_COMMIT_CONFIG_YAML, 333, ": repo 'local' does not exist under 'repos'")
)
def test_missing_hooks_in_repo(tmp_path):
"""Test missing hooks in repo."""
ProjectMock(tmp_path).style(
"""
[[".pre-commit-config.yaml".repos]]
repo = "whatever"
"""
).pre_commit(
"""
repos:
- repo: whatever
"""
).api_check_then_fix(
Fuss(False, PRE_COMMIT_CONFIG_YAML, 334, ": missing 'hooks' in repo 'whatever'")
)
def test_style_missing_hooks_in_repo(tmp_path):
"""Test style file is missing hooks in repo."""
ProjectMock(tmp_path).style(
"""
[[".pre-commit-config.yaml".repos]]
repo = "another"
"""
).pre_commit(
"""
repos:
- repo: another
hooks:
- id: isort
"""
).api_check_then_fix(
Fuss(False, PRE_COMMIT_CONFIG_YAML, 335, ": style file is missing 'hooks' in repo 'another'")
)
def test_style_missing_id_in_hook(tmp_path):
"""Test style file is missing id in hook."""
ProjectMock(tmp_path).style(
f'''
[[".pre-commit-config.yaml".repos]]
repo = "another"
hooks = """
- name: isort
entry: isort -sp {SETUP_CFG}
"""
'''
).pre_commit(
"""
repos:
- repo: another
hooks:
- id: isort
"""
).api_check_then_fix(
Fuss(
False,
PRE_COMMIT_CONFIG_YAML,
336,
": style file is missing 'id' in hook:",
f"""
{NBSP*4}name: isort
{NBSP*4}entry: isort -sp {SETUP_CFG}
""",
)
)
def test_missing_hook_with_id(tmp_path):
"""Test missing hook with specific id."""
ProjectMock(tmp_path).style(
'''
[[".pre-commit-config.yaml".repos]]
repo = "other"
hooks = """
- id: black
name: black
entry: black
"""
'''
).pre_commit(
"""
repos:
- repo: other
hooks:
- id: isort
"""
).api_check_then_fix(
Fuss(
False,
PRE_COMMIT_CONFIG_YAML,
337,
": missing hook with id 'black':",
f"""
{NBSP * 2}- id: black
{NBSP * 2} name: black
{NBSP * 2} entry: black
""",
)
)
def test_get_all_hooks_from():
"""Test if the get_all_hooks_from() method will split the YAML block in hooks and copy the repo info for each."""
data = """
- repo: https://github.com/user/repo
rev: v0.4.5
hooks:
- id: first
additional_dependencies: [package==1.0.0]
- id: second
args: [1, 2, 3]
- id: third
- id: fourth
args: [some, here]
additional_dependencies: [another>=2.0.3]
"""
rv = PreCommitHook.get_all_hooks_from(dedent(data))
def assert_hook_yaml(key, yaml_string):
expected = rv["https://github.com/user/repo_" + key].yaml.reformatted
actual = yaml_string
compare(dedent(actual).strip(), dedent(expected).strip())
assert_hook_yaml(
"first",
"""
- repo: https://github.com/user/repo
rev: v0.4.5
hooks:
- id: first
additional_dependencies: [package==1.0.0]
""",
)
assert_hook_yaml(
"second",
"""
- repo: https://github.com/user/repo
rev: v0.4.5
hooks:
- id: second
args: [1, 2, 3]
""",
)
assert_hook_yaml(
"third",
"""
- repo: https://github.com/user/repo
rev: v0.4.5
hooks:
- id: third
""",
)
assert_hook_yaml(
"fourth",
"""
- repo: https://github.com/user/repo
rev: v0.4.5
hooks:
- id: fourth
args: [some, here]
additional_dependencies: [another>=2.0.3]
""",
)
def test_missing_different_values(tmp_path):
"""Test missing and different values on the hooks."""
# TODO: add loaded and named styles automatically to pyproject_toml
# pylint: disable=line-too-long
ProjectMock(tmp_path).named_style(
"root",
'''
[[".pre-commit-config.yaml".repos]]
yaml = """
- repo: https://github.com/user/repo
rev: 1.2.3
hooks:
- id: my-hook
args: [--expected, arguments]
"""
''',
).load_styles("mypy", "pre-commit/python", "pre-commit/bash").pyproject_toml(
"""
[tool.nitpick]
style = ["root", "mypy", "pre-commit/python", "pre-commit/bash"]
"""
).pre_commit(
"""
repos:
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.1.0
hooks:
- id: python-check-blanket-noqa
- id: missing-hook-in-this-position
- id: python-no-eval
- id: python-no-log-warn
- id: rst-backticks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: debug-statements
- repo: https://github.com/asottile/pyupgrade
rev: v2.16.0
hooks:
- id: pyupgrade
- repo: https://github.com/openstack/bashate
rev: 0.5.0
hooks:
- id: extra-hook-before-should-be-ignored
- id: bashate
args: [extra, arguments, should, --not, --throw, errors]
- id: extra-hook-after-should-be-ignored
- repo: https://github.com/user/repo
rev: 1.2.3
hooks:
- id: my-hook
args: [--different, args, --should, throw, errors]
"""
).api_check_then_fix(
Fuss(
False,
PRE_COMMIT_CONFIG_YAML,
332,
": hook 'mypy' not found. Use this:",
f"""
{NBSP * 2}- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.812
hooks:
- id: mypy
""",
),
Fuss(
False,
PRE_COMMIT_CONFIG_YAML,
332,
": hook 'python-check-mock-methods' not found. Use this:",
f"""
{NBSP * 2}- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.8.0
hooks:
- id: python-check-mock-methods
""",
),
Fuss(
False,
PRE_COMMIT_CONFIG_YAML,
339,
": hook 'bashate' (rev: 0.5.0) has different values. Use this:",
"rev: 2.0.0",
),
Fuss(
False,
PRE_COMMIT_CONFIG_YAML,
339,
": hook 'python-check-blanket-noqa' (rev: v1.1.0) has different values. Use this:",
"rev: v1.8.0",
),
Fuss(
False,
PRE_COMMIT_CONFIG_YAML,
339,
": hook 'python-no-eval' (rev: v1.1.0) has different values. Use this:",
"rev: v1.8.0",
),
Fuss(
False,
PRE_COMMIT_CONFIG_YAML,
339,
": hook 'python-no-log-warn' (rev: v1.1.0) has different values. Use this:",
"rev: v1.8.0",
),
Fuss(
False,
PRE_COMMIT_CONFIG_YAML,
339,
": hook 'my-hook' (rev: 1.2.3) has different values. Use this:",
"""
args:
- --expected
- arguments
""",
),
Fuss(
False,
PRE_COMMIT_CONFIG_YAML,
339,
": hook 'rst-backticks' (rev: v1.1.0) has different values. Use this:",
"rev: v1.8.0",
),
partial_names=[PRE_COMMIT_CONFIG_YAML],
)
def test_pre_commit_section_without_dot_deprecated(tmp_path):
"""A pre-commit section without dot is deprecated."""
project = (
ProjectMock(tmp_path)
.style(
"""
["pre-commit-config.yaml"]
fail_fast = true
"""
)
.pre_commit("")
)
with pytest.deprecated_call() as warning_list:
project.flake8().assert_single_error("NIP331 File .pre-commit-config.yaml doesn't have the 'repos' root key")
assert len(warning_list) == 1
assert (
str(warning_list[0].message)
== 'The section name for dotfiles should start with a dot: [".pre-commit-config.yaml"]'
)