diff --git a/pkg/config/localconfig.go b/pkg/config/localconfig.go index 1e5eeda6..d02bfb92 100644 --- a/pkg/config/localconfig.go +++ b/pkg/config/localconfig.go @@ -3,7 +3,7 @@ package config import ( "fmt" "os" - "path" + "path/filepath" "slices" configUtil "github.com/microcks/microcks-cli/pkg/util" @@ -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 } @@ -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. @@ -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. @@ -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 } @@ -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 } diff --git a/pkg/config/localconfig_test.go b/pkg/config/localconfig_test.go new file mode 100644 index 00000000..641a3ec3 --- /dev/null +++ b/pkg/config/localconfig_test.go @@ -0,0 +1,124 @@ +/* + * 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 ( + "path/filepath" + "runtime" + "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) { + if runtime.GOOS != "windows" { + t.Skip("Skipping Windows-specific path test") + } + + // 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 := `C:\Users\JohnDoe` + t.Setenv("HOME", windowsHome) + t.Setenv("USERPROFILE", windowsHome) + + dir, err := DefaultConfigDir() + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expectedDir := `C:\Users\JohnDoe\.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 := `C:\Users\JohnDoe\.config\microcks\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 := `C:\Users\JohnDoe\.config\microcks\watch` + if watchPath != expectedWatchPath { + t.Errorf("expected watch path to be %q, got %q", expectedWatchPath, watchPath) + } +} + +func TestDefaultConfigDir_WithUnixHomeDir(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("Skipping Unix-specific path test") + } + + // Clear MICROCKS_CONFIG_DIR to ensure home dir logic is used + t.Setenv("MICROCKS_CONFIG_DIR", "") + + // Set HOME to mock a Unix-style home directory + unixHome := "/home/johndoe" + t.Setenv("HOME", unixHome) + + dir, err := DefaultConfigDir() + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expectedDir := "/home/johndoe/.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 := "/home/johndoe/.config/microcks/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 := "/home/johndoe/.config/microcks/watch" + if watchPath != expectedWatchPath { + t.Errorf("expected watch path to be %q, got %q", expectedWatchPath, watchPath) + } +}