diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 84860d8..590ff37 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -28,6 +28,7 @@ import ( "testing" "time" + "github.com/microcks/microcks-cli/pkg/errors" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -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") +} diff --git a/pkg/config/localconfig.go b/pkg/config/localconfig.go index 2aaa1b1..9358cf1 100644 --- a/pkg/config/localconfig.go +++ b/pkg/config/localconfig.go @@ -22,6 +22,7 @@ import ( "path/filepath" "slices" + "github.com/microcks/microcks-cli/pkg/errors" configUtil "github.com/microcks/microcks-cli/pkg/util" ) @@ -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) + } err = ValidateLocalConfig(config) if err != nil { @@ -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) + } return &config, nil }