Skip to content

Commit 666b52e

Browse files
leodidoona-agent
andcommitted
refactor(test): encapsulate expected values in want struct
Refactor TestExtractBuilderIDFromOIDC to use a nested 'want' struct for expected values, separating test inputs from expected outputs. Before: expectError bool expectedID string errorMsg string After: want struct { id string err string } Benefits: - Clear separation between inputs and expected outputs - More idiomatic Go testing pattern - Easier to understand test intent at a glance - Follows go.mdc testing conventions Co-authored-by: Ona <[email protected]>
1 parent 130268b commit 666b52e

File tree

1 file changed

+55
-24
lines changed

1 file changed

+55
-24
lines changed

pkg/leeway/signing/attestation_test.go

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,9 +1221,10 @@ func TestExtractBuilderIDFromOIDC(t *testing.T) {
12211221
name string
12221222
setupServer func() *httptest.Server
12231223
githubCtx *GitHubContext
1224-
expectError bool
1225-
expectedID string
1226-
errorMsg string
1224+
want struct {
1225+
id string
1226+
err string
1227+
}
12271228
}{
12281229
{
12291230
name: "valid OIDC token with reusable workflow",
@@ -1249,8 +1250,12 @@ func TestExtractBuilderIDFromOIDC(t *testing.T) {
12491250
Repository: "example-org/example-repo",
12501251
WorkflowRef: "example-org/example-repo/.github/workflows/calling-workflow.yml@refs/heads/main",
12511252
},
1252-
expectError: false,
1253-
expectedID: "https://github.com/example-org/example-repo/.github/workflows/_build.yml@refs/heads/leo/slsa/b",
1253+
want: struct {
1254+
id string
1255+
err string
1256+
}{
1257+
id: "https://github.com/example-org/example-repo/.github/workflows/_build.yml@refs/heads/leo/slsa/b",
1258+
},
12541259
},
12551260
{
12561261
name: "valid OIDC token with direct workflow",
@@ -1275,8 +1280,12 @@ func TestExtractBuilderIDFromOIDC(t *testing.T) {
12751280
Repository: "gitpod-io/leeway",
12761281
WorkflowRef: "gitpod-io/leeway/.github/workflows/build.yml@refs/heads/main",
12771282
},
1278-
expectError: false,
1279-
expectedID: "https://github.com/gitpod-io/leeway/.github/workflows/build.yml@refs/heads/main",
1283+
want: struct {
1284+
id string
1285+
err string
1286+
}{
1287+
id: "https://github.com/gitpod-io/leeway/.github/workflows/build.yml@refs/heads/main",
1288+
},
12801289
},
12811290
{
12821291
name: "invalid JWT format - only 2 parts",
@@ -1293,8 +1302,12 @@ func TestExtractBuilderIDFromOIDC(t *testing.T) {
12931302
ServerURL: "https://github.com",
12941303
Repository: "org/repo",
12951304
},
1296-
expectError: true,
1297-
errorMsg: "invalid JWT token format",
1305+
want: struct {
1306+
id string
1307+
err string
1308+
}{
1309+
err: "invalid JWT token format",
1310+
},
12981311
},
12991312
{
13001313
name: "missing sub claim",
@@ -1315,8 +1328,12 @@ func TestExtractBuilderIDFromOIDC(t *testing.T) {
13151328
ServerURL: "https://github.com",
13161329
Repository: "org/repo",
13171330
},
1318-
expectError: true,
1319-
errorMsg: "sub claim not found",
1331+
want: struct {
1332+
id string
1333+
err string
1334+
}{
1335+
err: "sub claim not found",
1336+
},
13201337
},
13211338
{
13221339
name: "whitespace-only sub claim",
@@ -1337,8 +1354,12 @@ func TestExtractBuilderIDFromOIDC(t *testing.T) {
13371354
ServerURL: "https://github.com",
13381355
Repository: "org/repo",
13391356
},
1340-
expectError: true,
1341-
errorMsg: "sub claim not found or empty",
1357+
want: struct {
1358+
id string
1359+
err string
1360+
}{
1361+
err: "sub claim not found or empty",
1362+
},
13421363
},
13431364
{
13441365
name: "job_workflow_ref in top-level claim (not in sub)",
@@ -1363,8 +1384,12 @@ func TestExtractBuilderIDFromOIDC(t *testing.T) {
13631384
ServerURL: "https://github.com",
13641385
Repository: "org/repo",
13651386
},
1366-
expectError: false,
1367-
expectedID: "https://github.com/org/repo/.github/workflows/deploy.yml@refs/heads/main",
1387+
want: struct {
1388+
id string
1389+
err string
1390+
}{
1391+
id: "https://github.com/org/repo/.github/workflows/deploy.yml@refs/heads/main",
1392+
},
13681393
},
13691394
{
13701395
name: "missing job_workflow_ref in sub claim and top-level",
@@ -1388,8 +1413,12 @@ func TestExtractBuilderIDFromOIDC(t *testing.T) {
13881413
ServerURL: "https://github.com",
13891414
Repository: "org/repo",
13901415
},
1391-
expectError: true,
1392-
errorMsg: "job_workflow_ref not found",
1416+
want: struct {
1417+
id string
1418+
err string
1419+
}{
1420+
err: "job_workflow_ref not found",
1421+
},
13931422
},
13941423
{
13951424
name: "OIDC token fetch failure",
@@ -1402,8 +1431,12 @@ func TestExtractBuilderIDFromOIDC(t *testing.T) {
14021431
ServerURL: "https://github.com",
14031432
Repository: "org/repo",
14041433
},
1405-
expectError: true,
1406-
errorMsg: "failed to fetch OIDC token",
1434+
want: struct {
1435+
id string
1436+
err string
1437+
}{
1438+
err: "failed to fetch OIDC token",
1439+
},
14071440
},
14081441
}
14091442

@@ -1417,14 +1450,12 @@ func TestExtractBuilderIDFromOIDC(t *testing.T) {
14171450

14181451
builderID, err := extractBuilderIDFromOIDC(context.Background(), tt.githubCtx)
14191452

1420-
if tt.expectError {
1453+
if tt.want.err != "" {
14211454
assert.Error(t, err)
1422-
if tt.errorMsg != "" {
1423-
assert.Contains(t, err.Error(), tt.errorMsg)
1424-
}
1455+
assert.Contains(t, err.Error(), tt.want.err)
14251456
} else {
14261457
assert.NoError(t, err)
1427-
assert.Equal(t, tt.expectedID, builderID)
1458+
assert.Equal(t, tt.want.id, builderID)
14281459
}
14291460
})
14301461
}

0 commit comments

Comments
 (0)