Skip to content

Commit

Permalink
Adding Validate() func to ConfigurationRequest along with test covera…
Browse files Browse the repository at this point in the history
…ge (#150)
  • Loading branch information
bendbennett committed Jul 26, 2023
1 parent 90907b8 commit 647801c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 21 deletions.
34 changes: 13 additions & 21 deletions internal/teststep/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ package teststep
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
)

const (
Expand Down Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions internal/teststep/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 647801c

Please sign in to comment.