Skip to content

Commit 8654097

Browse files
ericfrederichasottile
authored andcommitted
Add check for illegal Widnows names
Fixes #589
1 parent b73acb1 commit 8654097

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

.pre-commit-hooks.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
language: python
4141
types: [text, executable]
4242
stages: [commit, push, manual]
43+
- id: check-illegal-windows-names
44+
name: check illegal windows names
45+
entry: Illegal windows filenames detected
46+
language: fail
47+
files: '(?i)(^|/)(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(\.|/|$)'
4348
- id: check-json
4449
name: check json
4550
description: checks json files for parseable syntax.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ Checks for a common error of placing code before the docstring.
5151
#### `check-executables-have-shebangs`
5252
Checks that non-binary executables have a proper shebang.
5353

54+
#### `check-illegal-windows-names`
55+
Check for files that cannot be created on Windows.
56+
5457
#### `check-json`
5558
Attempts to load all json files to verify syntax.
5659

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from __future__ import annotations
2+
3+
import os.path
4+
import re
5+
6+
import pytest
7+
8+
from pre_commit_hooks.check_yaml import yaml
9+
10+
11+
@pytest.fixture(scope='module')
12+
def hook_re():
13+
here = os.path.dirname(__file__)
14+
with open(os.path.join(here, '..', '.pre-commit-hooks.yaml')) as f:
15+
hook_defs = yaml.load(f)
16+
hook, = (
17+
hook
18+
for hook in hook_defs
19+
if hook['id'] == 'check-illegal-windows-names'
20+
)
21+
yield re.compile(hook['files'])
22+
23+
24+
@pytest.mark.parametrize(
25+
's',
26+
(
27+
pytest.param('aux.txt', id='with ext'),
28+
pytest.param('aux', id='without ext'),
29+
pytest.param('AuX.tXt', id='capitals'),
30+
pytest.param('com7.dat', id='com with digit'),
31+
),
32+
)
33+
def test_check_illegal_windows_names_matches(hook_re, s):
34+
assert hook_re.search(s)
35+
36+
37+
@pytest.mark.parametrize(
38+
's',
39+
(
40+
pytest.param('README.md', id='standard file'),
41+
pytest.param('foo.aux', id='as ext'),
42+
pytest.param('com.dat', id='com without digit'),
43+
),
44+
)
45+
def test_check_illegal_windows_names_does_not_match(hook_re, s):
46+
assert hook_re.search(s) is None

0 commit comments

Comments
 (0)