mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-29 18:16:52 +00:00
Compare commits
383 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f1dff44d3a | ||
|
|
a77d5edb63 | ||
|
|
5dcc56558c | ||
|
|
a780519d03 | ||
|
|
6e513a2e71 | ||
|
|
401fb65f54 | ||
|
|
e59bbe6db7 | ||
|
|
ce6aed3bbe | ||
|
|
459b301556 | ||
|
|
d2cf95b9cf | ||
|
|
f4e025486b | ||
|
|
1592578eeb | ||
|
|
3fed74c572 | ||
|
|
a804ba5239 | ||
|
|
9ba250d7b3 | ||
|
|
b51ca91889 | ||
|
|
8e6ea961b7 | ||
|
|
3411068391 | ||
|
|
a2cdab0afe | ||
|
|
9148219126 | ||
|
|
3ab664f766 | ||
|
|
0925e7a9d1 | ||
|
|
e5e94e8702 | ||
|
|
3e8a870326 | ||
|
|
a8f86514f4 | ||
|
|
1acd88e5a4 | ||
|
|
1450343284 | ||
|
|
b554d0acae | ||
|
|
582b9c66e0 | ||
|
|
84f8a9d27f | ||
|
|
ec458dc1db | ||
|
|
1ea82dfbb9 | ||
|
|
540cd64f14 | ||
|
|
60e8a23c68 | ||
|
|
2a9bc76ca7 | ||
|
|
5920aee766 | ||
|
|
5c514f85cc | ||
|
|
0a0af6796b | ||
|
|
6db05e22aa | ||
|
|
09121de963 | ||
|
|
b4b46a81df | ||
|
|
31903eabdb | ||
|
|
16bd239a69 | ||
|
|
e505130ab7 | ||
|
|
e521ab251d | ||
|
|
bf885a482a | ||
|
|
6a28b51c01 | ||
|
|
73c57635b1 | ||
|
|
78084c34eb | ||
|
|
c0d5684142 | ||
|
|
afea58a65f | ||
|
|
ccf815675d | ||
|
|
318854d2b5 | ||
|
|
385434feb0 | ||
|
|
4c9457c08b | ||
|
|
e437b7ed51 | ||
|
|
475f1259da | ||
|
|
0beaecb1f7 | ||
|
|
6883485395 | ||
|
|
873992cf07 | ||
|
|
d7ef621445 | ||
|
|
51c43c4b29 | ||
|
|
a49f812a96 | ||
|
|
cef0300fd0 | ||
|
|
f47ab2f43c | ||
|
|
fd011242fd | ||
|
|
515e8b32ba | ||
|
|
c7d1e85f24 | ||
|
|
5b5b46dc75 | ||
|
|
003dfa55c9 | ||
|
|
ed714747d7 | ||
|
|
6553d02eb9 | ||
|
|
6952eeb163 | ||
|
|
429455474b | ||
|
|
2bcdd3ccff | ||
|
|
c6144a0218 | ||
|
|
effeb7a743 | ||
|
|
282f7ae54d | ||
|
|
0c625349aa | ||
|
|
9e7917b35e | ||
|
|
ae7cee538b | ||
|
|
f895cde3c7 | ||
|
|
9759ac692d | ||
|
|
2b5af89b17 | ||
|
|
6fc8413192 | ||
|
|
b6fb5ca711 | ||
|
|
cf1d5bf3bc | ||
|
|
bd4e487247 | ||
|
|
0f946c1b47 | ||
|
|
b06e32ef41 | ||
|
|
ee30c6ada9 | ||
|
|
a508c71a29 | ||
|
|
d0293ebc40 | ||
|
|
fcbfb901ea | ||
|
|
528a309bbd | ||
|
|
25cdd5457b | ||
|
|
8c24e2c2e6 | ||
|
|
714f1e57b1 | ||
|
|
0d20f18212 | ||
|
|
865409743e | ||
|
|
b73acb198e | ||
|
|
2c9f875913 | ||
|
|
d87a28b79d | ||
|
|
cef973f323 | ||
|
|
c8715b78af | ||
|
|
eadcce2e03 | ||
|
|
41912f6f5a | ||
|
|
6afc57465d | ||
|
|
6880fa1d4d | ||
|
|
2d948977cf | ||
|
|
bda540f53c | ||
|
|
aae27b8a3e | ||
|
|
0d6d0a79df | ||
|
|
454ade935b | ||
|
|
16f5239fab | ||
|
|
77c7147e52 | ||
|
|
8e040424dc | ||
|
|
e68665eb2e | ||
|
|
8212857ada | ||
|
|
42bc79146f | ||
|
|
34b4ce8685 | ||
|
|
7b2234e6dd | ||
|
|
d0d3f3ac05 | ||
|
|
6306a48f7d | ||
|
|
81954a7ca6 | ||
|
|
848f775476 | ||
|
|
82ad7d5eca | ||
|
|
27dcd3fd1d | ||
|
|
6e0df5d9bb | ||
|
|
c4a0b88311 | ||
|
|
ae9b59f2d4 | ||
|
|
f27ee318d2 | ||
|
|
6cad77080c | ||
|
|
038e3dd964 | ||
|
|
516a8a263c | ||
|
|
30f4ca331f | ||
|
|
f96db964c4 | ||
|
|
2e4efef07e | ||
|
|
064d489b35 | ||
|
|
0837e0aede | ||
|
|
7501a4639f | ||
|
|
95e2fe33af | ||
|
|
8ef58bed01 | ||
|
|
2a72b3df1c | ||
|
|
7bf53fac34 | ||
|
|
d20d9444df | ||
|
|
baa6589e9d | ||
|
|
b9a2d83876 | ||
|
|
98c67bd3b5 | ||
|
|
3a569ca957 | ||
|
|
7b72d56327 | ||
|
|
61b5f7e6f7 | ||
|
|
fdbaeb81fa | ||
|
|
1790c6b40a | ||
|
|
bc7973eb59 | ||
|
|
d2bd9cd497 | ||
|
|
8cd2371e27 | ||
|
|
268a93037b | ||
|
|
85ef03e3b7 | ||
|
|
a87e8bc645 | ||
|
|
b8256a5b1e | ||
|
|
249f515287 | ||
|
|
950e8063e1 | ||
|
|
e432efb74b | ||
|
|
aa54f0d4ac | ||
|
|
50a9fefe01 | ||
|
|
dd97157e54 | ||
|
|
59a2109884 | ||
|
|
f8d8c45220 | ||
|
|
36e6a062d2 | ||
|
|
bb4e628bb4 | ||
|
|
7ed9210de7 | ||
|
|
56205cd9e0 | ||
|
|
d05d4d7f17 | ||
|
|
361ce37796 | ||
|
|
a4d9d1386f | ||
|
|
f45f36e8ff | ||
|
|
ca84e50020 | ||
|
|
35d6db054a | ||
|
|
6fc46f9940 | ||
|
|
894c7c474f | ||
|
|
0122e76bfe | ||
|
|
f34b527ff1 | ||
|
|
84e478bc42 | ||
|
|
56107f5301 | ||
|
|
b0f11fe246 | ||
|
|
49cf0d461a | ||
|
|
cc9d172a22 | ||
|
|
e556d94c70 | ||
|
|
9260cbc9c8 | ||
|
|
be44903ab6 | ||
|
|
7e7049a0bf | ||
|
|
6c509f9650 | ||
|
|
5191f112df | ||
|
|
03cb9e7007 | ||
|
|
507fb40267 | ||
|
|
a13cf6492e | ||
|
|
66ecc5beb4 | ||
|
|
23e603312a | ||
|
|
931126759f | ||
|
|
a18227963d | ||
|
|
a41c4b94b2 | ||
|
|
9c40982c4a | ||
|
|
6336b8e792 | ||
|
|
6c514cc2c0 | ||
|
|
f71fa2c1f9 | ||
|
|
afb705d488 | ||
|
|
a63d8f319e | ||
|
|
b4982b5a6f | ||
|
|
360df52788 | ||
|
|
5d205e6459 | ||
|
|
c44770d972 | ||
|
|
c03226380c | ||
|
|
0a7d3b7d59 | ||
|
|
4dcb74a498 | ||
|
|
596f4aece8 | ||
|
|
5420c705a4 | ||
|
|
f9af4e2668 | ||
|
|
3971569588 | ||
|
|
14ac91fb70 | ||
|
|
8fe064d0bd | ||
|
|
6b03546fc3 | ||
|
|
a9b6a7e7fa | ||
|
|
927380bee2 | ||
|
|
3a406f068f | ||
|
|
d970d5bf10 | ||
|
|
3c82985dcc | ||
|
|
f9c8bfab65 | ||
|
|
1f52105403 | ||
|
|
5d4843d977 | ||
|
|
f508ebb46e | ||
|
|
98c88b23b7 | ||
|
|
ceeb44086c | ||
|
|
1697d0b133 | ||
|
|
52dac67bb4 | ||
|
|
ef24d53d5b | ||
|
|
08b4be84be | ||
|
|
085ce874ea | ||
|
|
18c2496e4c | ||
|
|
c2b640f9cb | ||
|
|
35d73e14d0 | ||
|
|
dd347f86bc | ||
|
|
b0f06dc9f2 | ||
|
|
2345f2efb2 | ||
|
|
3298ddab3c | ||
|
|
526d9b0c3d | ||
|
|
412564fa95 | ||
|
|
621f50e155 | ||
|
|
d17fe994c6 | ||
|
|
2b6a486744 | ||
|
|
fc88f3fa49 | ||
|
|
6a5ae704e5 | ||
|
|
091d224a6c | ||
|
|
08792de8d1 | ||
|
|
bd70bc119d | ||
|
|
0a88f14e07 | ||
|
|
974bb4de56 | ||
|
|
821fb690da | ||
|
|
4d46e7884a | ||
|
|
54611ef08a | ||
|
|
2cbabf90cc | ||
|
|
f5c26753e4 | ||
|
|
aa56da8535 | ||
|
|
5d1ab6d7f3 | ||
|
|
3a4b21ad3f | ||
|
|
c7a7ffb9a7 | ||
|
|
5d69701a83 | ||
|
|
db7346d375 | ||
|
|
372afe4210 | ||
|
|
b13ff9b868 | ||
|
|
3adbd36b39 | ||
|
|
a9b21c267c | ||
|
|
f5894ac96a | ||
|
|
1e48e904a8 | ||
|
|
48c60be15b | ||
|
|
202ec6b8f5 | ||
|
|
c1c8b0a543 | ||
|
|
30609f1b6d | ||
|
|
85db7df311 | ||
|
|
aabb4777dd | ||
|
|
da7c2b3cdb | ||
|
|
84e93fcbb5 | ||
|
|
69b4df5589 | ||
|
|
e7cf495dec | ||
|
|
40cc31b02c | ||
|
|
2fa82e87c6 | ||
|
|
c19f885057 | ||
|
|
c823ddba6f | ||
|
|
77dcefca95 | ||
|
|
8f6152921e | ||
|
|
2aef4c777f | ||
|
|
21350a2362 | ||
|
|
c8a7a4d446 | ||
|
|
ef9755e0a1 | ||
|
|
0d261aaf84 | ||
|
|
879055f991 | ||
|
|
fe7a536108 | ||
|
|
3b8bfd7d0e | ||
|
|
df25ab1ba8 | ||
|
|
8171601b96 | ||
|
|
8fe62d14e0 | ||
|
|
714bb60f15 | ||
|
|
91ee010907 | ||
|
|
829867bbda | ||
|
|
03a65ca357 | ||
|
|
b8d76787ff | ||
|
|
77a394ea41 | ||
|
|
ccecdbda02 | ||
|
|
16bc739ce7 | ||
|
|
083b0d8174 | ||
|
|
60c1d8e12f | ||
|
|
1630eb6c9e | ||
|
|
f4749f2fbb | ||
|
|
56b4a7e506 | ||
|
|
bdcfb09762 | ||
|
|
5b32186590 | ||
|
|
d750167495 | ||
|
|
67e52cbba5 | ||
|
|
39ab2ed85e | ||
|
|
9fc352be79 | ||
|
|
05a5297dbb | ||
|
|
8559bc5936 | ||
|
|
d7932b63a3 | ||
|
|
8a1d0fcbaa | ||
|
|
2006a4d81b | ||
|
|
3191f30600 | ||
|
|
61357d9b36 | ||
|
|
81c6c559e0 | ||
|
|
ccaa20da8d | ||
|
|
cb02c5b460 | ||
|
|
07af54024c | ||
|
|
f9ba5db3f1 | ||
|
|
262c12aba2 | ||
|
|
633151a3df | ||
|
|
1ca4ceba96 | ||
|
|
3e75bdde12 | ||
|
|
e35ba8d463 | ||
|
|
1b4e30e9aa | ||
|
|
ccdf02dfd4 | ||
|
|
cf059f0d24 | ||
|
|
1822f1b60a | ||
|
|
7eb0512c68 | ||
|
|
5a8726e37c | ||
|
|
96df60f75b | ||
|
|
3838410261 | ||
|
|
8c2e6113ec | ||
|
|
332a621dd9 | ||
|
|
25d61d4784 | ||
|
|
10c5e4e166 | ||
|
|
f95941eae9 | ||
|
|
5be200da58 | ||
|
|
8c1183c0c8 | ||
|
|
caca3a9aec | ||
|
|
4742cdf346 | ||
|
|
5758076a55 | ||
|
|
d0d9883648 | ||
|
|
e9689b29b0 | ||
|
|
5dc21c15b2 | ||
|
|
b008acc8b6 | ||
|
|
fe1db5fdb6 | ||
|
|
2468167371 | ||
|
|
8a4333fcff | ||
|
|
e71fd49e70 | ||
|
|
301195efa9 | ||
|
|
8fa80b3606 | ||
|
|
469e4b83f5 | ||
|
|
a7a4670f98 | ||
|
|
de3f6c33c7 | ||
|
|
b84362dd9a | ||
|
|
8041dff63f | ||
|
|
0bedbb96b9 | ||
|
|
41b29e8c8d | ||
|
|
252f04654d | ||
|
|
fabe3d8855 | ||
|
|
172a87e6a8 | ||
|
|
44effd2842 | ||
|
|
00948d0833 | ||
|
|
f48244a805 | ||
|
|
2d040b429d | ||
|
|
371a07eb4c | ||
|
|
a4f7dedfed | ||
|
|
7282c4b849 | ||
|
|
d1fac7f826 |
83 changed files with 1034 additions and 864 deletions
2
.github/FUNDING.yml
vendored
2
.github/FUNDING.yml
vendored
|
|
@ -1,2 +0,0 @@
|
||||||
github: asottile
|
|
||||||
open_collective: pre-commit
|
|
||||||
19
.github/workflows/main.yml
vendored
Normal file
19
.github/workflows/main.yml
vendored
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
name: main
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main, test-me-*]
|
||||||
|
tags: '*'
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
main-windows:
|
||||||
|
uses: asottile/workflows/.github/workflows/tox.yml@v1.8.1
|
||||||
|
with:
|
||||||
|
env: '["py310"]'
|
||||||
|
os: windows-latest
|
||||||
|
main-linux:
|
||||||
|
uses: asottile/workflows/.github/workflows/tox.yml@v1.8.1
|
||||||
|
with:
|
||||||
|
env: '["py310", "py311", "py312", "py313"]'
|
||||||
|
os: ubuntu-latest
|
||||||
5
.gitignore
vendored
5
.gitignore
vendored
|
|
@ -3,9 +3,4 @@
|
||||||
.*.sw[a-z]
|
.*.sw[a-z]
|
||||||
.coverage
|
.coverage
|
||||||
.tox
|
.tox
|
||||||
.venv.touch
|
|
||||||
/.mypy_cache
|
|
||||||
/.pytest_cache
|
|
||||||
/venv*
|
|
||||||
coverage-html
|
|
||||||
dist
|
dist
|
||||||
|
|
|
||||||
|
|
@ -1,46 +1,41 @@
|
||||||
repos:
|
repos:
|
||||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
rev: v4.0.1
|
rev: v6.0.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
- id: end-of-file-fixer
|
- id: end-of-file-fixer
|
||||||
- id: check-docstring-first
|
|
||||||
- id: check-json
|
|
||||||
- id: check-added-large-files
|
|
||||||
- id: check-yaml
|
- id: check-yaml
|
||||||
- id: debug-statements
|
- id: debug-statements
|
||||||
- id: name-tests-test
|
|
||||||
- id: double-quote-string-fixer
|
- id: double-quote-string-fixer
|
||||||
|
- id: name-tests-test
|
||||||
- id: requirements-txt-fixer
|
- id: requirements-txt-fixer
|
||||||
- repo: https://github.com/PyCQA/flake8
|
|
||||||
rev: 3.9.2
|
|
||||||
hooks:
|
|
||||||
- id: flake8
|
|
||||||
additional_dependencies: [flake8-typing-imports==1.7.0]
|
|
||||||
- repo: https://github.com/pre-commit/mirrors-autopep8
|
|
||||||
rev: v1.5.7
|
|
||||||
hooks:
|
|
||||||
- id: autopep8
|
|
||||||
- repo: https://github.com/asottile/reorder_python_imports
|
|
||||||
rev: v2.5.0
|
|
||||||
hooks:
|
|
||||||
- id: reorder-python-imports
|
|
||||||
args: [--py3-plus]
|
|
||||||
- repo: https://github.com/asottile/pyupgrade
|
|
||||||
rev: v2.15.0
|
|
||||||
hooks:
|
|
||||||
- id: pyupgrade
|
|
||||||
args: [--py36-plus]
|
|
||||||
- repo: https://github.com/asottile/add-trailing-comma
|
|
||||||
rev: v2.1.0
|
|
||||||
hooks:
|
|
||||||
- id: add-trailing-comma
|
|
||||||
args: [--py36-plus]
|
|
||||||
- repo: https://github.com/asottile/setup-cfg-fmt
|
- repo: https://github.com/asottile/setup-cfg-fmt
|
||||||
rev: v1.17.0
|
rev: v3.2.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: setup-cfg-fmt
|
- id: setup-cfg-fmt
|
||||||
|
- repo: https://github.com/asottile/reorder-python-imports
|
||||||
|
rev: v3.16.0
|
||||||
|
hooks:
|
||||||
|
- id: reorder-python-imports
|
||||||
|
args: [--py310-plus, --add-import, 'from __future__ import annotations']
|
||||||
|
- repo: https://github.com/asottile/add-trailing-comma
|
||||||
|
rev: v4.0.0
|
||||||
|
hooks:
|
||||||
|
- id: add-trailing-comma
|
||||||
|
- repo: https://github.com/asottile/pyupgrade
|
||||||
|
rev: v3.21.2
|
||||||
|
hooks:
|
||||||
|
- id: pyupgrade
|
||||||
|
args: [--py310-plus]
|
||||||
|
- repo: https://github.com/hhatto/autopep8
|
||||||
|
rev: v2.3.2
|
||||||
|
hooks:
|
||||||
|
- id: autopep8
|
||||||
|
- repo: https://github.com/PyCQA/flake8
|
||||||
|
rev: 7.3.0
|
||||||
|
hooks:
|
||||||
|
- id: flake8
|
||||||
- repo: https://github.com/pre-commit/mirrors-mypy
|
- repo: https://github.com/pre-commit/mirrors-mypy
|
||||||
rev: v0.812
|
rev: v1.19.1
|
||||||
hooks:
|
hooks:
|
||||||
- id: mypy
|
- id: mypy
|
||||||
|
|
|
||||||
|
|
@ -1,193 +1,212 @@
|
||||||
- id: check-added-large-files
|
- id: check-added-large-files
|
||||||
name: Check for added large files
|
name: check for added large files
|
||||||
description: Prevent giant files from being committed
|
description: prevents giant files from being committed.
|
||||||
entry: check-added-large-files
|
entry: check-added-large-files
|
||||||
language: python
|
language: python
|
||||||
|
stages: [pre-commit, pre-push, manual]
|
||||||
|
minimum_pre_commit_version: 3.2.0
|
||||||
- id: check-ast
|
- id: check-ast
|
||||||
name: Check python ast
|
name: check python ast
|
||||||
description: Simply check whether the files parse as valid python.
|
description: simply checks whether the files parse as valid python.
|
||||||
entry: check-ast
|
entry: check-ast
|
||||||
language: python
|
language: python
|
||||||
types: [python]
|
types: [python]
|
||||||
- id: check-byte-order-marker
|
- id: check-byte-order-marker
|
||||||
name: 'check BOM - deprecated: use fix-byte-order-marker'
|
name: check-byte-order-marker (removed)
|
||||||
description: forbid files which have a UTF-8 byte-order marker
|
description: (removed) use fix-byte-order-marker instead.
|
||||||
entry: check-byte-order-marker
|
entry: pre-commit-hooks-removed check-byte-order-marker fix-byte-order-marker https://github.com/pre-commit/pre-commit-hooks
|
||||||
language: python
|
language: python
|
||||||
types: [text]
|
types: [text]
|
||||||
- id: check-builtin-literals
|
- id: check-builtin-literals
|
||||||
name: Check builtin type constructor use
|
name: check builtin type constructor use
|
||||||
description: Require literal syntax when initializing empty or zero Python builtin types.
|
description: requires literal syntax when initializing empty or zero python builtin types.
|
||||||
entry: check-builtin-literals
|
entry: check-builtin-literals
|
||||||
language: python
|
language: python
|
||||||
types: [python]
|
types: [python]
|
||||||
- id: check-case-conflict
|
- id: check-case-conflict
|
||||||
name: Check for case conflicts
|
name: check for case conflicts
|
||||||
description: Check for files that would conflict in case-insensitive filesystems
|
description: checks for files that would conflict in case-insensitive filesystems.
|
||||||
entry: check-case-conflict
|
entry: check-case-conflict
|
||||||
language: python
|
language: python
|
||||||
- id: check-docstring-first
|
- id: check-docstring-first
|
||||||
name: Check docstring is first
|
name: check docstring is first (deprecated)
|
||||||
description: Checks a common error of defining a docstring after code.
|
description: checks a common error of defining a docstring after code.
|
||||||
entry: check-docstring-first
|
entry: check-docstring-first
|
||||||
language: python
|
language: python
|
||||||
types: [python]
|
types: [python]
|
||||||
- id: check-executables-have-shebangs
|
- id: check-executables-have-shebangs
|
||||||
name: Check that executables have shebangs
|
name: check that executables have shebangs
|
||||||
description: Ensures that (non-binary) executables have a shebang.
|
description: ensures that (non-binary) executables have a shebang.
|
||||||
entry: check-executables-have-shebangs
|
entry: check-executables-have-shebangs
|
||||||
language: python
|
language: python
|
||||||
types: [text, executable]
|
types: [text, executable]
|
||||||
stages: [commit, push, manual]
|
stages: [pre-commit, pre-push, manual]
|
||||||
|
minimum_pre_commit_version: 3.2.0
|
||||||
|
- id: check-illegal-windows-names
|
||||||
|
name: check illegal windows names
|
||||||
|
entry: Illegal Windows filenames detected
|
||||||
|
language: fail
|
||||||
|
files: '(?i)((^|/)(CON|PRN|AUX|NUL|COM[\d¹²³]|LPT[\d¹²³])(\.|/|$)|[<>:\"\\|?*\x00-\x1F]|/[^/]*[\.\s]/|[^/]*[\.\s]$)'
|
||||||
- id: check-json
|
- id: check-json
|
||||||
name: Check JSON
|
name: check json
|
||||||
description: This hook checks json files for parseable syntax.
|
description: checks json files for parseable syntax.
|
||||||
entry: check-json
|
entry: check-json
|
||||||
language: python
|
language: python
|
||||||
types: [json]
|
types: [json]
|
||||||
- id: check-shebang-scripts-are-executable
|
- id: check-shebang-scripts-are-executable
|
||||||
name: Check that scripts with shebangs are executable
|
name: check that scripts with shebangs are executable
|
||||||
description: Ensures that (non-binary) files with a shebang are executable.
|
description: ensures that (non-binary) files with a shebang are executable.
|
||||||
entry: check-shebang-scripts-are-executable
|
entry: check-shebang-scripts-are-executable
|
||||||
language: python
|
language: python
|
||||||
types: [text]
|
types: [text]
|
||||||
stages: [commit, push, manual]
|
stages: [pre-commit, pre-push, manual]
|
||||||
|
minimum_pre_commit_version: 3.2.0
|
||||||
- id: pretty-format-json
|
- id: pretty-format-json
|
||||||
name: Pretty format JSON
|
name: pretty format json
|
||||||
description: This hook sets a standard for formatting JSON files.
|
description: sets a standard for formatting json files.
|
||||||
entry: pretty-format-json
|
entry: pretty-format-json
|
||||||
language: python
|
language: python
|
||||||
types: [json]
|
types: [json]
|
||||||
- id: check-merge-conflict
|
- id: check-merge-conflict
|
||||||
name: Check for merge conflicts
|
name: check for merge conflicts
|
||||||
description: Check for files that contain merge conflict strings.
|
description: checks for files that contain merge conflict strings.
|
||||||
entry: check-merge-conflict
|
entry: check-merge-conflict
|
||||||
language: python
|
language: python
|
||||||
types: [text]
|
types: [text]
|
||||||
- id: check-symlinks
|
- id: check-symlinks
|
||||||
name: Check for broken symlinks
|
name: check for broken symlinks
|
||||||
description: Checks for symlinks which do not point to anything.
|
description: checks for symlinks which do not point to anything.
|
||||||
entry: check-symlinks
|
entry: check-symlinks
|
||||||
language: python
|
language: python
|
||||||
types: [symlink]
|
types: [symlink]
|
||||||
- id: check-toml
|
- id: check-toml
|
||||||
name: Check Toml
|
name: check toml
|
||||||
description: This hook checks toml files for parseable syntax.
|
description: checks toml files for parseable syntax.
|
||||||
entry: check-toml
|
entry: check-toml
|
||||||
language: python
|
language: python
|
||||||
types: [toml]
|
types: [toml]
|
||||||
- id: check-vcs-permalinks
|
- id: check-vcs-permalinks
|
||||||
name: Check vcs permalinks
|
name: check vcs permalinks
|
||||||
description: Ensures that links to vcs websites are permalinks.
|
description: ensures that links to vcs websites are permalinks.
|
||||||
entry: check-vcs-permalinks
|
entry: check-vcs-permalinks
|
||||||
language: python
|
language: python
|
||||||
types: [text]
|
types: [text]
|
||||||
- id: check-xml
|
- id: check-xml
|
||||||
name: Check Xml
|
name: check xml
|
||||||
description: This hook checks xml files for parseable syntax.
|
description: checks xml files for parseable syntax.
|
||||||
entry: check-xml
|
entry: check-xml
|
||||||
language: python
|
language: python
|
||||||
types: [xml]
|
types: [xml]
|
||||||
- id: check-yaml
|
- id: check-yaml
|
||||||
name: Check Yaml
|
name: check yaml
|
||||||
description: This hook checks yaml files for parseable syntax.
|
description: checks yaml files for parseable syntax.
|
||||||
entry: check-yaml
|
entry: check-yaml
|
||||||
language: python
|
language: python
|
||||||
types: [yaml]
|
types: [yaml]
|
||||||
- id: debug-statements
|
- id: debug-statements
|
||||||
name: Debug Statements (Python)
|
name: debug statements (python)
|
||||||
description: Check for debugger imports and py37+ `breakpoint()` calls in python source.
|
description: checks for debugger imports and py37+ `breakpoint()` calls in python source.
|
||||||
entry: debug-statement-hook
|
entry: debug-statement-hook
|
||||||
language: python
|
language: python
|
||||||
types: [python]
|
types: [python]
|
||||||
- id: destroyed-symlinks
|
- id: destroyed-symlinks
|
||||||
name: Detect Destroyed Symlinks
|
name: detect destroyed symlinks
|
||||||
description: Detects symlinks which are changed to regular files with a content of a path which that symlink was pointing to.
|
description: detects symlinks which are changed to regular files with a content of a path which that symlink was pointing to.
|
||||||
entry: destroyed-symlinks
|
entry: destroyed-symlinks
|
||||||
language: python
|
language: python
|
||||||
types: [file]
|
types: [file]
|
||||||
|
stages: [pre-commit, pre-push, manual]
|
||||||
- id: detect-aws-credentials
|
- id: detect-aws-credentials
|
||||||
name: Detect AWS Credentials
|
name: detect aws credentials
|
||||||
description: Detects *your* aws credentials from the aws cli credentials file
|
description: detects *your* aws credentials from the aws cli credentials file.
|
||||||
entry: detect-aws-credentials
|
entry: detect-aws-credentials
|
||||||
language: python
|
language: python
|
||||||
types: [text]
|
types: [text]
|
||||||
- id: detect-private-key
|
- id: detect-private-key
|
||||||
name: Detect Private Key
|
name: detect private key
|
||||||
description: Detects the presence of private keys
|
description: detects the presence of private keys.
|
||||||
entry: detect-private-key
|
entry: detect-private-key
|
||||||
language: python
|
language: python
|
||||||
types: [text]
|
types: [text]
|
||||||
- id: double-quote-string-fixer
|
- id: double-quote-string-fixer
|
||||||
name: Fix double quoted strings
|
name: fix double quoted strings
|
||||||
description: This hook replaces double quoted strings with single quoted strings
|
description: replaces double quoted strings with single quoted strings.
|
||||||
entry: double-quote-string-fixer
|
entry: double-quote-string-fixer
|
||||||
language: python
|
language: python
|
||||||
types: [python]
|
types: [python]
|
||||||
- id: end-of-file-fixer
|
- id: end-of-file-fixer
|
||||||
name: Fix End of Files
|
name: fix end of files
|
||||||
description: Ensures that a file is either empty, or ends with one newline.
|
description: ensures that a file is either empty, or ends with one newline.
|
||||||
entry: end-of-file-fixer
|
entry: end-of-file-fixer
|
||||||
language: python
|
language: python
|
||||||
types: [text]
|
types: [text]
|
||||||
stages: [commit, push, manual]
|
stages: [pre-commit, pre-push, manual]
|
||||||
|
minimum_pre_commit_version: 3.2.0
|
||||||
- id: file-contents-sorter
|
- id: file-contents-sorter
|
||||||
name: File Contents Sorter
|
name: file contents sorter
|
||||||
description: Sort the lines in specified files (defaults to alphabetical). You must provide list of target files as input in your .pre-commit-config.yaml file.
|
description: sorts the lines in specified files (defaults to alphabetical). you must provide list of target files as input in your .pre-commit-config.yaml file.
|
||||||
entry: file-contents-sorter
|
entry: file-contents-sorter
|
||||||
language: python
|
language: python
|
||||||
files: '^$'
|
files: '^$'
|
||||||
- id: fix-byte-order-marker
|
- id: fix-byte-order-marker
|
||||||
name: fix UTF-8 byte order marker
|
name: fix utf-8 byte order marker
|
||||||
description: removes UTF-8 byte order marker
|
description: removes utf-8 byte order marker.
|
||||||
entry: fix-byte-order-marker
|
entry: fix-byte-order-marker
|
||||||
language: python
|
language: python
|
||||||
types: [text]
|
types: [text]
|
||||||
- id: fix-encoding-pragma
|
- id: fix-encoding-pragma
|
||||||
name: Fix python encoding pragma
|
name: fix python encoding pragma (removed)
|
||||||
|
description: (removed) use pyupgrade instead.
|
||||||
|
entry: pre-commit-hooks-removed fix-encoding-pragma pyupgrade https://github.com/asottile/pyupgrade
|
||||||
language: python
|
language: python
|
||||||
entry: fix-encoding-pragma
|
|
||||||
description: 'Add # -*- coding: utf-8 -*- to the top of python files'
|
|
||||||
types: [python]
|
types: [python]
|
||||||
- id: forbid-new-submodules
|
- id: forbid-new-submodules
|
||||||
name: Forbid new submodules
|
name: forbid new submodules
|
||||||
|
description: prevents addition of new git submodules.
|
||||||
language: python
|
language: python
|
||||||
entry: forbid-new-submodules
|
entry: forbid-new-submodules
|
||||||
description: Prevent addition of new git submodules
|
types: [directory]
|
||||||
|
- id: forbid-submodules
|
||||||
|
name: forbid submodules
|
||||||
|
description: forbids any submodules in the repository
|
||||||
|
language: fail
|
||||||
|
entry: 'submodules are not allowed in this repository:'
|
||||||
|
types: [directory]
|
||||||
- id: mixed-line-ending
|
- id: mixed-line-ending
|
||||||
name: Mixed line ending
|
name: mixed line ending
|
||||||
description: Replaces or checks mixed line ending
|
description: replaces or checks mixed line ending.
|
||||||
entry: mixed-line-ending
|
entry: mixed-line-ending
|
||||||
language: python
|
language: python
|
||||||
types: [text]
|
types: [text]
|
||||||
- id: name-tests-test
|
- id: name-tests-test
|
||||||
name: Tests should end in _test.py
|
name: python tests naming
|
||||||
description: This verifies that test files are named correctly
|
description: verifies that test files are named correctly.
|
||||||
entry: name-tests-test
|
entry: name-tests-test
|
||||||
language: python
|
language: python
|
||||||
files: (^|/)tests/.+\.py$
|
files: (^|/)tests/.+\.py$
|
||||||
- id: no-commit-to-branch
|
- id: no-commit-to-branch
|
||||||
name: "Don't commit to branch"
|
name: "don't commit to branch"
|
||||||
entry: no-commit-to-branch
|
entry: no-commit-to-branch
|
||||||
language: python
|
language: python
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
always_run: true
|
always_run: true
|
||||||
- id: requirements-txt-fixer
|
- id: requirements-txt-fixer
|
||||||
name: Fix requirements.txt
|
name: fix requirements.txt
|
||||||
description: Sorts entries in requirements.txt
|
description: sorts entries in requirements.txt.
|
||||||
entry: requirements-txt-fixer
|
entry: requirements-txt-fixer
|
||||||
language: python
|
language: python
|
||||||
files: requirements.*\.txt$
|
files: (requirements|constraints).*\.txt$
|
||||||
- id: sort-simple-yaml
|
- id: sort-simple-yaml
|
||||||
name: Sort simple YAML files
|
name: sort simple yaml files
|
||||||
|
description: sorts simple yaml files which consist only of top-level keys, preserving comments and blocks.
|
||||||
language: python
|
language: python
|
||||||
entry: sort-simple-yaml
|
entry: sort-simple-yaml
|
||||||
description: Sorts simple YAML files which consist only of top-level keys, preserving comments and blocks.
|
|
||||||
files: '^$'
|
files: '^$'
|
||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
name: Trim Trailing Whitespace
|
name: trim trailing whitespace
|
||||||
description: This hook trims trailing whitespace.
|
description: trims trailing whitespace.
|
||||||
entry: trailing-whitespace-fixer
|
entry: trailing-whitespace-fixer
|
||||||
language: python
|
language: python
|
||||||
types: [text]
|
types: [text]
|
||||||
stages: [commit, push, manual]
|
stages: [pre-commit, pre-push, manual]
|
||||||
|
minimum_pre_commit_version: 3.2.0
|
||||||
|
|
|
||||||
166
CHANGELOG.md
166
CHANGELOG.md
|
|
@ -1,3 +1,169 @@
|
||||||
|
6.0.0 - 2025-08-09
|
||||||
|
==================
|
||||||
|
|
||||||
|
## Fixes
|
||||||
|
- `check-shebang-scripts-are-executable`: improve error message.
|
||||||
|
- #1115 PR by @homebysix.
|
||||||
|
|
||||||
|
## Migrating
|
||||||
|
- now requires python >= 3.9.
|
||||||
|
- #1098 PR by @asottile.
|
||||||
|
- `file-contents-sorter`: disallow `--unique` and `--ignore-case` at the same
|
||||||
|
time.
|
||||||
|
- #1095 PR by @nemacysts.
|
||||||
|
- #794 issue by @teksturi.
|
||||||
|
- Removed `check-byte-order-marker` and `fix-encoding-pragma`.
|
||||||
|
- `check-byte-order-marker`: migrate to `fix-byte-order-marker`.
|
||||||
|
- `fix-encoding-pragma`: migrate to `pyupgrade`.
|
||||||
|
- #1034 PR by @mxr.
|
||||||
|
- #1032 issue by @mxr.
|
||||||
|
- #522 PR by @jgowdy.
|
||||||
|
|
||||||
|
5.0.0 - 2024-10-05
|
||||||
|
==================
|
||||||
|
|
||||||
|
### Features
|
||||||
|
- `requirements-txt-fixer`: also remove `pkg_resources==...`.
|
||||||
|
- #850 PR by @ericfrederich.
|
||||||
|
- #1030 issue by @ericfrederich.
|
||||||
|
- `check-illegal-windows-names`: new hook!
|
||||||
|
- #1044 PR by @ericfrederich.
|
||||||
|
- #589 issue by @ericfrederich.
|
||||||
|
- #1049 PR by @Jeffrey-Lim.
|
||||||
|
- `pretty-format-json`: continue processing even if a file has a json error.
|
||||||
|
- #1039 PR by @amarvin.
|
||||||
|
- #1038 issue by @amarvin.
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- `destroyed-symlinks`: set `stages` to `[pre-commit, pre-push, manual]`
|
||||||
|
- PR #1085 by @AdrianDC.
|
||||||
|
|
||||||
|
### Migrating
|
||||||
|
- pre-commit-hooks now requires `pre-commit>=3.2.0`.
|
||||||
|
- use non-deprecated names for `stages`.
|
||||||
|
- #1093 PR by @asottile.
|
||||||
|
|
||||||
|
4.6.0 - 2024-04-06
|
||||||
|
==================
|
||||||
|
|
||||||
|
### Features
|
||||||
|
- `requirements-txt-fixer`: remove duplicate packages.
|
||||||
|
- #1014 PR by @vhoulbreque-withings.
|
||||||
|
- #960 issue @csibe17.
|
||||||
|
|
||||||
|
### Migrating
|
||||||
|
- `fix-encoding-pragma`: deprecated -- will be removed in 5.0.0. use
|
||||||
|
[pyupgrade](https://github.com/asottile/pyupgrade) or some other tool.
|
||||||
|
- #1033 PR by @mxr.
|
||||||
|
- #1032 issue by @mxr.
|
||||||
|
|
||||||
|
4.5.0 - 2023-10-07
|
||||||
|
==================
|
||||||
|
|
||||||
|
### Features
|
||||||
|
- `requirements-txt-fixer`: also sort `constraints.txt` by default.
|
||||||
|
- #857 PR by @lev-blit.
|
||||||
|
- #830 issue by @PLPeeters.
|
||||||
|
- `debug-statements`: add `bpdb` debugger.
|
||||||
|
- #942 PR by @mwip.
|
||||||
|
- #941 issue by @mwip.
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- `file-contents-sorter`: fix sorting an empty file.
|
||||||
|
- #944 PR by @RoelAdriaans.
|
||||||
|
- #935 issue by @paduszyk.
|
||||||
|
- `double-quote-string-fixer`: don't rewrite inside f-strings in 3.12+.
|
||||||
|
- #973 PR by @asottile.
|
||||||
|
- #971 issue by @XuehaiPan.
|
||||||
|
|
||||||
|
## Migrating
|
||||||
|
- now requires python >= 3.8.
|
||||||
|
- #926 PR by @asottile.
|
||||||
|
- #927 PR by @asottile.
|
||||||
|
|
||||||
|
4.4.0 - 2022-11-23
|
||||||
|
==================
|
||||||
|
|
||||||
|
### Features
|
||||||
|
- `forbid-submodules`: new hook which outright bans submodules.
|
||||||
|
- #815 PR by @asottile.
|
||||||
|
- #707 issue by @ChiefGokhlayeh.
|
||||||
|
|
||||||
|
4.3.0 - 2022-06-07
|
||||||
|
==================
|
||||||
|
|
||||||
|
### Features
|
||||||
|
- `check-executables-have-shebangs`: use `git config core.fileMode` to
|
||||||
|
determine if it should query `git`.
|
||||||
|
- #730 PR by @Kurt-von-Laven.
|
||||||
|
- `name-tests-test`: add `--pytest-test-first` test convention.
|
||||||
|
- #779 PR by @asottile.
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- `check-shebang-scripts-are-executable`: update windows instructions.
|
||||||
|
- #774 PR by @mdeweerd.
|
||||||
|
- #770 issue by @mdeweerd.
|
||||||
|
- `check-toml`: use stdlib `tomllib` when available.
|
||||||
|
- #771 PR by @DanielNoord.
|
||||||
|
- #755 issue by @sognetic.
|
||||||
|
- `check-added-large-files`: don't run on non-file `stages`.
|
||||||
|
- #778 PR by @asottile.
|
||||||
|
- #777 issue by @skyj.
|
||||||
|
|
||||||
|
4.2.0 - 2022-04-06
|
||||||
|
==================
|
||||||
|
|
||||||
|
### Features
|
||||||
|
- `name-tests-test`: updated display text.
|
||||||
|
- #713 PR by @asottile.
|
||||||
|
- `check-docstring-first`: make output more parsable.
|
||||||
|
- #748 PR by @asottile.
|
||||||
|
- `check-merge-conflict`: make output more parsable.
|
||||||
|
- #748 PR by @asottile.
|
||||||
|
- `debug-statements`: make output more parsable.
|
||||||
|
- #748 PR by @asottile.
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- `check-merge-conflict`: fix detection of `======` conflict marker on windows.
|
||||||
|
- #748 PR by @asottile.
|
||||||
|
|
||||||
|
### Updating
|
||||||
|
- Drop python<3.7.
|
||||||
|
- #719 PR by @asottile.
|
||||||
|
- Changed default branch from `master` to `main`.
|
||||||
|
- #744 PR by @asottile.
|
||||||
|
|
||||||
|
4.1.0 - 2021-12-22
|
||||||
|
==================
|
||||||
|
|
||||||
|
### Features
|
||||||
|
- `debug-statements`: add `pdbr` debugger.
|
||||||
|
- #614 PR by @cansarigol.
|
||||||
|
- `detect-private-key`: add detection for additional key types.
|
||||||
|
- #658 PR by @ljmf00.
|
||||||
|
- `check-executables-have-shebangs`: improve messaging on windows.
|
||||||
|
- #689 PR by @pujitm.
|
||||||
|
- #686 issue by @jmerdich.
|
||||||
|
- `check-added-large-files`: support `--enforce-all` with `git-lfs`.
|
||||||
|
- #674 PR by @amartani.
|
||||||
|
- #560 issue by @jeremy-coulon.
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- `check-case-conflict`: improve performance.
|
||||||
|
- #626 PR by @guykisel.
|
||||||
|
- #625 issue by @guykisel.
|
||||||
|
- `forbid-new-submodules`: fix false-negatives for `pre-push`.
|
||||||
|
- #619 PR by @m-khvoinitsky.
|
||||||
|
- #609 issue by @m-khvoinitsky.
|
||||||
|
- `check-merge-conflict`: fix execution in git worktrees.
|
||||||
|
- #662 PR by @errsyn.
|
||||||
|
- #638 issue by @daschuer.
|
||||||
|
|
||||||
|
### Misc.
|
||||||
|
- Normalize case of hook names and descriptions.
|
||||||
|
- #671 PR by @dennisroche.
|
||||||
|
- #673 PR by @revolter.
|
||||||
|
|
||||||
4.0.1 - 2021-05-16
|
4.0.1 - 2021-05-16
|
||||||
==================
|
==================
|
||||||
|
|
||||||
|
|
|
||||||
54
README.md
54
README.md
|
|
@ -1,6 +1,5 @@
|
||||||
[](https://asottile.visualstudio.com/asottile/_build/latest?definitionId=17&branchName=master)
|
[](https://github.com/pre-commit/pre-commit-hooks/actions/workflows/main.yml)
|
||||||
[](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=17&branchName=master)
|
[](https://results.pre-commit.ci/latest/github/pre-commit/pre-commit-hooks/main)
|
||||||
[](https://results.pre-commit.ci/latest/github/pre-commit/pre-commit-hooks/master)
|
|
||||||
|
|
||||||
pre-commit-hooks
|
pre-commit-hooks
|
||||||
================
|
================
|
||||||
|
|
@ -16,7 +15,7 @@ Add this to your `.pre-commit-config.yaml`
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
rev: v4.0.1 # Use the ref you want to point at
|
rev: v6.0.0 # Use the ref you want to point at
|
||||||
hooks:
|
hooks:
|
||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
# - id: ...
|
# - id: ...
|
||||||
|
|
@ -46,17 +45,18 @@ Require literal syntax when initializing empty or zero Python builtin types.
|
||||||
#### `check-case-conflict`
|
#### `check-case-conflict`
|
||||||
Check for files with names that would conflict on a case-insensitive filesystem like MacOS HFS+ or Windows FAT.
|
Check for files with names that would conflict on a case-insensitive filesystem like MacOS HFS+ or Windows FAT.
|
||||||
|
|
||||||
#### `check-docstring-first`
|
|
||||||
Checks for a common error of placing code before the docstring.
|
|
||||||
|
|
||||||
#### `check-executables-have-shebangs`
|
#### `check-executables-have-shebangs`
|
||||||
Checks that non-binary executables have a proper shebang.
|
Checks that non-binary executables have a proper shebang.
|
||||||
|
|
||||||
|
#### `check-illegal-windows-names`
|
||||||
|
Check for files that cannot be created on Windows.
|
||||||
|
|
||||||
#### `check-json`
|
#### `check-json`
|
||||||
Attempts to load all json files to verify syntax.
|
Attempts to load all json files to verify syntax.
|
||||||
|
|
||||||
#### `check-merge-conflict`
|
#### `check-merge-conflict`
|
||||||
Check for files that contain merge conflict strings.
|
Check for files that contain merge conflict strings.
|
||||||
|
- `--assume-in-merge` - Allows running the hook when there is no ongoing merge operation
|
||||||
|
|
||||||
#### `check-shebang-scripts-are-executable`
|
#### `check-shebang-scripts-are-executable`
|
||||||
Checks that scripts with shebangs are executable.
|
Checks that scripts with shebangs are executable.
|
||||||
|
|
@ -113,21 +113,28 @@ This hook replaces double quoted strings with single quoted strings.
|
||||||
#### `end-of-file-fixer`
|
#### `end-of-file-fixer`
|
||||||
Makes sure files end in a newline and only a newline.
|
Makes sure files end in a newline and only a newline.
|
||||||
|
|
||||||
|
#### `file-contents-sorter`
|
||||||
|
Sort the lines in specified files (defaults to alphabetical).
|
||||||
|
You must provide the target [`files`](https://pre-commit.com/#config-files) as input.
|
||||||
|
Note that this hook WILL remove blank lines and does NOT respect any comments.
|
||||||
|
All newlines will be converted to line feeds (`\n`).
|
||||||
|
|
||||||
|
The following arguments are available:
|
||||||
|
- `--ignore-case` - fold lower case to upper case characters.
|
||||||
|
- `--unique` - ensure each line is unique.
|
||||||
|
|
||||||
#### `fix-byte-order-marker`
|
#### `fix-byte-order-marker`
|
||||||
removes UTF-8 byte order marker
|
removes UTF-8 byte order marker
|
||||||
|
|
||||||
#### `fix-encoding-pragma`
|
|
||||||
Add `# -*- coding: utf-8 -*-` to the top of python files.
|
|
||||||
- To remove the coding pragma pass `--remove` (useful in a python3-only codebase)
|
|
||||||
|
|
||||||
#### `file-contents-sorter`
|
|
||||||
Sort the lines in specified files (defaults to alphabetical).
|
|
||||||
You must provide list of target files as input to it.
|
|
||||||
Note that this hook WILL remove blank lines and does NOT respect any comments.
|
|
||||||
|
|
||||||
#### `forbid-new-submodules`
|
#### `forbid-new-submodules`
|
||||||
Prevent addition of new git submodules.
|
Prevent addition of new git submodules.
|
||||||
|
|
||||||
|
This is intended as a helper to migrate away from submodules. If you want to
|
||||||
|
ban them entirely use `forbid-submodules`
|
||||||
|
|
||||||
|
#### `forbid-submodules`
|
||||||
|
forbids any submodules in the repository.
|
||||||
|
|
||||||
#### `mixed-line-ending`
|
#### `mixed-line-ending`
|
||||||
Replaces or checks mixed line ending.
|
Replaces or checks mixed line ending.
|
||||||
- `--fix={auto,crlf,lf,no}`
|
- `--fix={auto,crlf,lf,no}`
|
||||||
|
|
@ -137,13 +144,15 @@ Replaces or checks mixed line ending.
|
||||||
- `no` - Checks if there is any mixed line ending without modifying any file.
|
- `no` - Checks if there is any mixed line ending without modifying any file.
|
||||||
|
|
||||||
#### `name-tests-test`
|
#### `name-tests-test`
|
||||||
Assert that files in tests/ end in `_test.py`.
|
verifies that test files are named correctly.
|
||||||
- Use `args: ['--django']` to match `test*.py` instead.
|
- `--pytest` (the default): ensure tests match `.*_test\.py`
|
||||||
|
- `--pytest-test-first`: ensure tests match `test_.*\.py`
|
||||||
|
- `--django` / `--unittest`: ensure tests match `test.*\.py`
|
||||||
|
|
||||||
#### `no-commit-to-branch`
|
#### `no-commit-to-branch`
|
||||||
Protect specific branches from direct checkins.
|
Protect specific branches from direct checkins.
|
||||||
- Use `args: [--branch, staging, --branch, master]` to set the branch.
|
- Use `args: [--branch, staging, --branch, main]` to set the branch.
|
||||||
Both `master` and `main` are protected by default if no branch argument is set.
|
Both `main` and `master` are protected by default if no branch argument is set.
|
||||||
- `-b` / `--branch` may be specified multiple times to protect multiple
|
- `-b` / `--branch` may be specified multiple times to protect multiple
|
||||||
branches.
|
branches.
|
||||||
- `-p` / `--pattern` can be used to protect branches that match a supplied regex
|
- `-p` / `--pattern` can be used to protect branches that match a supplied regex
|
||||||
|
|
@ -167,7 +176,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 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`
|
||||||
|
|
||||||
#### `sort-simple-yaml`
|
#### `sort-simple-yaml`
|
||||||
Sorts simple YAML files which consist only of top-level
|
Sorts simple YAML files which consist only of top-level
|
||||||
|
|
@ -194,6 +203,9 @@ Trims trailing whitespace.
|
||||||
### Deprecated / replaced hooks
|
### Deprecated / replaced hooks
|
||||||
|
|
||||||
- `check-byte-order-marker`: instead use fix-byte-order-marker
|
- `check-byte-order-marker`: instead use fix-byte-order-marker
|
||||||
|
- `fix-encoding-pragma`: instead use [`pyupgrade`](https://github.com/asottile/pyupgrade)
|
||||||
|
- `check-docstring-first`: fundamentally flawed, deprecated without replacement.
|
||||||
|
|
||||||
|
|
||||||
### As a standalone package
|
### As a standalone package
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
trigger:
|
|
||||||
branches:
|
|
||||||
include: [master, test-me-*]
|
|
||||||
tags:
|
|
||||||
include: ['*']
|
|
||||||
|
|
||||||
resources:
|
|
||||||
repositories:
|
|
||||||
- repository: asottile
|
|
||||||
type: github
|
|
||||||
endpoint: github
|
|
||||||
name: asottile/azure-pipeline-templates
|
|
||||||
ref: refs/tags/v2.1.0
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
- template: job--python-tox.yml@asottile
|
|
||||||
parameters:
|
|
||||||
toxenvs: [py38]
|
|
||||||
os: windows
|
|
||||||
- template: job--python-tox.yml@asottile
|
|
||||||
parameters:
|
|
||||||
toxenvs: [pypy3, py36, py37, py38]
|
|
||||||
os: linux
|
|
||||||
|
|
@ -1,24 +1,33 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
|
||||||
import math
|
import math
|
||||||
import os
|
import os
|
||||||
from typing import Optional
|
import subprocess
|
||||||
from typing import Sequence
|
from collections.abc import Sequence
|
||||||
from typing import Set
|
|
||||||
|
|
||||||
from pre_commit_hooks.util import added_files
|
from pre_commit_hooks.util import added_files
|
||||||
from pre_commit_hooks.util import CalledProcessError
|
from pre_commit_hooks.util import zsplit
|
||||||
from pre_commit_hooks.util import cmd_output
|
|
||||||
|
|
||||||
|
|
||||||
def lfs_files() -> Set[str]:
|
def filter_lfs_files(filenames: set[str]) -> None: # pragma: no cover (lfs)
|
||||||
try:
|
"""Remove files tracked by git-lfs from the set."""
|
||||||
# Introduced in git-lfs 2.2.0, first working in 2.2.1
|
if not filenames:
|
||||||
lfs_ret = cmd_output('git', 'lfs', 'status', '--json')
|
return
|
||||||
except CalledProcessError: # pragma: no cover (with git-lfs)
|
|
||||||
lfs_ret = '{"files":{}}'
|
|
||||||
|
|
||||||
return set(json.loads(lfs_ret)['files'])
|
check_attr = subprocess.run(
|
||||||
|
('git', 'check-attr', 'filter', '-z', '--stdin'),
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.DEVNULL,
|
||||||
|
encoding='utf-8',
|
||||||
|
check=True,
|
||||||
|
input='\0'.join(filenames),
|
||||||
|
)
|
||||||
|
stdout = zsplit(check_attr.stdout)
|
||||||
|
for i in range(0, len(stdout), 3):
|
||||||
|
filename, filter_tag = stdout[i], stdout[i + 2]
|
||||||
|
if filter_tag == 'lfs':
|
||||||
|
filenames.remove(filename)
|
||||||
|
|
||||||
|
|
||||||
def find_large_added_files(
|
def find_large_added_files(
|
||||||
|
|
@ -30,12 +39,14 @@ def find_large_added_files(
|
||||||
# Find all added files that are also in the list of files pre-commit tells
|
# Find all added files that are also in the list of files pre-commit tells
|
||||||
# us about
|
# us about
|
||||||
retv = 0
|
retv = 0
|
||||||
filenames_filtered = set(filenames) - lfs_files()
|
filenames_filtered = set(filenames)
|
||||||
|
filter_lfs_files(filenames_filtered)
|
||||||
|
|
||||||
if not enforce_all:
|
if not enforce_all:
|
||||||
filenames_filtered &= added_files()
|
filenames_filtered &= added_files()
|
||||||
|
|
||||||
for filename in filenames_filtered:
|
for filename in filenames_filtered:
|
||||||
kb = int(math.ceil(os.stat(filename).st_size / 1024))
|
kb = math.ceil(os.stat(filename).st_size / 1024)
|
||||||
if kb > maxkb:
|
if kb > maxkb:
|
||||||
print(f'{filename} ({kb} KB) exceeds {maxkb} KB.')
|
print(f'{filename} ({kb} KB) exceeds {maxkb} KB.')
|
||||||
retv = 1
|
retv = 1
|
||||||
|
|
@ -43,7 +54,7 @@ def find_large_added_files(
|
||||||
return retv
|
return retv
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'filenames', nargs='*',
|
'filenames', nargs='*',
|
||||||
|
|
@ -55,7 +66,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--maxkb', type=int, default=500,
|
'--maxkb', type=int, default=500,
|
||||||
help='Maxmimum allowable KB for added files',
|
help='Maximum allowable KB for added files',
|
||||||
)
|
)
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
|
|
@ -67,4 +78,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,14 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import ast
|
import ast
|
||||||
import platform
|
import platform
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
from typing import Optional
|
from collections.abc import Sequence
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*')
|
parser.add_argument('filenames', nargs='*')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
@ -29,4 +30,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import ast
|
import ast
|
||||||
from typing import List
|
from collections.abc import Sequence
|
||||||
from typing import NamedTuple
|
from typing import NamedTuple
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
from typing import Set
|
|
||||||
|
|
||||||
|
|
||||||
BUILTIN_TYPES = {
|
BUILTIN_TYPES = {
|
||||||
|
|
@ -27,38 +26,39 @@ class Call(NamedTuple):
|
||||||
class Visitor(ast.NodeVisitor):
|
class Visitor(ast.NodeVisitor):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
ignore: Optional[Sequence[str]] = None,
|
ignore: set[str],
|
||||||
allow_dict_kwargs: bool = True,
|
allow_dict_kwargs: bool = True,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.builtin_type_calls: List[Call] = []
|
self.builtin_type_calls: list[Call] = []
|
||||||
self.ignore = set(ignore) if ignore else set()
|
|
||||||
self.allow_dict_kwargs = allow_dict_kwargs
|
self.allow_dict_kwargs = allow_dict_kwargs
|
||||||
|
self._disallowed = BUILTIN_TYPES.keys() - ignore
|
||||||
|
|
||||||
def _check_dict_call(self, node: ast.Call) -> bool:
|
def _check_dict_call(self, node: ast.Call) -> bool:
|
||||||
return self.allow_dict_kwargs and bool(node.keywords)
|
return self.allow_dict_kwargs and bool(node.keywords)
|
||||||
|
|
||||||
def visit_Call(self, node: ast.Call) -> None:
|
def visit_Call(self, node: ast.Call) -> None:
|
||||||
if not isinstance(node.func, ast.Name):
|
if (
|
||||||
# Ignore functions that are object attributes (`foo.bar()`).
|
# Ignore functions that are object attributes (`foo.bar()`).
|
||||||
# Assume that if the user calls `builtins.list()`, they know what
|
# Assume that if the user calls `builtins.list()`, they know what
|
||||||
# they're doing.
|
# they're doing.
|
||||||
return
|
isinstance(node.func, ast.Name) and
|
||||||
if node.func.id not in set(BUILTIN_TYPES).difference(self.ignore):
|
node.func.id in self._disallowed and
|
||||||
return
|
(node.func.id != 'dict' or not self._check_dict_call(node)) and
|
||||||
if node.func.id == 'dict' and self._check_dict_call(node):
|
not node.args
|
||||||
return
|
):
|
||||||
elif node.args:
|
self.builtin_type_calls.append(
|
||||||
return
|
Call(node.func.id, node.lineno, node.col_offset),
|
||||||
self.builtin_type_calls.append(
|
)
|
||||||
Call(node.func.id, node.lineno, node.col_offset),
|
|
||||||
)
|
self.generic_visit(node)
|
||||||
|
|
||||||
|
|
||||||
def check_file(
|
def check_file(
|
||||||
filename: str,
|
filename: str,
|
||||||
ignore: Optional[Sequence[str]] = None,
|
*,
|
||||||
|
ignore: set[str],
|
||||||
allow_dict_kwargs: bool = True,
|
allow_dict_kwargs: bool = True,
|
||||||
) -> List[Call]:
|
) -> list[Call]:
|
||||||
with open(filename, 'rb') as f:
|
with open(filename, 'rb') as f:
|
||||||
tree = ast.parse(f.read(), filename=filename)
|
tree = ast.parse(f.read(), filename=filename)
|
||||||
visitor = Visitor(ignore=ignore, allow_dict_kwargs=allow_dict_kwargs)
|
visitor = Visitor(ignore=ignore, allow_dict_kwargs=allow_dict_kwargs)
|
||||||
|
|
@ -66,11 +66,11 @@ def check_file(
|
||||||
return visitor.builtin_type_calls
|
return visitor.builtin_type_calls
|
||||||
|
|
||||||
|
|
||||||
def parse_ignore(value: str) -> Set[str]:
|
def parse_ignore(value: str) -> set[str]:
|
||||||
return set(value.split(','))
|
return set(value.split(','))
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*')
|
parser.add_argument('filenames', nargs='*')
|
||||||
parser.add_argument('--ignore', type=parse_ignore, default=set())
|
parser.add_argument('--ignore', type=parse_ignore, default=set())
|
||||||
|
|
@ -103,4 +103,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
import argparse
|
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
|
||||||
parser = argparse.ArgumentParser()
|
|
||||||
parser.add_argument('filenames', nargs='*', help='Filenames to check')
|
|
||||||
args = parser.parse_args(argv)
|
|
||||||
|
|
||||||
retv = 0
|
|
||||||
|
|
||||||
for filename in args.filenames:
|
|
||||||
with open(filename, 'rb') as f:
|
|
||||||
if f.read(3) == b'\xef\xbb\xbf':
|
|
||||||
retv = 1
|
|
||||||
print(f'{filename}: Has a byte-order marker')
|
|
||||||
|
|
||||||
return retv
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
exit(main())
|
|
||||||
|
|
@ -1,27 +1,27 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import os.path
|
from collections.abc import Iterable
|
||||||
from typing import Iterable
|
from collections.abc import Iterator
|
||||||
from typing import Iterator
|
from collections.abc import Sequence
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
from typing import Set
|
|
||||||
|
|
||||||
from pre_commit_hooks.util import added_files
|
from pre_commit_hooks.util import added_files
|
||||||
from pre_commit_hooks.util import cmd_output
|
from pre_commit_hooks.util import cmd_output
|
||||||
|
|
||||||
|
|
||||||
def lower_set(iterable: Iterable[str]) -> Set[str]:
|
def lower_set(iterable: Iterable[str]) -> set[str]:
|
||||||
return {x.lower() for x in iterable}
|
return {x.lower() for x in iterable}
|
||||||
|
|
||||||
|
|
||||||
def parents(file: str) -> Iterator[str]:
|
def parents(file: str) -> Iterator[str]:
|
||||||
file = os.path.dirname(file)
|
path_parts = file.split('/')
|
||||||
while file:
|
path_parts.pop()
|
||||||
yield file
|
while path_parts:
|
||||||
file = os.path.dirname(file)
|
yield '/'.join(path_parts)
|
||||||
|
path_parts.pop()
|
||||||
|
|
||||||
|
|
||||||
def directories_for(files: Set[str]) -> Set[str]:
|
def directories_for(files: set[str]) -> set[str]:
|
||||||
return {parent for file in files for parent in parents(file)}
|
return {parent for file in files for parent in parents(file)}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -56,7 +56,7 @@ def find_conflicting_filenames(filenames: Sequence[str]) -> int:
|
||||||
return retv
|
return retv
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'filenames', nargs='*',
|
'filenames', nargs='*',
|
||||||
|
|
@ -69,4 +69,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import io
|
import io
|
||||||
import tokenize
|
import tokenize
|
||||||
|
from collections.abc import Sequence
|
||||||
from tokenize import tokenize as tokenize_tokenize
|
from tokenize import tokenize as tokenize_tokenize
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
NON_CODE_TOKENS = frozenset((
|
NON_CODE_TOKENS = frozenset((
|
||||||
tokenize.COMMENT, tokenize.ENDMARKER, tokenize.NEWLINE, tokenize.NL,
|
tokenize.COMMENT, tokenize.ENDMARKER, tokenize.NEWLINE, tokenize.NL,
|
||||||
|
|
@ -27,13 +28,13 @@ def check_docstring_first(src: bytes, filename: str = '<unknown>') -> int:
|
||||||
if tok_type == tokenize.STRING and scol == 0:
|
if tok_type == tokenize.STRING and scol == 0:
|
||||||
if found_docstring_line is not None:
|
if found_docstring_line is not None:
|
||||||
print(
|
print(
|
||||||
f'{filename}:{sline} Multiple module docstrings '
|
f'{filename}:{sline}: Multiple module docstrings '
|
||||||
f'(first docstring on line {found_docstring_line}).',
|
f'(first docstring on line {found_docstring_line}).',
|
||||||
)
|
)
|
||||||
return 1
|
return 1
|
||||||
elif found_code_line is not None:
|
elif found_code_line is not None:
|
||||||
print(
|
print(
|
||||||
f'{filename}:{sline} Module docstring appears after code '
|
f'{filename}:{sline}: Module docstring appears after code '
|
||||||
f'(code seen on line {found_code_line}).',
|
f'(code seen on line {found_code_line}).',
|
||||||
)
|
)
|
||||||
return 1
|
return 1
|
||||||
|
|
@ -45,7 +46,7 @@ def check_docstring_first(src: bytes, filename: str = '<unknown>') -> int:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*')
|
parser.add_argument('filenames', nargs='*')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,12 @@
|
||||||
"""Check that executable text files have a shebang."""
|
"""Check that executable text files have a shebang."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import shlex
|
import shlex
|
||||||
import sys
|
import sys
|
||||||
from typing import Generator
|
from collections.abc import Generator
|
||||||
from typing import List
|
from collections.abc import Sequence
|
||||||
from typing import NamedTuple
|
from typing import NamedTuple
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
from typing import Set
|
|
||||||
|
|
||||||
from pre_commit_hooks.util import cmd_output
|
from pre_commit_hooks.util import cmd_output
|
||||||
from pre_commit_hooks.util import zsplit
|
from pre_commit_hooks.util import zsplit
|
||||||
|
|
@ -15,8 +14,11 @@ from pre_commit_hooks.util import zsplit
|
||||||
EXECUTABLE_VALUES = frozenset(('1', '3', '5', '7'))
|
EXECUTABLE_VALUES = frozenset(('1', '3', '5', '7'))
|
||||||
|
|
||||||
|
|
||||||
def check_executables(paths: List[str]) -> int:
|
def check_executables(paths: list[str]) -> int:
|
||||||
if sys.platform == 'win32': # pragma: win32 cover
|
fs_tracks_executable_bit = cmd_output(
|
||||||
|
'git', 'config', 'core.fileMode', retcode=None,
|
||||||
|
).strip()
|
||||||
|
if fs_tracks_executable_bit == 'false': # pragma: win32 cover
|
||||||
return _check_git_filemode(paths)
|
return _check_git_filemode(paths)
|
||||||
else: # pragma: win32 no cover
|
else: # pragma: win32 no cover
|
||||||
retv = 0
|
retv = 0
|
||||||
|
|
@ -33,7 +35,7 @@ class GitLsFile(NamedTuple):
|
||||||
filename: str
|
filename: str
|
||||||
|
|
||||||
|
|
||||||
def git_ls_files(paths: Sequence[str]) -> Generator[GitLsFile, None, None]:
|
def git_ls_files(paths: Sequence[str]) -> Generator[GitLsFile]:
|
||||||
outs = cmd_output('git', 'ls-files', '-z', '--stage', '--', *paths)
|
outs = cmd_output('git', 'ls-files', '-z', '--stage', '--', *paths)
|
||||||
for out in zsplit(outs):
|
for out in zsplit(outs):
|
||||||
metadata, filename = out.split('\t')
|
metadata, filename = out.split('\t')
|
||||||
|
|
@ -42,7 +44,7 @@ def git_ls_files(paths: Sequence[str]) -> Generator[GitLsFile, None, None]:
|
||||||
|
|
||||||
|
|
||||||
def _check_git_filemode(paths: Sequence[str]) -> int:
|
def _check_git_filemode(paths: Sequence[str]) -> int:
|
||||||
seen: Set[str] = set()
|
seen: set[str] = set()
|
||||||
for ls_file in git_ls_files(paths):
|
for ls_file in git_ls_files(paths):
|
||||||
is_executable = any(b in EXECUTABLE_VALUES for b in ls_file.mode[-3:])
|
is_executable = any(b in EXECUTABLE_VALUES for b in ls_file.mode[-3:])
|
||||||
if is_executable and not has_shebang(ls_file.filename):
|
if is_executable and not has_shebang(ls_file.filename):
|
||||||
|
|
@ -64,12 +66,14 @@ def _message(path: str) -> None:
|
||||||
f'{path}: marked executable but has no (or invalid) shebang!\n'
|
f'{path}: marked executable but has no (or invalid) shebang!\n'
|
||||||
f" If it isn't supposed to be executable, try: "
|
f" If it isn't supposed to be executable, try: "
|
||||||
f'`chmod -x {shlex.quote(path)}`\n'
|
f'`chmod -x {shlex.quote(path)}`\n'
|
||||||
|
f' If on Windows, you may also need to: '
|
||||||
|
f'`git add --chmod=-x {shlex.quote(path)}`\n'
|
||||||
f' If it is supposed to be executable, double-check its shebang.',
|
f' If it is supposed to be executable, double-check its shebang.',
|
||||||
file=sys.stderr,
|
file=sys.stderr,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser(description=__doc__)
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
parser.add_argument('filenames', nargs='*')
|
parser.add_argument('filenames', nargs='*')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
@ -78,4 +82,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,14 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
|
from collections.abc import Sequence
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import Dict
|
|
||||||
from typing import List
|
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
from typing import Tuple
|
|
||||||
|
|
||||||
|
|
||||||
def raise_duplicate_keys(
|
def raise_duplicate_keys(
|
||||||
ordered_pairs: List[Tuple[str, Any]],
|
ordered_pairs: list[tuple[str, Any]],
|
||||||
) -> Dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
d = {}
|
d = {}
|
||||||
for key, val in ordered_pairs:
|
for key, val in ordered_pairs:
|
||||||
if key in d:
|
if key in d:
|
||||||
|
|
@ -20,7 +18,7 @@ def raise_duplicate_keys(
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*', help='Filenames to check.')
|
parser.add_argument('filenames', nargs='*', help='Filenames to check.')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
@ -37,4 +35,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,34 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import os.path
|
import os.path
|
||||||
from typing import Optional
|
from collections.abc import Sequence
|
||||||
from typing import Sequence
|
|
||||||
|
from pre_commit_hooks.util import cmd_output
|
||||||
|
|
||||||
|
|
||||||
CONFLICT_PATTERNS = [
|
CONFLICT_PATTERNS = [
|
||||||
b'<<<<<<< ',
|
b'<<<<<<< ',
|
||||||
b'======= ',
|
b'======= ',
|
||||||
|
b'=======\r\n',
|
||||||
b'=======\n',
|
b'=======\n',
|
||||||
b'>>>>>>> ',
|
b'>>>>>>> ',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def is_in_merge() -> int:
|
def is_in_merge() -> bool:
|
||||||
|
git_dir = cmd_output('git', 'rev-parse', '--git-dir').rstrip()
|
||||||
return (
|
return (
|
||||||
os.path.exists(os.path.join('.git', 'MERGE_MSG')) and
|
os.path.exists(os.path.join(git_dir, 'MERGE_MSG')) and
|
||||||
(
|
(
|
||||||
os.path.exists(os.path.join('.git', 'MERGE_HEAD')) or
|
os.path.exists(os.path.join(git_dir, 'MERGE_HEAD')) or
|
||||||
os.path.exists(os.path.join('.git', 'rebase-apply')) or
|
os.path.exists(os.path.join(git_dir, 'rebase-apply')) or
|
||||||
os.path.exists(os.path.join('.git', 'rebase-merge'))
|
os.path.exists(os.path.join(git_dir, 'rebase-merge'))
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*')
|
parser.add_argument('filenames', nargs='*')
|
||||||
parser.add_argument('--assume-in-merge', action='store_true')
|
parser.add_argument('--assume-in-merge', action='store_true')
|
||||||
|
|
@ -35,12 +40,12 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
retcode = 0
|
retcode = 0
|
||||||
for filename in args.filenames:
|
for filename in args.filenames:
|
||||||
with open(filename, 'rb') as inputfile:
|
with open(filename, 'rb') as inputfile:
|
||||||
for i, line in enumerate(inputfile):
|
for i, line in enumerate(inputfile, start=1):
|
||||||
for pattern in CONFLICT_PATTERNS:
|
for pattern in CONFLICT_PATTERNS:
|
||||||
if line.startswith(pattern):
|
if line.startswith(pattern):
|
||||||
print(
|
print(
|
||||||
f'Merge conflict string "{pattern.decode()}" '
|
f'{filename}:{i}: Merge conflict string '
|
||||||
f'found in {filename}:{i + 1}',
|
f'{pattern.strip().decode()!r} found',
|
||||||
)
|
)
|
||||||
retcode = 1
|
retcode = 1
|
||||||
|
|
||||||
|
|
@ -48,4 +53,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,17 @@
|
||||||
"""Check that text files with a shebang are executable."""
|
"""Check that text files with a shebang are executable."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import shlex
|
import shlex
|
||||||
import sys
|
import sys
|
||||||
from typing import List
|
from collections.abc import Sequence
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
from typing import Set
|
|
||||||
|
|
||||||
from pre_commit_hooks.check_executables_have_shebangs import EXECUTABLE_VALUES
|
from pre_commit_hooks.check_executables_have_shebangs import EXECUTABLE_VALUES
|
||||||
from pre_commit_hooks.check_executables_have_shebangs import git_ls_files
|
from pre_commit_hooks.check_executables_have_shebangs import git_ls_files
|
||||||
from pre_commit_hooks.check_executables_have_shebangs import has_shebang
|
from pre_commit_hooks.check_executables_have_shebangs import has_shebang
|
||||||
|
|
||||||
|
|
||||||
def check_shebangs(paths: List[str]) -> int:
|
def check_shebangs(paths: list[str]) -> int:
|
||||||
# Cannot optimize on non-executability here if we intend this check to
|
# Cannot optimize on non-executability here if we intend this check to
|
||||||
# work on win32 -- and that's where problems caused by non-executability
|
# work on win32 -- and that's where problems caused by non-executability
|
||||||
# (elsewhere) are most likely to arise from.
|
# (elsewhere) are most likely to arise from.
|
||||||
|
|
@ -20,7 +19,7 @@ def check_shebangs(paths: List[str]) -> int:
|
||||||
|
|
||||||
|
|
||||||
def _check_git_filemode(paths: Sequence[str]) -> int:
|
def _check_git_filemode(paths: Sequence[str]) -> int:
|
||||||
seen: Set[str] = set()
|
seen: set[str] = set()
|
||||||
for ls_file in git_ls_files(paths):
|
for ls_file in git_ls_files(paths):
|
||||||
is_executable = any(b in EXECUTABLE_VALUES for b in ls_file.mode[-3:])
|
is_executable = any(b in EXECUTABLE_VALUES for b in ls_file.mode[-3:])
|
||||||
if not is_executable and has_shebang(ls_file.filename):
|
if not is_executable and has_shebang(ls_file.filename):
|
||||||
|
|
@ -35,13 +34,15 @@ def _message(path: str) -> None:
|
||||||
f'{path}: has a shebang but is not marked executable!\n'
|
f'{path}: has a shebang but is not marked executable!\n'
|
||||||
f' If it is supposed to be executable, try: '
|
f' If it is supposed to be executable, try: '
|
||||||
f'`chmod +x {shlex.quote(path)}`\n'
|
f'`chmod +x {shlex.quote(path)}`\n'
|
||||||
f' If it not supposed to be executable, double-check its shebang '
|
f' If on Windows, you may also need to: '
|
||||||
|
f'`git add --chmod=+x {shlex.quote(path)}`\n'
|
||||||
|
f' If it is not supposed to be executable, double-check its shebang '
|
||||||
f'is wanted.\n',
|
f'is wanted.\n',
|
||||||
file=sys.stderr,
|
file=sys.stderr,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser(description=__doc__)
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
parser.add_argument('filenames', nargs='*')
|
parser.add_argument('filenames', nargs='*')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
@ -50,4 +51,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import os.path
|
import os.path
|
||||||
from typing import Optional
|
from collections.abc import Sequence
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser(description='Checks for broken symlinks.')
|
parser = argparse.ArgumentParser(description='Checks for broken symlinks.')
|
||||||
parser.add_argument('filenames', nargs='*', help='Filenames to check')
|
parser.add_argument('filenames', nargs='*', help='Filenames to check')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
@ -23,4 +24,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,16 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
from typing import Optional
|
import sys
|
||||||
from typing import Sequence
|
from collections.abc import Sequence
|
||||||
|
|
||||||
import toml
|
if sys.version_info >= (3, 11): # pragma: >=3.11 cover
|
||||||
|
import tomllib
|
||||||
|
else: # pragma: <3.11 cover
|
||||||
|
import tomli as tomllib
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*', help='Filenames to check.')
|
parser.add_argument('filenames', nargs='*', help='Filenames to check.')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
@ -13,12 +18,13 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
retval = 0
|
retval = 0
|
||||||
for filename in args.filenames:
|
for filename in args.filenames:
|
||||||
try:
|
try:
|
||||||
toml.load(filename)
|
with open(filename, mode='rb') as fp:
|
||||||
except toml.TomlDecodeError as exc:
|
tomllib.load(fp)
|
||||||
|
except tomllib.TOMLDecodeError as exc:
|
||||||
print(f'{filename}: {exc}')
|
print(f'{filename}: {exc}')
|
||||||
retval = 1
|
retval = 1
|
||||||
return retval
|
return retval
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
from typing import List
|
from collections.abc import Sequence
|
||||||
from typing import Optional
|
from re import Pattern
|
||||||
from typing import Pattern
|
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
|
|
||||||
def _get_pattern(domain: str) -> Pattern[bytes]:
|
def _get_pattern(domain: str) -> Pattern[bytes]:
|
||||||
|
|
@ -15,7 +15,7 @@ def _get_pattern(domain: str) -> Pattern[bytes]:
|
||||||
return re.compile(regex.encode())
|
return re.compile(regex.encode())
|
||||||
|
|
||||||
|
|
||||||
def _check_filename(filename: str, patterns: List[Pattern[bytes]]) -> int:
|
def _check_filename(filename: str, patterns: list[Pattern[bytes]]) -> int:
|
||||||
retv = 0
|
retv = 0
|
||||||
with open(filename, 'rb') as f:
|
with open(filename, 'rb') as f:
|
||||||
for i, line in enumerate(f, 1):
|
for i, line in enumerate(f, 1):
|
||||||
|
|
@ -28,7 +28,7 @@ def _check_filename(filename: str, patterns: List[Pattern[bytes]]) -> int:
|
||||||
return retv
|
return retv
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*')
|
parser.add_argument('filenames', nargs='*')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
|
|
@ -57,4 +57,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import xml.sax.handler
|
import xml.sax.handler
|
||||||
from typing import Optional
|
from collections.abc import Sequence
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*', help='XML filenames to check.')
|
parser.add_argument('filenames', nargs='*', help='XML filenames to check.')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
@ -22,4 +23,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,17 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
from collections.abc import Generator
|
||||||
|
from collections.abc import Sequence
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import Generator
|
|
||||||
from typing import NamedTuple
|
from typing import NamedTuple
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
import ruamel.yaml
|
import ruamel.yaml
|
||||||
|
|
||||||
yaml = ruamel.yaml.YAML(typ='safe')
|
yaml = ruamel.yaml.YAML(typ='safe')
|
||||||
|
|
||||||
|
|
||||||
def _exhaust(gen: Generator[str, None, None]) -> None:
|
def _exhaust(gen: Generator[str]) -> None:
|
||||||
for _ in gen:
|
for _ in gen:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
@ -36,7 +37,7 @@ LOAD_FNS = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-m', '--multi', '--allow-multiple-documents', action='store_true',
|
'-m', '--multi', '--allow-multiple-documents', action='store_true',
|
||||||
|
|
@ -45,7 +46,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
'--unsafe', action='store_true',
|
'--unsafe', action='store_true',
|
||||||
help=(
|
help=(
|
||||||
'Instead of loading the files, simply parse them for syntax. '
|
'Instead of loading the files, simply parse them for syntax. '
|
||||||
'A syntax-only check enables extensions and unsafe contstructs '
|
'A syntax-only check enables extensions and unsafe constructs '
|
||||||
'which would otherwise be forbidden. Using this option removes '
|
'which would otherwise be forbidden. Using this option removes '
|
||||||
'all guarantees of portability to other yaml implementations. '
|
'all guarantees of portability to other yaml implementations. '
|
||||||
'Implies --allow-multiple-documents'
|
'Implies --allow-multiple-documents'
|
||||||
|
|
@ -68,4 +69,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,17 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import ast
|
import ast
|
||||||
import traceback
|
import traceback
|
||||||
from typing import List
|
from collections.abc import Sequence
|
||||||
from typing import NamedTuple
|
from typing import NamedTuple
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
|
|
||||||
DEBUG_STATEMENTS = {
|
DEBUG_STATEMENTS = {
|
||||||
|
'bpdb',
|
||||||
'ipdb',
|
'ipdb',
|
||||||
'pdb',
|
'pdb',
|
||||||
|
'pdbr',
|
||||||
'pudb',
|
'pudb',
|
||||||
'pydevd_pycharm',
|
'pydevd_pycharm',
|
||||||
'q',
|
'q',
|
||||||
|
|
@ -28,7 +30,7 @@ class Debug(NamedTuple):
|
||||||
|
|
||||||
class DebugStatementParser(ast.NodeVisitor):
|
class DebugStatementParser(ast.NodeVisitor):
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.breakpoints: List[Debug] = []
|
self.breakpoints: list[Debug] = []
|
||||||
|
|
||||||
def visit_Import(self, node: ast.Import) -> None:
|
def visit_Import(self, node: ast.Import) -> None:
|
||||||
for name in node.names:
|
for name in node.names:
|
||||||
|
|
@ -64,12 +66,12 @@ def check_file(filename: str) -> int:
|
||||||
visitor.visit(ast_obj)
|
visitor.visit(ast_obj)
|
||||||
|
|
||||||
for bp in visitor.breakpoints:
|
for bp in visitor.breakpoints:
|
||||||
print(f'{filename}:{bp.line}:{bp.col} - {bp.name} {bp.reason}')
|
print(f'{filename}:{bp.line}:{bp.col}: {bp.name} {bp.reason}')
|
||||||
|
|
||||||
return int(bool(visitor.breakpoints))
|
return int(bool(visitor.breakpoints))
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*', help='Filenames to run')
|
parser.add_argument('filenames', nargs='*', help='Filenames to run')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
@ -81,4 +83,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
20
pre_commit_hooks/destroyed_symlinks.py
Executable file → Normal file
20
pre_commit_hooks/destroyed_symlinks.py
Executable file → Normal file
|
|
@ -1,9 +1,9 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import shlex
|
import shlex
|
||||||
import subprocess
|
import subprocess
|
||||||
from typing import List
|
from collections.abc import Sequence
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
from pre_commit_hooks.util import cmd_output
|
from pre_commit_hooks.util import cmd_output
|
||||||
from pre_commit_hooks.util import zsplit
|
from pre_commit_hooks.util import zsplit
|
||||||
|
|
@ -13,8 +13,8 @@ PERMS_LINK = '120000'
|
||||||
PERMS_NONEXIST = '000000'
|
PERMS_NONEXIST = '000000'
|
||||||
|
|
||||||
|
|
||||||
def find_destroyed_symlinks(files: Sequence[str]) -> List[str]:
|
def find_destroyed_symlinks(files: Sequence[str]) -> list[str]:
|
||||||
destroyed_links: List[str] = []
|
destroyed_links: list[str] = []
|
||||||
if not files:
|
if not files:
|
||||||
return destroyed_links
|
return destroyed_links
|
||||||
for line in zsplit(
|
for line in zsplit(
|
||||||
|
|
@ -66,7 +66,7 @@ def find_destroyed_symlinks(files: Sequence[str]) -> List[str]:
|
||||||
return destroyed_links
|
return destroyed_links
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*', help='Filenames to check.')
|
parser.add_argument('filenames', nargs='*', help='Filenames to check.')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
@ -76,11 +76,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
for destroyed_link in destroyed_links:
|
for destroyed_link in destroyed_links:
|
||||||
print(f'- {destroyed_link}')
|
print(f'- {destroyed_link}')
|
||||||
print('You should unstage affected files:')
|
print('You should unstage affected files:')
|
||||||
print(
|
print(f'\tgit reset HEAD -- {shlex.join(destroyed_links)}')
|
||||||
'\tgit reset HEAD -- {}'.format(
|
|
||||||
' '.join(shlex.quote(link) for link in destroyed_links),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
print(
|
print(
|
||||||
'And retry commit. As a long term solution '
|
'And retry commit. As a long term solution '
|
||||||
'you may try to explicitly tell git that your '
|
'you may try to explicitly tell git that your '
|
||||||
|
|
@ -93,4 +89,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import configparser
|
import configparser
|
||||||
import os
|
import os
|
||||||
from typing import List
|
from collections.abc import Sequence
|
||||||
from typing import NamedTuple
|
from typing import NamedTuple
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
from typing import Set
|
|
||||||
|
|
||||||
|
|
||||||
class BadFile(NamedTuple):
|
class BadFile(NamedTuple):
|
||||||
|
|
@ -13,7 +12,7 @@ class BadFile(NamedTuple):
|
||||||
key: str
|
key: str
|
||||||
|
|
||||||
|
|
||||||
def get_aws_cred_files_from_env() -> Set[str]:
|
def get_aws_cred_files_from_env() -> set[str]:
|
||||||
"""Extract credential file paths from environment variables."""
|
"""Extract credential file paths from environment variables."""
|
||||||
return {
|
return {
|
||||||
os.environ[env_var]
|
os.environ[env_var]
|
||||||
|
|
@ -25,7 +24,7 @@ def get_aws_cred_files_from_env() -> Set[str]:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def get_aws_secrets_from_env() -> Set[str]:
|
def get_aws_secrets_from_env() -> set[str]:
|
||||||
"""Extract AWS secrets from environment variables."""
|
"""Extract AWS secrets from environment variables."""
|
||||||
keys = set()
|
keys = set()
|
||||||
for env_var in (
|
for env_var in (
|
||||||
|
|
@ -36,7 +35,7 @@ def get_aws_secrets_from_env() -> Set[str]:
|
||||||
return keys
|
return keys
|
||||||
|
|
||||||
|
|
||||||
def get_aws_secrets_from_file(credentials_file: str) -> Set[str]:
|
def get_aws_secrets_from_file(credentials_file: str) -> set[str]:
|
||||||
"""Extract AWS secrets from configuration files.
|
"""Extract AWS secrets from configuration files.
|
||||||
|
|
||||||
Read an ini-style configuration file and return a set with all found AWS
|
Read an ini-style configuration file and return a set with all found AWS
|
||||||
|
|
@ -69,8 +68,8 @@ def get_aws_secrets_from_file(credentials_file: str) -> Set[str]:
|
||||||
|
|
||||||
def check_file_for_aws_keys(
|
def check_file_for_aws_keys(
|
||||||
filenames: Sequence[str],
|
filenames: Sequence[str],
|
||||||
keys: Set[bytes],
|
keys: set[bytes],
|
||||||
) -> List[BadFile]:
|
) -> list[BadFile]:
|
||||||
"""Check if files contain AWS secrets.
|
"""Check if files contain AWS secrets.
|
||||||
|
|
||||||
Return a list of all files containing AWS secrets and keys found, with all
|
Return a list of all files containing AWS secrets and keys found, with all
|
||||||
|
|
@ -90,7 +89,7 @@ def check_file_for_aws_keys(
|
||||||
return bad_files
|
return bad_files
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='+', help='Filenames to run')
|
parser.add_argument('filenames', nargs='+', help='Filenames to run')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
|
|
@ -119,7 +118,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
# of files to to gather AWS secrets from.
|
# of files to to gather AWS secrets from.
|
||||||
credential_files |= get_aws_cred_files_from_env()
|
credential_files |= get_aws_cred_files_from_env()
|
||||||
|
|
||||||
keys: Set[str] = set()
|
keys: set[str] = set()
|
||||||
for credential_file in credential_files:
|
for credential_file in credential_files:
|
||||||
keys |= get_aws_secrets_from_file(credential_file)
|
keys |= get_aws_secrets_from_file(credential_file)
|
||||||
|
|
||||||
|
|
@ -149,4 +148,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
from typing import Optional
|
from collections.abc import Sequence
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
BLACKLIST = [
|
BLACKLIST = [
|
||||||
b'BEGIN RSA PRIVATE KEY',
|
b'BEGIN RSA PRIVATE KEY',
|
||||||
|
|
@ -11,10 +12,12 @@ BLACKLIST = [
|
||||||
b'PuTTY-User-Key-File-2',
|
b'PuTTY-User-Key-File-2',
|
||||||
b'BEGIN SSH2 ENCRYPTED PRIVATE KEY',
|
b'BEGIN SSH2 ENCRYPTED PRIVATE KEY',
|
||||||
b'BEGIN PGP PRIVATE KEY BLOCK',
|
b'BEGIN PGP PRIVATE KEY BLOCK',
|
||||||
|
b'BEGIN ENCRYPTED PRIVATE KEY',
|
||||||
|
b'BEGIN OpenVPN Static key V1',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*', help='Filenames to check')
|
parser.add_argument('filenames', nargs='*', help='Filenames to check')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
@ -36,4 +39,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
|
from collections.abc import Sequence
|
||||||
from typing import IO
|
from typing import IO
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
|
|
||||||
def fix_file(file_obj: IO[bytes]) -> int:
|
def fix_file(file_obj: IO[bytes]) -> int:
|
||||||
|
|
@ -48,7 +49,7 @@ def fix_file(file_obj: IO[bytes]) -> int:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = 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')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
@ -67,4 +68,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -9,13 +9,14 @@ per line. Various users are adding/removing lines from this file; using
|
||||||
this hook on that file should reduce the instances of git merge
|
this hook on that file should reduce the instances of git merge
|
||||||
conflicts and keep the file nicely ordered.
|
conflicts and keep the file nicely ordered.
|
||||||
"""
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
from collections.abc import Callable
|
||||||
|
from collections.abc import Iterable
|
||||||
|
from collections.abc import Sequence
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import Callable
|
|
||||||
from typing import IO
|
from typing import IO
|
||||||
from typing import Iterable
|
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
PASS = 0
|
PASS = 0
|
||||||
FAIL = 1
|
FAIL = 1
|
||||||
|
|
@ -23,7 +24,7 @@ FAIL = 1
|
||||||
|
|
||||||
def sort_file_contents(
|
def sort_file_contents(
|
||||||
f: IO[bytes],
|
f: IO[bytes],
|
||||||
key: Optional[Callable[[bytes], Any]],
|
key: Callable[[bytes], Any] | None,
|
||||||
*,
|
*,
|
||||||
unique: bool = False,
|
unique: bool = False,
|
||||||
) -> int:
|
) -> int:
|
||||||
|
|
@ -36,7 +37,10 @@ def sort_file_contents(
|
||||||
after = sorted(lines, key=key)
|
after = sorted(lines, key=key)
|
||||||
|
|
||||||
before_string = b''.join(before)
|
before_string = b''.join(before)
|
||||||
after_string = b'\n'.join(after) + b'\n'
|
after_string = b'\n'.join(after)
|
||||||
|
|
||||||
|
if after_string:
|
||||||
|
after_string += b'\n'
|
||||||
|
|
||||||
if before_string == after_string:
|
if before_string == after_string:
|
||||||
return PASS
|
return PASS
|
||||||
|
|
@ -47,21 +51,24 @@ def sort_file_contents(
|
||||||
return FAIL
|
return FAIL
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='+', help='Files to sort')
|
parser.add_argument('filenames', nargs='+', help='Files to sort')
|
||||||
parser.add_argument(
|
|
||||||
|
mutex = parser.add_mutually_exclusive_group(required=False)
|
||||||
|
mutex.add_argument(
|
||||||
'--ignore-case',
|
'--ignore-case',
|
||||||
action='store_const',
|
action='store_const',
|
||||||
const=bytes.lower,
|
const=bytes.lower,
|
||||||
default=None,
|
default=None,
|
||||||
help='fold lower case to upper case characters',
|
help='fold lower case to upper case characters',
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
mutex.add_argument(
|
||||||
'--unique',
|
'--unique',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help='ensure each line is unique',
|
help='ensure each line is unique',
|
||||||
)
|
)
|
||||||
|
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
retv = PASS
|
retv = PASS
|
||||||
|
|
@ -81,4 +88,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
from typing import Optional
|
from collections.abc import Sequence
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*', help='Filenames to check')
|
parser.add_argument('filenames', nargs='*', help='Filenames to check')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
@ -27,4 +28,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,148 +0,0 @@
|
||||||
import argparse
|
|
||||||
from typing import IO
|
|
||||||
from typing import NamedTuple
|
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
DEFAULT_PRAGMA = b'# -*- coding: utf-8 -*-'
|
|
||||||
|
|
||||||
|
|
||||||
def has_coding(line: bytes) -> bool:
|
|
||||||
if not line.strip():
|
|
||||||
return False
|
|
||||||
return (
|
|
||||||
line.lstrip()[:1] == b'#' and (
|
|
||||||
b'unicode' in line or
|
|
||||||
b'encoding' in line or
|
|
||||||
b'coding:' in line or
|
|
||||||
b'coding=' in line
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class ExpectedContents(NamedTuple):
|
|
||||||
shebang: bytes
|
|
||||||
rest: bytes
|
|
||||||
# True: has exactly the coding pragma expected
|
|
||||||
# False: missing coding pragma entirely
|
|
||||||
# None: has a coding pragma, but it does not match
|
|
||||||
pragma_status: Optional[bool]
|
|
||||||
ending: bytes
|
|
||||||
|
|
||||||
@property
|
|
||||||
def has_any_pragma(self) -> bool:
|
|
||||||
return self.pragma_status is not False
|
|
||||||
|
|
||||||
def is_expected_pragma(self, remove: bool) -> bool:
|
|
||||||
expected_pragma_status = not remove
|
|
||||||
return self.pragma_status is expected_pragma_status
|
|
||||||
|
|
||||||
|
|
||||||
def _get_expected_contents(
|
|
||||||
first_line: bytes,
|
|
||||||
second_line: bytes,
|
|
||||||
rest: bytes,
|
|
||||||
expected_pragma: bytes,
|
|
||||||
) -> ExpectedContents:
|
|
||||||
ending = b'\r\n' if first_line.endswith(b'\r\n') else b'\n'
|
|
||||||
|
|
||||||
if first_line.startswith(b'#!'):
|
|
||||||
shebang = first_line
|
|
||||||
potential_coding = second_line
|
|
||||||
else:
|
|
||||||
shebang = b''
|
|
||||||
potential_coding = first_line
|
|
||||||
rest = second_line + rest
|
|
||||||
|
|
||||||
if potential_coding.rstrip(b'\r\n') == expected_pragma:
|
|
||||||
pragma_status: Optional[bool] = True
|
|
||||||
elif has_coding(potential_coding):
|
|
||||||
pragma_status = None
|
|
||||||
else:
|
|
||||||
pragma_status = False
|
|
||||||
rest = potential_coding + rest
|
|
||||||
|
|
||||||
return ExpectedContents(
|
|
||||||
shebang=shebang, rest=rest, pragma_status=pragma_status, ending=ending,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def fix_encoding_pragma(
|
|
||||||
f: IO[bytes],
|
|
||||||
remove: bool = False,
|
|
||||||
expected_pragma: bytes = DEFAULT_PRAGMA,
|
|
||||||
) -> int:
|
|
||||||
expected = _get_expected_contents(
|
|
||||||
f.readline(), f.readline(), f.read(), expected_pragma,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Special cases for empty files
|
|
||||||
if not expected.rest.strip():
|
|
||||||
# If a file only has a shebang or a coding pragma, remove it
|
|
||||||
if expected.has_any_pragma or expected.shebang:
|
|
||||||
f.seek(0)
|
|
||||||
f.truncate()
|
|
||||||
f.write(b'')
|
|
||||||
return 1
|
|
||||||
else:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
if expected.is_expected_pragma(remove):
|
|
||||||
return 0
|
|
||||||
|
|
||||||
# Otherwise, write out the new file
|
|
||||||
f.seek(0)
|
|
||||||
f.truncate()
|
|
||||||
f.write(expected.shebang)
|
|
||||||
if not remove:
|
|
||||||
f.write(expected_pragma + expected.ending)
|
|
||||||
f.write(expected.rest)
|
|
||||||
|
|
||||||
return 1
|
|
||||||
|
|
||||||
|
|
||||||
def _normalize_pragma(pragma: str) -> bytes:
|
|
||||||
return pragma.encode().rstrip()
|
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
|
||||||
parser = argparse.ArgumentParser(
|
|
||||||
'Fixes the encoding pragma of python files',
|
|
||||||
)
|
|
||||||
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
|
||||||
parser.add_argument(
|
|
||||||
'--pragma', default=DEFAULT_PRAGMA, type=_normalize_pragma,
|
|
||||||
help=(
|
|
||||||
f'The encoding pragma to use. '
|
|
||||||
f'Default: {DEFAULT_PRAGMA.decode()}'
|
|
||||||
),
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'--remove', action='store_true',
|
|
||||||
help='Remove the encoding pragma (Useful in a python3-only codebase)',
|
|
||||||
)
|
|
||||||
args = parser.parse_args(argv)
|
|
||||||
|
|
||||||
retv = 0
|
|
||||||
|
|
||||||
if args.remove:
|
|
||||||
fmt = 'Removed encoding pragma from {filename}'
|
|
||||||
else:
|
|
||||||
fmt = 'Added `{pragma}` to {filename}'
|
|
||||||
|
|
||||||
for filename in args.filenames:
|
|
||||||
with open(filename, 'r+b') as f:
|
|
||||||
file_ret = fix_encoding_pragma(
|
|
||||||
f, remove=args.remove, expected_pragma=args.pragma,
|
|
||||||
)
|
|
||||||
retv |= file_ret
|
|
||||||
if file_ret:
|
|
||||||
print(
|
|
||||||
fmt.format(pragma=args.pragma.decode(), filename=filename),
|
|
||||||
)
|
|
||||||
|
|
||||||
return retv
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
exit(main())
|
|
||||||
|
|
@ -1,14 +1,30 @@
|
||||||
from typing import Optional
|
from __future__ import annotations
|
||||||
from typing import Sequence
|
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
from collections.abc import Sequence
|
||||||
|
|
||||||
from pre_commit_hooks.util import cmd_output
|
from pre_commit_hooks.util import cmd_output
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
# `argv` is ignored, pre-commit will send us a list of files that we
|
parser = argparse.ArgumentParser()
|
||||||
# don't care about
|
parser.add_argument('filenames', nargs='*')
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
|
if (
|
||||||
|
'PRE_COMMIT_FROM_REF' in os.environ and
|
||||||
|
'PRE_COMMIT_TO_REF' in os.environ
|
||||||
|
):
|
||||||
|
diff_arg = '...'.join((
|
||||||
|
os.environ['PRE_COMMIT_FROM_REF'],
|
||||||
|
os.environ['PRE_COMMIT_TO_REF'],
|
||||||
|
))
|
||||||
|
else:
|
||||||
|
diff_arg = '--staged'
|
||||||
added_diff = cmd_output(
|
added_diff = cmd_output(
|
||||||
'git', 'diff', '--staged', '--diff-filter=A', '--raw',
|
'git', 'diff', '--diff-filter=A', '--raw', diff_arg, '--',
|
||||||
|
*args.filenames,
|
||||||
)
|
)
|
||||||
retv = 0
|
retv = 0
|
||||||
for line in added_diff.splitlines():
|
for line in added_diff.splitlines():
|
||||||
|
|
@ -29,4 +45,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import collections
|
import collections
|
||||||
from typing import Dict
|
from collections.abc import Sequence
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
|
|
||||||
CRLF = b'\r\n'
|
CRLF = b'\r\n'
|
||||||
|
|
@ -25,7 +25,7 @@ def fix_filename(filename: str, fix: str) -> int:
|
||||||
with open(filename, 'rb') as f:
|
with open(filename, 'rb') as f:
|
||||||
contents = f.read()
|
contents = f.read()
|
||||||
|
|
||||||
counts: Dict[bytes, int] = collections.defaultdict(int)
|
counts: dict[bytes, int] = collections.defaultdict(int)
|
||||||
|
|
||||||
for line in contents.splitlines(True):
|
for line in contents.splitlines(True):
|
||||||
for ending in ALL_ENDINGS:
|
for ending in ALL_ENDINGS:
|
||||||
|
|
@ -62,7 +62,7 @@ def fix_filename(filename: str, fix: str) -> int:
|
||||||
return other_endings
|
return other_endings
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-f', '--fix',
|
'-f', '--fix',
|
||||||
|
|
@ -85,4 +85,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import re
|
import re
|
||||||
|
from collections.abc import Sequence
|
||||||
from typing import AbstractSet
|
from typing import AbstractSet
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
from pre_commit_hooks.util import CalledProcessError
|
from pre_commit_hooks.util import CalledProcessError
|
||||||
from pre_commit_hooks.util import cmd_output
|
from pre_commit_hooks.util import cmd_output
|
||||||
|
|
@ -23,7 +24,7 @@ def is_on_branch(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-b', '--branch', action='append',
|
'-b', '--branch', action='append',
|
||||||
|
|
@ -44,4 +45,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,11 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
|
from collections.abc import Mapping
|
||||||
|
from collections.abc import Sequence
|
||||||
from difflib import unified_diff
|
from difflib import unified_diff
|
||||||
from typing import List
|
|
||||||
from typing import Mapping
|
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
from typing import Tuple
|
|
||||||
from typing import Union
|
|
||||||
|
|
||||||
|
|
||||||
def _get_pretty_format(
|
def _get_pretty_format(
|
||||||
|
|
@ -17,7 +15,7 @@ def _get_pretty_format(
|
||||||
sort_keys: bool = True,
|
sort_keys: bool = True,
|
||||||
top_keys: Sequence[str] = (),
|
top_keys: Sequence[str] = (),
|
||||||
) -> str:
|
) -> str:
|
||||||
def pairs_first(pairs: Sequence[Tuple[str, str]]) -> Mapping[str, str]:
|
def pairs_first(pairs: Sequence[tuple[str, str]]) -> Mapping[str, str]:
|
||||||
before = [pair for pair in pairs if pair[0] in top_keys]
|
before = [pair for pair in pairs if pair[0] in top_keys]
|
||||||
before = sorted(before, key=lambda x: top_keys.index(x[0]))
|
before = sorted(before, key=lambda x: top_keys.index(x[0]))
|
||||||
after = [pair for pair in pairs if pair[0] not in top_keys]
|
after = [pair for pair in pairs if pair[0] not in top_keys]
|
||||||
|
|
@ -38,7 +36,7 @@ def _autofix(filename: str, new_contents: str) -> None:
|
||||||
f.write(new_contents)
|
f.write(new_contents)
|
||||||
|
|
||||||
|
|
||||||
def parse_num_to_int(s: str) -> Union[int, str]:
|
def parse_num_to_int(s: str) -> int | str:
|
||||||
"""Convert string numbers to int, leaving strings as is."""
|
"""Convert string numbers to int, leaving strings as is."""
|
||||||
try:
|
try:
|
||||||
return int(s)
|
return int(s)
|
||||||
|
|
@ -46,7 +44,7 @@ def parse_num_to_int(s: str) -> Union[int, str]:
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
def parse_topkeys(s: str) -> List[str]:
|
def parse_topkeys(s: str) -> list[str]:
|
||||||
return s.split(',')
|
return s.split(',')
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -57,7 +55,7 @@ def get_diff(source: str, target: str, file: str) -> str:
|
||||||
return ''.join(diff)
|
return ''.join(diff)
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--autofix',
|
'--autofix',
|
||||||
|
|
@ -117,19 +115,23 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
f'Input File {json_file} is not a valid JSON, consider using '
|
f'Input File {json_file} is not a valid JSON, consider using '
|
||||||
f'check-json',
|
f'check-json',
|
||||||
)
|
)
|
||||||
return 1
|
|
||||||
|
|
||||||
if contents != pretty_contents:
|
|
||||||
if args.autofix:
|
|
||||||
_autofix(json_file, pretty_contents)
|
|
||||||
else:
|
|
||||||
diff_output = get_diff(contents, pretty_contents, json_file)
|
|
||||||
sys.stdout.buffer.write(diff_output.encode())
|
|
||||||
|
|
||||||
status = 1
|
status = 1
|
||||||
|
else:
|
||||||
|
if contents != pretty_contents:
|
||||||
|
if args.autofix:
|
||||||
|
_autofix(json_file, pretty_contents)
|
||||||
|
else:
|
||||||
|
diff_output = get_diff(
|
||||||
|
contents,
|
||||||
|
pretty_contents,
|
||||||
|
json_file,
|
||||||
|
)
|
||||||
|
sys.stdout.buffer.write(diff_output.encode())
|
||||||
|
|
||||||
|
status = 1
|
||||||
|
|
||||||
return status
|
return status
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
from typing import Optional
|
from collections.abc import Sequence
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
argv = argv if argv is not None else sys.argv[1:]
|
argv = argv if argv is not None else sys.argv[1:]
|
||||||
hookid, new_hookid, url = argv[:3]
|
hookid, new_hookid, url = argv[:3]
|
||||||
raise SystemExit(
|
raise SystemExit(
|
||||||
|
|
@ -12,4 +13,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import re
|
import re
|
||||||
|
from collections.abc import Sequence
|
||||||
from typing import IO
|
from typing import IO
|
||||||
from typing import List
|
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
|
|
||||||
PASS = 0
|
PASS = 0
|
||||||
|
|
@ -15,8 +15,8 @@ class Requirement:
|
||||||
UNTIL_SEP = re.compile(rb'[^;\s]+')
|
UNTIL_SEP = re.compile(rb'[^;\s]+')
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.value: Optional[bytes] = None
|
self.value: bytes | None = None
|
||||||
self.comments: List[bytes] = []
|
self.comments: list[bytes] = []
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> bytes:
|
def name(self) -> bytes:
|
||||||
|
|
@ -36,7 +36,7 @@ class Requirement:
|
||||||
|
|
||||||
return name[:m.start()]
|
return name[:m.start()]
|
||||||
|
|
||||||
def __lt__(self, requirement: 'Requirement') -> bool:
|
def __lt__(self, requirement: Requirement) -> bool:
|
||||||
# \n means top of file comment, so always return True,
|
# \n means top of file comment, so always return True,
|
||||||
# otherwise just do a string comparison with value.
|
# otherwise just do a string comparison with value.
|
||||||
assert self.value is not None, self.value
|
assert self.value is not None, self.value
|
||||||
|
|
@ -45,6 +45,11 @@ class Requirement:
|
||||||
elif requirement.value == b'\n':
|
elif requirement.value == b'\n':
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
|
# if 2 requirements have the same name, the one with comments
|
||||||
|
# needs to go first (so that when removing duplicates, the one
|
||||||
|
# with comments is kept)
|
||||||
|
if self.name == requirement.name:
|
||||||
|
return bool(self.comments) > bool(requirement.comments)
|
||||||
return self.name < requirement.name
|
return self.name < requirement.name
|
||||||
|
|
||||||
def is_complete(self) -> bool:
|
def is_complete(self) -> bool:
|
||||||
|
|
@ -61,9 +66,9 @@ class Requirement:
|
||||||
|
|
||||||
|
|
||||||
def fix_requirements(f: IO[bytes]) -> int:
|
def fix_requirements(f: IO[bytes]) -> int:
|
||||||
requirements: List[Requirement] = []
|
requirements: list[Requirement] = []
|
||||||
before = list(f)
|
before = list(f)
|
||||||
after: List[bytes] = []
|
after: list[bytes] = []
|
||||||
|
|
||||||
before_string = b''.join(before)
|
before_string = b''.join(before)
|
||||||
|
|
||||||
|
|
@ -110,13 +115,20 @@ def fix_requirements(f: IO[bytes]) -> int:
|
||||||
# which is automatically added by broken pip package under Debian
|
# which is automatically added by broken pip package under Debian
|
||||||
requirements = [
|
requirements = [
|
||||||
req for req in requirements
|
req for req in requirements
|
||||||
if req.value != b'pkg-resources==0.0.0\n'
|
if req.value not in [
|
||||||
|
b'pkg-resources==0.0.0\n',
|
||||||
|
b'pkg_resources==0.0.0\n',
|
||||||
|
]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# sort the requirements and remove duplicates
|
||||||
|
prev = None
|
||||||
for requirement in sorted(requirements):
|
for requirement in sorted(requirements):
|
||||||
after.extend(requirement.comments)
|
after.extend(requirement.comments)
|
||||||
assert requirement.value, requirement.value
|
assert requirement.value, requirement.value
|
||||||
after.append(requirement.value)
|
if prev is None or requirement.value != prev.value:
|
||||||
|
after.append(requirement.value)
|
||||||
|
prev = requirement
|
||||||
after.extend(rest)
|
after.extend(rest)
|
||||||
|
|
||||||
after_string = b''.join(after)
|
after_string = b''.join(after)
|
||||||
|
|
@ -130,7 +142,7 @@ def fix_requirements(f: IO[bytes]) -> int:
|
||||||
return FAIL
|
return FAIL
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = 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')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
@ -150,4 +162,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
19
pre_commit_hooks/sort_simple_yaml.py
Executable file → Normal file
19
pre_commit_hooks/sort_simple_yaml.py
Executable file → Normal file
|
|
@ -1,4 +1,3 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
"""Sort a simple YAML file, keeping blocks of comments and definitions
|
"""Sort a simple YAML file, keeping blocks of comments and definitions
|
||||||
together.
|
together.
|
||||||
|
|
||||||
|
|
@ -18,16 +17,16 @@ We assume a strict subset of YAML that looks like:
|
||||||
In other words, we don't sort deeper than the top layer, and might corrupt
|
In other words, we don't sort deeper than the top layer, and might corrupt
|
||||||
complicated YAML files.
|
complicated YAML files.
|
||||||
"""
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
from typing import List
|
from collections.abc import Sequence
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
|
|
||||||
QUOTES = ["'", '"']
|
QUOTES = ["'", '"']
|
||||||
|
|
||||||
|
|
||||||
def sort(lines: List[str]) -> List[str]:
|
def sort(lines: list[str]) -> list[str]:
|
||||||
"""Sort a YAML file in alphabetical order, keeping blocks together.
|
"""Sort a YAML file in alphabetical order, keeping blocks together.
|
||||||
|
|
||||||
:param lines: array of strings (without newlines)
|
:param lines: array of strings (without newlines)
|
||||||
|
|
@ -45,7 +44,7 @@ def sort(lines: List[str]) -> List[str]:
|
||||||
return new_lines
|
return new_lines
|
||||||
|
|
||||||
|
|
||||||
def parse_block(lines: List[str], header: bool = False) -> List[str]:
|
def parse_block(lines: list[str], header: bool = False) -> list[str]:
|
||||||
"""Parse and return a single block, popping off the start of `lines`.
|
"""Parse and return a single block, popping off the start of `lines`.
|
||||||
|
|
||||||
If parsing a header block, we stop after we reach a line that is not a
|
If parsing a header block, we stop after we reach a line that is not a
|
||||||
|
|
@ -61,7 +60,7 @@ def parse_block(lines: List[str], header: bool = False) -> List[str]:
|
||||||
return block_lines
|
return block_lines
|
||||||
|
|
||||||
|
|
||||||
def parse_blocks(lines: List[str]) -> List[List[str]]:
|
def parse_blocks(lines: list[str]) -> list[list[str]]:
|
||||||
"""Parse and return all possible blocks, popping off the start of `lines`.
|
"""Parse and return all possible blocks, popping off the start of `lines`.
|
||||||
|
|
||||||
:param lines: list of lines
|
:param lines: list of lines
|
||||||
|
|
@ -78,7 +77,7 @@ def parse_blocks(lines: List[str]) -> List[List[str]]:
|
||||||
return blocks
|
return blocks
|
||||||
|
|
||||||
|
|
||||||
def first_key(lines: List[str]) -> str:
|
def first_key(lines: list[str]) -> str:
|
||||||
"""Returns a string representing the sort key of a block.
|
"""Returns a string representing the sort key of a block.
|
||||||
|
|
||||||
The sort key is the first YAML key we encounter, ignoring comments, and
|
The sort key is the first YAML key we encounter, ignoring comments, and
|
||||||
|
|
@ -100,7 +99,7 @@ def first_key(lines: List[str]) -> str:
|
||||||
return '' # not actually reached in reality
|
return '' # not actually reached in reality
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = 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')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
@ -123,4 +122,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,17 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import io
|
import io
|
||||||
import re
|
import re
|
||||||
|
import sys
|
||||||
import tokenize
|
import tokenize
|
||||||
from typing import List
|
from collections.abc import Sequence
|
||||||
from typing import Optional
|
|
||||||
from typing import Sequence
|
if sys.version_info >= (3, 12): # pragma: >=3.12 cover
|
||||||
|
FSTRING_START = tokenize.FSTRING_START
|
||||||
|
FSTRING_END = tokenize.FSTRING_END
|
||||||
|
else: # pragma: <3.12 cover
|
||||||
|
FSTRING_START = FSTRING_END = -1
|
||||||
|
|
||||||
START_QUOTE_RE = re.compile('^[a-zA-Z]*"')
|
START_QUOTE_RE = re.compile('^[a-zA-Z]*"')
|
||||||
|
|
||||||
|
|
@ -24,7 +31,7 @@ def handle_match(token_text: str) -> str:
|
||||||
return token_text
|
return token_text
|
||||||
|
|
||||||
|
|
||||||
def get_line_offsets_by_line_no(src: str) -> List[int]:
|
def get_line_offsets_by_line_no(src: str) -> list[int]:
|
||||||
# Padded so we can index with line number
|
# Padded so we can index with line number
|
||||||
offsets = [-1, 0]
|
offsets = [-1, 0]
|
||||||
for line in src.splitlines(True):
|
for line in src.splitlines(True):
|
||||||
|
|
@ -40,11 +47,17 @@ def fix_strings(filename: str) -> int:
|
||||||
# Basically a mutable string
|
# Basically a mutable string
|
||||||
splitcontents = list(contents)
|
splitcontents = list(contents)
|
||||||
|
|
||||||
|
fstring_depth = 0
|
||||||
|
|
||||||
# Iterate in reverse so the offsets are always correct
|
# Iterate in reverse so the offsets are always correct
|
||||||
tokens_l = list(tokenize.generate_tokens(io.StringIO(contents).readline))
|
tokens_l = list(tokenize.generate_tokens(io.StringIO(contents).readline))
|
||||||
tokens = reversed(tokens_l)
|
tokens = reversed(tokens_l)
|
||||||
for token_type, token_text, (srow, scol), (erow, ecol), _ in tokens:
|
for token_type, token_text, (srow, scol), (erow, ecol), _ in tokens:
|
||||||
if token_type == tokenize.STRING:
|
if token_type == FSTRING_START: # pragma: >=3.12 cover
|
||||||
|
fstring_depth += 1
|
||||||
|
elif token_type == FSTRING_END: # pragma: >=3.12 cover
|
||||||
|
fstring_depth -= 1
|
||||||
|
elif fstring_depth == 0 and token_type == tokenize.STRING:
|
||||||
new_text = handle_match(token_text)
|
new_text = handle_match(token_text)
|
||||||
splitcontents[
|
splitcontents[
|
||||||
line_offsets[srow] + scol:
|
line_offsets[srow] + scol:
|
||||||
|
|
@ -60,7 +73,7 @@ def fix_strings(filename: str) -> int:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = 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')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
@ -77,4 +90,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,33 +1,53 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import os.path
|
import os.path
|
||||||
import re
|
import re
|
||||||
from typing import Optional
|
from collections.abc import Sequence
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*')
|
parser.add_argument('filenames', nargs='*')
|
||||||
parser.add_argument(
|
mutex = parser.add_mutually_exclusive_group()
|
||||||
'--django', default=False, action='store_true',
|
mutex.add_argument(
|
||||||
help='Use Django-style test naming pattern (test*.py)',
|
'--pytest',
|
||||||
|
dest='pattern',
|
||||||
|
action='store_const',
|
||||||
|
const=r'.*_test\.py',
|
||||||
|
default=r'.*_test\.py',
|
||||||
|
help='(the default) ensure tests match %(const)s',
|
||||||
|
)
|
||||||
|
mutex.add_argument(
|
||||||
|
'--pytest-test-first',
|
||||||
|
dest='pattern',
|
||||||
|
action='store_const',
|
||||||
|
const=r'test_.*\.py',
|
||||||
|
help='ensure tests match %(const)s',
|
||||||
|
)
|
||||||
|
mutex.add_argument(
|
||||||
|
'--django', '--unittest',
|
||||||
|
dest='pattern',
|
||||||
|
action='store_const',
|
||||||
|
const=r'test.*\.py',
|
||||||
|
help='ensure tests match %(const)s',
|
||||||
)
|
)
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
retcode = 0
|
retcode = 0
|
||||||
test_name_pattern = r'test.*\.py' if args.django else r'.*_test\.py'
|
reg = re.compile(args.pattern)
|
||||||
for filename in args.filenames:
|
for filename in args.filenames:
|
||||||
base = os.path.basename(filename)
|
base = os.path.basename(filename)
|
||||||
if (
|
if (
|
||||||
not re.match(test_name_pattern, base) and
|
not reg.fullmatch(base) and
|
||||||
not base == '__init__.py' and
|
not base == '__init__.py' and
|
||||||
not base == 'conftest.py'
|
not base == 'conftest.py'
|
||||||
):
|
):
|
||||||
retcode = 1
|
retcode = 1
|
||||||
print(f'{filename} does not match pattern "{test_name_pattern}"')
|
print(f'{filename} does not match pattern "{args.pattern}"')
|
||||||
|
|
||||||
return retcode
|
return retcode
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,14 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
from typing import Optional
|
from collections.abc import Sequence
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
|
|
||||||
def _fix_file(
|
def _fix_file(
|
||||||
filename: str,
|
filename: str,
|
||||||
is_markdown: bool,
|
is_markdown: bool,
|
||||||
chars: Optional[bytes],
|
chars: bytes | None,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
with open(filename, mode='rb') as file_processed:
|
with open(filename, mode='rb') as file_processed:
|
||||||
lines = file_processed.readlines()
|
lines = file_processed.readlines()
|
||||||
|
|
@ -24,7 +25,7 @@ def _fix_file(
|
||||||
def _process_line(
|
def _process_line(
|
||||||
line: bytes,
|
line: bytes,
|
||||||
is_markdown: bool,
|
is_markdown: bool,
|
||||||
chars: Optional[bytes],
|
chars: bytes | None,
|
||||||
) -> bytes:
|
) -> bytes:
|
||||||
if line[-2:] == b'\r\n':
|
if line[-2:] == b'\r\n':
|
||||||
eol = b'\r\n'
|
eol = b'\r\n'
|
||||||
|
|
@ -40,7 +41,7 @@ def _process_line(
|
||||||
return line.rstrip(chars) + eol
|
return line.rstrip(chars) + eol
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--no-markdown-linebreak-ext',
|
'--no-markdown-linebreak-ext',
|
||||||
|
|
@ -99,4 +100,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(main())
|
raise SystemExit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,19 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import List
|
|
||||||
from typing import Optional
|
|
||||||
from typing import Set
|
|
||||||
|
|
||||||
|
|
||||||
class CalledProcessError(RuntimeError):
|
class CalledProcessError(RuntimeError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def added_files() -> Set[str]:
|
def added_files() -> set[str]:
|
||||||
cmd = ('git', 'diff', '--staged', '--name-only', '--diff-filter=A')
|
cmd = ('git', 'diff', '--staged', '--name-only', '--diff-filter=A')
|
||||||
return set(cmd_output(*cmd).splitlines())
|
return set(cmd_output(*cmd).splitlines())
|
||||||
|
|
||||||
|
|
||||||
def cmd_output(*cmd: str, retcode: Optional[int] = 0, **kwargs: Any) -> str:
|
def cmd_output(*cmd: str, retcode: int | None = 0, **kwargs: Any) -> str:
|
||||||
kwargs.setdefault('stdout', subprocess.PIPE)
|
kwargs.setdefault('stdout', subprocess.PIPE)
|
||||||
kwargs.setdefault('stderr', subprocess.PIPE)
|
kwargs.setdefault('stderr', subprocess.PIPE)
|
||||||
proc = subprocess.Popen(cmd, **kwargs)
|
proc = subprocess.Popen(cmd, **kwargs)
|
||||||
|
|
@ -25,7 +24,7 @@ def cmd_output(*cmd: str, retcode: Optional[int] = 0, **kwargs: Any) -> str:
|
||||||
return stdout
|
return stdout
|
||||||
|
|
||||||
|
|
||||||
def zsplit(s: str) -> List[str]:
|
def zsplit(s: str) -> list[str]:
|
||||||
s = s.strip('\0')
|
s = s.strip('\0')
|
||||||
if s:
|
if s:
|
||||||
return s.split('\0')
|
return s.split('\0')
|
||||||
|
|
|
||||||
18
setup.cfg
18
setup.cfg
|
|
@ -1,6 +1,6 @@
|
||||||
[metadata]
|
[metadata]
|
||||||
name = pre_commit_hooks
|
name = pre_commit_hooks
|
||||||
version = 4.0.1
|
version = 6.0.0
|
||||||
description = Some out-of-the-box hooks for pre-commit.
|
description = Some out-of-the-box hooks for pre-commit.
|
||||||
long_description = file: README.md
|
long_description = file: README.md
|
||||||
long_description_content_type = text/markdown
|
long_description_content_type = text/markdown
|
||||||
|
|
@ -8,15 +8,10 @@ url = https://github.com/pre-commit/pre-commit-hooks
|
||||||
author = Anthony Sottile
|
author = Anthony Sottile
|
||||||
author_email = asottile@umich.edu
|
author_email = asottile@umich.edu
|
||||||
license = MIT
|
license = MIT
|
||||||
license_file = LICENSE
|
license_files = LICENSE
|
||||||
classifiers =
|
classifiers =
|
||||||
License :: OSI Approved :: MIT License
|
|
||||||
Programming Language :: Python :: 3
|
Programming Language :: Python :: 3
|
||||||
Programming Language :: Python :: 3 :: Only
|
Programming Language :: Python :: 3 :: Only
|
||||||
Programming Language :: Python :: 3.6
|
|
||||||
Programming Language :: Python :: 3.7
|
|
||||||
Programming Language :: Python :: 3.8
|
|
||||||
Programming Language :: Python :: 3.9
|
|
||||||
Programming Language :: Python :: Implementation :: CPython
|
Programming Language :: Python :: Implementation :: CPython
|
||||||
Programming Language :: Python :: Implementation :: PyPy
|
Programming Language :: Python :: Implementation :: PyPy
|
||||||
|
|
||||||
|
|
@ -24,8 +19,8 @@ classifiers =
|
||||||
packages = find:
|
packages = find:
|
||||||
install_requires =
|
install_requires =
|
||||||
ruamel.yaml>=0.15
|
ruamel.yaml>=0.15
|
||||||
toml
|
tomli>=1.1.0;python_version<"3.11"
|
||||||
python_requires = >=3.6.1
|
python_requires = >=3.10
|
||||||
|
|
||||||
[options.packages.find]
|
[options.packages.find]
|
||||||
exclude =
|
exclude =
|
||||||
|
|
@ -37,7 +32,6 @@ console_scripts =
|
||||||
check-added-large-files = pre_commit_hooks.check_added_large_files:main
|
check-added-large-files = pre_commit_hooks.check_added_large_files:main
|
||||||
check-ast = pre_commit_hooks.check_ast:main
|
check-ast = pre_commit_hooks.check_ast:main
|
||||||
check-builtin-literals = pre_commit_hooks.check_builtin_literals:main
|
check-builtin-literals = pre_commit_hooks.check_builtin_literals:main
|
||||||
check-byte-order-marker = pre_commit_hooks.check_byte_order_marker:main
|
|
||||||
check-case-conflict = pre_commit_hooks.check_case_conflict:main
|
check-case-conflict = pre_commit_hooks.check_case_conflict:main
|
||||||
check-docstring-first = pre_commit_hooks.check_docstring_first:main
|
check-docstring-first = pre_commit_hooks.check_docstring_first:main
|
||||||
check-executables-have-shebangs = pre_commit_hooks.check_executables_have_shebangs:main
|
check-executables-have-shebangs = pre_commit_hooks.check_executables_have_shebangs:main
|
||||||
|
|
@ -57,7 +51,6 @@ console_scripts =
|
||||||
end-of-file-fixer = pre_commit_hooks.end_of_file_fixer:main
|
end-of-file-fixer = pre_commit_hooks.end_of_file_fixer:main
|
||||||
file-contents-sorter = pre_commit_hooks.file_contents_sorter:main
|
file-contents-sorter = pre_commit_hooks.file_contents_sorter:main
|
||||||
fix-byte-order-marker = pre_commit_hooks.fix_byte_order_marker:main
|
fix-byte-order-marker = pre_commit_hooks.fix_byte_order_marker:main
|
||||||
fix-encoding-pragma = pre_commit_hooks.fix_encoding_pragma:main
|
|
||||||
forbid-new-submodules = pre_commit_hooks.forbid_new_submodules:main
|
forbid-new-submodules = pre_commit_hooks.forbid_new_submodules:main
|
||||||
mixed-line-ending = pre_commit_hooks.mixed_line_ending:main
|
mixed-line-ending = pre_commit_hooks.mixed_line_ending:main
|
||||||
name-tests-test = pre_commit_hooks.tests_should_end_in_test:main
|
name-tests-test = pre_commit_hooks.tests_should_end_in_test:main
|
||||||
|
|
@ -79,7 +72,8 @@ check_untyped_defs = true
|
||||||
disallow_any_generics = true
|
disallow_any_generics = true
|
||||||
disallow_incomplete_defs = true
|
disallow_incomplete_defs = true
|
||||||
disallow_untyped_defs = true
|
disallow_untyped_defs = true
|
||||||
no_implicit_optional = true
|
warn_redundant_casts = true
|
||||||
|
warn_unused_ignores = true
|
||||||
|
|
||||||
[mypy-testing.*]
|
[mypy-testing.*]
|
||||||
disallow_untyped_defs = false
|
disallow_untyped_defs = false
|
||||||
|
|
|
||||||
2
setup.py
2
setup.py
|
|
@ -1,2 +1,4 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from setuptools import setup
|
from setuptools import setup
|
||||||
setup()
|
setup()
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,7 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import os.path
|
import os.path
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
TESTING_DIR = os.path.abspath(os.path.dirname(__file__))
|
TESTING_DIR = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
|
@ -6,3 +9,8 @@ TESTING_DIR = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
|
||||||
def get_resource_path(path):
|
def get_resource_path(path):
|
||||||
return os.path.join(TESTING_DIR, 'resources', path)
|
return os.path.join(TESTING_DIR, 'resources', path)
|
||||||
|
|
||||||
|
|
||||||
|
def git_commit(*args, **kwargs):
|
||||||
|
cmd = ('git', 'commit', '--no-gpg-sign', '--no-verify', '--no-edit', *args)
|
||||||
|
subprocess.check_call(cmd, **kwargs)
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,13 @@
|
||||||
import distutils.spawn
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import shutil
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.check_added_large_files import find_large_added_files
|
from pre_commit_hooks.check_added_large_files import find_large_added_files
|
||||||
from pre_commit_hooks.check_added_large_files import main
|
from pre_commit_hooks.check_added_large_files import main
|
||||||
from pre_commit_hooks.util import cmd_output
|
from pre_commit_hooks.util import cmd_output
|
||||||
|
from testing.util import git_commit
|
||||||
|
|
||||||
|
|
||||||
def test_nothing_added(temp_git_dir):
|
def test_nothing_added(temp_git_dir):
|
||||||
|
|
@ -75,7 +78,7 @@ def test_integration(temp_git_dir):
|
||||||
|
|
||||||
|
|
||||||
def has_gitlfs():
|
def has_gitlfs():
|
||||||
return distutils.spawn.find_executable('git-lfs') is not None
|
return shutil.which('git-lfs') is not None
|
||||||
|
|
||||||
|
|
||||||
xfailif_no_gitlfs = pytest.mark.xfail(
|
xfailif_no_gitlfs = pytest.mark.xfail(
|
||||||
|
|
@ -84,10 +87,9 @@ xfailif_no_gitlfs = pytest.mark.xfail(
|
||||||
|
|
||||||
|
|
||||||
@xfailif_no_gitlfs
|
@xfailif_no_gitlfs
|
||||||
def test_allows_gitlfs(temp_git_dir, monkeypatch): # pragma: no cover
|
def test_allows_gitlfs(temp_git_dir): # pragma: no cover
|
||||||
with temp_git_dir.as_cwd():
|
with temp_git_dir.as_cwd():
|
||||||
monkeypatch.setenv('HOME', str(temp_git_dir))
|
cmd_output('git', 'lfs', 'install', '--local')
|
||||||
cmd_output('git', 'lfs', 'install')
|
|
||||||
temp_git_dir.join('f.py').write('a' * 10000)
|
temp_git_dir.join('f.py').write('a' * 10000)
|
||||||
cmd_output('git', 'lfs', 'track', 'f.py')
|
cmd_output('git', 'lfs', 'track', 'f.py')
|
||||||
cmd_output('git', 'add', '--', '.')
|
cmd_output('git', 'add', '--', '.')
|
||||||
|
|
@ -96,27 +98,37 @@ def test_allows_gitlfs(temp_git_dir, monkeypatch): # pragma: no cover
|
||||||
|
|
||||||
|
|
||||||
@xfailif_no_gitlfs
|
@xfailif_no_gitlfs
|
||||||
def test_moves_with_gitlfs(temp_git_dir, monkeypatch): # pragma: no cover
|
def test_moves_with_gitlfs(temp_git_dir): # pragma: no cover
|
||||||
with temp_git_dir.as_cwd():
|
with temp_git_dir.as_cwd():
|
||||||
monkeypatch.setenv('HOME', str(temp_git_dir))
|
cmd_output('git', 'lfs', 'install', '--local')
|
||||||
cmd_output('git', 'lfs', 'install')
|
|
||||||
cmd_output('git', 'lfs', 'track', 'a.bin', 'b.bin')
|
cmd_output('git', 'lfs', 'track', 'a.bin', 'b.bin')
|
||||||
# First add the file we're going to move
|
# First add the file we're going to move
|
||||||
temp_git_dir.join('a.bin').write('a' * 10000)
|
temp_git_dir.join('a.bin').write('a' * 10000)
|
||||||
cmd_output('git', 'add', '--', '.')
|
cmd_output('git', 'add', '--', '.')
|
||||||
cmd_output('git', 'commit', '--no-gpg-sign', '-am', 'foo')
|
git_commit('-am', 'foo')
|
||||||
# Now move it and make sure the hook still succeeds
|
# Now move it and make sure the hook still succeeds
|
||||||
cmd_output('git', 'mv', 'a.bin', 'b.bin')
|
cmd_output('git', 'mv', 'a.bin', 'b.bin')
|
||||||
assert main(('--maxkb', '9', 'b.bin')) == 0
|
assert main(('--maxkb', '9', 'b.bin')) == 0
|
||||||
|
|
||||||
|
|
||||||
@xfailif_no_gitlfs
|
@xfailif_no_gitlfs
|
||||||
def test_enforce_allows_gitlfs(temp_git_dir, monkeypatch): # pragma: no cover
|
def test_enforce_allows_gitlfs(temp_git_dir): # pragma: no cover
|
||||||
with temp_git_dir.as_cwd():
|
with temp_git_dir.as_cwd():
|
||||||
monkeypatch.setenv('HOME', str(temp_git_dir))
|
cmd_output('git', 'lfs', 'install', '--local')
|
||||||
cmd_output('git', 'lfs', 'install')
|
|
||||||
temp_git_dir.join('f.py').write('a' * 10000)
|
temp_git_dir.join('f.py').write('a' * 10000)
|
||||||
cmd_output('git', 'lfs', 'track', 'f.py')
|
cmd_output('git', 'lfs', 'track', 'f.py')
|
||||||
cmd_output('git', 'add', '--', '.')
|
cmd_output('git', 'add', '--', '.')
|
||||||
# With --enforce-all large files on git lfs should succeed
|
# With --enforce-all large files on git lfs should succeed
|
||||||
assert main(('--enforce-all', '--maxkb', '9', 'f.py')) == 0
|
assert main(('--enforce-all', '--maxkb', '9', 'f.py')) == 0
|
||||||
|
|
||||||
|
|
||||||
|
@xfailif_no_gitlfs
|
||||||
|
def test_enforce_allows_gitlfs_after_commit(temp_git_dir): # pragma: no cover
|
||||||
|
with temp_git_dir.as_cwd():
|
||||||
|
cmd_output('git', 'lfs', 'install', '--local')
|
||||||
|
temp_git_dir.join('f.py').write('a' * 10000)
|
||||||
|
cmd_output('git', 'lfs', 'track', 'f.py')
|
||||||
|
cmd_output('git', 'add', '--', '.')
|
||||||
|
git_commit('-am', 'foo')
|
||||||
|
# With --enforce-all large files on git lfs should succeed
|
||||||
|
assert main(('--enforce-all', '--maxkb', '9', 'f.py')) == 0
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from pre_commit_hooks.check_ast import main
|
from pre_commit_hooks.check_ast import main
|
||||||
from testing.util import get_resource_path
|
from testing.util import get_resource_path
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import ast
|
import ast
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
@ -36,11 +38,6 @@ t1 = ()
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def visitor():
|
|
||||||
return Visitor()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
('expression', 'calls'),
|
('expression', 'calls'),
|
||||||
[
|
[
|
||||||
|
|
@ -83,7 +80,8 @@ def visitor():
|
||||||
('builtins.tuple()', []),
|
('builtins.tuple()', []),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_non_dict_exprs(visitor, expression, calls):
|
def test_non_dict_exprs(expression, calls):
|
||||||
|
visitor = Visitor(ignore=set())
|
||||||
visitor.visit(ast.parse(expression))
|
visitor.visit(ast.parse(expression))
|
||||||
assert visitor.builtin_type_calls == calls
|
assert visitor.builtin_type_calls == calls
|
||||||
|
|
||||||
|
|
@ -100,7 +98,8 @@ def test_non_dict_exprs(visitor, expression, calls):
|
||||||
('builtins.dict()', []),
|
('builtins.dict()', []),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_dict_allow_kwargs_exprs(visitor, expression, calls):
|
def test_dict_allow_kwargs_exprs(expression, calls):
|
||||||
|
visitor = Visitor(ignore=set())
|
||||||
visitor.visit(ast.parse(expression))
|
visitor.visit(ast.parse(expression))
|
||||||
assert visitor.builtin_type_calls == calls
|
assert visitor.builtin_type_calls == calls
|
||||||
|
|
||||||
|
|
@ -112,17 +111,18 @@ def test_dict_allow_kwargs_exprs(visitor, expression, calls):
|
||||||
('dict(a=1, b=2, c=3)', [Call('dict', 1, 0)]),
|
('dict(a=1, b=2, c=3)', [Call('dict', 1, 0)]),
|
||||||
("dict(**{'a': 1, 'b': 2, 'c': 3})", [Call('dict', 1, 0)]),
|
("dict(**{'a': 1, 'b': 2, 'c': 3})", [Call('dict', 1, 0)]),
|
||||||
('builtins.dict()', []),
|
('builtins.dict()', []),
|
||||||
|
pytest.param('f(dict())', [Call('dict', 1, 2)], id='nested'),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_dict_no_allow_kwargs_exprs(expression, calls):
|
def test_dict_no_allow_kwargs_exprs(expression, calls):
|
||||||
visitor = Visitor(allow_dict_kwargs=False)
|
visitor = Visitor(ignore=set(), allow_dict_kwargs=False)
|
||||||
visitor.visit(ast.parse(expression))
|
visitor.visit(ast.parse(expression))
|
||||||
assert visitor.builtin_type_calls == calls
|
assert visitor.builtin_type_calls == calls
|
||||||
|
|
||||||
|
|
||||||
def test_ignore_constructors():
|
def test_ignore_constructors():
|
||||||
visitor = Visitor(
|
visitor = Visitor(
|
||||||
ignore=('complex', 'dict', 'float', 'int', 'list', 'str', 'tuple'),
|
ignore={'complex', 'dict', 'float', 'int', 'list', 'str', 'tuple'},
|
||||||
)
|
)
|
||||||
visitor.visit(ast.parse(BUILTIN_CONSTRUCTORS))
|
visitor.visit(ast.parse(BUILTIN_CONSTRUCTORS))
|
||||||
assert visitor.builtin_type_calls == []
|
assert visitor.builtin_type_calls == []
|
||||||
|
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
from pre_commit_hooks import check_byte_order_marker
|
|
||||||
|
|
||||||
|
|
||||||
def test_failure(tmpdir):
|
|
||||||
f = tmpdir.join('f.txt')
|
|
||||||
f.write_text('ohai', encoding='utf-8-sig')
|
|
||||||
assert check_byte_order_marker.main((str(f),)) == 1
|
|
||||||
|
|
||||||
|
|
||||||
def test_success(tmpdir):
|
|
||||||
f = tmpdir.join('f.txt')
|
|
||||||
f.write_text('ohai', encoding='utf-8')
|
|
||||||
assert check_byte_order_marker.main((str(f),)) == 0
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
@ -6,6 +8,7 @@ from pre_commit_hooks.check_case_conflict import find_conflicting_filenames
|
||||||
from pre_commit_hooks.check_case_conflict import main
|
from pre_commit_hooks.check_case_conflict import main
|
||||||
from pre_commit_hooks.check_case_conflict import parents
|
from pre_commit_hooks.check_case_conflict import parents
|
||||||
from pre_commit_hooks.util import cmd_output
|
from pre_commit_hooks.util import cmd_output
|
||||||
|
from testing.util import git_commit
|
||||||
|
|
||||||
skip_win32 = pytest.mark.skipif(
|
skip_win32 = pytest.mark.skipif(
|
||||||
sys.platform == 'win32',
|
sys.platform == 'win32',
|
||||||
|
|
@ -85,7 +88,7 @@ def test_file_conflicts_with_committed_file(temp_git_dir):
|
||||||
with temp_git_dir.as_cwd():
|
with temp_git_dir.as_cwd():
|
||||||
temp_git_dir.join('f.py').write("print('hello world')")
|
temp_git_dir.join('f.py').write("print('hello world')")
|
||||||
cmd_output('git', 'add', 'f.py')
|
cmd_output('git', 'add', 'f.py')
|
||||||
cmd_output('git', 'commit', '--no-gpg-sign', '-n', '-m', 'Add f.py')
|
git_commit('-m', 'Add f.py')
|
||||||
|
|
||||||
temp_git_dir.join('F.py').write("print('hello world')")
|
temp_git_dir.join('F.py').write("print('hello world')")
|
||||||
cmd_output('git', 'add', 'F.py')
|
cmd_output('git', 'add', 'F.py')
|
||||||
|
|
@ -98,7 +101,7 @@ def test_file_conflicts_with_committed_dir(temp_git_dir):
|
||||||
with temp_git_dir.as_cwd():
|
with temp_git_dir.as_cwd():
|
||||||
temp_git_dir.mkdir('dir').join('x').write('foo')
|
temp_git_dir.mkdir('dir').join('x').write('foo')
|
||||||
cmd_output('git', 'add', '-A')
|
cmd_output('git', 'add', '-A')
|
||||||
cmd_output('git', 'commit', '--no-gpg-sign', '-n', '-m', 'Add f.py')
|
git_commit('-m', 'Add f.py')
|
||||||
|
|
||||||
temp_git_dir.join('DIR').write('foo')
|
temp_git_dir.join('DIR').write('foo')
|
||||||
cmd_output('git', 'add', '-A')
|
cmd_output('git', 'add', '-A')
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.check_docstring_first import check_docstring_first
|
from pre_commit_hooks.check_docstring_first import check_docstring_first
|
||||||
|
|
@ -15,7 +17,7 @@ TESTS = (
|
||||||
b'from __future__ import unicode_literals\n'
|
b'from __future__ import unicode_literals\n'
|
||||||
b'"foo"\n',
|
b'"foo"\n',
|
||||||
1,
|
1,
|
||||||
'{filename}:2 Module docstring appears after code '
|
'{filename}:2: Module docstring appears after code '
|
||||||
'(code seen on line 1).\n',
|
'(code seen on line 1).\n',
|
||||||
),
|
),
|
||||||
# Test double docstring
|
# Test double docstring
|
||||||
|
|
@ -24,7 +26,7 @@ TESTS = (
|
||||||
b'from __future__ import absolute_import\n'
|
b'from __future__ import absolute_import\n'
|
||||||
b'"fake docstring"\n',
|
b'"fake docstring"\n',
|
||||||
1,
|
1,
|
||||||
'{filename}:3 Multiple module docstrings '
|
'{filename}:3: Multiple module docstrings '
|
||||||
'(first docstring on line 1).\n',
|
'(first docstring on line 1).\n',
|
||||||
),
|
),
|
||||||
# Test multiple lines of code above
|
# Test multiple lines of code above
|
||||||
|
|
@ -33,7 +35,7 @@ TESTS = (
|
||||||
b'import sys\n'
|
b'import sys\n'
|
||||||
b'"docstring"\n',
|
b'"docstring"\n',
|
||||||
1,
|
1,
|
||||||
'{filename}:3 Module docstring appears after code '
|
'{filename}:3: Module docstring appears after code '
|
||||||
'(code seen on line 1).\n',
|
'(code seen on line 1).\n',
|
||||||
),
|
),
|
||||||
# String literals in expressions are ok.
|
# String literals in expressions are ok.
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
|
||||||
63
tests/check_illegal_windows_names_test.py
Normal file
63
tests/check_illegal_windows_names_test.py
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os.path
|
||||||
|
import re
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from pre_commit_hooks.check_yaml import yaml
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='module')
|
||||||
|
def hook_re():
|
||||||
|
here = os.path.dirname(__file__)
|
||||||
|
with open(os.path.join(here, '..', '.pre-commit-hooks.yaml')) as f:
|
||||||
|
hook_defs = yaml.load(f)
|
||||||
|
hook, = (
|
||||||
|
hook
|
||||||
|
for hook in hook_defs
|
||||||
|
if hook['id'] == 'check-illegal-windows-names'
|
||||||
|
)
|
||||||
|
yield re.compile(hook['files'])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
's',
|
||||||
|
(
|
||||||
|
pytest.param('aux.txt', id='with ext'),
|
||||||
|
pytest.param('aux', id='without ext'),
|
||||||
|
pytest.param('AuX.tXt', id='capitals'),
|
||||||
|
pytest.param('com7.dat', id='com with digit'),
|
||||||
|
pytest.param(':', id='bare colon'),
|
||||||
|
pytest.param('file:Zone.Identifier', id='mid colon'),
|
||||||
|
pytest.param('path/COM¹.json', id='com with superscript'),
|
||||||
|
pytest.param('dir/LPT³.toml', id='lpt with superscript'),
|
||||||
|
pytest.param('with < less than', id='with less than'),
|
||||||
|
pytest.param('Fast or Slow?.md', id='with question mark'),
|
||||||
|
pytest.param('with "double" quotes', id='with double quotes'),
|
||||||
|
pytest.param('with_null\x00byte', id='with null byte'),
|
||||||
|
pytest.param('ends_with.', id='ends with period'),
|
||||||
|
pytest.param('ends_with ', id='ends with space'),
|
||||||
|
pytest.param('ends_with\t', id='ends with tab'),
|
||||||
|
pytest.param('dir/ends./with.txt', id='directory ends with period'),
|
||||||
|
pytest.param('dir/ends /with.txt', id='directory ends with space'),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
def test_check_illegal_windows_names_matches(hook_re, s):
|
||||||
|
assert hook_re.search(s)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
's',
|
||||||
|
(
|
||||||
|
pytest.param('README.md', id='standard file'),
|
||||||
|
pytest.param('foo.aux', id='as ext'),
|
||||||
|
pytest.param('com.dat', id='com without digit'),
|
||||||
|
pytest.param('.python-version', id='starts with period'),
|
||||||
|
pytest.param(' pseudo nan', id='with spaces'),
|
||||||
|
pytest.param('!@#$%^&;=≤\'~`¡¿€🤗', id='with allowed characters'),
|
||||||
|
pytest.param('path.to/file.py', id='standard path'),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
def test_check_illegal_windows_names_does_not_match(hook_re, s):
|
||||||
|
assert hook_re.search(s) is None
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.check_json import main
|
from pre_commit_hooks.check_json import main
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
|
|
@ -6,6 +8,7 @@ import pytest
|
||||||
from pre_commit_hooks.check_merge_conflict import main
|
from pre_commit_hooks.check_merge_conflict import main
|
||||||
from pre_commit_hooks.util import cmd_output
|
from pre_commit_hooks.util import cmd_output
|
||||||
from testing.util import get_resource_path
|
from testing.util import get_resource_path
|
||||||
|
from testing.util import git_commit
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|
@ -20,19 +23,19 @@ def f1_is_a_conflict_file(tmpdir):
|
||||||
with repo1.as_cwd():
|
with repo1.as_cwd():
|
||||||
repo1_f1.ensure()
|
repo1_f1.ensure()
|
||||||
cmd_output('git', 'add', '.')
|
cmd_output('git', 'add', '.')
|
||||||
cmd_output('git', 'commit', '--no-gpg-sign', '-m', 'commit1')
|
git_commit('-m', 'commit1')
|
||||||
|
|
||||||
cmd_output('git', 'clone', str(repo1), str(repo2))
|
cmd_output('git', 'clone', str(repo1), str(repo2))
|
||||||
|
|
||||||
# Commit in master
|
# Commit in mainline
|
||||||
with repo1.as_cwd():
|
with repo1.as_cwd():
|
||||||
repo1_f1.write('parent\n')
|
repo1_f1.write('parent\n')
|
||||||
cmd_output('git', 'commit', '--no-gpg-sign', '-am', 'master commit2')
|
git_commit('-am', 'mainline commit2')
|
||||||
|
|
||||||
# Commit in clone and pull
|
# Commit in clone and pull
|
||||||
with repo2.as_cwd():
|
with repo2.as_cwd():
|
||||||
repo2_f1.write('child\n')
|
repo2_f1.write('child\n')
|
||||||
cmd_output('git', 'commit', '--no-gpg-sign', '-am', 'clone commit2')
|
git_commit('-am', 'clone commit2')
|
||||||
cmd_output('git', 'pull', '--no-rebase', retcode=None)
|
cmd_output('git', 'pull', '--no-rebase', retcode=None)
|
||||||
# We should end up in a merge conflict!
|
# We should end up in a merge conflict!
|
||||||
f1 = repo2_f1.read()
|
f1 = repo2_f1.read()
|
||||||
|
|
@ -75,20 +78,20 @@ def repository_pending_merge(tmpdir):
|
||||||
with repo1.as_cwd():
|
with repo1.as_cwd():
|
||||||
repo1_f1.ensure()
|
repo1_f1.ensure()
|
||||||
cmd_output('git', 'add', '.')
|
cmd_output('git', 'add', '.')
|
||||||
cmd_output('git', 'commit', '--no-gpg-sign', '-m', 'commit1')
|
git_commit('-m', 'commit1')
|
||||||
|
|
||||||
cmd_output('git', 'clone', str(repo1), str(repo2))
|
cmd_output('git', 'clone', str(repo1), str(repo2))
|
||||||
|
|
||||||
# Commit in master
|
# Commit in mainline
|
||||||
with repo1.as_cwd():
|
with repo1.as_cwd():
|
||||||
repo1_f1.write('parent\n')
|
repo1_f1.write('parent\n')
|
||||||
cmd_output('git', 'commit', '--no-gpg-sign', '-am', 'master commit2')
|
git_commit('-am', 'mainline commit2')
|
||||||
|
|
||||||
# Commit in clone and pull without committing
|
# Commit in clone and pull without committing
|
||||||
with repo2.as_cwd():
|
with repo2.as_cwd():
|
||||||
repo2_f2.write('child\n')
|
repo2_f2.write('child\n')
|
||||||
cmd_output('git', 'add', '.')
|
cmd_output('git', 'add', '.')
|
||||||
cmd_output('git', 'commit', '--no-gpg-sign', '-m', 'clone commit2')
|
git_commit('-m', 'clone commit2')
|
||||||
cmd_output('git', 'pull', '--no-commit', '--no-rebase')
|
cmd_output('git', 'pull', '--no-commit', '--no-rebase')
|
||||||
# We should end up in a pending merge
|
# We should end up in a pending merge
|
||||||
assert repo2_f1.read() == 'parent\n'
|
assert repo2_f1.read() == 'parent\n'
|
||||||
|
|
@ -98,12 +101,18 @@ def repository_pending_merge(tmpdir):
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures('f1_is_a_conflict_file')
|
@pytest.mark.usefixtures('f1_is_a_conflict_file')
|
||||||
def test_merge_conflicts_git():
|
def test_merge_conflicts_git(capsys):
|
||||||
assert main(['f1']) == 1
|
assert main(['f1']) == 1
|
||||||
|
out, _ = capsys.readouterr()
|
||||||
|
assert out == (
|
||||||
|
"f1:1: Merge conflict string '<<<<<<<' found\n"
|
||||||
|
"f1:3: Merge conflict string '=======' found\n"
|
||||||
|
"f1:5: Merge conflict string '>>>>>>>' found\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
'contents', (b'<<<<<<< HEAD\n', b'=======\n', b'>>>>>>> master\n'),
|
'contents', (b'<<<<<<< HEAD\n', b'=======\n', b'>>>>>>> main\n'),
|
||||||
)
|
)
|
||||||
def test_merge_conflicts_failing(contents, repository_pending_merge):
|
def test_merge_conflicts_failing(contents, repository_pending_merge):
|
||||||
repository_pending_merge.join('f2').write_binary(contents)
|
repository_pending_merge.join('f2').write_binary(contents)
|
||||||
|
|
@ -134,3 +143,15 @@ def test_care_when_assumed_merge(tmpdir):
|
||||||
f = tmpdir.join('README.md')
|
f = tmpdir.join('README.md')
|
||||||
f.write_binary(b'problem\n=======\n')
|
f.write_binary(b'problem\n=======\n')
|
||||||
assert main([str(f.realpath()), '--assume-in-merge']) == 1
|
assert main([str(f.realpath()), '--assume-in-merge']) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_worktree_merge_conflicts(f1_is_a_conflict_file, tmpdir, capsys):
|
||||||
|
worktree = tmpdir.join('worktree')
|
||||||
|
cmd_output('git', 'worktree', 'add', str(worktree))
|
||||||
|
with worktree.as_cwd():
|
||||||
|
cmd_output(
|
||||||
|
'git', 'pull', '--no-rebase', 'origin', 'HEAD', retcode=None,
|
||||||
|
)
|
||||||
|
msg = f1_is_a_conflict_file.join('.git/worktrees/worktree/MERGE_MSG')
|
||||||
|
assert msg.exists()
|
||||||
|
test_merge_conflicts_git(capsys)
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from pre_commit_hooks.check_toml import main
|
from pre_commit_hooks.check_toml import main
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from pre_commit_hooks.check_vcs_permalinks import main
|
from pre_commit_hooks.check_vcs_permalinks import main
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -14,9 +16,9 @@ def test_passing(tmpdir):
|
||||||
# tags are ok
|
# tags are ok
|
||||||
b'https://github.com/asottile/test/blob/1.0.0/foo%20bar#L1\n'
|
b'https://github.com/asottile/test/blob/1.0.0/foo%20bar#L1\n'
|
||||||
# links to files but not line numbers are ok
|
# links to files but not line numbers are ok
|
||||||
b'https://github.com/asottile/test/blob/master/foo%20bar\n'
|
b'https://github.com/asottile/test/blob/main/foo%20bar\n'
|
||||||
# regression test for overly-greedy regex
|
# regression test for overly-greedy regex
|
||||||
b'https://github.com/ yes / no ? /blob/master/foo#L1\n',
|
b'https://github.com/ yes / no ? /blob/main/foo#L1\n',
|
||||||
)
|
)
|
||||||
assert not main((str(f),))
|
assert not main((str(f),))
|
||||||
|
|
||||||
|
|
@ -24,17 +26,15 @@ def test_passing(tmpdir):
|
||||||
def test_failing(tmpdir, capsys):
|
def test_failing(tmpdir, capsys):
|
||||||
with tmpdir.as_cwd():
|
with tmpdir.as_cwd():
|
||||||
tmpdir.join('f.txt').write_binary(
|
tmpdir.join('f.txt').write_binary(
|
||||||
b'https://github.com/asottile/test/blob/master/foo#L1\n'
|
b'https://github.com/asottile/test/blob/main/foo#L1\n'
|
||||||
b'https://example.com/asottile/test/blob/master/foo#L1\n'
|
|
||||||
b'https://example.com/asottile/test/blob/main/foo#L1\n',
|
b'https://example.com/asottile/test/blob/main/foo#L1\n',
|
||||||
)
|
)
|
||||||
|
|
||||||
assert main(('f.txt', '--additional-github-domain', 'example.com'))
|
assert main(('f.txt', '--additional-github-domain', 'example.com'))
|
||||||
out, _ = capsys.readouterr()
|
out, _ = capsys.readouterr()
|
||||||
assert out == (
|
assert out == (
|
||||||
'f.txt:1:https://github.com/asottile/test/blob/master/foo#L1\n'
|
'f.txt:1:https://github.com/asottile/test/blob/main/foo#L1\n'
|
||||||
'f.txt:2:https://example.com/asottile/test/blob/master/foo#L1\n'
|
'f.txt:2:https://example.com/asottile/test/blob/main/foo#L1\n'
|
||||||
'f.txt:3:https://example.com/asottile/test/blob/main/foo#L1\n'
|
|
||||||
'\n'
|
'\n'
|
||||||
'Non-permanent github link detected.\n'
|
'Non-permanent github link detected.\n'
|
||||||
'On any page on github press [y] to load a permalink.\n'
|
'On any page on github press [y] to load a permalink.\n'
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.check_xml import main
|
from pre_commit_hooks.check_xml import main
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.check_yaml import main
|
from pre_commit_hooks.check_yaml import main
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.util import cmd_output
|
from pre_commit_hooks.util import cmd_output
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import ast
|
import ast
|
||||||
|
|
||||||
from pre_commit_hooks.debug_statement_hook import Debug
|
from pre_commit_hooks.debug_statement_hook import Debug
|
||||||
|
|
@ -53,7 +55,9 @@ def test_non_utf8_file(tmpdir):
|
||||||
assert main((str(f_py),)) == 0
|
assert main((str(f_py),)) == 0
|
||||||
|
|
||||||
|
|
||||||
def test_py37_breakpoint(tmpdir):
|
def test_py37_breakpoint(tmpdir, capsys):
|
||||||
f_py = tmpdir.join('f.py')
|
f_py = tmpdir.join('f.py')
|
||||||
f_py.write('def f():\n breakpoint()\n')
|
f_py.write('def f():\n breakpoint()\n')
|
||||||
assert main((str(f_py),)) == 1
|
assert main((str(f_py),)) == 1
|
||||||
|
out, _ = capsys.readouterr()
|
||||||
|
assert out == f'{f_py}:2:4: breakpoint called\n'
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
|
@ -5,6 +7,7 @@ import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.destroyed_symlinks import find_destroyed_symlinks
|
from pre_commit_hooks.destroyed_symlinks import find_destroyed_symlinks
|
||||||
from pre_commit_hooks.destroyed_symlinks import main
|
from pre_commit_hooks.destroyed_symlinks import main
|
||||||
|
from testing.util import git_commit
|
||||||
|
|
||||||
TEST_SYMLINK = 'test_symlink'
|
TEST_SYMLINK = 'test_symlink'
|
||||||
TEST_SYMLINK_TARGET = '/doesnt/really/matters'
|
TEST_SYMLINK_TARGET = '/doesnt/really/matters'
|
||||||
|
|
@ -23,9 +26,7 @@ def repo_with_destroyed_symlink(tmpdir):
|
||||||
with open(TEST_FILE, 'w') as f:
|
with open(TEST_FILE, 'w') as f:
|
||||||
print('some random content', file=f)
|
print('some random content', file=f)
|
||||||
subprocess.check_call(('git', 'add', '.'))
|
subprocess.check_call(('git', 'add', '.'))
|
||||||
subprocess.check_call(
|
git_commit('-m', 'initial')
|
||||||
('git', 'commit', '--no-gpg-sign', '-m', 'initial'),
|
|
||||||
)
|
|
||||||
assert b'120000 ' in subprocess.check_output(
|
assert b'120000 ' in subprocess.check_output(
|
||||||
('git', 'cat-file', '-p', 'HEAD^{tree}'),
|
('git', 'cat-file', '-p', 'HEAD^{tree}'),
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.detect_private_key import main
|
from pre_commit_hooks.detect_private_key import main
|
||||||
|
|
@ -10,6 +12,8 @@ TESTS = (
|
||||||
(b'-----BEGIN OPENSSH PRIVATE KEY-----', 1),
|
(b'-----BEGIN OPENSSH PRIVATE KEY-----', 1),
|
||||||
(b'PuTTY-User-Key-File-2: ssh-rsa', 1),
|
(b'PuTTY-User-Key-File-2: ssh-rsa', 1),
|
||||||
(b'---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----', 1),
|
(b'---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----', 1),
|
||||||
|
(b'-----BEGIN ENCRYPTED PRIVATE KEY-----', 1),
|
||||||
|
(b'-----BEGIN OpenVPN Static key V1-----', 1),
|
||||||
(b'ssh-rsa DATA', 0),
|
(b'ssh-rsa DATA', 0),
|
||||||
(b'ssh-dsa DATA', 0),
|
(b'ssh-dsa DATA', 0),
|
||||||
# Some arbitrary binary data
|
# Some arbitrary binary data
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import io
|
import io
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.file_contents_sorter import FAIL
|
from pre_commit_hooks.file_contents_sorter import FAIL
|
||||||
|
|
@ -8,7 +10,9 @@ from pre_commit_hooks.file_contents_sorter import PASS
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
('input_s', 'argv', 'expected_retval', 'output'),
|
('input_s', 'argv', 'expected_retval', 'output'),
|
||||||
(
|
(
|
||||||
(b'', [], FAIL, b'\n'),
|
(b'', [], PASS, b''),
|
||||||
|
(b'\n', [], FAIL, b''),
|
||||||
|
(b'\n\n', [], FAIL, b''),
|
||||||
(b'lonesome\n', [], PASS, b'lonesome\n'),
|
(b'lonesome\n', [], PASS, b'lonesome\n'),
|
||||||
(b'missing_newline', [], FAIL, b'missing_newline\n'),
|
(b'missing_newline', [], FAIL, b'missing_newline\n'),
|
||||||
(b'newline\nmissing', [], FAIL, b'missing\nnewline\n'),
|
(b'newline\nmissing', [], FAIL, b'missing\nnewline\n'),
|
||||||
|
|
@ -63,18 +67,6 @@ from pre_commit_hooks.file_contents_sorter import PASS
|
||||||
FAIL,
|
FAIL,
|
||||||
b'Fie\nFoe\nfee\nfum\n',
|
b'Fie\nFoe\nfee\nfum\n',
|
||||||
),
|
),
|
||||||
(
|
|
||||||
b'fee\nFie\nFoe\nfum\n',
|
|
||||||
['--unique', '--ignore-case'],
|
|
||||||
PASS,
|
|
||||||
b'fee\nFie\nFoe\nfum\n',
|
|
||||||
),
|
|
||||||
(
|
|
||||||
b'fee\nfee\nFie\nFoe\nfum\n',
|
|
||||||
['--unique', '--ignore-case'],
|
|
||||||
FAIL,
|
|
||||||
b'fee\nFie\nFoe\nfum\n',
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
def test_integration(input_s, argv, expected_retval, output, tmpdir):
|
def test_integration(input_s, argv, expected_retval, output, tmpdir):
|
||||||
|
|
@ -85,3 +77,24 @@ def test_integration(input_s, argv, expected_retval, output, tmpdir):
|
||||||
|
|
||||||
assert path.read_binary() == output
|
assert path.read_binary() == output
|
||||||
assert output_retval == expected_retval
|
assert output_retval == expected_retval
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
('input_s', 'argv'),
|
||||||
|
(
|
||||||
|
(
|
||||||
|
b'fee\nFie\nFoe\nfum\n',
|
||||||
|
['--unique', '--ignore-case'],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
b'fee\nfee\nFie\nFoe\nfum\n',
|
||||||
|
['--unique', '--ignore-case'],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
def test_integration_invalid_args(input_s, argv, tmpdir):
|
||||||
|
path = tmpdir.join('file.txt')
|
||||||
|
path.write_binary(input_s)
|
||||||
|
|
||||||
|
with pytest.raises(SystemExit):
|
||||||
|
main([str(path)] + argv)
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from pre_commit_hooks import fix_byte_order_marker
|
from pre_commit_hooks import fix_byte_order_marker
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,159 +0,0 @@
|
||||||
import io
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from pre_commit_hooks.fix_encoding_pragma import _normalize_pragma
|
|
||||||
from pre_commit_hooks.fix_encoding_pragma import fix_encoding_pragma
|
|
||||||
from pre_commit_hooks.fix_encoding_pragma import main
|
|
||||||
|
|
||||||
|
|
||||||
def test_integration_inserting_pragma(tmpdir):
|
|
||||||
path = tmpdir.join('foo.py')
|
|
||||||
path.write_binary(b'import httplib\n')
|
|
||||||
|
|
||||||
assert main((str(path),)) == 1
|
|
||||||
|
|
||||||
assert path.read_binary() == (
|
|
||||||
b'# -*- coding: utf-8 -*-\n'
|
|
||||||
b'import httplib\n'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_integration_ok(tmpdir):
|
|
||||||
path = tmpdir.join('foo.py')
|
|
||||||
path.write_binary(b'# -*- coding: utf-8 -*-\nx = 1\n')
|
|
||||||
assert main((str(path),)) == 0
|
|
||||||
|
|
||||||
|
|
||||||
def test_integration_remove(tmpdir):
|
|
||||||
path = tmpdir.join('foo.py')
|
|
||||||
path.write_binary(b'# -*- coding: utf-8 -*-\nx = 1\n')
|
|
||||||
|
|
||||||
assert main((str(path), '--remove')) == 1
|
|
||||||
|
|
||||||
assert path.read_binary() == b'x = 1\n'
|
|
||||||
|
|
||||||
|
|
||||||
def test_integration_remove_ok(tmpdir):
|
|
||||||
path = tmpdir.join('foo.py')
|
|
||||||
path.write_binary(b'x = 1\n')
|
|
||||||
assert main((str(path), '--remove')) == 0
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
'input_str',
|
|
||||||
(
|
|
||||||
b'',
|
|
||||||
(
|
|
||||||
b'# -*- coding: utf-8 -*-\n'
|
|
||||||
b'x = 1\n'
|
|
||||||
),
|
|
||||||
(
|
|
||||||
b'#!/usr/bin/env python\n'
|
|
||||||
b'# -*- coding: utf-8 -*-\n'
|
|
||||||
b'foo = "bar"\n'
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
def test_ok_inputs(input_str):
|
|
||||||
bytesio = io.BytesIO(input_str)
|
|
||||||
assert fix_encoding_pragma(bytesio) == 0
|
|
||||||
bytesio.seek(0)
|
|
||||||
assert bytesio.read() == input_str
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
('input_str', 'output'),
|
|
||||||
(
|
|
||||||
(
|
|
||||||
b'import httplib\n',
|
|
||||||
b'# -*- coding: utf-8 -*-\n'
|
|
||||||
b'import httplib\n',
|
|
||||||
),
|
|
||||||
(
|
|
||||||
b'#!/usr/bin/env python\n'
|
|
||||||
b'x = 1\n',
|
|
||||||
b'#!/usr/bin/env python\n'
|
|
||||||
b'# -*- coding: utf-8 -*-\n'
|
|
||||||
b'x = 1\n',
|
|
||||||
),
|
|
||||||
(
|
|
||||||
b'#coding=utf-8\n'
|
|
||||||
b'x = 1\n',
|
|
||||||
b'# -*- coding: utf-8 -*-\n'
|
|
||||||
b'x = 1\n',
|
|
||||||
),
|
|
||||||
(
|
|
||||||
b'#!/usr/bin/env python\n'
|
|
||||||
b'#coding=utf8\n'
|
|
||||||
b'x = 1\n',
|
|
||||||
b'#!/usr/bin/env python\n'
|
|
||||||
b'# -*- coding: utf-8 -*-\n'
|
|
||||||
b'x = 1\n',
|
|
||||||
),
|
|
||||||
# These should each get truncated
|
|
||||||
(b'#coding: utf-8\n', b''),
|
|
||||||
(b'# -*- coding: utf-8 -*-\n', b''),
|
|
||||||
(b'#!/usr/bin/env python\n', b''),
|
|
||||||
(b'#!/usr/bin/env python\n#coding: utf8\n', b''),
|
|
||||||
(b'#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n', b''),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
def test_not_ok_inputs(input_str, output):
|
|
||||||
bytesio = io.BytesIO(input_str)
|
|
||||||
assert fix_encoding_pragma(bytesio) == 1
|
|
||||||
bytesio.seek(0)
|
|
||||||
assert bytesio.read() == output
|
|
||||||
|
|
||||||
|
|
||||||
def test_ok_input_alternate_pragma():
|
|
||||||
input_s = b'# coding: utf-8\nx = 1\n'
|
|
||||||
bytesio = io.BytesIO(input_s)
|
|
||||||
ret = fix_encoding_pragma(bytesio, expected_pragma=b'# coding: utf-8')
|
|
||||||
assert ret == 0
|
|
||||||
bytesio.seek(0)
|
|
||||||
assert bytesio.read() == input_s
|
|
||||||
|
|
||||||
|
|
||||||
def test_not_ok_input_alternate_pragma():
|
|
||||||
bytesio = io.BytesIO(b'x = 1\n')
|
|
||||||
ret = fix_encoding_pragma(bytesio, expected_pragma=b'# coding: utf-8')
|
|
||||||
assert ret == 1
|
|
||||||
bytesio.seek(0)
|
|
||||||
assert bytesio.read() == b'# coding: utf-8\nx = 1\n'
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
('input_s', 'expected'),
|
|
||||||
(
|
|
||||||
('# coding: utf-8', b'# coding: utf-8'),
|
|
||||||
# trailing whitespace
|
|
||||||
('# coding: utf-8\n', b'# coding: utf-8'),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
def test_normalize_pragma(input_s, expected):
|
|
||||||
assert _normalize_pragma(input_s) == expected
|
|
||||||
|
|
||||||
|
|
||||||
def test_integration_alternate_pragma(tmpdir, capsys):
|
|
||||||
f = tmpdir.join('f.py')
|
|
||||||
f.write('x = 1\n')
|
|
||||||
|
|
||||||
pragma = '# coding: utf-8'
|
|
||||||
assert main((str(f), '--pragma', pragma)) == 1
|
|
||||||
assert f.read() == '# coding: utf-8\nx = 1\n'
|
|
||||||
out, _ = capsys.readouterr()
|
|
||||||
assert out == f'Added `# coding: utf-8` to {str(f)}\n'
|
|
||||||
|
|
||||||
|
|
||||||
def test_crlf_ok(tmpdir):
|
|
||||||
f = tmpdir.join('f.py')
|
|
||||||
f.write_binary(b'# -*- coding: utf-8 -*-\r\nx = 1\r\n')
|
|
||||||
assert not main((str(f),))
|
|
||||||
|
|
||||||
|
|
||||||
def test_crfl_adds(tmpdir):
|
|
||||||
f = tmpdir.join('f.py')
|
|
||||||
f.write_binary(b'x = 1\r\n')
|
|
||||||
assert main((str(f),))
|
|
||||||
assert f.read_binary() == b'# -*- coding: utf-8 -*-\r\nx = 1\r\n'
|
|
||||||
|
|
@ -1,22 +1,22 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.forbid_new_submodules import main
|
from pre_commit_hooks.forbid_new_submodules import main
|
||||||
|
from testing.util import git_commit
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def git_dir_with_git_dir(tmpdir):
|
def git_dir_with_git_dir(tmpdir):
|
||||||
with tmpdir.as_cwd():
|
with tmpdir.as_cwd():
|
||||||
subprocess.check_call(('git', 'init', '.'))
|
subprocess.check_call(('git', 'init', '.'))
|
||||||
subprocess.check_call((
|
git_commit('--allow-empty', '-m', 'init')
|
||||||
'git', 'commit', '-m', 'init', '--allow-empty', '--no-gpg-sign',
|
|
||||||
))
|
|
||||||
subprocess.check_call(('git', 'init', 'foo'))
|
subprocess.check_call(('git', 'init', 'foo'))
|
||||||
subprocess.check_call(
|
git_commit('--allow-empty', '-m', 'init', cwd=str(tmpdir.join('foo')))
|
||||||
('git', 'commit', '-m', 'init', '--allow-empty', '--no-gpg-sign'),
|
|
||||||
cwd=str(tmpdir.join('foo')),
|
|
||||||
)
|
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -31,7 +31,24 @@ def git_dir_with_git_dir(tmpdir):
|
||||||
)
|
)
|
||||||
def test_main_new_submodule(git_dir_with_git_dir, capsys, cmd):
|
def test_main_new_submodule(git_dir_with_git_dir, capsys, cmd):
|
||||||
subprocess.check_call(cmd)
|
subprocess.check_call(cmd)
|
||||||
assert main() == 1
|
assert main(('random_non-related_file',)) == 0
|
||||||
|
assert main(('foo',)) == 1
|
||||||
|
out, _ = capsys.readouterr()
|
||||||
|
assert out.startswith('foo: new submodule introduced\n')
|
||||||
|
|
||||||
|
|
||||||
|
def test_main_new_submodule_committed(git_dir_with_git_dir, capsys):
|
||||||
|
rev_parse_cmd = ('git', 'rev-parse', 'HEAD')
|
||||||
|
from_ref = subprocess.check_output(rev_parse_cmd).decode().strip()
|
||||||
|
subprocess.check_call(('git', 'submodule', 'add', './foo'))
|
||||||
|
git_commit('-m', 'new submodule')
|
||||||
|
to_ref = subprocess.check_output(rev_parse_cmd).decode().strip()
|
||||||
|
with mock.patch.dict(
|
||||||
|
os.environ,
|
||||||
|
{'PRE_COMMIT_FROM_REF': from_ref, 'PRE_COMMIT_TO_REF': to_ref},
|
||||||
|
):
|
||||||
|
assert main(('random_non-related_file',)) == 0
|
||||||
|
assert main(('foo',)) == 1
|
||||||
out, _ = capsys.readouterr()
|
out, _ = capsys.readouterr()
|
||||||
assert out.startswith('foo: new submodule introduced\n')
|
assert out.startswith('foo: new submodule introduced\n')
|
||||||
|
|
||||||
|
|
@ -39,4 +56,4 @@ def test_main_new_submodule(git_dir_with_git_dir, capsys, cmd):
|
||||||
def test_main_no_new_submodule(git_dir_with_git_dir):
|
def test_main_no_new_submodule(git_dir_with_git_dir):
|
||||||
open('test.py', 'a+').close()
|
open('test.py', 'a+').close()
|
||||||
subprocess.check_call(('git', 'add', 'test.py'))
|
subprocess.check_call(('git', 'add', 'test.py'))
|
||||||
assert main() == 0
|
assert main(('test.py',)) == 0
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.mixed_line_ending import main
|
from pre_commit_hooks.mixed_line_ending import main
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,23 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.no_commit_to_branch import is_on_branch
|
from pre_commit_hooks.no_commit_to_branch import is_on_branch
|
||||||
from pre_commit_hooks.no_commit_to_branch import main
|
from pre_commit_hooks.no_commit_to_branch import main
|
||||||
from pre_commit_hooks.util import cmd_output
|
from pre_commit_hooks.util import cmd_output
|
||||||
|
from testing.util import git_commit
|
||||||
|
|
||||||
|
|
||||||
def test_other_branch(temp_git_dir):
|
def test_other_branch(temp_git_dir):
|
||||||
with temp_git_dir.as_cwd():
|
with temp_git_dir.as_cwd():
|
||||||
cmd_output('git', 'checkout', '-b', 'anotherbranch')
|
cmd_output('git', 'checkout', '-b', 'anotherbranch')
|
||||||
assert is_on_branch({'master'}) is False
|
assert is_on_branch({'placeholder'}) is False
|
||||||
|
|
||||||
|
|
||||||
def test_multi_branch(temp_git_dir):
|
def test_multi_branch(temp_git_dir):
|
||||||
with temp_git_dir.as_cwd():
|
with temp_git_dir.as_cwd():
|
||||||
cmd_output('git', 'checkout', '-b', 'another/branch')
|
cmd_output('git', 'checkout', '-b', 'another/branch')
|
||||||
assert is_on_branch({'master'}) is False
|
assert is_on_branch({'placeholder'}) is False
|
||||||
|
|
||||||
|
|
||||||
def test_multi_branch_fail(temp_git_dir):
|
def test_multi_branch_fail(temp_git_dir):
|
||||||
|
|
@ -23,9 +26,10 @@ def test_multi_branch_fail(temp_git_dir):
|
||||||
assert is_on_branch({'another/branch'}) is True
|
assert is_on_branch({'another/branch'}) is True
|
||||||
|
|
||||||
|
|
||||||
def test_master_branch(temp_git_dir):
|
def test_exact_branch(temp_git_dir):
|
||||||
with temp_git_dir.as_cwd():
|
with temp_git_dir.as_cwd():
|
||||||
assert is_on_branch({'master'}) is True
|
cmd_output('git', 'checkout', '-b', 'branchname')
|
||||||
|
assert is_on_branch({'branchname'}) is True
|
||||||
|
|
||||||
|
|
||||||
def test_main_branch_call(temp_git_dir):
|
def test_main_branch_call(temp_git_dir):
|
||||||
|
|
@ -47,11 +51,11 @@ def test_branch_pattern_fail(temp_git_dir):
|
||||||
assert is_on_branch(set(), {'another/.*'}) is True
|
assert is_on_branch(set(), {'another/.*'}) is True
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('branch_name', ('master', 'another/branch'))
|
@pytest.mark.parametrize('branch_name', ('somebranch', 'another/branch'))
|
||||||
def test_branch_pattern_multiple_branches_fail(temp_git_dir, branch_name):
|
def test_branch_pattern_multiple_branches_fail(temp_git_dir, branch_name):
|
||||||
with temp_git_dir.as_cwd():
|
with temp_git_dir.as_cwd():
|
||||||
cmd_output('git', 'checkout', '-b', branch_name)
|
cmd_output('git', 'checkout', '-b', branch_name)
|
||||||
assert main(('--branch', 'master', '--pattern', 'another/.*'))
|
assert main(('--branch', 'somebranch', '--pattern', 'another/.*'))
|
||||||
|
|
||||||
|
|
||||||
def test_main_default_call(temp_git_dir):
|
def test_main_default_call(temp_git_dir):
|
||||||
|
|
@ -62,7 +66,7 @@ def test_main_default_call(temp_git_dir):
|
||||||
|
|
||||||
def test_not_on_a_branch(temp_git_dir):
|
def test_not_on_a_branch(temp_git_dir):
|
||||||
with temp_git_dir.as_cwd():
|
with temp_git_dir.as_cwd():
|
||||||
cmd_output('git', 'commit', '--no-gpg-sign', '--allow-empty', '-m1')
|
git_commit('--allow-empty', '-m1')
|
||||||
head = cmd_output('git', 'rev-parse', 'HEAD').strip()
|
head = cmd_output('git', 'rev-parse', 'HEAD').strip()
|
||||||
cmd_output('git', 'checkout', head)
|
cmd_output('git', 'checkout', head)
|
||||||
# we're not on a branch!
|
# we're not on a branch!
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
|
|
@ -80,6 +82,24 @@ def test_autofix_main(tmpdir):
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_invalid_main(tmpdir):
|
||||||
|
srcfile1 = tmpdir.join('not_valid_json.json')
|
||||||
|
srcfile1.write(
|
||||||
|
'{\n'
|
||||||
|
' // not json\n'
|
||||||
|
' "a": "b"\n'
|
||||||
|
'}',
|
||||||
|
)
|
||||||
|
srcfile2 = tmpdir.join('to_be_json_formatted.json')
|
||||||
|
srcfile2.write('{ "a": "b" }')
|
||||||
|
|
||||||
|
# it should have skipped the first file and formatted the second one
|
||||||
|
assert main(['--autofix', str(srcfile1), str(srcfile2)]) == 1
|
||||||
|
|
||||||
|
# confirm second file was formatted (shouldn't trigger linter again)
|
||||||
|
assert main([str(srcfile2)]) == 0
|
||||||
|
|
||||||
|
|
||||||
def test_orderfile_get_pretty_format():
|
def test_orderfile_get_pretty_format():
|
||||||
ret = main((
|
ret = main((
|
||||||
'--top-keys=alist', get_resource_path('pretty_formatted_json.json'),
|
'--top-keys=alist', get_resource_path('pretty_formatted_json.json'),
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from pre_commit_hooks.check_yaml import yaml
|
from pre_commit_hooks.check_yaml import yaml
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.removed import main
|
from pre_commit_hooks.removed import main
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.requirements_txt_fixer import FAIL
|
from pre_commit_hooks.requirements_txt_fixer import FAIL
|
||||||
|
|
@ -66,6 +68,12 @@ from pre_commit_hooks.requirements_txt_fixer import Requirement
|
||||||
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\n#comment about a\na==1\n',
|
||||||
|
FAIL,
|
||||||
|
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',
|
||||||
|
|
@ -74,6 +82,8 @@ from pre_commit_hooks.requirements_txt_fixer import Requirement
|
||||||
),
|
),
|
||||||
(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'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,
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
@ -35,6 +37,12 @@ TESTS = (
|
||||||
1,
|
1,
|
||||||
),
|
),
|
||||||
('"foo""bar"', "'foo''bar'", 1),
|
('"foo""bar"', "'foo''bar'", 1),
|
||||||
|
pytest.param(
|
||||||
|
"f'hello{\"world\"}'",
|
||||||
|
"f'hello{\"world\"}'",
|
||||||
|
0,
|
||||||
|
id='ignore nested fstrings',
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from pre_commit_hooks.tests_should_end_in_test import main
|
from pre_commit_hooks.tests_should_end_in_test import main
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -41,3 +43,8 @@ def test_main_not_django_fails():
|
||||||
def test_main_django_fails():
|
def test_main_django_fails():
|
||||||
ret = main(['--django', 'foo_test.py', 'test_bar.py', 'test_baz.py'])
|
ret = main(['--django', 'foo_test.py', 'test_bar.py', 'test_baz.py'])
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_main_pytest_test_first():
|
||||||
|
assert main(['--pytest-test-first', 'test_foo.py']) == 0
|
||||||
|
assert main(['--pytest-test-first', 'foo_test.py']) == 1
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.trailing_whitespace_fixer import main
|
from pre_commit_hooks.trailing_whitespace_fixer import main
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.util import CalledProcessError
|
from pre_commit_hooks.util import CalledProcessError
|
||||||
|
|
|
||||||
4
tox.ini
4
tox.ini
|
|
@ -1,5 +1,5 @@
|
||||||
[tox]
|
[tox]
|
||||||
envlist = py36,py37,py38,pypy3,pre-commit
|
envlist = py,pre-commit
|
||||||
|
|
||||||
[testenv]
|
[testenv]
|
||||||
deps = -rrequirements-dev.txt
|
deps = -rrequirements-dev.txt
|
||||||
|
|
@ -11,7 +11,7 @@ setenv =
|
||||||
commands =
|
commands =
|
||||||
coverage erase
|
coverage erase
|
||||||
coverage run -m pytest {posargs:tests}
|
coverage run -m pytest {posargs:tests}
|
||||||
coverage report --fail-under 100
|
coverage report
|
||||||
|
|
||||||
[testenv:pre-commit]
|
[testenv:pre-commit]
|
||||||
skip_install = true
|
skip_install = true
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue