|
| 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() |
0 commit comments