Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions pkg/options/resolver/dependency_test.go
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))
}
117 changes: 117 additions & 0 deletions pkg/options/resolver/validation_test.go
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")
Comment thread
skevetter marked this conversation as resolved.
Resolved
}

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)
}
})
}
}
Loading