diff --git a/src/pkg/variables/templates_test.go b/src/pkg/variables/templates_test.go index 2e3ffe2292..0f22facd3f 100644 --- a/src/pkg/variables/templates_test.go +++ b/src/pkg/variables/templates_test.go @@ -4,10 +4,11 @@ package variables import ( - "errors" "os" "path/filepath" "testing" + + "github.com/stretchr/testify/require" ) var start = ` @@ -61,7 +62,7 @@ func TestReplaceTextTemplate(t *testing.T) { type test struct { vc VariableConfig path string - wantErr error + wantErrMsg string wantContents string } @@ -69,7 +70,7 @@ func TestReplaceTextTemplate(t *testing.T) { { 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, }, { @@ -83,7 +84,7 @@ func TestReplaceTextTemplate(t *testing.T) { "###PREFIX_APP_REPLACE_ME###": {Value: "APP_REPLACED"}, }, }, - wantErr: nil, + wantErrMsg: "", wantContents: simple, }, { @@ -97,7 +98,7 @@ func TestReplaceTextTemplate(t *testing.T) { "###PREFIX_APP_REPLACE_ME###": {Value: "APP_REPLACED\nAPP_SECOND"}, }, }, - wantErr: nil, + wantErrMsg: "", wantContents: multiline, }, { @@ -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, }, { @@ -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) } - } } diff --git a/src/pkg/variables/variables.go b/src/pkg/variables/variables.go index 7411116397..5658ba0c7e 100644 --- a/src/pkg/variables/variables.go +++ b/src/pkg/variables/variables.go @@ -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) diff --git a/src/pkg/variables/variables_test.go b/src/pkg/variables/variables_test.go index fcfa0bc5e9..b88082b00d 100644 --- a/src/pkg/variables/variables_test.go +++ b/src/pkg/variables/variables_test.go @@ -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"}, }, @@ -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"}, }, @@ -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}}, }, @@ -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"}, }, @@ -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"}, }, @@ -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"}, }, @@ -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"}, }, @@ -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) } } }