Skip to content

Commit 5c72dd8

Browse files
committed
test(golden): update_goldens.py CLI + initial pinned outputs
CLI supports --filter / --check / --diff / --new. Dry-run modes never mutate disk; --new only writes missing files. Three sentinel cases have expected.{build,package}.yaml pinned.
1 parent 972d0c7 commit 5c72dd8

8 files changed

Lines changed: 439 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Resources:
3+
AlphaFunction:
4+
Properties:
5+
Code: <<BUILT_ARTIFACT>>
6+
Handler: app.handler
7+
Role:
8+
Fn::GetAtt:
9+
- AlphaFunctionRole
10+
- Arn
11+
Runtime: python3.11
12+
Tags:
13+
- Key: lambda:createdBy
14+
Value: SAM
15+
Type: AWS::Lambda::Function
16+
AlphaFunctionRole:
17+
Properties:
18+
AssumeRolePolicyDocument:
19+
Statement:
20+
- Action:
21+
- sts:AssumeRole
22+
Effect: Allow
23+
Principal:
24+
Service:
25+
- lambda.amazonaws.com
26+
Version: '2012-10-17'
27+
ManagedPolicyArns:
28+
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
29+
Tags:
30+
- Key: lambda:createdBy
31+
Value: SAM
32+
Type: AWS::IAM::Role
33+
BetaFunction:
34+
Properties:
35+
Code: <<BUILT_ARTIFACT>>
36+
Handler: app.handler
37+
Role:
38+
Fn::GetAtt:
39+
- BetaFunctionRole
40+
- Arn
41+
Runtime: python3.11
42+
Tags:
43+
- Key: lambda:createdBy
44+
Value: SAM
45+
Type: AWS::Lambda::Function
46+
BetaFunctionRole:
47+
Properties:
48+
AssumeRolePolicyDocument:
49+
Statement:
50+
- Action:
51+
- sts:AssumeRole
52+
Effect: Allow
53+
Principal:
54+
Service:
55+
- lambda.amazonaws.com
56+
Version: '2012-10-17'
57+
ManagedPolicyArns:
58+
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
59+
Tags:
60+
- Key: lambda:createdBy
61+
Value: SAM
62+
Type: AWS::IAM::Role
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Resources:
3+
AlphaFunction:
4+
Properties:
5+
Code:
6+
S3Bucket: golden-bucket
7+
S3Key: cbe90f4157bf2048dddade15fccab9bb6ea1ff37413fe0f78b7543e206e8910d
8+
Handler: app.handler
9+
Role:
10+
Fn::GetAtt:
11+
- AlphaFunctionRole
12+
- Arn
13+
Runtime: python3.11
14+
Tags:
15+
- Key: lambda:createdBy
16+
Value: SAM
17+
Type: AWS::Lambda::Function
18+
AlphaFunctionRole:
19+
Properties:
20+
AssumeRolePolicyDocument:
21+
Statement:
22+
- Action:
23+
- sts:AssumeRole
24+
Effect: Allow
25+
Principal:
26+
Service:
27+
- lambda.amazonaws.com
28+
Version: '2012-10-17'
29+
ManagedPolicyArns:
30+
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
31+
Tags:
32+
- Key: lambda:createdBy
33+
Value: SAM
34+
Type: AWS::IAM::Role
35+
BetaFunction:
36+
Properties:
37+
Code:
38+
S3Bucket: golden-bucket
39+
S3Key: cbe90f4157bf2048dddade15fccab9bb6ea1ff37413fe0f78b7543e206e8910d
40+
Handler: app.handler
41+
Role:
42+
Fn::GetAtt:
43+
- BetaFunctionRole
44+
- Arn
45+
Runtime: python3.11
46+
Tags:
47+
- Key: lambda:createdBy
48+
Value: SAM
49+
Type: AWS::Lambda::Function
50+
BetaFunctionRole:
51+
Properties:
52+
AssumeRolePolicyDocument:
53+
Statement:
54+
- Action:
55+
- sts:AssumeRole
56+
Effect: Allow
57+
Principal:
58+
Service:
59+
- lambda.amazonaws.com
60+
Version: '2012-10-17'
61+
ManagedPolicyArns:
62+
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
63+
Tags:
64+
- Key: lambda:createdBy
65+
Value: SAM
66+
Type: AWS::IAM::Role
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Resources:
3+
HelloFunction:
4+
Properties:
5+
Code: <<BUILT_ARTIFACT>>
6+
FunctionName: hello
7+
Handler: app.handler
8+
MemorySize: 128
9+
Role: arn:aws:iam::123456789012:role/lambda-role
10+
Runtime: python3.11
11+
Type: AWS::Lambda::Function
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Resources:
3+
HelloFunction:
4+
Properties:
5+
Code:
6+
S3Bucket: golden-bucket
7+
S3Key: cbe90f4157bf2048dddade15fccab9bb6ea1ff37413fe0f78b7543e206e8910d
8+
FunctionName: hello
9+
Handler: app.handler
10+
MemorySize: 128
11+
Role: arn:aws:iam::123456789012:role/lambda-role
12+
Runtime: python3.11
13+
Type: AWS::Lambda::Function
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Resources:
3+
HelloFunction:
4+
Properties:
5+
Code: <<BUILT_ARTIFACT>>
6+
Handler: app.handler
7+
MemorySize: 128
8+
Role:
9+
Fn::GetAtt:
10+
- HelloFunctionRole
11+
- Arn
12+
Runtime: python3.11
13+
Tags:
14+
- Key: lambda:createdBy
15+
Value: SAM
16+
Type: AWS::Lambda::Function
17+
HelloFunctionRole:
18+
Properties:
19+
AssumeRolePolicyDocument:
20+
Statement:
21+
- Action:
22+
- sts:AssumeRole
23+
Effect: Allow
24+
Principal:
25+
Service:
26+
- lambda.amazonaws.com
27+
Version: '2012-10-17'
28+
ManagedPolicyArns:
29+
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
30+
Tags:
31+
- Key: lambda:createdBy
32+
Value: SAM
33+
Type: AWS::IAM::Role
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Resources:
3+
HelloFunction:
4+
Properties:
5+
Code:
6+
S3Bucket: golden-bucket
7+
S3Key: cbe90f4157bf2048dddade15fccab9bb6ea1ff37413fe0f78b7543e206e8910d
8+
Handler: app.handler
9+
MemorySize: 128
10+
Role:
11+
Fn::GetAtt:
12+
- HelloFunctionRole
13+
- Arn
14+
Runtime: python3.11
15+
Tags:
16+
- Key: lambda:createdBy
17+
Value: SAM
18+
Type: AWS::Lambda::Function
19+
HelloFunctionRole:
20+
Properties:
21+
AssumeRolePolicyDocument:
22+
Statement:
23+
- Action:
24+
- sts:AssumeRole
25+
Effect: Allow
26+
Principal:
27+
Service:
28+
- lambda.amazonaws.com
29+
Version: '2012-10-17'
30+
ManagedPolicyArns:
31+
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
32+
Tags:
33+
- Key: lambda:createdBy
34+
Value: SAM
35+
Type: AWS::IAM::Role
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""Unit tests for update_goldens.py CLI behavior."""
2+
3+
import shutil
4+
from pathlib import Path
5+
6+
import pytest
7+
8+
from tests.golden import update_goldens
9+
10+
REPO_CASES = Path(__file__).parent / "templates"
11+
12+
13+
@pytest.fixture
14+
def isolated_corpus(tmp_path, monkeypatch):
15+
"""Copy the real corpus to tmp_path and point the CLI at it."""
16+
dest = tmp_path / "templates"
17+
shutil.copytree(REPO_CASES, dest)
18+
monkeypatch.setattr(update_goldens, "TEMPLATES_ROOT", dest)
19+
return dest
20+
21+
22+
def test_default_regenerates_all_cases(isolated_corpus):
23+
# Wipe pinned files first, then regenerate
24+
for p in isolated_corpus.rglob("expected.*.yaml"):
25+
p.unlink()
26+
rc = update_goldens.main([])
27+
assert rc == 0
28+
# Every case dir now has both expected files
29+
for case_dir in (p.parent for p in isolated_corpus.rglob("template.yaml")):
30+
assert (case_dir / "expected.build.yaml").exists()
31+
assert (case_dir / "expected.package.yaml").exists()
32+
33+
34+
def test_filter_only_regenerates_matching(isolated_corpus):
35+
# Pre-populate by running a full regen
36+
update_goldens.main([])
37+
sam_case = isolated_corpus / "sam_resources" / "serverless_function_zip"
38+
le_case = isolated_corpus / "language_extensions" / "foreach_static_zip"
39+
(sam_case / "expected.build.yaml").write_text("STALE\n")
40+
rc = update_goldens.main(["--filter", "language_extensions/*"])
41+
assert rc == 0
42+
# SAM case stays stale
43+
assert (sam_case / "expected.build.yaml").read_text() == "STALE\n"
44+
# LE case regenerated
45+
assert (le_case / "expected.build.yaml").read_text() != "STALE\n"
46+
47+
48+
def test_check_returns_nonzero_when_diff(isolated_corpus):
49+
update_goldens.main([]) # pin everything fresh
50+
sam_case = isolated_corpus / "sam_resources" / "serverless_function_zip"
51+
(sam_case / "expected.build.yaml").write_text("STALE\n")
52+
rc = update_goldens.main(["--check"])
53+
assert rc != 0
54+
# File NOT modified by --check
55+
assert (sam_case / "expected.build.yaml").read_text() == "STALE\n"
56+
57+
58+
def test_check_returns_zero_when_clean(isolated_corpus):
59+
update_goldens.main([])
60+
rc = update_goldens.main(["--check"])
61+
assert rc == 0
62+
63+
64+
def test_diff_prints_unified_diff(isolated_corpus, capsys):
65+
update_goldens.main([])
66+
sam_case = isolated_corpus / "sam_resources" / "serverless_function_zip"
67+
(sam_case / "expected.build.yaml").write_text("STALE\n")
68+
rc = update_goldens.main(["--diff"])
69+
assert rc != 0
70+
out = capsys.readouterr().out
71+
assert "STALE" in out
72+
assert "---" in out and "+++" in out
73+
74+
75+
def test_new_only_writes_missing_pins(isolated_corpus):
76+
sam_case = isolated_corpus / "sam_resources" / "serverless_function_zip"
77+
# Pre-pin everything
78+
update_goldens.main([])
79+
# Wipe just one file
80+
(sam_case / "expected.build.yaml").unlink()
81+
# Add an unrelated stale modification to a *different* case
82+
le_case = isolated_corpus / "language_extensions" / "foreach_static_zip"
83+
le_pin = le_case / "expected.build.yaml"
84+
le_pin.write_text("STALE\n")
85+
rc = update_goldens.main(["--new"])
86+
assert rc == 0
87+
# SAM case now has the file back
88+
assert (sam_case / "expected.build.yaml").exists()
89+
# LE case stays stale (--new does not touch existing pins)
90+
assert le_pin.read_text() == "STALE\n"

0 commit comments

Comments
 (0)