Skip to content

Commit 130268b

Browse files
leodidoona-agent
andcommitted
refactor(test): use cmp.Diff for struct comparisons
Replace assert.Equal with cmp.Diff for struct comparisons to get better error messages when tests fail. This makes it easier to see exactly which fields differ. Changes: - Add google/go-cmp import (already in go.mod) - Use cmp.Diff for GitHubContext comparisons - Use cmp.Diff for SigningError comparisons - Remove unused testEnv map variable Benefits: - Better test failure messages showing field-by-field diffs - More idiomatic Go testing pattern for complex types - Easier debugging when tests fail Co-authored-by: Ona <[email protected]>
1 parent 74d751f commit 130268b

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

pkg/leeway/signing/attestation_test.go

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"testing"
1616

1717
"github.com/gitpod-io/leeway/pkg/leeway/cache"
18+
"github.com/google/go-cmp/cmp"
1819
"github.com/stretchr/testify/assert"
1920
"github.com/stretchr/testify/require"
2021
)
@@ -640,28 +641,22 @@ func TestGetGitHubContext(t *testing.T) {
640641
t.Setenv("GITHUB_SERVER_URL", "test-server")
641642
t.Setenv("GITHUB_WORKFLOW_REF", "test-workflow")
642643

643-
testEnv := map[string]string{
644-
"GITHUB_RUN_ID": "test-run-id",
645-
"GITHUB_RUN_NUMBER": "test-run-number",
646-
"GITHUB_ACTOR": "test-actor",
647-
"GITHUB_REPOSITORY": "test-repo",
648-
"GITHUB_REF": "test-ref",
649-
"GITHUB_SHA": "test-sha",
650-
"GITHUB_SERVER_URL": "test-server",
651-
"GITHUB_WORKFLOW_REF": "test-workflow",
644+
// Test GetGitHubContext
645+
got := GetGitHubContext()
646+
want := &GitHubContext{
647+
RunID: "test-run-id",
648+
RunNumber: "test-run-number",
649+
Actor: "test-actor",
650+
Repository: "test-repo",
651+
Ref: "test-ref",
652+
SHA: "test-sha",
653+
ServerURL: "test-server",
654+
WorkflowRef: "test-workflow",
652655
}
653656

654-
// Test GetGitHubContext
655-
ctx := GetGitHubContext()
656-
657-
assert.Equal(t, testEnv["GITHUB_RUN_ID"], ctx.RunID)
658-
assert.Equal(t, testEnv["GITHUB_RUN_NUMBER"], ctx.RunNumber)
659-
assert.Equal(t, testEnv["GITHUB_ACTOR"], ctx.Actor)
660-
assert.Equal(t, testEnv["GITHUB_REPOSITORY"], ctx.Repository)
661-
assert.Equal(t, testEnv["GITHUB_REF"], ctx.Ref)
662-
assert.Equal(t, testEnv["GITHUB_SHA"], ctx.SHA)
663-
assert.Equal(t, testEnv["GITHUB_SERVER_URL"], ctx.ServerURL)
664-
assert.Equal(t, testEnv["GITHUB_WORKFLOW_REF"], ctx.WorkflowRef)
657+
if diff := cmp.Diff(want, got); diff != "" {
658+
t.Errorf("GetGitHubContext() mismatch (-want +got):\n%s", diff)
659+
}
665660
}
666661

667662
// TestGetGitHubContext_EmptyEnvironment tests with empty environment
@@ -677,16 +672,21 @@ func TestGetGitHubContext_EmptyEnvironment(t *testing.T) {
677672
t.Setenv("GITHUB_WORKFLOW_REF", "")
678673

679674
// Test GetGitHubContext with empty environment
680-
ctx := GetGitHubContext()
681-
682-
assert.Empty(t, ctx.RunID)
683-
assert.Empty(t, ctx.RunNumber)
684-
assert.Empty(t, ctx.Actor)
685-
assert.Empty(t, ctx.Repository)
686-
assert.Empty(t, ctx.Ref)
687-
assert.Empty(t, ctx.SHA)
688-
assert.Empty(t, ctx.ServerURL)
689-
assert.Empty(t, ctx.WorkflowRef)
675+
got := GetGitHubContext()
676+
want := &GitHubContext{
677+
RunID: "",
678+
RunNumber: "",
679+
Actor: "",
680+
Repository: "",
681+
Ref: "",
682+
SHA: "",
683+
ServerURL: "",
684+
WorkflowRef: "",
685+
}
686+
687+
if diff := cmp.Diff(want, got); diff != "" {
688+
t.Errorf("GetGitHubContext() with empty env mismatch (-want +got):\n%s", diff)
689+
}
690690
}
691691

692692
// TestSigningError tests the error types
@@ -942,12 +942,12 @@ func TestCategorizeError_ExistingSigningError(t *testing.T) {
942942
Message: "access denied",
943943
}
944944

945-
result := CategorizeError("different.tar.gz", originalErr)
945+
got := CategorizeError("different.tar.gz", originalErr)
946946

947947
// Should return the original error unchanged
948-
assert.Equal(t, originalErr, result)
949-
assert.Equal(t, ErrorTypePermission, result.Type)
950-
assert.Equal(t, "test.tar.gz", result.Artifact) // Original artifact preserved
948+
if diff := cmp.Diff(originalErr, got); diff != "" {
949+
t.Errorf("CategorizeError() should preserve original error (-want +got):\n%s", diff)
950+
}
951951
}
952952

953953
// TestWithRetry_MaxAttemptsExceeded tests retry exhaustion

0 commit comments

Comments
 (0)