diff --git a/config/config.go b/config/config.go index 59ff0d0..57c48df 100644 --- a/config/config.go +++ b/config/config.go @@ -9,7 +9,7 @@ func Prepare(key, value *string) (skip bool, deferredFn func(), err error) { case "root": deferredFn, err = processRoot(value) - if err != nil && (strings.Contains(err.Error(), "blocked") || strings.Contains(err.Error(), "NVM4313")) { + if err != nil && (strings.Contains(err.Error(), "blocked") || strings.Contains(err.Error(), "NVM4313") || strings.Contains(err.Error(), "non-empty path")) { skip = true } diff --git a/config/root.go b/config/root.go index c20b535..02a4698 100644 --- a/config/root.go +++ b/config/root.go @@ -18,6 +18,10 @@ func processRoot(value *string) (deferredFn func(), err error) { } *value = strings.Trim(*value, `"`) // Remove potential quotes from input + if strings.TrimSpace(*value) == "" { + err = fmt.Errorf("root must be a non-empty path") + return + } sourceRoot := filepath.Clean(s.Root) targetRoot := filepath.Clean(settings.Expand(*value)) diff --git a/config/root_test.go b/config/root_test.go new file mode 100644 index 0000000..02b67e3 --- /dev/null +++ b/config/root_test.go @@ -0,0 +1,34 @@ +package config + +import ( + "os/exec" + "strings" + "testing" + + prefs "common/preferences" + "common/settings" +) + +func TestPrepareRootRejectsBlank(t *testing.T) { + prefs.ROOT = "HKCU/Software/NVMTest/config_empty_root" + prefs.ROOTS = []string{prefs.ROOT} + t.Cleanup(func() { + exec.Command("reg", "delete", `HKCU\Software\NVMTest\config_empty_root`, "/f").Run() + }) + settings.Load(true) + + for _, raw := range []string{"", " ", `""`, `" "`} { + key := "root" + value := raw + skip, _, err := Prepare(&key, &value) + if err == nil || !strings.Contains(err.Error(), "root must be a non-empty path") { + t.Fatalf("Prepare(root=%q) = skip=%v err=%v, want non-empty path", raw, skip, err) + } + if strings.Contains(err.Error(), "NVM4313") { + t.Fatalf("Prepare(root=%q) used NVM4313: %v", raw, err) + } + if !skip { + t.Fatalf("Prepare(root=%q) skip=false, want true", raw) + } + } +}