From 647801cd75b725a60bf9a87b05047c00f24f370c Mon Sep 17 00:00:00 2001 From: Benjamin Bennett Date: Wed, 26 Jul 2023 10:08:18 +0100 Subject: [PATCH] Adding Validate() func to ConfigurationRequest along with test coverage (#150) --- internal/teststep/config.go | 34 ++++++++------------ internal/teststep/config_test.go | 53 ++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 21 deletions(-) diff --git a/internal/teststep/config.go b/internal/teststep/config.go index 727e3335c..bc5764024 100644 --- a/internal/teststep/config.go +++ b/internal/teststep/config.go @@ -6,12 +6,12 @@ package teststep import ( "context" "encoding/json" + "errors" "fmt" "io" "os" "path/filepath" "regexp" - "strings" ) const ( @@ -42,31 +42,23 @@ type ConfigurationRequest struct { Raw *string } -func Configuration(req ConfigurationRequest) (configuration, error) { - var populatedConfig []string - var config configuration - - if req.Directory != nil && *req.Directory != "" { - populatedConfig = append(populatedConfig, fmt.Sprintf("%q", "directory")) - - config = configuration{ - directory: *req.Directory, - } +func (c ConfigurationRequest) Validate() error { + if c.Directory != nil && c.Raw != nil && *c.Directory != "" && *c.Raw != "" { + return errors.New(`both "directory" and "raw" are populated, only one configuration option is allowed`) } - if req.Raw != nil && *req.Raw != "" { - populatedConfig = append(populatedConfig, fmt.Sprintf("%q", "raw")) + return nil +} - config = configuration{ - raw: *req.Raw, - } +func Configuration(req ConfigurationRequest) (configuration, error) { + var config configuration + + if req.Directory != nil { + config.directory = *req.Directory } - if len(populatedConfig) > 1 { - return configuration{}, fmt.Errorf( - "both %s are populated, only one configuration option is allowed", - strings.Join(populatedConfig, " and "), - ) + if req.Raw != nil { + config.raw = *req.Raw } return config, nil diff --git a/internal/teststep/config_test.go b/internal/teststep/config_test.go index 6d4a35851..0789df650 100644 --- a/internal/teststep/config_test.go +++ b/internal/teststep/config_test.go @@ -6,8 +6,61 @@ package teststep import ( "context" "testing" + + "github.com/google/go-cmp/cmp" ) +func TestConfigurationRequest_Validate(t *testing.T) { + t.Parallel() + + testCases := map[string]struct { + configRequest ConfigurationRequest + expectedError string + }{ + "directory": { + configRequest: ConfigurationRequest{ + Directory: Pointer("directory"), + }, + }, + "raw": { + configRequest: ConfigurationRequest{ + Raw: Pointer("raw"), + }, + }, + "directory-raw": { + configRequest: ConfigurationRequest{ + Directory: Pointer("directory"), + Raw: Pointer("raw"), + }, + expectedError: `both "directory" and "raw" are populated, only one configuration option is allowed`, + }, + } + + for name, testCase := range testCases { + name, testCase := name, testCase + + t.Run(name, func(t *testing.T) { + t.Parallel() + + err := testCase.configRequest.Validate() + + if testCase.expectedError == "" && err != nil { + t.Errorf("unexpected error %s", err) + } + + if testCase.expectedError != "" && err == nil { + t.Errorf("expected error but got none") + } + + if testCase.expectedError != "" && err != nil { + if diff := cmp.Diff(err.Error(), testCase.expectedError); diff != "" { + t.Errorf("expected error %s, got error %s", testCase.expectedError, err) + } + } + }) + } +} + func TestConfiguration_HasProviderBlock(t *testing.T) { t.Parallel()