Skip to content

Commit 246348a

Browse files
authored
Merge pull request #24 from gruntwork-io/yori-check-skipenv
Check skipenv
2 parents 86acc66 + e20ca37 commit 246348a

13 files changed

+386
-0
lines changed

.circleci/config.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
version: 2.1
2+
3+
jobs:
4+
unit_test:
5+
machine:
6+
enabled: true
7+
image: "ubuntu-1604:201903-01"
8+
steps:
9+
- checkout
10+
- run:
11+
name: run py tests
12+
command: |
13+
cd test
14+
pip install --upgrade pip
15+
pip install tox
16+
tox
17+
18+
19+
workflows:
20+
version: 2
21+
test:
22+
jobs:
23+
- unit_test:
24+
filters:
25+
tags:
26+
only: /^v.*/

.gitignore

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

.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: script
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_|TERRATEST_REGION)', 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()

test/.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.7.12:3.5.2

test/check_skip_env_test.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import os
2+
import sys
3+
import glob
4+
import unittest
5+
6+
if sys.version_info[0] < 3:
7+
import subprocess32 as subprocess
8+
else:
9+
import subprocess
10+
11+
12+
class TestCheckSkipEnv(unittest.TestCase):
13+
def setUp(self):
14+
self.root = get_git_root()
15+
self.hook_script = os.path.join(self.root, 'hooks', 'check_skip_env.py')
16+
self.fixture_dir = os.path.join(self.root, 'test', 'fixtures')
17+
18+
def _check_success(self, files):
19+
subprocess.run([self.hook_script] + files, check=True)
20+
21+
def _check_failure(self, files, failed_files):
22+
result = subprocess.run([self.hook_script] + files, stderr=subprocess.PIPE)
23+
self.assertEqual(result.returncode, 1)
24+
for f in failed_files:
25+
self.assertIn(f, result.stderr.decode('utf-8'))
26+
27+
def test_everything_commented(self):
28+
self._check_success([os.path.join(self.fixture_dir, 'everything_commented_test.go')])
29+
30+
def test_non_skip_uncommented(self):
31+
self._check_success([os.path.join(self.fixture_dir, 'non_skip_uncommented_test.go')])
32+
33+
def test_skip_uncommented(self):
34+
test_file_path = os.path.join(self.fixture_dir, 'skip_uncommented_test.go')
35+
self._check_failure([test_file_path], [test_file_path])
36+
37+
def test_terratest_region_uncommented(self):
38+
test_file_path = os.path.join(self.fixture_dir, 'terratest_region_uncommented_test.go')
39+
self._check_failure([test_file_path], [test_file_path])
40+
41+
def test_multiple_skip_uncommented(self):
42+
test_file_path = os.path.join(self.fixture_dir, 'multiple_skip_uncommented_test.go')
43+
self._check_failure([test_file_path], [test_file_path])
44+
45+
def test_nested_uncommented(self):
46+
test_file_path = os.path.join(self.fixture_dir, 'nested_uncommented_test.go')
47+
self._check_failure([test_file_path], [test_file_path])
48+
49+
def test_everything(self):
50+
all_test_files = glob.glob(os.path.join(self.fixture_dir, '*.go'))
51+
failed_files = [
52+
os.path.join(self.fixture_dir, 'skip_uncommented_test.go'),
53+
os.path.join(self.fixture_dir, 'multiple_skip_uncommented_test.go'),
54+
os.path.join(self.fixture_dir, 'nested_uncommented_test.go'),
55+
os.path.join(self.fixture_dir, 'terratest_region_uncommented_test.go'),
56+
]
57+
self._check_failure(all_test_files, failed_files)
58+
59+
60+
def get_git_root():
61+
""" Returns the root directory of the git repository, assuming this script is run from within the repository. """
62+
result = subprocess.run(['git', 'rev-parse', '--show-toplevel'], stdout=subprocess.PIPE, check=True)
63+
if result.stdout is None:
64+
# TODO: concrete exception
65+
raise Exception('Did not get any output from git: stderr is "{}"'.format(result.stderr))
66+
return result.stdout.decode('utf-8').rstrip('\n')
67+
68+
69+
if __name__ == '__main__':
70+
unittest.main()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package basecase
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gruntwork-io/terratest/modules/logger"
7+
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
8+
)
9+
10+
func TestWithStages(t *testing.T) {
11+
t.Parallel()
12+
13+
// Uncomment the items below to skip certain parts of the test
14+
//os.Setenv("TERRATEST_REGION", "eu-west-1")
15+
//os.Setenv("SKIP_setup", "true")
16+
//os.Setenv("SKIP_deploy", "true")
17+
//os.Setenv("SKIP_validate", "true")
18+
//os.Setenv("SKIP_cleanup", "true")
19+
20+
test_structure.RunTestStage(t, "setup", func() {
21+
logger.Logf(t, "setup")
22+
})
23+
24+
defer test_structure.RunTestStage(t, "cleanup", func() {
25+
logger.Logf(t, "cleanup")
26+
})
27+
28+
test_structure.RunTestStage(t, "deploy", func() {
29+
logger.Logf(t, "deploy")
30+
})
31+
32+
test_structure.RunTestStage(t, "validate", func() {
33+
logger.Logf(t, "validate")
34+
})
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package basecase
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/gruntwork-io/terratest/modules/logger"
8+
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
9+
)
10+
11+
func TestWithStages(t *testing.T) {
12+
t.Parallel()
13+
14+
// Uncomment the items below to skip certain parts of the test
15+
//os.Setenv("TERRATEST_REGION", "eu-west-1")
16+
//os.Setenv("SKIP_setup", "true")
17+
os.Setenv("SKIP_deploy", "true")
18+
os.Setenv("SKIP_validate", "true")
19+
//os.Setenv("SKIP_cleanup", "true")
20+
21+
test_structure.RunTestStage(t, "setup", func() {
22+
logger.Logf(t, "setup")
23+
})
24+
25+
defer test_structure.RunTestStage(t, "cleanup", func() {
26+
logger.Logf(t, "cleanup")
27+
})
28+
29+
test_structure.RunTestStage(t, "deploy", func() {
30+
logger.Logf(t, "deploy")
31+
})
32+
33+
test_structure.RunTestStage(t, "validate", func() {
34+
logger.Logf(t, "validate")
35+
})
36+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package basecase
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/gruntwork-io/terratest/modules/logger"
8+
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
9+
)
10+
11+
func TestWithStages(t *testing.T) {
12+
t.Parallel()
13+
14+
// Uncomment the items below to skip certain parts of the test
15+
//os.Setenv("TERRATEST_REGION", "eu-west-1")
16+
//os.Setenv("SKIP_setup", "true")
17+
//os.Setenv("SKIP_deploy", "true")
18+
//os.Setenv("SKIP_cleanup", "true")
19+
20+
test_structure.RunTestStage(t, "setup", func() {
21+
logger.Logf(t, "setup")
22+
})
23+
24+
defer test_structure.RunTestStage(t, "cleanup", func() {
25+
logger.Logf(t, "cleanup")
26+
})
27+
28+
test_structure.RunTestStage(t, "deploy", func() {
29+
logger.Logf(t, "deploy")
30+
})
31+
32+
for _, data := range testCases {
33+
t.Run(data.name, func(t *testing.T) {
34+
os.Setenv("SKIP_validate")
35+
test_structure.RunTestStage(t, "validate", func() {
36+
logger.Logf(t, "validate")
37+
})
38+
})
39+
}
40+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package basecase
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/gruntwork-io/terratest/modules/logger"
8+
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
9+
)
10+
11+
func TestWithStages(t *testing.T) {
12+
t.Parallel()
13+
14+
// Uncomment the items below to skip certain parts of the test
15+
os.Setenv("THE_ANSWER", "42")
16+
//os.Setenv("SKIP_setup", "true")
17+
//os.Setenv("SKIP_deploy", "true")
18+
//os.Setenv("SKIP_validate", "true")
19+
//os.Setenv("SKIP_cleanup", "true")
20+
21+
test_structure.RunTestStage(t, "setup", func() {
22+
logger.Logf(t, "setup")
23+
})
24+
25+
defer test_structure.RunTestStage(t, "cleanup", func() {
26+
logger.Logf(t, "cleanup")
27+
})
28+
29+
test_structure.RunTestStage(t, "deploy", func() {
30+
logger.Logf(t, "deploy")
31+
})
32+
33+
test_structure.RunTestStage(t, "validate", func() {
34+
logger.Logf(t, "validate")
35+
})
36+
}

0 commit comments

Comments
 (0)