Skip to content

Commit

Permalink
test: variable name can not start with a number
Browse files Browse the repository at this point in the history
  • Loading branch information
majori committed Jan 22, 2024
1 parent fce23a2 commit f131b7d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 16 deletions.
29 changes: 14 additions & 15 deletions internal/cli/cmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,27 @@ func TestFeatures(t *testing.T) {
return ctx, nil
})

// Initialize context values
s.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
// Initialize additional flags to empty map before each step
return context.WithValue(
ctx = context.WithValue(
ctx,
cmdAdditionalFlagsCtxKey{},
make(map[string]string)), nil
make(map[string]string))

ctx = context.WithValue(
ctx,
dockerResourcesCtxKey{},
[]*dockertest.Resource{})

return ctx, nil
})

s.After(cleanDockerResources)
s.After(cleanTempDirs)
},
Options: &godog.Options{
Strict: true,
Concurrency: 4,
Concurrency: 8,
Format: "pretty",
Paths: []string{"../../test"},
TestingT: t, // Testing instance that will run subtests.
Expand Down Expand Up @@ -205,12 +213,7 @@ func readSauceFile(ctx context.Context) ([]map[string]interface{}, error) {
*/

func cleanDockerResources(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) {
resources, ok := ctx.Value(dockerResourcesCtxKey{}).([]*dockertest.Resource)

// Resource list was probably empty, skip
if !ok {
return ctx, err
}
resources := ctx.Value(dockerResourcesCtxKey{}).([]*dockertest.Resource)

for _, resource := range resources {
err := resource.Close()
Expand Down Expand Up @@ -491,11 +494,7 @@ func createLocalRegistry(opts *dockertest.RunOptions) (*dockertest.Resource, err
}

func addDockerResourceToContext(ctx context.Context, resource *dockertest.Resource) context.Context {
resources, ok := ctx.Value(dockerResourcesCtxKey{}).([]*dockertest.Resource)
if !ok {
resources = make([]*dockertest.Resource, 0)
}

resources := ctx.Value(dockerResourcesCtxKey{}).([]*dockertest.Resource)
return context.WithValue(ctx, dockerResourcesCtxKey{}, append(resources, resource))
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/recipe/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,17 @@ type TableValue struct {
Rows [][]string `yaml:"rows,flow"`
}

var startsWithNumber = regexp.MustCompile(`^\d.*`)

func (v *Variable) Validate() error {
if v.Name == "" {
return errors.New("variable name is required")
}

if startsWithNumber.MatchString(v.Name) {
return errors.New("variable name can not start with a number")
}

if v.Confirm {
if len(v.Options) > 0 {
return errors.New("`confirm` and `options` properties can not be defined at the same time")
Expand Down
59 changes: 58 additions & 1 deletion pkg/recipe/variable_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package recipe

import "testing"
import (
"strings"
"testing"
)

func TestVariableRegExpValidation(t *testing.T) {
variable := &Variable{
Expand Down Expand Up @@ -45,3 +48,57 @@ func TestVariableRegExpValidation(t *testing.T) {
t.Error("Incorrectly invalidated valid string")
}
}

func TestVariableValidation(t *testing.T) {
scenarios := []struct {
name string
variable Variable
expectedErr string
}{
{
"empty name",
Variable{},
"variable name is required",
},
{
"variable name starts with number",
Variable{
Name: "1foo",
},
"variable name can not start with a number",
},
{
"both `confirm` and `options` defined",
Variable{
Name: "foo",
Confirm: true,
Options: []string{"foo", "bar"},
},
"`confirm` and `options` properties can not be defined",
},
{
"both `confirm` and `columns` defined",
Variable{
Name: "foo",
Confirm: true,
Columns: []string{"foo", "bar"},
},
"`confirm` and `columns` properties can not be defined",
},
}

for _, scenario := range scenarios {
t.Run(scenario.name, func(t *testing.T) {
err := scenario.variable.Validate()
if err != nil {
if !strings.Contains(err.Error(), scenario.expectedErr) {
t.Errorf("Expected error '%s', got '%s'", scenario.expectedErr, err.Error())
}
} else {
if scenario.expectedErr != "" {
t.Errorf("Expected error '%s', got nil", scenario.expectedErr)
}
}
})
}
}

0 comments on commit f131b7d

Please sign in to comment.