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.KindEnvironment, errors.KindOf(err), "a malformed local config should match how the commands report config read failures")

wCfg, err := ReadLocalWatchConfig(watchPath)
assert.Error(t, err, "Should fail on malformed YAML")
assert.Nil(t, wCfg)
assert.Equal(t, errors.KindEnvironment, errors.KindOf(err), "a malformed local watch config should match how the commands report config read failures")
}
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.KindEnvironment, "reading config %s: %w", path, err)
}
Comment thread
Sarthak-Shreshtha01 marked this conversation as resolved.

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.KindEnvironment, "reading config %s: %w", path, err)
}
Comment thread
Sarthak-Shreshtha01 marked this conversation as resolved.

return &config, nil
}
Expand Down