Skip to content

Commit ff53ebe

Browse files
Refactor tests.
1 parent 5665c0e commit ff53ebe

1 file changed

Lines changed: 29 additions & 36 deletions

File tree

test/unit/test_load.py

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,46 @@
11
""" Test parsing a variety of hcl files"""
22
import json
3-
import os
4-
from os.path import dirname
3+
from pathlib import Path
54
from unittest import TestCase
65

76
import hcl2
87
from hcl2.parser import PARSER_FILE, create_parser_file
98

10-
HCL2_DIR = 'terraform-config'
11-
JSON_DIR = 'terraform-config-json'
129

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()]
1314

14-
class TestLoad(TestCase):
15-
""" Test parsing a variety of hcl files"""
1615

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"""
2018

2119
def test_load_terraform(self):
2220
"""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()
2523
create_parser_file()
26-
self._load_test_files()
24+
for hcl_path in HCL2_FILES:
25+
yield self.check_terraform, hcl_path
2726

2827
def test_load_terraform_from_cache(self):
2928
"""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

Comments
 (0)