Skip to content

Commit b7e6554

Browse files
committed
Add unit tests for new python hook
1 parent 9a91d3a commit b7e6554

File tree

7 files changed

+274
-0
lines changed

7 files changed

+274
-0
lines changed

.circleci/config.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
version: 2.1
2+
3+
jobs:
4+
unit_test_py3:
5+
docker:
6+
- image: python:3
7+
steps:
8+
- checkout
9+
- run:
10+
name: run tests
11+
command: |
12+
python test/check-skip-env/check_skip_env_test.py
13+
unit_test_py2:
14+
docker:
15+
- image: python:2
16+
steps:
17+
- checkout
18+
- run:
19+
name: run tests
20+
command: |
21+
python test/check-skip-env/check_skip_env_test.py
22+
23+
24+
workflows:
25+
version: 2
26+
test:
27+
jobs:
28+
- unit_test:
29+
filters:
30+
tags:
31+
only: /^v.*/
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import os
2+
import glob
3+
import unittest
4+
import subprocess
5+
6+
7+
class TestCheckSkipEnv(unittest.TestCase):
8+
def setUp(self):
9+
self.root = get_git_root()
10+
self.hook_script = os.path.join(self.root, 'hooks', 'check_skip_env.py')
11+
self.fixture_dir = os.path.join(self.root, 'test', 'check-skip-env', 'fixtures')
12+
13+
def _check_success(self, files):
14+
subprocess.run([self.hook_script] + files, check=True)
15+
16+
def _check_failure(self, files, failed_files):
17+
result = subprocess.run([self.hook_script] + files, stderr=subprocess.PIPE)
18+
self.assertEqual(result.returncode, 1)
19+
for f in failed_files:
20+
self.assertIn(f, result.stderr.decode('utf-8'))
21+
22+
def test_everything_commented(self):
23+
self._check_success([os.path.join(self.fixture_dir, 'everything_commented_test.go')])
24+
25+
def test_non_skip_uncommented(self):
26+
self._check_success([os.path.join(self.fixture_dir, 'non_skip_uncommented_test.go')])
27+
28+
def test_skip_uncommented(self):
29+
test_file_path = os.path.join(self.fixture_dir, 'skip_uncommented_test.go')
30+
self._check_failure([test_file_path], [test_file_path])
31+
32+
def test_multiple_skip_uncommented(self):
33+
test_file_path = os.path.join(self.fixture_dir, 'multiple_skip_uncommented_test.go')
34+
self._check_failure([test_file_path], [test_file_path])
35+
36+
def test_nested_uncommented(self):
37+
test_file_path = os.path.join(self.fixture_dir, 'nested_uncommented_test.go')
38+
self._check_failure([test_file_path], [test_file_path])
39+
40+
def test_everything(self):
41+
all_test_files = glob.glob(os.path.join(self.fixture_dir, '*.go'))
42+
failed_files = [
43+
os.path.join(self.fixture_dir, 'skip_uncommented_test.go'),
44+
os.path.join(self.fixture_dir, 'multiple_skip_uncommented_test.go'),
45+
os.path.join(self.fixture_dir, 'nested_uncommented_test.go'),
46+
]
47+
self._check_failure(all_test_files, failed_files)
48+
49+
50+
def get_git_root():
51+
""" Returns the root directory of the git repository, assuming this script is run from within the repository. """
52+
result = subprocess.run(['git', 'rev-parse', '--show-toplevel'], stdout=subprocess.PIPE, check=True)
53+
if result.stdout is None:
54+
# TODO: concrete exception
55+
raise Exception('Did not get any output from git: stderr is "{}"'.format(result.stderr))
56+
return result.stdout.decode('utf-8').rstrip('\n')
57+
58+
59+
if __name__ == '__main__':
60+
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("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: 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+
}

0 commit comments

Comments
 (0)