Coverage for src/governed_harness/configuration/loader.py: 62%

78 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-12 16:06 +0000

1from __future__ import annotations 

2 

3import json 

4from importlib import resources 

5from pathlib import Path 

6from typing import Any 

7 

8import yaml 

9from jsonschema import Draft202012Validator 

10from referencing import Registry, Resource 

11 

12from governed_harness.configuration.models import ( 

13 ProjectConfiguration, 

14 TechnologyProfileDefinition, 

15 WorkflowDefinition, 

16) 

17from governed_harness.domain.errors import ConfigurationError 

18 

19 

20def load_yaml(path: Path) -> dict[str, Any]: 

21 try: 

22 value = yaml.safe_load(path.read_text(encoding="utf-8")) 

23 except OSError as error: 

24 raise ConfigurationError(f"cannot read configuration: {path}: {error}") from error 

25 except yaml.YAMLError as error: 

26 raise ConfigurationError(f"invalid YAML in {path}: {error}") from error 

27 if not isinstance(value, dict): 27 ↛ 28line 27 didn't jump to line 28 because the condition on line 27 was never true

28 raise ConfigurationError(f"configuration must be an object: {path}") 

29 return value 

30 

31 

32def find_project_config(start: Path) -> Path: 

33 current = start.resolve(strict=True) 

34 if current.is_file(): 34 ↛ 35line 34 didn't jump to line 35 because the condition on line 34 was never true

35 current = current.parent 

36 for candidate in (current, *current.parents): 

37 config = candidate / ".harness" / "project.yaml" 

38 if config.exists(): 

39 return config 

40 raise ConfigurationError(f"no .harness/project.yaml found from {start}") 

41 

42 

43def load_project_config(path: Path) -> ProjectConfiguration: 

44 config_path = path if path.name.endswith(".yaml") else find_project_config(path) 

45 value = load_yaml(config_path) 

46 try: 

47 return ProjectConfiguration.model_validate(value) 

48 except Exception as error: 

49 raise ConfigurationError(f"invalid project configuration {config_path}: {error}") from error 

50 

51 

52def resource_text(*parts: str) -> str: 

53 target = resources.files("governed_harness.resources") 

54 for part in parts: 

55 target = target.joinpath(part) 

56 return target.read_text(encoding="utf-8") 

57 

58 

59_BUILTIN_PROFILES = { 

60 "python": "python.yaml", 

61 "python_default": "python.yaml", 

62 "node": "node.yaml", 

63 "node_default": "node.yaml", 

64} 

65BUILTIN_PROFILE_IDS = frozenset(_BUILTIN_PROFILES) 

66"""Ids (and aliases) of the built-in profiles; a project profile cannot reuse them.""" 

67 

68EXTENDED_PROFILES = { 

69 "go_default": "go.yaml", 

70 "rust_default": "rust.yaml", 

71 "jvm_gradle": "jvm_gradle.yaml", 

72 "jvm_maven": "jvm_maven.yaml", 

73 "swift_default": "swift.yaml", 

74 "android_default": "android.yaml", 

75} 

76"""Built-in profiles detected only under ``toolchain.extendedProfiles`` (since #55); a project 

77may also name them in ``profiles``.""" 

78 

79 

80def load_extended_profiles() -> dict[str, TechnologyProfileDefinition]: 

81 return {profile_id: load_builtin_profile(profile_id) for profile_id in EXTENDED_PROFILES} 

82 

83 

84def load_builtin_profile(profile_id: str) -> TechnologyProfileDefinition: 

85 filename = _BUILTIN_PROFILES.get(profile_id) or EXTENDED_PROFILES.get(profile_id) 

86 if filename is None: 86 ↛ 87line 86 didn't jump to line 87 because the condition on line 86 was never true

87 raise ConfigurationError(f"unknown built-in profile: {profile_id}") 

88 try: 

89 return TechnologyProfileDefinition.model_validate( 

90 yaml.safe_load(resource_text("profiles", filename)) 

91 ) 

92 except Exception as error: 

93 raise ConfigurationError(f"invalid built-in profile {profile_id}: {error}") from error 

94 

95 

96def load_builtin_workflow(workflow_id: str) -> WorkflowDefinition: 

97 if workflow_id != "default_development": 97 ↛ 98line 97 didn't jump to line 98 because the condition on line 97 was never true

98 raise ConfigurationError(f"unknown built-in workflow: {workflow_id}") 

99 try: 

100 return WorkflowDefinition.model_validate( 

101 yaml.safe_load(resource_text("workflows", "default.yaml")) 

102 ) 

103 except Exception as error: 

104 raise ConfigurationError(f"invalid built-in workflow {workflow_id}: {error}") from error 

105 

106 

107def validate_json_against_schema(instance: dict[str, Any], schema_name: str) -> list[str]: 

108 schema_root = resources.files("governed_harness.resources").joinpath("schemas", "v1") 

109 schemas: dict[str, dict[str, Any]] = {} 

110 for item in schema_root.iterdir(): 

111 if item.name.endswith(".json"): 

112 parsed = json.loads(item.read_text(encoding="utf-8")) 

113 schemas[parsed.get("$id", item.name)] = parsed 

114 schemas[item.name] = parsed 

115 schema = schemas[schema_name] 

116 registry = Registry() 

117 for key, value in schemas.items(): 

118 if key.startswith("http"): 

119 registry = registry.with_resource(key, Resource.from_contents(value)) 

120 validator = Draft202012Validator(schema, registry=registry) 

121 return [error.message for error in sorted(validator.iter_errors(instance), key=str)]