Skip to content
Closed
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
12 changes: 9 additions & 3 deletions cmd/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ func TestLoginHonorsConfigFlag(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/keycloak/config" {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"enabled": false}`))
if _, err := w.Write([]byte(`{"enabled": false}`)); err != nil {
t.Errorf("writing Keycloak config response: %v", err)
}
return
}
http.NotFound(w, r)
Expand Down Expand Up @@ -147,7 +149,9 @@ func TestLoginHonorsConfigFlagWithCustomContextName(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/keycloak/config" {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"enabled": false}`))
if _, err := w.Write([]byte(`{"enabled": false}`)); err != nil {
t.Errorf("writing Keycloak config response: %v", err)
}
return
}
http.NotFound(w, r)
Expand All @@ -174,7 +178,9 @@ func TestLoginPreservesExistingContextsInCustomConfig(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/keycloak/config" {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"enabled": false}`))
if _, err := w.Write([]byte(`{"enabled": false}`)); err != nil {
t.Errorf("writing Keycloak config response: %v", err)
}
return
}
http.NotFound(w, r)
Expand Down
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"

clierrors "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, clierrors.Wrap(clierrors.KindEnvironment, fmt.Errorf("reading local config: %w", err))
}

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, clierrors.Wrap(clierrors.KindEnvironment, fmt.Errorf("reading local watch config: %w", err))
}

return &config, nil
}
Expand Down
59 changes: 59 additions & 0 deletions pkg/config/localconfig_malformed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright The Microcks Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package config

import (
"os"
"path/filepath"
"testing"

clierrors "github.com/microcks/microcks-cli/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestReadLocalConfigMalformedFile(t *testing.T) {
path := filepath.Join(t.TempDir(), "config")
want := []byte("current-context: [broken\n")
require.NoError(t, os.WriteFile(path, want, 0o600))

config, err := ReadLocalConfig(path)

assert.Nil(t, config)
require.Error(t, err)
assert.Equal(t, clierrors.KindEnvironment, clierrors.KindOf(err))
assert.Contains(t, err.Error(), "reading local config")
got, readErr := os.ReadFile(path)
require.NoError(t, readErr)
assert.Equal(t, want, got)
}

func TestReadLocalWatchConfigMalformedFile(t *testing.T) {
path := filepath.Join(t.TempDir(), "watch")
want := []byte("entries: [broken\n")
require.NoError(t, os.WriteFile(path, want, 0o600))

config, err := ReadLocalWatchConfig(path)

assert.Nil(t, config)
require.Error(t, err)
assert.Equal(t, clierrors.KindEnvironment, clierrors.KindOf(err))
assert.Contains(t, err.Error(), "reading local watch config")
got, readErr := os.ReadFile(path)
require.NoError(t, readErr)
assert.Equal(t, want, got)
}
Loading