-
Notifications
You must be signed in to change notification settings - Fork 296
Add scope enum validation to cache-memory parser #19406
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| //go:build !integration | ||
|
|
||
| package workflow | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestCacheMemoryScopeValidationObject tests scope validation with object notation | ||
| func TestCacheMemoryScopeValidationObject(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| scope string | ||
| wantError bool | ||
| errorText string | ||
| }{ | ||
| { | ||
| name: "valid workflow scope", | ||
| scope: "workflow", | ||
| wantError: false, | ||
| }, | ||
| { | ||
| name: "valid repo scope", | ||
| scope: "repo", | ||
| wantError: false, | ||
| }, | ||
| { | ||
| name: "invalid organization scope", | ||
| scope: "organization", | ||
| wantError: true, | ||
| errorText: `invalid cache-memory scope "organization": must be one of [workflow repo]`, | ||
| }, | ||
| { | ||
| name: "invalid global scope", | ||
| scope: "global", | ||
| wantError: true, | ||
| errorText: `invalid cache-memory scope "global": must be one of [workflow repo]`, | ||
| }, | ||
| { | ||
| name: "invalid whitespace scope", | ||
| scope: " ", | ||
| wantError: true, | ||
| errorText: `invalid cache-memory scope " ": must be one of [workflow repo]`, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| toolsMap := map[string]any{ | ||
| "cache-memory": map[string]any{ | ||
| "scope": tt.scope, | ||
| }, | ||
| } | ||
|
|
||
| toolsConfig, err := ParseToolsConfig(toolsMap) | ||
| require.NoError(t, err, "Should parse tools config") | ||
|
|
||
| compiler := NewCompiler() | ||
| _, err = compiler.extractCacheMemoryConfig(toolsConfig) | ||
|
|
||
| if tt.wantError { | ||
| require.Error(t, err, "Should return error for invalid scope") | ||
| assert.ErrorContains(t, err, tt.errorText, "Error should contain expected message") | ||
| } else { | ||
| assert.NoError(t, err, "Should not return error for valid scope") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestCacheMemoryScopeValidationArray tests scope validation with array notation | ||
| func TestCacheMemoryScopeValidationArray(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| scope string | ||
| wantError bool | ||
| errorText string | ||
| }{ | ||
| { | ||
| name: "valid workflow scope in array", | ||
| scope: "workflow", | ||
| wantError: false, | ||
| }, | ||
| { | ||
| name: "valid repo scope in array", | ||
| scope: "repo", | ||
| wantError: false, | ||
| }, | ||
| { | ||
| name: "invalid scope in array", | ||
| scope: "global", | ||
| wantError: true, | ||
| errorText: `invalid cache-memory scope "global": must be one of [workflow repo]`, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| toolsMap := map[string]any{ | ||
| "cache-memory": []any{ | ||
| map[string]any{ | ||
| "id": "test-cache", | ||
| "scope": tt.scope, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| toolsConfig, err := ParseToolsConfig(toolsMap) | ||
| require.NoError(t, err, "Should parse tools config") | ||
|
|
||
| compiler := NewCompiler() | ||
| _, err = compiler.extractCacheMemoryConfig(toolsConfig) | ||
|
|
||
| if tt.wantError { | ||
| require.Error(t, err, "Should return error for invalid scope") | ||
| assert.ErrorContains(t, err, tt.errorText, "Error should contain expected message") | ||
| } else { | ||
| assert.NoError(t, err, "Should not return error for valid scope") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestCacheMemoryScopeDefault tests that omitting scope defaults to "workflow" | ||
| func TestCacheMemoryScopeDefault(t *testing.T) { | ||
| toolsMap := map[string]any{ | ||
| "cache-memory": map[string]any{ | ||
| "key": "my-cache-key", | ||
| }, | ||
| } | ||
|
|
||
| toolsConfig, err := ParseToolsConfig(toolsMap) | ||
| require.NoError(t, err, "Should parse tools config") | ||
|
|
||
| compiler := NewCompiler() | ||
| config, err := compiler.extractCacheMemoryConfig(toolsConfig) | ||
|
|
||
| require.NoError(t, err, "Should not error when scope is omitted") | ||
| require.NotNil(t, config, "Config should not be nil") | ||
| require.Len(t, config.Caches, 1, "Should have exactly one cache entry") | ||
|
|
||
| assert.Equal(t, "workflow", config.Caches[0].Scope, "Default scope should be 'workflow'") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 list of valid scopes is duplicated (once in the
slices.Containscall and again in the error string). This can drift if a new scope is added. Define avalidScopesslice once (optionally at package scope) and use it both for validation and for formatting the error message (e.g.,%v), so there’s a single source of truth.