From 656bfc3111ac8e5874a1727647fc49b26f92743d Mon Sep 17 00:00:00 2001 From: Mohammed Al Sahaf Date: Mon, 20 Oct 2025 02:54:00 +0300 Subject: [PATCH] another Windows fix Signed-off-by: Mohammed Al Sahaf --- storage_test.go | 103 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 68 insertions(+), 35 deletions(-) diff --git a/storage_test.go b/storage_test.go index c0c375432..4298d6185 100644 --- a/storage_test.go +++ b/storage_test.go @@ -55,68 +55,61 @@ func TestHomeDir_CrossPlatform(t *testing.T) { expected string }{ { - name: "normal HOME set", + name: "normal HOME set", + skipOS: []string{"windows"}, // Skip on Windows - HOME isn't typically used on Windows envVars: map[string]string{ - "HOME": func() string { - if runtime.GOOS == "windows" { - return "C:\\Users\\user" - } - return "/home/user" - }(), + "HOME": "/home/user", }, unsetVars: []string{"HOMEDRIVE", "HOMEPATH", "USERPROFILE", "home"}, - expected: func() string { - if runtime.GOOS == "windows" { - return "C:\\Users\\user" - } - return "/home/user" - }(), + expected: "/home/user", }, { name: "no environment variables", unsetVars: []string{"HOME", "HOMEDRIVE", "HOMEPATH", "USERPROFILE", "home"}, expected: ".", // Fallback to current directory }, + } + + // Windows-specific tests + windowsTests := []struct { + name string + envVars map[string]string + unsetVars []string + expected string + }{ { - name: "windows style with HOMEDRIVE and HOMEPATH", + name: "windows HOMEDRIVE and HOMEPATH", envVars: map[string]string{ "HOMEDRIVE": "C:", "HOMEPATH": "\\Users\\user", }, unsetVars: []string{"HOME", "USERPROFILE", "home"}, - expected: func() string { - if runtime.GOOS == "windows" { - return "C:\\Users\\user" - } - return "." // Non-windows systems fall back to current dir - }(), + expected: "C:\\Users\\user", }, { - name: "windows style with USERPROFILE", + name: "windows USERPROFILE", envVars: map[string]string{ "USERPROFILE": "C:\\Users\\user", }, unsetVars: []string{"HOME", "HOMEDRIVE", "HOMEPATH", "home"}, - expected: func() string { - if runtime.GOOS == "windows" { - return "C:\\Users\\user" - } - return "." - }(), + expected: "C:\\Users\\user", }, + } + + // Plan9-specific tests + plan9Tests := []struct { + name string + envVars map[string]string + unsetVars []string + expected string + }{ { - name: "plan9 style", - skipOS: []string{"windows"}, // Skip on Windows + name: "plan9 home variable", envVars: map[string]string{ "home": "/usr/user", }, unsetVars: []string{"HOME", "HOMEDRIVE", "HOMEPATH", "USERPROFILE"}, - expected: func() string { - if runtime.GOOS == "plan9" { - return "/usr/user" - } - return "." - }(), + expected: "/usr/user", }, } @@ -149,6 +142,46 @@ func TestHomeDir_CrossPlatform(t *testing.T) { } }) } + + // Run Windows-specific tests only on Windows + if runtime.GOOS == "windows" { + for _, test := range windowsTests { + t.Run(test.name, func(t *testing.T) { + for key, value := range test.envVars { + os.Setenv(key, value) + } + for _, key := range test.unsetVars { + os.Unsetenv(key) + } + + result := HomeDir() + + if result != test.expected { + t.Errorf("Expected '%s', got '%s'", test.expected, result) + } + }) + } + } + + // Run Plan9-specific tests only on Plan9 + if runtime.GOOS == "plan9" { + for _, test := range plan9Tests { + t.Run(test.name, func(t *testing.T) { + for key, value := range test.envVars { + os.Setenv(key, value) + } + for _, key := range test.unsetVars { + os.Unsetenv(key) + } + + result := HomeDir() + + if result != test.expected { + t.Errorf("Expected '%s', got '%s'", test.expected, result) + } + }) + } + } } func TestHomeDirUnsafe_EdgeCases(t *testing.T) {