Skip to content

Commit 948d3d2

Browse files
committed
Round 24 fixes: chain JSON parse error, contain log-file import side effect
- _load_permission_data now reports both JSON and YAML parse errors in the ValueError message. A user feeding a JSON file with a syntax error was previously shown only the downstream YAML error, which described the file in YAML-grammar terms and was confusing. - TestPermissionFileParsing.setUpClass chdir's to ROOT around the exec_module call. The module's top-level dictConfig writes provider-kubeconfig.log via RotatingFileHandler with a relative path; the log now lands in the gitignored location regardless of where pytest was invoked from.
1 parent 12b0c4d commit 948d3d2

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

provider-kubeconfig.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ def _load_permission_data(self, permissionfile):
5454
contents = fp.read()
5555
try:
5656
perms_data = json.loads(contents)
57-
except json.JSONDecodeError:
57+
except json.JSONDecodeError as json_exc:
5858
try:
5959
perms_data = yaml.safe_load(contents)
60-
except yaml.YAMLError as exc:
61-
raise ValueError(f"Permission file is neither valid JSON nor YAML: {exc}") from exc
60+
except yaml.YAMLError as yaml_exc:
61+
raise ValueError(
62+
f"Permission file is neither valid JSON ({json_exc}) nor YAML ({yaml_exc})."
63+
) from yaml_exc
6264
if not isinstance(perms_data, dict) or "perms" not in perms_data:
6365
raise ValueError("Permission file must define a top-level 'perms' object.")
6466
if not isinstance(perms_data["perms"], dict):

tests/test_provider_kubeconfig.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,15 @@ def setUpClass(cls):
9292
script_path = os.path.join(ROOT, SCRIPT)
9393
spec = importlib.util.spec_from_file_location("provider_kubeconfig_module", script_path)
9494
module = importlib.util.module_from_spec(spec)
95-
spec.loader.exec_module(module)
95+
# The module's top-level dictConfig creates provider-kubeconfig.log in cwd as a
96+
# side effect of import. Run the import with cwd=ROOT so the file lands in the
97+
# gitignored location regardless of where pytest was invoked from.
98+
prev_cwd = os.getcwd()
99+
os.chdir(ROOT)
100+
try:
101+
spec.loader.exec_module(module)
102+
finally:
103+
os.chdir(prev_cwd)
96104
cls.generator = module.KubeconfigGenerator()
97105

98106
def _write_temp_file(self, suffix, content):

0 commit comments

Comments
 (0)