Skip to content

Commit ecde06d

Browse files
committed
Add validation to explicitly check for bare AWS strings in excerpt descriptions.
There is technically more nuance, but this has caused more problems being loose than restrictive.
1 parent 7425df6 commit ecde06d

File tree

4 files changed

+69
-19
lines changed

4 files changed

+69
-19
lines changed

aws_doc_sdk_examples_tools/doc_gen_cli_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
from .doc_gen import DocGen, MetadataError, Example
77
from .doc_gen_cli import main
8-
from .metadata import DocFilenames, Sdk, Language, SDKPageVersion, Version
9-
from .sdks import SdkVersion
8+
from .metadata import DocFilenames, Language, SDKPageVersion, Version
9+
from .sdks import Sdk, SdkVersion
1010

1111

1212
@pytest.fixture
@@ -24,7 +24,7 @@ def mock_example():
2424
},
2525
sdk_pages={
2626
"cpp": {
27-
1: SDKPageVersion(actions_scenarios={"medical-imaging": f"link"})
27+
1: SDKPageVersion(actions_scenarios={"medical-imaging": "link"})
2828
}
2929
},
3030
),

aws_doc_sdk_examples_tools/metadata.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from collections import defaultdict
88
from dataclasses import dataclass, field
9-
from typing import Any, Dict, Literal, List, Optional, Set, Union, Iterable
9+
from typing import Dict, Literal, List, Optional, Set, Iterable
1010
from os.path import splitext
1111
from pathlib import Path
1212

@@ -18,9 +18,7 @@
1818
ExampleMergeMismatchedLanguage,
1919
ExampleMergeConflict,
2020
)
21-
from .project_validator import ValidationConfig
22-
from .services import Service
23-
from .sdks import Sdk
21+
from .project_validator import verify_no_invalid_bare_aws
2422

2523

2624
@dataclass
@@ -44,8 +42,9 @@ class Excerpt:
4442
# all: This content was entirely written by GenAI, and has not been reviewed by a human.
4543
genai: Literal["none", "some", "most", "all"] = "none"
4644

47-
def validate(self, errors: MetadataErrors):
48-
pass
45+
def validate(self, errors: MetadataErrors, path: str):
46+
if self.description:
47+
verify_no_invalid_bare_aws(self.description, path, errors)
4948

5049

5150
@dataclass
@@ -63,7 +62,7 @@ class Version:
6362
# Link to additional topic places.
6463
more_info: List[Url] = field(default_factory=list)
6564

66-
def validate(self, errors: MetadataErrors, root: Path):
65+
def validate(self, errors: MetadataErrors, root: Path, path: str):
6766
github = self.github
6867
if github is not None:
6968
_, ext = splitext(github)
@@ -82,8 +81,8 @@ def validate(self, errors: MetadataErrors, root: Path):
8281
)
8382
)
8483

85-
for excerpt in self.excerpts:
86-
excerpt.validate(errors)
84+
for i, excerpt in enumerate(self.excerpts):
85+
excerpt.validate(errors, f"{path}[{i}]")
8786

8887

8988
@dataclass
@@ -117,10 +116,10 @@ def merge(self, other: "Language", errors: MetadataErrors):
117116
# within the language. If a tributary or writer feels they need to
118117
# modify an excerpt, they should go modify the excerpt directly.
119118

120-
def validate(self, errors: MetadataErrors, root: Path):
119+
def validate(self, errors: MetadataErrors, root: Path, path: str):
121120
errs = MetadataErrors()
122-
for version in self.versions:
123-
version.validate(errs, root)
121+
for i, version in enumerate(self.versions):
122+
version.validate(errs, root, f"{path}[{i}]")
124123
for error in errs:
125124
if isinstance(error, MetadataParseError):
126125
error.language = self.name
@@ -189,8 +188,8 @@ def merge(self, other: Example, errors: MetadataErrors):
189188

190189
def validate(self, errors: MetadataErrors, root: Path):
191190
errs = MetadataErrors()
192-
for language in self.languages.values():
193-
language.validate(errs, root)
191+
for name, language in self.languages.items():
192+
language.validate(errs, root, f"{self.file}: {self.id}.languages.{name}")
194193
for error in errs:
195194
error.file = self.file
196195
error.id = self.id

aws_doc_sdk_examples_tools/metadata_test.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
Excerpt,
2323
)
2424
from .doc_gen import DocGen, parse_examples, check_id_format
25-
from .project_validator import ValidationConfig
25+
from .project_validator import ValidationConfig, InvalidBareAWS
2626
from .sdks import Sdk
2727
from .services import Service, ServiceExpanded
2828

@@ -977,5 +977,39 @@ def test_no_duplicate_title_abbrev():
977977
assert expected == [*errors]
978978

979979

980+
def test_excerpt_with_bare_aws():
981+
errors = MetadataErrors()
982+
doc_gen = DocGen(
983+
Path(__file__).parent / "test_excerpt_with_bare_aws",
984+
errors=errors,
985+
examples={
986+
"a": Example(
987+
id="a",
988+
file=Path("a"),
989+
title_abbrev="abbr",
990+
category="cat",
991+
languages={
992+
"java": Language(
993+
name="java", property="java", versions=[Version(sdk_version=1, excerpts=[Excerpt(description="Bare AWS Here", snippet_files=[], snippet_tags=[])])]
994+
)
995+
},
996+
services={"svc": set()},
997+
),
998+
},
999+
)
1000+
doc_gen.validate()
1001+
1002+
expected = [
1003+
InvalidBareAWS(
1004+
file=Path("a"),
1005+
id="a",
1006+
content="Bare AWS Here",
1007+
path="a: a.languages.java[0][0]"
1008+
)
1009+
]
1010+
1011+
assert expected == [*errors]
1012+
1013+
9801014
if __name__ == "__main__":
9811015
pytest.main([__file__, "-vv"])

aws_doc_sdk_examples_tools/project_validator.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import logging
2525
from dataclasses import dataclass, field
2626
from pathlib import Path
27-
from typing import List, Set
27+
from typing import Dict, List, Set
2828

2929
from .file_utils import get_files
3030
from .metadata_errors import (
@@ -206,3 +206,20 @@ def verify_no_secret_keys(
206206
keys -= validation.allow_list
207207
for word in keys:
208208
errors.append(PossibleSecretKey(file=file_location, word=word))
209+
210+
211+
@dataclass
212+
class InvalidBareAWS(MetadataError):
213+
content: str = ""
214+
path: str = ""
215+
216+
def message(self):
217+
return f"Possible bare AWS in {self.path} ({self.content})"
218+
219+
220+
BARE_AWS_REGEX = r"\bAWS\s+[A-Za-z0-9]+\b"
221+
222+
223+
def verify_no_invalid_bare_aws(content: str, path: str, errors: MetadataErrors):
224+
if re.findall(BARE_AWS_REGEX, content):
225+
errors.append(InvalidBareAWS(content=content, path=path))

0 commit comments

Comments
 (0)