|
1 | 1 | """ Test parsing a variety of hcl files""" |
2 | 2 | import json |
3 | | -import os |
4 | | -from os.path import dirname |
| 3 | +from pathlib import Path |
5 | 4 | from unittest import TestCase |
6 | 5 |
|
7 | 6 | import hcl2 |
8 | 7 | from hcl2.parser import PARSER_FILE, create_parser_file |
9 | 8 |
|
10 | | -HCL2_DIR = 'terraform-config' |
11 | | -JSON_DIR = 'terraform-config-json' |
12 | 9 |
|
| 10 | +HELPERS_DIR = Path(__file__).absolute().parent.parent / "helpers" |
| 11 | +HCL2_DIR = HELPERS_DIR / "terraform-config" |
| 12 | +JSON_DIR = HELPERS_DIR / "terraform-config-json" |
| 13 | +HCL2_FILES = [str(file.relative_to(HCL2_DIR)) for file in HCL2_DIR.iterdir()] |
13 | 14 |
|
14 | | -class TestLoad(TestCase): |
15 | | - """ Test parsing a variety of hcl files""" |
16 | 15 |
|
17 | | - def setUp(self): |
18 | | - self.prev_dir = os.getcwd() |
19 | | - os.chdir(os.path.join(os.path.dirname(__file__), '../helpers')) |
| 16 | +class TestLoad(TestCase): |
| 17 | + """Test parsing a variety of hcl files""" |
20 | 18 |
|
21 | 19 | def test_load_terraform(self): |
22 | 20 | """Test parsing a set of hcl2 files and force recreating the parser file""" |
23 | | - # delete the parser file to force it to be recreated |
24 | | - os.remove(os.path.join(dirname(hcl2.__file__), PARSER_FILE)) |
| 21 | + parser_file = Path(hcl2.__file__).absolute().parent / PARSER_FILE |
| 22 | + parser_file.unlink() |
25 | 23 | create_parser_file() |
26 | | - self._load_test_files() |
| 24 | + for hcl_path in HCL2_FILES: |
| 25 | + yield self.check_terraform, hcl_path |
27 | 26 |
|
28 | 27 | def test_load_terraform_from_cache(self): |
29 | 28 | """Test parsing a set of hcl2 files from a cached parser file""" |
30 | | - self._load_test_files() |
31 | | - |
32 | | - def _load_test_files(self): |
33 | | - """Recursively parse all files in a directory""" |
34 | | - # pylint: disable=unused-variable |
35 | | - for current_dir, dirs, files in os.walk("terraform-config"): |
36 | | - dir_prefix = os.path.commonpath([HCL2_DIR, current_dir]) |
37 | | - relative_current_dir = current_dir.replace(dir_prefix, '') |
38 | | - current_out_dir = os.path.join(JSON_DIR, relative_current_dir) |
39 | | - for file_name in files: |
40 | | - file_path = os.path.join(current_dir, file_name) |
41 | | - json_file_path = os.path.join(current_out_dir, file_name) |
42 | | - json_file_path = os.path.splitext(json_file_path)[0] + '.json' |
43 | | - |
44 | | - with self.subTest(msg=file_path): |
45 | | - with open(file_path, 'r', encoding="utf-8") as hcl2_file,\ |
46 | | - open(json_file_path, 'r', encoding="utf-8") as json_file: |
47 | | - try: |
48 | | - hcl2_dict = hcl2.load(hcl2_file) |
49 | | - except Exception as ex: |
50 | | - raise RuntimeError(f"failed to tokenize terraform in `{file_path}`") from ex |
51 | | - json_dict = json.load(json_file) |
52 | | - |
53 | | - self.assertDictEqual(hcl2_dict, json_dict, f"failed comparing {file_path}") |
| 29 | + for hcl_path in HCL2_FILES: |
| 30 | + yield self.check_terraform, hcl_path |
| 31 | + |
| 32 | + def check_terraform(self, hcl_path_str: str): |
| 33 | + """Loads a single hcl2 file, parses it and compares with the expected json""" |
| 34 | + hcl_path = (HCL2_DIR / hcl_path_str).absolute() |
| 35 | + json_path = JSON_DIR / hcl_path.relative_to(HCL2_DIR).with_suffix(".json") |
| 36 | + if not json_path.exists(): |
| 37 | + assert False, f"Expected json equivalent of the hcl file doesn't exist {json_path}" |
| 38 | + |
| 39 | + with hcl_path.open("r") as hcl_file, json_path.open("r") as json_file: |
| 40 | + try: |
| 41 | + hcl2_dict = hcl2.load(hcl_file) |
| 42 | + except Exception as exc: |
| 43 | + assert False, f"failed to tokenize terraform in `{hcl_path_str}`: {exc}" |
| 44 | + |
| 45 | + json_dict = json.load(json_file) |
| 46 | + self.assertDictEqual(hcl2_dict, json_dict, f"failed comparing {hcl_path_str}") |
0 commit comments