Skip to content

Commit

Permalink
err on malformed json config (#1138)
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrkren authored Jul 24, 2023
1 parent 20f62a8 commit 0bdaa79
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
8 changes: 6 additions & 2 deletions flowkit/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ func (l *Loader) loadConfig(confPath string) (*Config, error) {
return nil, err
}

preProcessed := l.preprocess(raw)
preProcessed, err := l.preprocess(raw)
if err != nil {
return nil, fmt.Errorf("failed to preprocess config: %w", err)
}

configParser := l.configParsers.FindForFormat(filepath.Ext(confPath))
if configParser == nil {
return nil, fmt.Errorf("parser not found for config: %s", confPath)
Expand Down Expand Up @@ -170,7 +174,7 @@ func (l *Loader) Load(paths []string) (*Config, error) {
}

// preprocess does all manipulations to the raw configuration format happens here.
func (l *Loader) preprocess(raw []byte) []byte {
func (l *Loader) preprocess(raw []byte) ([]byte, error) {
return processorRun(raw)
}

Expand Down
34 changes: 34 additions & 0 deletions flowkit/config/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,40 @@ func Test_MissingConfiguration(t *testing.T) {
assert.EqualError(t, err, "missing configuration")
}

func Test_ConfigurationMalformedJSON(t *testing.T) {
b := []byte(`{
"emulators": {
"default": {
"port": 3569,
"serviceAccount": "emulator-account",
}
},
"contracts": {},
"networks": {
"emulator": "127.0.0.1:3569"
},
"accounts": {
"emulator-account": {
"address": "f8d6e0586b0a20c7",
"key": "21c5dfdeb0ff03a7a73ef39788563b62c89adea67bbb21ab95e5f710bd1d40b7"
}
},
"deployments": {}
}`)

mockFS := afero.NewMemMapFs()
err := afero.WriteFile(mockFS, "flow.json", b, 0644)

assert.NoError(t, err)

composer := config.NewLoader(afero.Afero{Fs: mockFS})
composer.AddConfigParser(json.NewParser())

conf, err := composer.Load(config.DefaultPaths())
assert.EqualError(t, err, "failed to preprocess config: failed to parse config JSON: invalid character '}' looking for beginning of object key string")
assert.Nil(t, conf)
}

func Test_ConfigurationWrongFormat(t *testing.T) {
b := []byte(`{
"deployments": {
Expand Down
16 changes: 12 additions & 4 deletions flowkit/config/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ package config

import (
"encoding/json"
"fmt"
)

// processorRun all pre-processors.
func processorRun(raw []byte) []byte {
func processorRun(raw []byte) ([]byte, error) {
type config struct {
Accounts map[string]map[string]any `json:"accounts,omitempty"`
Contracts any `json:"contracts,omitempty"`
Expand All @@ -33,8 +34,15 @@ func processorRun(raw []byte) []byte {
}

var conf config
_ = json.Unmarshal(raw, &conf)
err := json.Unmarshal(raw, &conf)
if err != nil {
return nil, fmt.Errorf("failed to parse config JSON: %w", err)
}

raw, err = json.Marshal(conf)
if err != nil {
return nil, fmt.Errorf("failed to marshal config: %w", err)
}

raw, _ = json.Marshal(conf)
return raw
return raw, nil
}
6 changes: 5 additions & 1 deletion flowkit/config/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

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

func Test_PrivateConfigFileAccounts(t *testing.T) {
Expand All @@ -45,6 +46,9 @@ func Test_PrivateConfigFileAccounts(t *testing.T) {
}
}`)

processorRunRes, err := processorRun(b)
require.NoError(t, err)

assert.JSONEq(t, `{
"emulators": {
"default": {
Expand All @@ -63,5 +67,5 @@ func Test_PrivateConfigFileAccounts(t *testing.T) {
"key": "11c5dfdeb0ff03a7a73ef39788563b62c89adea67bbb21ab95e5f710bd1d40b7"
}
}
}`, string(processorRun(b)))
}`, string(processorRunRes))
}

0 comments on commit 0bdaa79

Please sign in to comment.