Skip to content

Commit 61440a8

Browse files
committed
Add validation step to yaml_writer main.
1 parent 5148e30 commit 61440a8

File tree

2 files changed

+97
-3
lines changed

2 files changed

+97
-3
lines changed

aws_doc_sdk_examples_tools/yaml_writer.py

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
from collections import defaultdict
33
from dataclasses import asdict
44
from pathlib import Path
5-
from typing import Any, DefaultDict, Dict, List
5+
from typing import Any, DefaultDict, Dict, List, Set, Tuple
66

7+
import difflib
78
import logging
89
import yaml
910

@@ -12,7 +13,7 @@
1213

1314
logging.basicConfig(level=logging.INFO)
1415

15-
logger = logging.getLogger(__file__)
16+
logger = logging.getLogger(Path(__file__).name)
1617

1718

1819
def write_many(root: Path, to_write: Dict[str, str]):
@@ -111,6 +112,45 @@ def excerpt_dict(excerpt: Dict) -> Dict:
111112
return reordered
112113

113114

115+
def collect_yaml(root: Path) -> Dict[str, Dict]:
116+
yaml_files: Dict[str, Dict] = {}
117+
metadata_dir = root / ".doc_gen" / "metadata"
118+
119+
if not metadata_dir.exists():
120+
return yaml_files
121+
122+
for yaml_path in metadata_dir.glob("**/*.yaml"):
123+
rel_path = yaml_path.relative_to(root)
124+
125+
with open(yaml_path, "r") as file:
126+
try:
127+
content = yaml.safe_load(file)
128+
yaml_files[str(rel_path)] = content
129+
except yaml.YAMLError as e:
130+
logger.error(f"Error parsing YAML file {yaml_path}: {e}")
131+
132+
return yaml_files
133+
134+
135+
def report_yaml_differences(
136+
before_values: Dict[str, Dict], after_values: Dict[str, Dict]
137+
) -> List[Tuple[str, str]]:
138+
differences = []
139+
for file_path in set(before_values.keys()) | set(after_values.keys()):
140+
before = before_values.get(file_path)
141+
after = after_values.get(file_path)
142+
143+
if before != after:
144+
if file_path not in before_values:
145+
differences.append((file_path, "added"))
146+
elif file_path not in after_values:
147+
differences.append((file_path, "removed"))
148+
else:
149+
differences.append((file_path, "modified"))
150+
151+
return differences
152+
153+
114154
def main():
115155
parser = ArgumentParser(
116156
description="Build a DocGen instance and normalize the metadata."
@@ -123,9 +163,21 @@ def main():
123163
if not root.is_dir():
124164
logger.error(f"Expected {root} to be a directory.")
125165

166+
before_values = collect_yaml(root)
126167
doc_gen = DocGen.from_root(root)
127168
writes = prepare_write(doc_gen.examples)
128169
write_many(root, writes)
170+
after_values = collect_yaml(root)
171+
172+
if before_values != after_values:
173+
differences = report_yaml_differences(before_values, after_values)
174+
logger.error(f"YAML content changed in {len(differences)} files after writing:")
175+
for file_path, diff_type in differences:
176+
logger.error(f" - {file_path}: {diff_type}")
177+
else:
178+
logger.info(
179+
f"Metadata for {root.name} has been normalized and verified for consistency."
180+
)
129181

130182

131183
if __name__ == "__main__":

aws_doc_sdk_examples_tools/yaml_writer_test.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
import pytest
33

44
from aws_doc_sdk_examples_tools.doc_gen import DocGen
5-
from aws_doc_sdk_examples_tools.yaml_writer import prepare_write
5+
from aws_doc_sdk_examples_tools.yaml_writer import (
6+
prepare_write,
7+
report_yaml_differences,
8+
)
69

710

811
ROOT = Path(__file__).parent / "test_resources" / "doc_gen_test"
@@ -46,3 +49,42 @@ def test_doc_gen(sample_doc_gen: DocGen):
4649
}
4750

4851
assert writes == expected_writes
52+
53+
54+
def test_report_yaml_differences_with_changes():
55+
"""Test that report_yaml_differences correctly identifies added, removed, and modified files."""
56+
before = {
57+
"file1.yaml": {"key1": "value1"},
58+
"file2.yaml": {"key2": "value2"},
59+
"file3.yaml": {"key3": "value3"},
60+
}
61+
62+
after = {
63+
"file1.yaml": {"key1": "changed_value"}, # Modified
64+
"file3.yaml": {"key3": "value3"}, # Unchanged
65+
"file4.yaml": {"key4": "value4"}, # Added
66+
# file2.yaml is removed
67+
}
68+
69+
differences = report_yaml_differences(before, after)
70+
71+
# Sort the differences for consistent comparison
72+
differences.sort()
73+
74+
expected = [
75+
("file1.yaml", "modified"),
76+
("file2.yaml", "removed"),
77+
("file4.yaml", "added"),
78+
]
79+
expected.sort()
80+
81+
assert differences == expected
82+
83+
84+
def test_report_yaml_differences_no_changes():
85+
"""Test that report_yaml_differences returns an empty list when dictionaries are identical."""
86+
before = {"file1.yaml": {"key": "value"}}
87+
after = {"file1.yaml": {"key": "value"}}
88+
89+
differences = report_yaml_differences(before, after)
90+
assert differences == []

0 commit comments

Comments
 (0)