Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor both substitute tests
  • Loading branch information
JordanSussman committed Jun 2, 2022
commit 4889b4976985b2730abe208bf9e624fc7f5c8172
191 changes: 104 additions & 87 deletions compiler/native/substitute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,112 +6,130 @@ package native

import (
"flag"
"github.com/google/go-cmp/cmp"
"reflect"
"testing"

"github.com/go-vela/types/yaml"

"github.com/google/go-cmp/cmp"
"github.com/urfave/cli/v2"

"github.com/go-vela/types/yaml"
)

func TestNative_SubstituteStages(t *testing.T) {
func Test_client_SubstituteStages(t *testing.T) {
type args struct {
stages yaml.StageSlice
}

// setup types
set := flag.NewFlagSet("test", 0)
c := cli.NewContext(nil, set, nil)

s := yaml.StageSlice{
{
Name: "simple",
Steps: yaml.StepSlice{
{
Commands: []string{"echo ${FOO}", "echo $${BAR}"},
Environment: map[string]string{"FOO": "baz", "BAR": "baz"},
Image: "alpine:latest",
Name: "simple",
Pull: "always",
},
},
},
{
Name: "advanced",
Steps: yaml.StepSlice{
{
Commands: []string{"echo ${COMPLEX}"},
Environment: map[string]string{"COMPLEX": "{\"hello\":\n \"world\"}"},
Image: "alpine:latest",
Name: "advanced",
Pull: "always",
},
},
},
tests := []struct {
name string
args args
want yaml.StageSlice
wantErr bool
}{
{
Name: "not_found",
Steps: yaml.StepSlice{
{
Commands: []string{"echo $NOT_FOUND", "echo ${NOT_FOUND}", "echo $${NOT_FOUND}"},
Environment: map[string]string{"FOO": "baz", "BAR": "baz"},
Image: "alpine:latest",
Name: "not_found",
Pull: "always",
name: "normal",
args: args{
stages: yaml.StageSlice{
{
Name: "simple",
Steps: yaml.StepSlice{
{
Commands: []string{"echo ${FOO}", "echo $${BAR}"},
Environment: map[string]string{"FOO": "baz", "BAR": "baz"},
Image: "alpine:latest",
Name: "simple",
Pull: "always",
},
},
},
{
Name: "advanced",
Steps: yaml.StepSlice{
{
Commands: []string{"echo ${COMPLEX}"},
Environment: map[string]string{"COMPLEX": "{\"hello\":\n \"world\"}"},
Image: "alpine:latest",
Name: "advanced",
Pull: "always",
},
},
},
{
Name: "not_found",
Steps: yaml.StepSlice{
{
Commands: []string{"echo $NOT_FOUND", "echo ${NOT_FOUND}", "echo $${NOT_FOUND}"},
Environment: map[string]string{"FOO": "baz", "BAR": "baz"},
Image: "alpine:latest",
Name: "not_found",
Pull: "always",
},
},
},
},
},
},
}

want := yaml.StageSlice{
{
Name: "simple",
Steps: yaml.StepSlice{
want: yaml.StageSlice{
{
Commands: []string{"echo baz", "echo ${BAR}"},
Environment: map[string]string{"FOO": "baz", "BAR": "baz"},
Image: "alpine:latest",
Name: "simple",
Pull: "always",
Name: "simple",
Steps: yaml.StepSlice{
{
Commands: []string{"echo baz", "echo ${BAR}"},
Environment: map[string]string{"FOO": "baz", "BAR": "baz"},
Image: "alpine:latest",
Name: "simple",
Pull: "always",
},
},
},
},
},
{
Name: "advanced",
Steps: yaml.StepSlice{
{
Commands: []string{"echo \"{\\\"hello\\\":\\n \\\"world\\\"}\""},
Environment: map[string]string{"COMPLEX": "{\"hello\":\n \"world\"}"},
Image: "alpine:latest",
Name: "advanced",
Pull: "always",
Name: "advanced",
Steps: yaml.StepSlice{
{
Commands: []string{"echo \"{\\\"hello\\\":\\n \\\"world\\\"}\""},
Environment: map[string]string{"COMPLEX": "{\"hello\":\n \"world\"}"},
Image: "alpine:latest",
Name: "advanced",
Pull: "always",
},
},
},
},
},
{
Name: "not_found",
Steps: yaml.StepSlice{
{
Commands: []string{"echo $NOT_FOUND", "echo ${NOT_FOUND}", "echo ${NOT_FOUND}"},
Environment: map[string]string{"FOO": "baz", "BAR": "baz"},
Image: "alpine:latest",
Name: "not_found",
Pull: "always",
Name: "not_found",
Steps: yaml.StepSlice{
{
Commands: []string{"echo $NOT_FOUND", "echo ${NOT_FOUND}", "echo ${NOT_FOUND}"},
Environment: map[string]string{"FOO": "baz", "BAR": "baz"},
Image: "alpine:latest",
Name: "not_found",
Pull: "always",
},
},
},
},
wantErr: false,
},
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compiler, err := New(c)
if err != nil {
t.Errorf("Creating compiler returned err: %v", err)
}

// run test
compiler, err := New(c)
if err != nil {
t.Errorf("Creating compiler returned err: %v", err)
}

got, err := compiler.SubstituteStages(s)

if err != nil {
t.Errorf("SubstituteStages returned err: %v", err)
}
got, err := compiler.SubstituteStages(tt.args.stages)
if (err != nil) != tt.wantErr {
t.Errorf("SubstituteStages() error = %v, wantErr %v", err, tt.wantErr)
return
}

if !reflect.DeepEqual(got, want) {
t.Errorf("SubstituteStages is %v, want %v", got, want)
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("SubstituteStages() mismatch (-want +got):\n%s", diff)
}
})
}
}

Expand Down Expand Up @@ -192,7 +210,7 @@ func Test_client_SubstituteSteps(t *testing.T) {
Name: "go",
Variables: map[string]interface{}{
"build_author": "${BUILD_AUTHOR}",
"unknown": "${UNKNOWN:-test}",
"unknown": "${DEPLOYMENT_PARAMETER_API_IMAGE}",
},
},
Environment: map[string]string{
Expand All @@ -208,7 +226,7 @@ func Test_client_SubstituteSteps(t *testing.T) {
Name: "go",
Variables: map[string]interface{}{
"build_author": "testauthor",
"unknown": "test",
"unknown": "${DEPLOYMENT_PARAMETER_API_IMAGE}",
},
},
Environment: map[string]string{
Expand All @@ -220,7 +238,6 @@ func Test_client_SubstituteSteps(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// run test
compiler, err := New(c)
if err != nil {
t.Errorf("Creating compiler returned err: %v", err)
Expand All @@ -232,7 +249,7 @@ func Test_client_SubstituteSteps(t *testing.T) {
return
}

if diff := cmp.Diff(got, tt.want); diff != "" {
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("SubstituteSteps() mismatch (-want +got):\n%s", diff)
}
})
Expand Down