Skip to content

Commit 20d6941

Browse files
committed
Check terratest skip env
1 parent 86acc66 commit 20d6941

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@
1111
build/
1212
*/build/
1313
out/
14+
15+
# Python
16+
.mypy_cache
17+
*.pyc
18+
__pycache__

.pre-commit-hooks.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,10 @@
9494
language: script
9595
files: \.md$
9696
exclude: vendor\/.*$
97+
98+
- id: check-terratest-skip-env
99+
name: check-terratest-skip-env
100+
description: Check all go source files for any uncommented os.Setenv calls setting a terratest SKIP environment.
101+
entry: ./hooks/check_skip_env.py
102+
language: python
103+
files: \.go$

hooks/check_skip_env.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
import re
3+
import sys
4+
import argparse
5+
import logging
6+
7+
8+
logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s', level=logging.INFO)
9+
10+
11+
def has_setenv_skip(fpath):
12+
with open(fpath) as f:
13+
for line in f:
14+
if re.match(r'^\s+os.Setenv\(\"SKIP_', line):
15+
return True
16+
return False
17+
18+
19+
def parse_args():
20+
parser = argparse.ArgumentParser(
21+
description=(
22+
'A CLI for checking to make sure no uncommented os.Setenv calls are '
23+
'committed in test golang files. Each positional argument should be a golang source file.'
24+
),
25+
)
26+
parser.add_argument(
27+
'files',
28+
metavar='FILE',
29+
type=str,
30+
nargs='+',
31+
help='The file to check.',
32+
)
33+
args = parser.parse_args()
34+
return args
35+
36+
37+
def main():
38+
args = parse_args()
39+
files_with_setenv_skip = [fpath for fpath in args.files if has_setenv_skip(fpath)]
40+
if files_with_setenv_skip:
41+
logging.error('Found files with os.Setenv calls setting terratest SKIP environment variables.')
42+
for f in files_with_setenv_skip:
43+
logging.error('- {}'.format(f))
44+
sys.exit(1)
45+
46+
47+
if __name__ == '__main__':
48+
main()

0 commit comments

Comments
 (0)