From 0a3a1e4906cb4af498854c188344db48e8804c02 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Tue, 20 Oct 2026 08:34:34 -0600 Subject: [PATCH 1/3] test(resolver): cover user value validation and dependency guards --- pkg/options/resolver/dependency_test.go | 153 ++++++++++++++++++++++++ pkg/options/resolver/validation_test.go | 98 +++++++++++++++ 2 files changed, 251 insertions(+) create mode 100644 pkg/options/resolver/dependency_test.go create mode 100644 pkg/options/resolver/validation_test.go diff --git a/pkg/options/resolver/dependency_test.go b/pkg/options/resolver/dependency_test.go new file mode 100644 index 0000000000..81a5f0f77b --- /dev/null +++ b/pkg/options/resolver/dependency_test.go @@ -0,0 +1,153 @@ +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" +) + +type DependencyGuardTestSuite struct { + suite.Suite +} + +func TestDependencyGuardTestSuite(t *testing.T) { + suite.Run(t, new(DependencyGuardTestSuite)) +} + +// dependencyCase: wantErr is the scope error that must be returned (empty means accepted). +// from->to must be an edge when accepted and absent when rejected. +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{"parent": parent, "child": child}, + values: map[string]config.OptionValue{"parent": {Children: []string{"child"}}}, + wantErr: wantErr, + from: "parent", + to: "child", + } +} + +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) 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)) +} + +func (suite *DependencyGuardTestSuite) TestChildScopeGuards() { + cases := map[string]dependencyCase{ + "global child under non-global parent rejected": childCase( + &types.Option{}, + &types.Option{Global: true}, + `cannot use a global option as a dependency of a non-global option. Option "child" used in children of option "parent"`, + ), + "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{}, + `cannot use a non-local option as a dependency of a local option. Option "child" used in children of option "parent"`, + ), + "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: "${V}"}, + &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: "${V}"}, &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: "${V}"}, &types.Option{}, "", + ), + } + + for name, tc := range cases { + suite.Run(name, func() { suite.runCase(tc) }) + } +} + +func (suite *DependencyGuardTestSuite) TestUndefinedReferencesAreSkipped() { + defs := config.OptionDefinitions{"parent": {Global: true, Default: "${MISSING}"}} + values := map[string]config.OptionValue{"parent": {Children: []string{"ghost"}}} + + g := graph.NewGraph[*types.Option]() + suite.Require().NoError(addOptionsToGraph(g, defs, values)) + suite.False(g.HasEdge("parent", "ghost")) + suite.False(g.HasEdge("MISSING", "parent")) +} + +func (suite *DependencyGuardTestSuite) TestResolveRejectsIncompatibleGraph() { + defs := map[string]*types.Option{"parent": {}, "child": {Global: true}} + values := map[string]config.OptionValue{"parent": {Children: []string{"child"}}} + + resolved, _, err := New(map[string]string{}, map[string]string{}).Resolve( + context.Background(), nil, defs, values, + ) + suite.Nil(resolved) + suite.ErrorContains( + err, + `cannot use a global option as a dependency of a non-global option. Option "child" used in children of option "parent"`, + ) +} + +func (suite *DependencyGuardTestSuite) TestResolveKeepsValidBoundaryUserValue() { + defs := map[string]*types.Option{"count": {Type: "number"}} + + 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) +} diff --git a/pkg/options/resolver/validation_test.go b/pkg/options/resolver/validation_test.go new file mode 100644 index 0000000000..c19b2d5f81 --- /dev/null +++ b/pkg/options/resolver/validation_test.go @@ -0,0 +1,98 @@ +package resolver + +import ( + "testing" + + "github.com/devsy-org/devsy/pkg/types" + "github.com/stretchr/testify/suite" +) + +type ValidateUserValueTestSuite struct { + suite.Suite +} + +func TestValidateUserValueTestSuite(t *testing.T) { + suite.Run(t, new(ValidateUserValueTestSuite)) +} + +func (suite *ValidateUserValueTestSuite) TestValidateUserValueBoundaries() { + dev := types.OptionEnumArray{{Value: "dev"}} + cases := map[string]struct { + value string + option *types.Option + wantError bool + }{ + "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}, + "number accepts negative": {"-42", &types.Option{Type: "number"}, false}, + "number rejects decimal": {"1.5", &types.Option{Type: "number"}, true}, + "number accepts int64 max": { + "9223372036854775807", + &types.Option{Type: "number"}, + false, + }, + "number rejects int64 overflow": { + "9223372036854775808", + &types.Option{Type: "number"}, + true, + }, + "boolean accepts 1": {"1", &types.Option{Type: "boolean"}, false}, + "boolean rejects yes": {"yes", &types.Option{Type: "boolean"}, 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}, + "pattern passes but enum excludes": { + "7", + &types.Option{ValidationPattern: "^[0-9]+$", Enum: types.OptionEnumArray{{Value: "5"}}}, + true, + }, + "enum passes but type fails": { + "yes", + &types.Option{Type: "boolean", Enum: types.OptionEnumArray{{Value: "yes"}}}, + true, + }, + "all checks pass": { + "5", + &types.Option{ + Type: "number", + ValidationPattern: "^[0-9]+$", + Enum: types.OptionEnumArray{{Value: "5"}}, + }, + false, + }, + } + + 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) + } + }) + } +} + +func (suite *ValidateUserValueTestSuite) TestPatternErrorTakesPrecedence() { + option := &types.Option{ + ValidationPattern: "^[0-9]+$", + ValidationMessage: "must be digits", + Type: "number", + Enum: types.OptionEnumArray{{Value: "5"}}, + } + + suite.EqualError(validateUserValue("opt", "x", option), "must be digits") +} From bdf9884185913fb48d5f2a716c121841df5cfae3 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Tue, 20 Oct 2026 08:41:00 -0600 Subject: [PATCH 2/3] test(resolver): drop restating dependency case comments --- pkg/options/resolver/dependency_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/options/resolver/dependency_test.go b/pkg/options/resolver/dependency_test.go index 81a5f0f77b..5cd3e8ae03 100644 --- a/pkg/options/resolver/dependency_test.go +++ b/pkg/options/resolver/dependency_test.go @@ -18,8 +18,6 @@ func TestDependencyGuardTestSuite(t *testing.T) { suite.Run(t, new(DependencyGuardTestSuite)) } -// dependencyCase: wantErr is the scope error that must be returned (empty means accepted). -// from->to must be an edge when accepted and absent when rejected. type dependencyCase struct { defs config.OptionDefinitions values map[string]config.OptionValue From 7fa5c308875115f7a8ad971c432257ea796f1234 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Tue, 20 Oct 2026 08:51:49 -0600 Subject: [PATCH 3/3] test(resolver): satisfy lint for resolver test helpers --- pkg/options/resolver/dependency_test.go | 73 ++++++++++--------- pkg/options/resolver/validation_test.go | 97 +++++++++++++++---------- 2 files changed, 98 insertions(+), 72 deletions(-) diff --git a/pkg/options/resolver/dependency_test.go b/pkg/options/resolver/dependency_test.go index 5cd3e8ae03..ea5d126c67 100644 --- a/pkg/options/resolver/dependency_test.go +++ b/pkg/options/resolver/dependency_test.go @@ -10,6 +10,16 @@ import ( "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 } @@ -28,11 +38,11 @@ type dependencyCase struct { func childCase(parent, child *types.Option, wantErr string) dependencyCase { return dependencyCase{ - defs: config.OptionDefinitions{"parent": parent, "child": child}, - values: map[string]config.OptionValue{"parent": {Children: []string{"child"}}}, + defs: config.OptionDefinitions{testParent: parent, testChild: child}, + values: map[string]config.OptionValue{testParent: {Children: []string{testChild}}}, wantErr: wantErr, - from: "parent", - to: "child", + from: testParent, + to: testChild, } } @@ -46,25 +56,12 @@ func variableCase(option, dep *types.Option, wantErr string) dependencyCase { } } -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)) -} - func (suite *DependencyGuardTestSuite) TestChildScopeGuards() { cases := map[string]dependencyCase{ "global child under non-global parent rejected": childCase( &types.Option{}, &types.Option{Global: true}, - `cannot use a global option as a dependency of a non-global option. Option "child" used in children of option "parent"`, + testGlobalChildErr, ), "global child under global parent accepted": childCase( &types.Option{Global: true}, &types.Option{Global: true}, "", @@ -75,7 +72,7 @@ func (suite *DependencyGuardTestSuite) TestChildScopeGuards() { "non-local child under local parent rejected": childCase( &types.Option{Local: true}, &types.Option{}, - `cannot use a non-local option as a dependency of a local option. Option "child" used in children of option "parent"`, + testLocalChildErr, ), "local child under local parent accepted": childCase( &types.Option{Local: true}, &types.Option{Local: true}, "", @@ -93,12 +90,12 @@ func (suite *DependencyGuardTestSuite) TestChildScopeGuards() { func (suite *DependencyGuardTestSuite) TestVariableScopeGuards() { cases := map[string]dependencyCase{ "global default referencing non-global variable rejected": variableCase( - &types.Option{Global: true, Default: "${V}"}, + &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: "${V}"}, &types.Option{Global: true}, "", + &types.Option{Global: true, Default: testDefaultRef}, &types.Option{Global: true}, "", ), "non-local command referencing local variable rejected": variableCase( &types.Option{Command: "echo ${V}"}, @@ -106,7 +103,7 @@ func (suite *DependencyGuardTestSuite) TestVariableScopeGuards() { `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: "${V}"}, &types.Option{}, "", + &types.Option{Local: true, Default: testDefaultRef}, &types.Option{}, "", ), } @@ -116,31 +113,28 @@ func (suite *DependencyGuardTestSuite) TestVariableScopeGuards() { } func (suite *DependencyGuardTestSuite) TestUndefinedReferencesAreSkipped() { - defs := config.OptionDefinitions{"parent": {Global: true, Default: "${MISSING}"}} - values := map[string]config.OptionValue{"parent": {Children: []string{"ghost"}}} + 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("parent", "ghost")) - suite.False(g.HasEdge("MISSING", "parent")) + suite.False(g.HasEdge(testParent, "ghost")) + suite.False(g.HasEdge("MISSING", testParent)) } func (suite *DependencyGuardTestSuite) TestResolveRejectsIncompatibleGraph() { - defs := map[string]*types.Option{"parent": {}, "child": {Global: true}} - values := map[string]config.OptionValue{"parent": {Children: []string{"child"}}} + 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, - `cannot use a global option as a dependency of a non-global option. Option "child" used in children of option "parent"`, - ) + suite.ErrorContains(err, testGlobalChildErr) } func (suite *DependencyGuardTestSuite) TestResolveKeepsValidBoundaryUserValue() { - defs := map[string]*types.Option{"count": {Type: "number"}} + defs := map[string]*types.Option{"count": {Type: typeNumber}} resolved, _, err := New( map[string]string{"count": "-9223372036854775808"}, @@ -149,3 +143,16 @@ func (suite *DependencyGuardTestSuite) TestResolveKeepsValidBoundaryUserValue() 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)) +} diff --git a/pkg/options/resolver/validation_test.go b/pkg/options/resolver/validation_test.go index c19b2d5f81..a83a5056fa 100644 --- a/pkg/options/resolver/validation_test.go +++ b/pkg/options/resolver/validation_test.go @@ -7,21 +7,30 @@ import ( "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) TestValidateUserValueBoundaries() { +func (suite *ValidateUserValueTestSuite) TestValidateUserValuePatternAndEnum() { dev := types.OptionEnumArray{{Value: "dev"}} - cases := map[string]struct { - value string - option *types.Option - wantError bool - }{ + suite.runUserValueCases(map[string]userValueCase{ "pattern mismatch rejected": { "Abc", &types.Option{ValidationPattern: "^[a-z]+$"}, @@ -36,44 +45,65 @@ func (suite *ValidateUserValueTestSuite) TestValidateUserValueBoundaries() { "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}, - "number accepts negative": {"-42", &types.Option{Type: "number"}, false}, - "number rejects decimal": {"1.5", &types.Option{Type: "number"}, true}, + "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: "number"}, + &types.Option{Type: typeNumber}, false, }, "number rejects int64 overflow": { "9223372036854775808", - &types.Option{Type: "number"}, + &types.Option{Type: typeNumber}, true, }, - "boolean accepts 1": {"1", &types.Option{Type: "boolean"}, false}, - "boolean rejects yes": {"yes", &types.Option{Type: "boolean"}, 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}, - "pattern passes but enum excludes": { - "7", - &types.Option{ValidationPattern: "^[0-9]+$", Enum: types.OptionEnumArray{{Value: "5"}}}, - true, - }, "enum passes but type fails": { - "yes", - &types.Option{Type: "boolean", Enum: types.OptionEnumArray{{Value: "yes"}}}, + yesValue, + &types.Option{Type: typeBoolean, Enum: types.OptionEnumArray{{Value: yesValue}}}, true, }, - "all checks pass": { - "5", - &types.Option{ - Type: "number", - ValidationPattern: "^[0-9]+$", - Enum: types.OptionEnumArray{{Value: "5"}}, - }, - false, - }, + }) +} + +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) @@ -85,14 +115,3 @@ func (suite *ValidateUserValueTestSuite) TestValidateUserValueBoundaries() { }) } } - -func (suite *ValidateUserValueTestSuite) TestPatternErrorTakesPrecedence() { - option := &types.Option{ - ValidationPattern: "^[0-9]+$", - ValidationMessage: "must be digits", - Type: "number", - Enum: types.OptionEnumArray{{Value: "5"}}, - } - - suite.EqualError(validateUserValue("opt", "x", option), "must be digits") -}