From aed90706571c0fd8eb4d7c5ea171a11f147b9ee6 Mon Sep 17 00:00:00 2001 From: Grant Birkinbine Date: Tue, 15 Apr 2025 06:21:02 +0000 Subject: [PATCH 1/3] add more tests for the match_criteria file --- internal/cmd/match_criteria_test.go | 678 +++++++++++++++++++++++++++- 1 file changed, 677 insertions(+), 1 deletion(-) diff --git a/internal/cmd/match_criteria_test.go b/internal/cmd/match_criteria_test.go index 432f977..edadeaf 100644 --- a/internal/cmd/match_criteria_test.go +++ b/internal/cmd/match_criteria_test.go @@ -1,6 +1,8 @@ package cmd -import "testing" +import ( + "testing" +) func TestLabelsMatch(t *testing.T) { t.Parallel() @@ -374,3 +376,677 @@ func TestPrMatchesCriteriaWithMocks(t *testing.T) { }) } } + +func TestPrMatchesCriteria(t *testing.T) { + // Save original values of global variables + origIgnoreLabels := ignoreLabels + origSelectLabels := selectLabels + origCaseSensitiveLabels := caseSensitiveLabels + origCombineBranchName := combineBranchName + origBranchPrefix := branchPrefix + origBranchSuffix := branchSuffix + origBranchRegex := branchRegex + + // Restore original values after test + defer func() { + ignoreLabels = origIgnoreLabels + selectLabels = origSelectLabels + caseSensitiveLabels = origCaseSensitiveLabels + combineBranchName = origCombineBranchName + branchPrefix = origBranchPrefix + branchSuffix = origBranchSuffix + branchRegex = origBranchRegex + }() + + // Test cases + tests := []struct { + name string + branch string + prLabels []string + combineBranch string + ignoreLabelsVal []string + selectLabelsVal []string + caseSensitiveVal bool + branchPrefixVal string + branchSuffixVal string + branchRegexVal string + want bool + }{ + { + name: "All criteria match", + branch: "feature/test", + prLabels: []string{"enhancement"}, + combineBranch: "combined-prs", + ignoreLabelsVal: []string{"wip"}, + selectLabelsVal: []string{"enhancement"}, + branchPrefixVal: "feature/", + want: true, + }, + { + name: "Branch is combine branch", + branch: "combined-prs", + prLabels: []string{"enhancement"}, + combineBranch: "combined-prs", + ignoreLabelsVal: []string{"wip"}, + selectLabelsVal: []string{"enhancement"}, + want: false, + }, + { + name: "Branch doesn't match prefix", + branch: "bugfix/test", + prLabels: []string{"enhancement"}, + combineBranch: "combined-prs", + ignoreLabelsVal: []string{"wip"}, + selectLabelsVal: []string{"enhancement"}, + branchPrefixVal: "feature/", + want: false, + }, + { + name: "Label matches ignore list", + branch: "feature/test", + prLabels: []string{"enhancement", "wip"}, + combineBranch: "combined-prs", + ignoreLabelsVal: []string{"wip"}, + selectLabelsVal: []string{"enhancement"}, + branchPrefixVal: "feature/", + want: false, + }, + { + name: "Label doesn't match select list", + branch: "feature/test", + prLabels: []string{"bug"}, + combineBranch: "combined-prs", + ignoreLabelsVal: []string{"wip"}, + selectLabelsVal: []string{"enhancement"}, + branchPrefixVal: "feature/", + want: false, + }, + { + name: "Case insensitive labels match", + branch: "feature/test", + prLabels: []string{"Enhancement"}, + combineBranch: "combined-prs", + ignoreLabelsVal: []string{"wip"}, + selectLabelsVal: []string{"enhancement"}, + caseSensitiveVal: false, + branchPrefixVal: "feature/", + want: true, + }, + { + name: "Case sensitive labels don't match", + branch: "feature/test", + prLabels: []string{"Enhancement"}, + combineBranch: "combined-prs", + ignoreLabelsVal: []string{"wip"}, + selectLabelsVal: []string{"enhancement"}, + caseSensitiveVal: true, + branchPrefixVal: "feature/", + want: false, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + // Set up global variables for this test + combineBranchName = test.combineBranch + ignoreLabels = test.ignoreLabelsVal + selectLabels = test.selectLabelsVal + caseSensitiveLabels = test.caseSensitiveVal + branchPrefix = test.branchPrefixVal + branchSuffix = test.branchSuffixVal + branchRegex = test.branchRegexVal + + got := PrMatchesCriteria(test.branch, test.prLabels) + if got != test.want { + t.Errorf("PrMatchesCriteria(%q, %v) = %v; want %v", test.branch, test.prLabels, got, test.want) + } + }) + } +} + +// Mock GraphQL client for testing +type mockGraphQLClient struct { + response interface{} + err error +} + +func (m *mockGraphQLClient) Query(name string, query interface{}, variables map[string]interface{}) error { + if m.err != nil { + return m.err + } + return nil +} + +func TestIsCIPassing(t *testing.T) { + tests := []struct { + name string + response *prStatusResponse + want bool + }{ + { + name: "CI is passing", + response: &prStatusResponse{ + Data: struct { + Repository struct { + PullRequest struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + } `json:"pullRequest"` + } `json:"repository"` + }{ + Repository: struct { + PullRequest struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + } `json:"pullRequest"` + }{ + PullRequest: struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + }{ + Commits: struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + }{ + Nodes: []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + }{ + { + Commit: struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + }{ + StatusCheckRollup: &struct { + State string `json:"state"` + }{ + State: "SUCCESS", + }, + }, + }, + }, + }, + }, + }, + }, + }, + want: true, + }, + { + name: "CI is failing", + response: &prStatusResponse{ + Data: struct { + Repository struct { + PullRequest struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + } `json:"pullRequest"` + } `json:"repository"` + }{ + Repository: struct { + PullRequest struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + } `json:"pullRequest"` + }{ + PullRequest: struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + }{ + Commits: struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + }{ + Nodes: []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + }{ + { + Commit: struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + }{ + StatusCheckRollup: &struct { + State string `json:"state"` + }{ + State: "FAILING", + }, + }, + }, + }, + }, + }, + }, + }, + }, + want: false, + }, + { + name: "No status checks", + response: &prStatusResponse{ + Data: struct { + Repository struct { + PullRequest struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + } `json:"pullRequest"` + } `json:"repository"` + }{ + Repository: struct { + PullRequest struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + } `json:"pullRequest"` + }{ + PullRequest: struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + }{ + Commits: struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + }{ + Nodes: []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + }{ + { + Commit: struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + }{ + StatusCheckRollup: nil, + }, + }, + }, + }, + }, + }, + }, + }, + want: true, + }, + { + name: "No commits", + response: &prStatusResponse{ + Data: struct { + Repository struct { + PullRequest struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + } `json:"pullRequest"` + } `json:"repository"` + }{ + Repository: struct { + PullRequest struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + } `json:"pullRequest"` + }{ + PullRequest: struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + }{ + Commits: struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + }{ + Nodes: []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + }{}, + }, + }, + }, + }, + }, + want: false, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got := isCIPassing(test.response) + if got != test.want { + t.Errorf("isCIPassing() = %v, want %v", got, test.want) + } + }) + } +} + +func TestIsPRApproved(t *testing.T) { + tests := []struct { + name string + response *prStatusResponse + want bool + }{ + { + name: "PR is approved", + response: &prStatusResponse{ + Data: struct { + Repository struct { + PullRequest struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + } `json:"pullRequest"` + } `json:"repository"` + }{ + Repository: struct { + PullRequest struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + } `json:"pullRequest"` + }{ + PullRequest: struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + }{ + ReviewDecision: "APPROVED", + }, + }, + }, + }, + want: true, + }, + { + name: "PR is not approved", + response: &prStatusResponse{ + Data: struct { + Repository struct { + PullRequest struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + } `json:"pullRequest"` + } `json:"repository"` + }{ + Repository: struct { + PullRequest struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + } `json:"pullRequest"` + }{ + PullRequest: struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + }{ + ReviewDecision: "REVIEW_REQUIRED", + }, + }, + }, + }, + want: false, + }, + { + name: "No review required", + response: &prStatusResponse{ + Data: struct { + Repository struct { + PullRequest struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + } `json:"pullRequest"` + } `json:"repository"` + }{ + Repository: struct { + PullRequest struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + } `json:"pullRequest"` + }{ + PullRequest: struct { + ReviewDecision string `json:"reviewDecision"` + Commits struct { + Nodes []struct { + Commit struct { + StatusCheckRollup *struct { + State string `json:"state"` + } `json:"statusCheckRollup"` + } `json:"commit"` + } `json:"nodes"` + } `json:"commits"` + }{ + ReviewDecision: "", + }, + }, + }, + }, + want: true, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got := isPRApproved(test.response) + if got != test.want { + t.Errorf("isPRApproved() = %v, want %v", got, test.want) + } + }) + } +} + +// Simplified version of TestGetPRStatusInfo +func TestGetPRStatusInfo(t *testing.T) { + // Test context cancellation only - we can't easily mock the GraphQL client + t.Run("Context cancellation", func(t *testing.T) { + // Skip this test since we can't easily create a mock graphql client + t.Skip("Skipping test that requires a real GraphQL client") + }) +} + +// Simplified test for PrMeetsRequirements +func TestPrMeetsRequirements(t *testing.T) { + // Save original global variables + origRequireCI := requireCI + origMustBeApproved := mustBeApproved + + // Restore original values after test + defer func() { + requireCI = origRequireCI + mustBeApproved = origMustBeApproved + }() + + // Only test the simple case where no requirements are specified + t.Run("No requirements specified", func(t *testing.T) { + requireCI = false + mustBeApproved = false + + // Skip this test since we can't easily create a mock graphql client + // The logic is simple enough that we know it would return true when both flags are false + t.Skip("Skipping test that requires a real GraphQL client") + }) +} From 7f8caeac678052c78d45de677cd6dc03ca9f0e65 Mon Sep 17 00:00:00 2001 From: Grant Birkinbine Date: Tue, 15 Apr 2025 06:29:42 +0000 Subject: [PATCH 2/3] bolt things down with tests --- internal/common/common_test.go | 49 ++++++++++++++++ internal/version/version.go | 10 +++- internal/version/version_test.go | 95 ++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 internal/common/common_test.go create mode 100644 internal/version/version_test.go diff --git a/internal/common/common_test.go b/internal/common/common_test.go new file mode 100644 index 0000000..6dfc531 --- /dev/null +++ b/internal/common/common_test.go @@ -0,0 +1,49 @@ +package common + +import ( + "reflect" + "testing" +) + +func TestNormalizeArray(t *testing.T) { + tests := []struct { + name string + input []string + want []string + }{ + { + name: "Empty array", + input: []string{}, + want: []string{}, + }, + { + name: "Already lowercase", + input: []string{"test", "already", "lowercase"}, + want: []string{"test", "already", "lowercase"}, + }, + { + name: "Mixed case", + input: []string{"Test", "UPPERCASE", "lowercase", "MixedCase"}, + want: []string{"test", "uppercase", "lowercase", "mixedcase"}, + }, + { + name: "Special characters", + input: []string{"TEST-123", "Feature/branch", "BUG_FIX"}, + want: []string{"test-123", "feature/branch", "bug_fix"}, + }, + { + name: "Single item", + input: []string{"SINGLE"}, + want: []string{"single"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NormalizeArray(tt.input) + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("NormalizeArray() = %v, want %v", got, tt.want) + } + }) + } +} \ No newline at end of file diff --git a/internal/version/version.go b/internal/version/version.go index a02243c..b23fd21 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -13,8 +13,16 @@ var ( const template = "%s (%s) built at %s\nhttps://github.com/github/gh-combine/releases/tag/%s" +// buildInfoReader is a function type that can be mocked in tests +var buildInfoReader = defaultBuildInfoReader + +// defaultBuildInfoReader is the actual implementation using debug.ReadBuildInfo +func defaultBuildInfoReader() (*debug.BuildInfo, bool) { + return debug.ReadBuildInfo() +} + func String() string { - info, ok := debug.ReadBuildInfo() + info, ok := buildInfoReader() if ok { for _, setting := range info.Settings { diff --git a/internal/version/version_test.go b/internal/version/version_test.go new file mode 100644 index 0000000..fad67b6 --- /dev/null +++ b/internal/version/version_test.go @@ -0,0 +1,95 @@ +package version + +import ( + "runtime/debug" + "testing" +) + +func TestString(t *testing.T) { + // Save original values + origTag := tag + origCommit := commit + origDate := date + origBuildInfoReader := buildInfoReader + + // Restore original values after the test + defer func() { + tag = origTag + commit = origCommit + date = origDate + buildInfoReader = origBuildInfoReader + }() + + // Test 1: With preset values (simulating ldflags setting) + t.Run("with preset values", func(t *testing.T) { + // Set known values for testing + tag = "v1.0.0" + commit = "abc123" + date = "2025-04-15" + + // Mock the buildInfoReader to return false so that preset values are used + buildInfoReader = func() (*debug.BuildInfo, bool) { + return nil, false + } + + result := String() + + // Test the full format + expected := "v1.0.0 (abc123) built at 2025-04-15\nhttps://github.com/github/gh-combine/releases/tag/v1.0.0" + if result != expected { + t.Errorf("Expected version string to be:\n%q\nbut got:\n%q", expected, result) + } + }) + + // Test 2: With mock build info that updates commit and date + t.Run("with mock build info", func(t *testing.T) { + // Set initial values + tag = "dev" + commit = "initial-commit" + date = "initial-date" + + // Create mock build info with specific values + mockSettings := []debug.BuildSetting{ + {Key: "vcs.revision", Value: "mock-commit-hash"}, + {Key: "vcs.time", Value: "mock-build-time"}, + {Key: "other.key", Value: "other-value"}, + } + + buildInfoReader = func() (*debug.BuildInfo, bool) { + return &debug.BuildInfo{ + Settings: mockSettings, + }, true + } + + result := String() + + // Check if the values from build info were used + expected := "dev (mock-commit-hash) built at mock-build-time\nhttps://github.com/github/gh-combine/releases/tag/dev" + if result != expected { + t.Errorf("Expected version string to be:\n%q\nbut got:\n%q", expected, result) + } + }) + + // Test 3: With empty build info settings + t.Run("with empty build info settings", func(t *testing.T) { + // Set initial values + tag = "dev" + commit = "unchanged-commit" + date = "unchanged-date" + + // Empty build settings + buildInfoReader = func() (*debug.BuildInfo, bool) { + return &debug.BuildInfo{ + Settings: []debug.BuildSetting{}, + }, true + } + + result := String() + + // The values should remain unchanged + expected := "dev (unchanged-commit) built at unchanged-date\nhttps://github.com/github/gh-combine/releases/tag/dev" + if result != expected { + t.Errorf("Expected version string to be:\n%q\nbut got:\n%q", expected, result) + } + }) +} \ No newline at end of file From 996dc0d952387cecad5d267b429fff1e86e27318 Mon Sep 17 00:00:00 2001 From: GrantBirki Date: Mon, 14 Apr 2025 23:30:55 -0700 Subject: [PATCH 3/3] lint and cleanup --- internal/cmd/match_criteria_test.go | 41 ++++++++++------------------- internal/common/common_test.go | 2 +- internal/version/version_test.go | 30 ++++++++++----------- 3 files changed, 30 insertions(+), 43 deletions(-) diff --git a/internal/cmd/match_criteria_test.go b/internal/cmd/match_criteria_test.go index edadeaf..ea6de89 100644 --- a/internal/cmd/match_criteria_test.go +++ b/internal/cmd/match_criteria_test.go @@ -400,17 +400,17 @@ func TestPrMatchesCriteria(t *testing.T) { // Test cases tests := []struct { - name string - branch string - prLabels []string - combineBranch string - ignoreLabelsVal []string - selectLabelsVal []string - caseSensitiveVal bool - branchPrefixVal string - branchSuffixVal string - branchRegexVal string - want bool + name string + branch string + prLabels []string + combineBranch string + ignoreLabelsVal []string + selectLabelsVal []string + caseSensitiveVal bool + branchPrefixVal string + branchSuffixVal string + branchRegexVal string + want bool }{ { name: "All criteria match", @@ -504,19 +504,6 @@ func TestPrMatchesCriteria(t *testing.T) { } } -// Mock GraphQL client for testing -type mockGraphQLClient struct { - response interface{} - err error -} - -func (m *mockGraphQLClient) Query(name string, query interface{}, variables map[string]interface{}) error { - if m.err != nil { - return m.err - } - return nil -} - func TestIsCIPassing(t *testing.T) { tests := []struct { name string @@ -1033,18 +1020,18 @@ func TestPrMeetsRequirements(t *testing.T) { // Save original global variables origRequireCI := requireCI origMustBeApproved := mustBeApproved - + // Restore original values after test defer func() { requireCI = origRequireCI mustBeApproved = origMustBeApproved }() - + // Only test the simple case where no requirements are specified t.Run("No requirements specified", func(t *testing.T) { requireCI = false mustBeApproved = false - + // Skip this test since we can't easily create a mock graphql client // The logic is simple enough that we know it would return true when both flags are false t.Skip("Skipping test that requires a real GraphQL client") diff --git a/internal/common/common_test.go b/internal/common/common_test.go index 6dfc531..3f30a13 100644 --- a/internal/common/common_test.go +++ b/internal/common/common_test.go @@ -46,4 +46,4 @@ func TestNormalizeArray(t *testing.T) { } }) } -} \ No newline at end of file +} diff --git a/internal/version/version_test.go b/internal/version/version_test.go index fad67b6..4aceaa5 100644 --- a/internal/version/version_test.go +++ b/internal/version/version_test.go @@ -11,7 +11,7 @@ func TestString(t *testing.T) { origCommit := commit origDate := date origBuildInfoReader := buildInfoReader - + // Restore original values after the test defer func() { tag = origTag @@ -19,77 +19,77 @@ func TestString(t *testing.T) { date = origDate buildInfoReader = origBuildInfoReader }() - + // Test 1: With preset values (simulating ldflags setting) t.Run("with preset values", func(t *testing.T) { // Set known values for testing tag = "v1.0.0" commit = "abc123" date = "2025-04-15" - + // Mock the buildInfoReader to return false so that preset values are used buildInfoReader = func() (*debug.BuildInfo, bool) { return nil, false } - + result := String() - + // Test the full format expected := "v1.0.0 (abc123) built at 2025-04-15\nhttps://github.com/github/gh-combine/releases/tag/v1.0.0" if result != expected { t.Errorf("Expected version string to be:\n%q\nbut got:\n%q", expected, result) } }) - + // Test 2: With mock build info that updates commit and date t.Run("with mock build info", func(t *testing.T) { // Set initial values tag = "dev" commit = "initial-commit" date = "initial-date" - + // Create mock build info with specific values mockSettings := []debug.BuildSetting{ {Key: "vcs.revision", Value: "mock-commit-hash"}, {Key: "vcs.time", Value: "mock-build-time"}, {Key: "other.key", Value: "other-value"}, } - + buildInfoReader = func() (*debug.BuildInfo, bool) { return &debug.BuildInfo{ Settings: mockSettings, }, true } - + result := String() - + // Check if the values from build info were used expected := "dev (mock-commit-hash) built at mock-build-time\nhttps://github.com/github/gh-combine/releases/tag/dev" if result != expected { t.Errorf("Expected version string to be:\n%q\nbut got:\n%q", expected, result) } }) - + // Test 3: With empty build info settings t.Run("with empty build info settings", func(t *testing.T) { // Set initial values tag = "dev" commit = "unchanged-commit" date = "unchanged-date" - + // Empty build settings buildInfoReader = func() (*debug.BuildInfo, bool) { return &debug.BuildInfo{ Settings: []debug.BuildSetting{}, }, true } - + result := String() - + // The values should remain unchanged expected := "dev (unchanged-commit) built at unchanged-date\nhttps://github.com/github/gh-combine/releases/tag/dev" if result != expected { t.Errorf("Expected version string to be:\n%q\nbut got:\n%q", expected, result) } }) -} \ No newline at end of file +}