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
12 changes: 6 additions & 6 deletions pkg/config/localconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config
import (
"fmt"
"os"
"path"
"path/filepath"
"slices"

configUtil "github.com/microcks/microcks-cli/pkg/util"
Expand Down Expand Up @@ -109,7 +109,7 @@ func DefaultConfigDir() (string, error) {
return "", nil
}

configDir = path.Join(homeDir, ".config", "microcks")
configDir = filepath.Join(homeDir, ".config", "microcks")

return configDir, nil
}
Expand All @@ -130,7 +130,7 @@ func DefaultLocalConfigPath() (string, error) {
if err != nil {
return "", err
}
return path.Join(dir, "config"), nil
return filepath.Join(dir, "config"), nil
}

// DefaultLocalWatchPath returns the local watch configuration path.
Expand All @@ -139,7 +139,7 @@ func DefaultLocalWatchPath() (string, error) {
if err != nil {
return "", err
}
return path.Join(dir, "watch"), nil
return filepath.Join(dir, "watch"), nil
}

// ValidateLocalConfig validates the local configuration.
Expand All @@ -155,7 +155,7 @@ func ValidateLocalConfig(config LocalConfig) error {

// WriteLocalConfig writes a new local configuration file.
func WriteLocalConfig(config LocalConfig, configPath string) error {
err := os.MkdirAll(path.Dir(configPath), os.ModePerm)
err := os.MkdirAll(filepath.Dir(configPath), os.ModePerm)
if err != nil {
return err
}
Expand Down Expand Up @@ -407,7 +407,7 @@ func ReadLocalWatchConfig(path string) (*WatchConfig, error) {

// WriteLocalWatchConfig writes a new local watch configuration file.
func WriteLocalWatchConfig(config WatchConfig, cfgPath string) error {
err := os.MkdirAll(path.Dir(cfgPath), os.ModePerm)
err := os.MkdirAll(filepath.Dir(cfgPath), os.ModePerm)
if err != nil {
return err
}
Expand Down
61 changes: 61 additions & 0 deletions pkg/config/localconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package config

import (
"path/filepath"
"testing"
)

func TestDefaultConfigDir_WithEnvVar(t *testing.T) {
// Set the environment variable MICROCKS_CONFIG_DIR
customDir := filepath.Join("C:", "Users", "JohnDoe", "custom-config")
t.Setenv("MICROCKS_CONFIG_DIR", customDir)

dir, err := DefaultConfigDir()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if dir != customDir {
t.Errorf("expected config dir to be %q, got %q", customDir, dir)
}
}

func TestDefaultConfigDir_WithWindowsHomeDir(t *testing.T) {
// Clear MICROCKS_CONFIG_DIR to ensure home dir logic is used
t.Setenv("MICROCKS_CONFIG_DIR", "")

// Set both HOME and USERPROFILE to mock a Windows-style home directory
windowsHome := filepath.Join("C:", "Users", "JohnDoe")
t.Setenv("HOME", windowsHome)
t.Setenv("USERPROFILE", windowsHome)

dir, err := DefaultConfigDir()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

expectedDir := filepath.Join(windowsHome, ".config", "microcks")
if dir != expectedDir {
t.Errorf("expected config dir to be %q, got %q", expectedDir, dir)
}

// Verify DefaultLocalConfigPath
configPath, err := DefaultLocalConfigPath()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expectedConfigPath := filepath.Join(expectedDir, "config")
if configPath != expectedConfigPath {
t.Errorf("expected config path to be %q, got %q", expectedConfigPath, configPath)
}

// Verify DefaultLocalWatchPath
watchPath, err := DefaultLocalWatchPath()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expectedWatchPath := filepath.Join(expectedDir, "watch")
if watchPath != expectedWatchPath {
t.Errorf("expected watch path to be %q, got %q", expectedWatchPath, watchPath)
}
}