Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"testing"
"time"

"github.com/microcks/microcks-cli/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -459,3 +460,24 @@ func TestWatchConfig(t *testing.T) {
}
}
}

func TestReadMalformedConfig(t *testing.T) {
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, "config")
watchPath := filepath.Join(tmpDir, "watch")

// A tab used for indentation is invalid YAML.
malformed := []byte("contexts:\n\t- name: ctx1\n")
require.NoError(t, os.WriteFile(configPath, malformed, 0o600))
require.NoError(t, os.WriteFile(watchPath, malformed, 0o600))

cfg, err := ReadLocalConfig(configPath)
assert.Error(t, err, "Should fail on malformed YAML")
assert.Nil(t, cfg)
assert.Equal(t, errors.KindUsage, errors.KindOf(err), "a malformed local config is the user's input, not an environment or connection failure")

wCfg, err := ReadLocalWatchConfig(watchPath)
assert.Error(t, err, "Should fail on malformed YAML")
assert.Nil(t, wCfg)
assert.Equal(t, errors.KindUsage, errors.KindOf(err), "a malformed local watch config is the user's input, not an environment or connection failure")
}
7 changes: 7 additions & 0 deletions pkg/config/localconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"
"slices"

"github.com/microcks/microcks-cli/pkg/errors"
configUtil "github.com/microcks/microcks-cli/pkg/util"
)

Expand Down Expand Up @@ -104,6 +105,9 @@ func ReadLocalConfig(path string) (*LocalConfig, error) {
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, errors.Wrapf(errors.KindUsage, "reading config %s: %w", path, err)
}
Comment on lines +108 to +110

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's make sure we're using our centralised error handling mechanism.


err = ValidateLocalConfig(config)
if err != nil {
Expand Down Expand Up @@ -417,6 +421,9 @@ func ReadLocalWatchConfig(path string) (*WatchConfig, error) {
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, errors.Wrapf(errors.KindUsage, "reading config %s: %w", path, err)
}
Comment on lines +424 to +426

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here


return &config, nil
}
Expand Down