Repository navigation
test(resolver): cover user value validation and dependency guards #1618
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
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| package resolver | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/devsy-org/devsy/pkg/config" | ||
| "github.com/devsy-org/devsy/pkg/devcontainer/graph" | ||
| "github.com/devsy-org/devsy/pkg/types" | ||
| "github.com/stretchr/testify/suite" | ||
| ) | ||
|
|
||
| const ( | ||
| testParent = "parent" | ||
| testChild = "child" | ||
| testDefaultRef = "${V}" | ||
| testGlobalChildErr = "cannot use a global option as a dependency of a non-global option. " + | ||
| `Option "child" used in children of option "parent"` | ||
| testLocalChildErr = "cannot use a non-local option as a dependency of a local option. " + | ||
| `Option "child" used in children of option "parent"` | ||
| ) | ||
|
|
||
| type DependencyGuardTestSuite struct { | ||
| suite.Suite | ||
| } | ||
|
|
||
| func TestDependencyGuardTestSuite(t *testing.T) { | ||
| suite.Run(t, new(DependencyGuardTestSuite)) | ||
| } | ||
|
|
||
| type dependencyCase struct { | ||
| defs config.OptionDefinitions | ||
| values map[string]config.OptionValue | ||
| wantErr string | ||
| from string | ||
| to string | ||
| } | ||
|
|
||
| func childCase(parent, child *types.Option, wantErr string) dependencyCase { | ||
| return dependencyCase{ | ||
| defs: config.OptionDefinitions{testParent: parent, testChild: child}, | ||
| values: map[string]config.OptionValue{testParent: {Children: []string{testChild}}}, | ||
| wantErr: wantErr, | ||
| from: testParent, | ||
| to: testChild, | ||
| } | ||
| } | ||
|
|
||
| func variableCase(option, dep *types.Option, wantErr string) dependencyCase { | ||
| return dependencyCase{ | ||
| defs: config.OptionDefinitions{"opt": option, "V": dep}, | ||
| values: map[string]config.OptionValue{}, | ||
| wantErr: wantErr, | ||
| from: "V", | ||
| to: "opt", | ||
| } | ||
| } | ||
|
|
||
| func (suite *DependencyGuardTestSuite) TestChildScopeGuards() { | ||
| cases := map[string]dependencyCase{ | ||
| "global child under non-global parent rejected": childCase( | ||
| &types.Option{}, | ||
| &types.Option{Global: true}, | ||
| testGlobalChildErr, | ||
| ), | ||
| "global child under global parent accepted": childCase( | ||
| &types.Option{Global: true}, &types.Option{Global: true}, "", | ||
| ), | ||
| "non-global child under global parent accepted": childCase( | ||
| &types.Option{Global: true}, &types.Option{}, "", | ||
| ), | ||
| "non-local child under local parent rejected": childCase( | ||
| &types.Option{Local: true}, | ||
| &types.Option{}, | ||
| testLocalChildErr, | ||
| ), | ||
| "local child under local parent accepted": childCase( | ||
| &types.Option{Local: true}, &types.Option{Local: true}, "", | ||
| ), | ||
| "local child under non-local parent accepted": childCase( | ||
| &types.Option{}, &types.Option{Local: true}, "", | ||
| ), | ||
| } | ||
|
|
||
| for name, tc := range cases { | ||
| suite.Run(name, func() { suite.runCase(tc) }) | ||
| } | ||
| } | ||
|
|
||
| func (suite *DependencyGuardTestSuite) TestVariableScopeGuards() { | ||
| cases := map[string]dependencyCase{ | ||
| "global default referencing non-global variable rejected": variableCase( | ||
| &types.Option{Global: true, Default: testDefaultRef}, | ||
| &types.Option{}, | ||
| `cannot use a non-global option as a dependency of a global option. Option "V" used in default of option "opt"`, | ||
| ), | ||
| "global default referencing global variable accepted": variableCase( | ||
| &types.Option{Global: true, Default: testDefaultRef}, &types.Option{Global: true}, "", | ||
| ), | ||
| "non-local command referencing local variable rejected": variableCase( | ||
| &types.Option{Command: "echo ${V}"}, | ||
| &types.Option{Local: true}, | ||
| `cannot use a local option as a dependency of a non-local option. Option "V" used in command of option "opt"`, | ||
| ), | ||
| "local default referencing non-local variable accepted": variableCase( | ||
| &types.Option{Local: true, Default: testDefaultRef}, &types.Option{}, "", | ||
| ), | ||
| } | ||
|
|
||
| for name, tc := range cases { | ||
| suite.Run(name, func() { suite.runCase(tc) }) | ||
| } | ||
| } | ||
|
|
||
| func (suite *DependencyGuardTestSuite) TestUndefinedReferencesAreSkipped() { | ||
| defs := config.OptionDefinitions{testParent: {Global: true, Default: "${MISSING}"}} | ||
| values := map[string]config.OptionValue{testParent: {Children: []string{"ghost"}}} | ||
|
|
||
| g := graph.NewGraph[*types.Option]() | ||
| suite.Require().NoError(addOptionsToGraph(g, defs, values)) | ||
| suite.False(g.HasEdge(testParent, "ghost")) | ||
| suite.False(g.HasEdge("MISSING", testParent)) | ||
| } | ||
|
|
||
| func (suite *DependencyGuardTestSuite) TestResolveRejectsIncompatibleGraph() { | ||
| defs := map[string]*types.Option{testParent: {}, testChild: {Global: true}} | ||
| values := map[string]config.OptionValue{testParent: {Children: []string{testChild}}} | ||
|
|
||
| resolved, _, err := New(map[string]string{}, map[string]string{}).Resolve( | ||
| context.Background(), nil, defs, values, | ||
| ) | ||
| suite.Nil(resolved) | ||
| suite.ErrorContains(err, testGlobalChildErr) | ||
| } | ||
|
|
||
| func (suite *DependencyGuardTestSuite) TestResolveKeepsValidBoundaryUserValue() { | ||
| defs := map[string]*types.Option{"count": {Type: typeNumber}} | ||
|
|
||
| resolved, _, err := New( | ||
| map[string]string{"count": "-9223372036854775808"}, | ||
| map[string]string{}, | ||
| ).Resolve(context.Background(), nil, defs, map[string]config.OptionValue{}) | ||
| suite.Require().NoError(err) | ||
| suite.Equal("-9223372036854775808", resolved["count"].Value) | ||
| } | ||
|
|
||
| func (suite *DependencyGuardTestSuite) runCase(tc dependencyCase) { | ||
| g := graph.NewGraph[*types.Option]() | ||
| err := addOptionsToGraph(g, tc.defs, tc.values) | ||
| if tc.wantErr != "" { | ||
| suite.ErrorContains(err, tc.wantErr) | ||
| suite.False(g.HasEdge(tc.from, tc.to)) | ||
| return | ||
| } | ||
| suite.Require().NoError(err) | ||
| suite.True(g.HasEdge(tc.from, tc.to)) | ||
| suite.False(g.HasEdge(tc.to, tc.from)) | ||
| } |
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,117 @@ | ||
| package resolver | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/devsy-org/devsy/pkg/types" | ||
| "github.com/stretchr/testify/suite" | ||
| ) | ||
|
|
||
| const ( | ||
| typeNumber = "number" | ||
| typeBoolean = "boolean" | ||
| digitsRegex = "^[0-9]+$" | ||
| yesValue = "yes" | ||
| ) | ||
|
|
||
| type ValidateUserValueTestSuite struct { | ||
| suite.Suite | ||
| } | ||
|
|
||
| type userValueCase struct { | ||
| value string | ||
| option *types.Option | ||
| wantError bool | ||
| } | ||
|
|
||
| func TestValidateUserValueTestSuite(t *testing.T) { | ||
| suite.Run(t, new(ValidateUserValueTestSuite)) | ||
| } | ||
|
|
||
| func (suite *ValidateUserValueTestSuite) TestValidateUserValuePatternAndEnum() { | ||
| dev := types.OptionEnumArray{{Value: "dev"}} | ||
| suite.runUserValueCases(map[string]userValueCase{ | ||
| "pattern mismatch rejected": { | ||
| "Abc", | ||
| &types.Option{ValidationPattern: "^[a-z]+$"}, | ||
| true, | ||
| }, | ||
| "pattern match accepted": { | ||
| "abc", | ||
| &types.Option{ValidationPattern: "^[a-z]+$"}, | ||
| false, | ||
| }, | ||
| "invalid regex pattern rejected": {"x", &types.Option{ValidationPattern: "[a-"}, true}, | ||
| "enum prefix of member rejected": {"de", &types.Option{Enum: dev}, true}, | ||
| "enum member accepted": {"dev", &types.Option{Enum: dev}, false}, | ||
| "empty enum accepts any value": {"anything", &types.Option{}, false}, | ||
| "pattern passes but enum excludes": { | ||
| "7", | ||
| &types.Option{ | ||
| ValidationPattern: digitsRegex, | ||
| Enum: types.OptionEnumArray{{Value: "5"}}, | ||
| }, | ||
| true, | ||
| }, | ||
| "all checks pass": { | ||
| "5", | ||
| &types.Option{ | ||
| Type: typeNumber, | ||
| ValidationPattern: digitsRegex, | ||
| Enum: types.OptionEnumArray{{Value: "5"}}, | ||
| }, | ||
| false, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func (suite *ValidateUserValueTestSuite) TestValidateUserValueTypes() { | ||
| suite.runUserValueCases(map[string]userValueCase{ | ||
| "number accepts negative": {"-42", &types.Option{Type: typeNumber}, false}, | ||
| "number rejects decimal": {"1.5", &types.Option{Type: typeNumber}, true}, | ||
| "number accepts int64 max": { | ||
| "9223372036854775807", | ||
| &types.Option{Type: typeNumber}, | ||
| false, | ||
| }, | ||
| "number rejects int64 overflow": { | ||
| "9223372036854775808", | ||
| &types.Option{Type: typeNumber}, | ||
| true, | ||
| }, | ||
| "boolean accepts 1": {"1", &types.Option{Type: typeBoolean}, false}, | ||
| "boolean rejects yes": {yesValue, &types.Option{Type: typeBoolean}, true}, | ||
| "duration accepts hours": {"24h", &types.Option{Type: "duration"}, false}, | ||
| "duration rejects bare number": {"10", &types.Option{Type: "duration"}, true}, | ||
| "unknown type is not checked": {"anything", &types.Option{Type: "string"}, false}, | ||
| "enum passes but type fails": { | ||
| yesValue, | ||
| &types.Option{Type: typeBoolean, Enum: types.OptionEnumArray{{Value: yesValue}}}, | ||
| true, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func (suite *ValidateUserValueTestSuite) TestPatternErrorTakesPrecedence() { | ||
| option := &types.Option{ | ||
| ValidationPattern: digitsRegex, | ||
| ValidationMessage: "must be digits", | ||
| Type: typeNumber, | ||
| Enum: types.OptionEnumArray{{Value: "5"}}, | ||
| } | ||
|
|
||
| suite.EqualError(validateUserValue("opt", "x", option), "must be digits") | ||
| } | ||
|
|
||
| func (suite *ValidateUserValueTestSuite) runUserValueCases(cases map[string]userValueCase) { | ||
| for name, tc := range cases { | ||
| suite.Run(name, func() { | ||
| err := validateUserValue("opt", tc.value, tc.option) | ||
| if tc.wantError { | ||
| suite.Error(err) | ||
| } else { | ||
| suite.NoError(err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.