-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpygments_pre_commit_test.py
More file actions
113 lines (85 loc) · 2.93 KB
/
pygments_pre_commit_test.py
File metadata and controls
113 lines (85 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from __future__ import annotations
import os.path
import re
import subprocess
import sys
import pygments.formatters
import pygments.lexers
import pytest
import pygments_pre_commit
ANSI_LEXER = pygments.lexers.get_lexer_by_name('ansi', stripnl=False)
LEXER = pygments.lexers.get_lexer_by_name('pre-commit', stripnl=False)
HTML_FORMATTER = pygments.formatters.HtmlFormatter()
ANSI_ESCAPE = re.compile(r'\033\[[^m]*m')
NORM_WS_START_RE = re.compile(r'(<[^/][^>]+>)(\s*)')
NORM_WS_END_RE = re.compile(r'(\s*)(</[^>]+>)')
EMPTY_TAG_RE = re.compile(r'<[^/][^>]+></[^>]+>')
DEMO_DIR = os.path.join(os.path.dirname(__file__), '../demo')
HTML = '''\
<!doctype html>
<html><head>
<style>body { background-color: #2d0922; color: #fff; } STYLES</style>
</head><body>HTML</body></html>
'''
HTML = HTML.replace('STYLES', pygments_pre_commit.stylesheet())
def uncolor(s):
return ANSI_ESCAPE.sub('', s)
def highlight(lexer, s):
ret = pygments.highlight(s, lexer=lexer, formatter=HTML_FORMATTER)
ret = NORM_WS_START_RE.sub(r'\2\1', ret)
ret = NORM_WS_END_RE.sub(r'\2\1', ret)
ret = EMPTY_TAG_RE.sub('', ret)
return HTML.replace('HTML', ret)
def compare(request, *args, **kwargs):
cmd = (sys.executable, '-mpre_commit', 'run', '--color=always') + args
kwargs.update(stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
proc = subprocess.Popen(cmd, **kwargs)
out = proc.communicate()[0].decode()
assert 'No module named pre_commit' not in out
ansi = highlight(ANSI_LEXER, out)
pre_commit = highlight(LEXER, uncolor(out))
fname = f'{request.node.name}_ansi.html'
with open(os.path.join(DEMO_DIR, fname), 'w') as f:
f.write(ansi)
fname = f'{request.node.name}_pre_commit.html'
with open(os.path.join(DEMO_DIR, fname), 'w') as f:
f.write(pre_commit)
assert ansi == pre_commit
PRE_COMMIT_CONFIG = '''\
repos:
- repo: local
hooks:
- id: passing
name: passing
language: system
entry: 'true'
- id: failing
name: failing
language: system
entry: python -c 'import sys; print(sys.argv[1:]); exit(1)'
- id: skipped-no-files
name: skipped (no files)
language: system
entry: 'true'
files: ^$
- id: skip-me
name: skip me
language: system
entry: 'true'
'''
@pytest.fixture
def pcdir(tmpdir):
with tmpdir.as_cwd():
subprocess.check_call(('git', 'init'))
tmpdir.join('.pre-commit-config.yaml').write(PRE_COMMIT_CONFIG)
tmpdir.join('a').ensure()
subprocess.check_call(('git', 'add', ':/'))
yield tmpdir
def test_basic(pcdir, request):
compare(request, env=dict(os.environ, SKIP='skip-me'))
def test_unstaged_changes(pcdir, request):
pcdir.join('a').write('unstaged')
compare(request, 'passing')
def test_error_unstaged_config(pcdir, request):
pcdir.join('.pre-commit-config.yaml').write('\n\n', mode='a+')
compare(request)