-
Notifications
You must be signed in to change notification settings - Fork 3
Consolidate duplicate SetGatewayVersion implementations #695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package version | ||
|
|
||
| // gatewayVersion stores the gateway version string, used across multiple packages | ||
| // for error reporting, health checks, and MCP client implementation info. | ||
| // It defaults to "dev" and should be set once at startup. | ||
| // | ||
| // Thread-safety note: This variable is written once at application startup | ||
| // (in SetVersion) before any concurrent access, and read-only thereafter. | ||
| // No mutex is needed as the write happens before any goroutines are spawned. | ||
| var gatewayVersion = "dev" | ||
|
|
||
| // Set updates the gateway version string if the provided version is non-empty. | ||
| // This should be called once at application startup from main. | ||
| func Set(v string) { | ||
| if v != "" { | ||
| gatewayVersion = v | ||
| } | ||
|
Comment on lines
+14
to
+17
|
||
| } | ||
|
|
||
| // Get returns the current gateway version string. | ||
| func Get() string { | ||
| return gatewayVersion | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package version | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSet(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| inputVersion string | ||
| expectedResult string | ||
| }{ | ||
| { | ||
| name: "set valid version", | ||
| inputVersion: "v1.2.3", | ||
| expectedResult: "v1.2.3", | ||
| }, | ||
| { | ||
| name: "set version with build metadata", | ||
| inputVersion: "v1.2.3, commit: abc1234, built: 2024-01-01", | ||
| expectedResult: "v1.2.3, commit: abc1234, built: 2024-01-01", | ||
| }, | ||
| { | ||
| name: "empty string does not change version", | ||
| inputVersion: "", | ||
| expectedResult: "dev", // should remain default | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // Reset to default before each test | ||
| gatewayVersion = "dev" | ||
|
|
||
| Set(tt.inputVersion) | ||
| result := Get() | ||
| assert.Equal(t, tt.expectedResult, result) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGet(t *testing.T) { | ||
| // Reset to default | ||
| gatewayVersion = "dev" | ||
|
|
||
| // Test default value | ||
| result := Get() | ||
| require.Equal(t, "dev", result, "Default version should be 'dev'") | ||
|
|
||
| // Test after setting a value | ||
| Set("v2.0.0") | ||
| result = Get() | ||
| assert.Equal(t, "v2.0.0", result, "Version should be updated to 'v2.0.0'") | ||
| } | ||
|
|
||
| func TestSetPreservesVersionOnEmpty(t *testing.T) { | ||
| // Set an initial version | ||
| gatewayVersion = "v1.0.0" | ||
|
|
||
| // Try to set empty string | ||
| Set("") | ||
|
|
||
| // Version should remain unchanged | ||
| result := Get() | ||
| assert.Equal(t, "v1.0.0", result, "Version should not change when empty string is provided") | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment mentions "in SetVersion" but the function is actually named "Set". This should be corrected to "in Set()" for accuracy.