Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Racer159 committed May 20, 2024
1 parent 6bf38a4 commit a30b14a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 76 deletions.
41 changes: 19 additions & 22 deletions src/pkg/variables/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
package variables

import (
"errors"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

var start = `
Expand Down Expand Up @@ -61,15 +62,15 @@ func TestReplaceTextTemplate(t *testing.T) {
type test struct {
vc VariableConfig
path string
wantErr error
wantErrMsg string
wantContents string
}

tests := []test{
{
vc: VariableConfig{setVariableMap: SetVariableMap{}, applicationTemplates: map[string]*TextTemplate{}},
path: "non-existent.test",
wantErr: errors.New("open non-existent.test: no such file or directory"),
wantErrMsg: "open non-existent.test: no such file or directory",
wantContents: start,
},
{
Expand All @@ -83,7 +84,7 @@ func TestReplaceTextTemplate(t *testing.T) {
"###PREFIX_APP_REPLACE_ME###": {Value: "APP_REPLACED"},
},
},
wantErr: nil,
wantErrMsg: "",
wantContents: simple,
},
{
Expand All @@ -97,7 +98,7 @@ func TestReplaceTextTemplate(t *testing.T) {
"###PREFIX_APP_REPLACE_ME###": {Value: "APP_REPLACED\nAPP_SECOND"},
},
},
wantErr: nil,
wantErrMsg: "",
wantContents: multiline,
},
{
Expand All @@ -111,7 +112,7 @@ func TestReplaceTextTemplate(t *testing.T) {
"###PREFIX_APP_REPLACE_ME###": {Value: "APP_REPLACED\nAPP_SECOND", AutoIndent: true},
},
},
wantErr: nil,
wantErrMsg: "",
wantContents: autoIndent,
},
{
Expand All @@ -125,33 +126,29 @@ func TestReplaceTextTemplate(t *testing.T) {
"###PREFIX_APP_REPLACE_ME###": {Value: "testdata/file.txt", Type: FileVariableType},
},
},
wantErr: nil,
wantErrMsg: "",
wantContents: file,
},
}

for _, tc := range tests {
tmpDir := t.TempDir()
tc.path = filepath.Join(tmpDir, "templates.test")
if tc.path == "" {
tmpDir := t.TempDir()
tc.path = filepath.Join(tmpDir, "templates.test")

f, _ := os.Create(tc.path)
defer f.Close()
f, _ := os.Create(tc.path)
defer f.Close()

f.WriteString(start)
f.WriteString(start)
}

gotErr := tc.vc.ReplaceTextTemplate(tc.path)
if gotErr != nil && tc.wantErr != nil {
if gotErr.Error() != tc.wantErr.Error() {
t.Fatalf("wanted err: %s, got err: %s", tc.wantErr, gotErr)
}
} else if gotErr != nil {
t.Fatalf("got unexpected err: %s", gotErr)
if tc.wantErrMsg != "" {
require.EqualError(t, gotErr, tc.wantErrMsg)
} else {
require.NoError(t, gotErr)
gotContents, _ := os.ReadFile(tc.path)
if string(gotContents) != tc.wantContents {
t.Fatalf("wanted contents: %s, got contents: %s", tc.wantContents, string(gotContents))
}
require.Equal(t, string(gotContents), tc.wantContents)
}

}
}
11 changes: 8 additions & 3 deletions src/pkg/variables/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,16 @@ func (vc *VariableConfig) SetVariable(name, value string, sensitive bool, autoIn
// CheckVariablePattern checks to see if a current variable is set to a value that matches its pattern
func (vc *VariableConfig) CheckVariablePattern(name, pattern string) error {
if variable, ok := vc.setVariableMap[name]; ok {
if regexp.MustCompile(pattern).MatchString(variable.Value) {
return nil
r, err := regexp.Compile(pattern)
if err != nil {
return err
}

if !r.MatchString(variable.Value) {
return fmt.Errorf("provided value for variable %q does not match pattern %q", name, pattern)
}

return fmt.Errorf("provided value for variable %q does not match pattern %q", name, pattern)
return nil
}

return fmt.Errorf("variable %q was not found in the current variable map", name)
Expand Down
99 changes: 48 additions & 51 deletions src/pkg/variables/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@
package variables

import (
"errors"
"reflect"
"testing"

"github.com/stretchr/testify/require"
)

func TestPopulateVariables(t *testing.T) {
type test struct {
vc VariableConfig
vars []InteractiveVariable
presets map[string]string
wantErr error
wantVars SetVariableMap
vc VariableConfig
vars []InteractiveVariable
presets map[string]string
wantErrMsg string
wantVars SetVariableMap
}

prompt := func(_ InteractiveVariable) (value string, err error) { return "Prompt", nil }

tests := []test{
{
vc: VariableConfig{setVariableMap: SetVariableMap{}},
vars: []InteractiveVariable{{Variable: Variable{Name: "NAME"}}},
presets: map[string]string{},
wantErr: nil,
wantVars: SetVariableMap{"NAME": {Variable: Variable{Name: "NAME"}}},
vc: VariableConfig{setVariableMap: SetVariableMap{}},
vars: []InteractiveVariable{{Variable: Variable{Name: "NAME"}}},
presets: map[string]string{},
wantErrMsg: "",
wantVars: SetVariableMap{"NAME": {Variable: Variable{Name: "NAME"}}},
},
{
vc: VariableConfig{setVariableMap: SetVariableMap{}},
vars: []InteractiveVariable{
{Variable: Variable{Name: "NAME"}, Default: "Default"},
},
presets: map[string]string{},
wantErr: nil,
presets: map[string]string{},
wantErrMsg: "",
wantVars: SetVariableMap{
"NAME": {Variable: Variable{Name: "NAME"}, Value: "Default"},
},
Expand All @@ -44,8 +44,8 @@ func TestPopulateVariables(t *testing.T) {
vars: []InteractiveVariable{
{Variable: Variable{Name: "NAME"}, Default: "Default"},
},
presets: map[string]string{"NAME": "Set"},
wantErr: nil,
presets: map[string]string{"NAME": "Set"},
wantErrMsg: "",
wantVars: SetVariableMap{
"NAME": {Variable: Variable{Name: "NAME"}, Value: "Set"},
},
Expand All @@ -55,8 +55,8 @@ func TestPopulateVariables(t *testing.T) {
vars: []InteractiveVariable{
{Variable: Variable{Name: "NAME", Sensitive: true, AutoIndent: true, Type: FileVariableType}},
},
presets: map[string]string{},
wantErr: nil,
presets: map[string]string{},
wantErrMsg: "",
wantVars: SetVariableMap{
"NAME": {Variable: Variable{Name: "NAME", Sensitive: true, AutoIndent: true, Type: FileVariableType}},
},
Expand All @@ -66,8 +66,8 @@ func TestPopulateVariables(t *testing.T) {
vars: []InteractiveVariable{
{Variable: Variable{Name: "NAME", Sensitive: true, AutoIndent: true, Type: FileVariableType}},
},
presets: map[string]string{"NAME": "Set"},
wantErr: nil,
presets: map[string]string{"NAME": "Set"},
wantErrMsg: "",
wantVars: SetVariableMap{
"NAME": {Variable: Variable{Name: "NAME", Sensitive: true, AutoIndent: true, Type: FileVariableType}, Value: "Set"},
},
Expand All @@ -77,8 +77,8 @@ func TestPopulateVariables(t *testing.T) {
vars: []InteractiveVariable{
{Variable: Variable{Name: "NAME"}, Prompt: true},
},
presets: map[string]string{},
wantErr: nil,
presets: map[string]string{},
wantErrMsg: "",
wantVars: SetVariableMap{
"NAME": {Variable: Variable{Name: "NAME"}, Value: "Prompt"},
},
Expand All @@ -88,8 +88,8 @@ func TestPopulateVariables(t *testing.T) {
vars: []InteractiveVariable{
{Variable: Variable{Name: "NAME"}, Default: "Default", Prompt: true},
},
presets: map[string]string{},
wantErr: nil,
presets: map[string]string{},
wantErrMsg: "",
wantVars: SetVariableMap{
"NAME": {Variable: Variable{Name: "NAME"}, Value: "Prompt"},
},
Expand All @@ -99,8 +99,8 @@ func TestPopulateVariables(t *testing.T) {
vars: []InteractiveVariable{
{Variable: Variable{Name: "NAME"}, Prompt: true},
},
presets: map[string]string{"NAME": "Set"},
wantErr: nil,
presets: map[string]string{"NAME": "Set"},
wantErrMsg: "",
wantVars: SetVariableMap{
"NAME": {Variable: Variable{Name: "NAME"}, Value: "Set"},
},
Expand All @@ -109,62 +109,59 @@ func TestPopulateVariables(t *testing.T) {

for _, tc := range tests {
gotErr := tc.vc.PopulateVariables(tc.vars, tc.presets)
if gotErr != nil && tc.wantErr != nil {
if gotErr.Error() != tc.wantErr.Error() {
t.Fatalf("wanted err: %s, got err: %s", tc.wantErr, gotErr)
}
} else if gotErr != nil {
t.Fatalf("got unexpected err: %s", gotErr)
if tc.wantErrMsg != "" {
require.EqualError(t, gotErr, tc.wantErrMsg)
} else {
require.NoError(t, gotErr)
}

gotVars := tc.vc.setVariableMap

if len(gotVars) != len(tc.wantVars) {
t.Fatalf("wanted vars len: %d, got vars len: %d", len(tc.wantVars), len(gotVars))
}
require.Equal(t, len(gotVars), len(tc.wantVars))

for key := range gotVars {
if !reflect.DeepEqual(gotVars[key], tc.wantVars[key]) {
t.Fatalf("for key %s: wanted var: %v, got var: %v", key, tc.wantVars[key], gotVars[key])
}
require.Equal(t, gotVars[key], tc.wantVars[key])
}
}
}

func TestCheckVariablePattern(t *testing.T) {
type test struct {
vc VariableConfig
name string
pattern string
want error
vc VariableConfig
name string
pattern string
wantErrMsg string
}

tests := []test{
{
vc: VariableConfig{setVariableMap: SetVariableMap{}}, name: "NAME", pattern: "n[a-z]me",
want: errors.New("variable \"NAME\" was not found in the current variable map"),
wantErrMsg: "variable \"NAME\" was not found in the current variable map",
},
{
vc: VariableConfig{
setVariableMap: SetVariableMap{"NAME": &SetVariable{Value: "name"}},
}, name: "NAME", pattern: "n[^a]me",
want: errors.New("provided value for variable \"NAME\" does not match pattern \"n[^a]me\""),
wantErrMsg: "provided value for variable \"NAME\" does not match pattern \"n[^a]me\"",
},
{
vc: VariableConfig{
setVariableMap: SetVariableMap{"NAME": &SetVariable{Value: "name"}},
}, name: "NAME", pattern: "n[a-z]me", wantErrMsg: "",
},
{
vc: VariableConfig{
setVariableMap: SetVariableMap{"NAME": &SetVariable{Value: "name"}},
}, name: "NAME", pattern: "n[a-z]me", want: nil,
}, name: "NAME", pattern: "n[a-z-bad-pattern", wantErrMsg: "error parsing regexp: missing closing ]: `[a-z-bad-pattern`",
},
}

for _, tc := range tests {
got := tc.vc.CheckVariablePattern(tc.name, tc.pattern)
if got != nil && tc.want != nil {
if got.Error() != tc.want.Error() {
t.Fatalf("wanted err: %s, got err: %s", tc.want, got)
}
} else if got != nil {
t.Fatalf("got unexpected err: %s", got)
if tc.wantErrMsg != "" {
require.EqualError(t, got, tc.wantErrMsg)
} else {
require.NoError(t, got)
}
}
}

0 comments on commit a30b14a

Please sign in to comment.