mirror of
https://github.com/caddyserver/caddy.git
synced 2025-11-10 16:46:56 -05:00
refactor storage_test to not clear env
Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com>
This commit is contained in:
parent
c6367fb774
commit
c2d586c458
120
storage_test.go
120
storage_test.go
@ -48,32 +48,32 @@ func TestHomeDir_CrossPlatform(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
setup func()
|
skipOS []string
|
||||||
expected string
|
envVars map[string]string // Environment variables to set
|
||||||
|
unsetVars []string // Environment variables to unset
|
||||||
|
expected string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "normal HOME set",
|
name: "normal HOME set",
|
||||||
setup: func() {
|
envVars: map[string]string{
|
||||||
os.Clearenv()
|
"HOME": "/home/user",
|
||||||
os.Setenv("HOME", "/home/user")
|
|
||||||
},
|
},
|
||||||
expected: "/home/user",
|
unsetVars: []string{"HOMEDRIVE", "HOMEPATH", "USERPROFILE", "home"},
|
||||||
|
expected: "/home/user",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "no environment variables",
|
name: "no environment variables",
|
||||||
setup: func() {
|
unsetVars: []string{"HOME", "HOMEDRIVE", "HOMEPATH", "USERPROFILE", "home"},
|
||||||
os.Clearenv()
|
expected: ".", // Fallback to current directory
|
||||||
},
|
|
||||||
expected: ".", // Fallback to current directory
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "windows style with HOMEDRIVE and HOMEPATH",
|
name: "windows style with HOMEDRIVE and HOMEPATH",
|
||||||
setup: func() {
|
envVars: map[string]string{
|
||||||
os.Clearenv()
|
"HOMEDRIVE": "C:",
|
||||||
os.Setenv("HOMEDRIVE", "C:")
|
"HOMEPATH": "\\Users\\user",
|
||||||
os.Setenv("HOMEPATH", "\\Users\\user")
|
|
||||||
},
|
},
|
||||||
|
unsetVars: []string{"HOME", "USERPROFILE", "home"},
|
||||||
expected: func() string {
|
expected: func() string {
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
return "C:\\Users\\user"
|
return "C:\\Users\\user"
|
||||||
@ -83,10 +83,10 @@ func TestHomeDir_CrossPlatform(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "windows style with USERPROFILE",
|
name: "windows style with USERPROFILE",
|
||||||
setup: func() {
|
envVars: map[string]string{
|
||||||
os.Clearenv()
|
"USERPROFILE": "C:\\Users\\user",
|
||||||
os.Setenv("USERPROFILE", "C:\\Users\\user")
|
|
||||||
},
|
},
|
||||||
|
unsetVars: []string{"HOME", "HOMEDRIVE", "HOMEPATH", "home"},
|
||||||
expected: func() string {
|
expected: func() string {
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
return "C:\\Users\\user"
|
return "C:\\Users\\user"
|
||||||
@ -95,11 +95,12 @@ func TestHomeDir_CrossPlatform(t *testing.T) {
|
|||||||
}(),
|
}(),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "plan9 style",
|
name: "plan9 style",
|
||||||
setup: func() {
|
skipOS: []string{"windows"}, // Skip on Windows
|
||||||
os.Clearenv()
|
envVars: map[string]string{
|
||||||
os.Setenv("home", "/usr/user")
|
"home": "/usr/user",
|
||||||
},
|
},
|
||||||
|
unsetVars: []string{"HOME", "HOMEDRIVE", "HOMEPATH", "USERPROFILE"},
|
||||||
expected: func() string {
|
expected: func() string {
|
||||||
if runtime.GOOS == "plan9" {
|
if runtime.GOOS == "plan9" {
|
||||||
return "/usr/user"
|
return "/usr/user"
|
||||||
@ -111,7 +112,21 @@ func TestHomeDir_CrossPlatform(t *testing.T) {
|
|||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
test.setup()
|
// Check if we should skip this test on current OS
|
||||||
|
for _, skipOS := range test.skipOS {
|
||||||
|
if runtime.GOOS == skipOS {
|
||||||
|
t.Skipf("Skipping test on %s", skipOS)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up environment for this test
|
||||||
|
for key, value := range test.envVars {
|
||||||
|
os.Setenv(key, value)
|
||||||
|
}
|
||||||
|
for _, key := range test.unsetVars {
|
||||||
|
os.Unsetenv(key)
|
||||||
|
}
|
||||||
|
|
||||||
result := HomeDir()
|
result := HomeDir()
|
||||||
|
|
||||||
if result != test.expected {
|
if result != test.expected {
|
||||||
@ -146,24 +161,22 @@ func TestHomeDirUnsafe_EdgeCases(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
setup func()
|
envVars map[string]string
|
||||||
expected string
|
unsetVars []string
|
||||||
|
expected string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "no environment variables",
|
name: "no environment variables",
|
||||||
setup: func() {
|
unsetVars: []string{"HOME", "HOMEDRIVE", "HOMEPATH", "USERPROFILE", "home"},
|
||||||
os.Clearenv()
|
expected: "", // homeDirUnsafe can return empty
|
||||||
},
|
|
||||||
expected: "", // homeDirUnsafe can return empty
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "windows with incomplete HOMEDRIVE/HOMEPATH",
|
name: "windows with incomplete HOMEDRIVE/HOMEPATH",
|
||||||
setup: func() {
|
envVars: map[string]string{
|
||||||
os.Clearenv()
|
"HOMEDRIVE": "C:",
|
||||||
os.Setenv("HOMEDRIVE", "C:")
|
|
||||||
// HOMEPATH missing
|
|
||||||
},
|
},
|
||||||
|
unsetVars: []string{"HOME", "HOMEPATH", "USERPROFILE", "home"},
|
||||||
expected: func() string {
|
expected: func() string {
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
return ""
|
return ""
|
||||||
@ -173,11 +186,10 @@ func TestHomeDirUnsafe_EdgeCases(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "windows with only HOMEPATH",
|
name: "windows with only HOMEPATH",
|
||||||
setup: func() {
|
envVars: map[string]string{
|
||||||
os.Clearenv()
|
"HOMEPATH": "\\Users\\user",
|
||||||
os.Setenv("HOMEPATH", "\\Users\\user")
|
|
||||||
// HOMEDRIVE missing
|
|
||||||
},
|
},
|
||||||
|
unsetVars: []string{"HOME", "HOMEDRIVE", "USERPROFILE", "home"},
|
||||||
expected: func() string {
|
expected: func() string {
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
return ""
|
return ""
|
||||||
@ -189,7 +201,14 @@ func TestHomeDirUnsafe_EdgeCases(t *testing.T) {
|
|||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
test.setup()
|
// Set up environment for this test
|
||||||
|
for key, value := range test.envVars {
|
||||||
|
os.Setenv(key, value)
|
||||||
|
}
|
||||||
|
for _, key := range test.unsetVars {
|
||||||
|
os.Unsetenv(key)
|
||||||
|
}
|
||||||
|
|
||||||
result := homeDirUnsafe()
|
result := homeDirUnsafe()
|
||||||
|
|
||||||
if result != test.expected {
|
if result != test.expected {
|
||||||
@ -282,8 +301,9 @@ func TestAppDataDir_PlatformSpecific(t *testing.T) {
|
|||||||
switch runtime.GOOS {
|
switch runtime.GOOS {
|
||||||
case "windows":
|
case "windows":
|
||||||
// Test Windows AppData
|
// Test Windows AppData
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("AppData", "C:\\Users\\user\\AppData\\Roaming")
|
os.Setenv("AppData", "C:\\Users\\user\\AppData\\Roaming")
|
||||||
|
os.Unsetenv("HOME")
|
||||||
|
os.Unsetenv("home")
|
||||||
|
|
||||||
result := AppDataDir()
|
result := AppDataDir()
|
||||||
expected := "C:\\Users\\user\\AppData\\Roaming\\Caddy"
|
expected := "C:\\Users\\user\\AppData\\Roaming\\Caddy"
|
||||||
@ -293,8 +313,9 @@ func TestAppDataDir_PlatformSpecific(t *testing.T) {
|
|||||||
|
|
||||||
case "darwin":
|
case "darwin":
|
||||||
// Test macOS Application Support
|
// Test macOS Application Support
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("HOME", "/Users/user")
|
os.Setenv("HOME", "/Users/user")
|
||||||
|
os.Unsetenv("AppData")
|
||||||
|
os.Unsetenv("home")
|
||||||
|
|
||||||
result := AppDataDir()
|
result := AppDataDir()
|
||||||
expected := "/Users/user/Library/Application Support/Caddy"
|
expected := "/Users/user/Library/Application Support/Caddy"
|
||||||
@ -304,8 +325,9 @@ func TestAppDataDir_PlatformSpecific(t *testing.T) {
|
|||||||
|
|
||||||
case "plan9":
|
case "plan9":
|
||||||
// Test Plan9 lib directory
|
// Test Plan9 lib directory
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("home", "/usr/user")
|
os.Setenv("home", "/usr/user")
|
||||||
|
os.Unsetenv("AppData")
|
||||||
|
os.Unsetenv("HOME")
|
||||||
|
|
||||||
result := AppDataDir()
|
result := AppDataDir()
|
||||||
expected := "/usr/user/lib/caddy"
|
expected := "/usr/user/lib/caddy"
|
||||||
@ -315,8 +337,9 @@ func TestAppDataDir_PlatformSpecific(t *testing.T) {
|
|||||||
|
|
||||||
default:
|
default:
|
||||||
// Test Unix-like systems
|
// Test Unix-like systems
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("HOME", "/home/user")
|
os.Setenv("HOME", "/home/user")
|
||||||
|
os.Unsetenv("AppData")
|
||||||
|
os.Unsetenv("home")
|
||||||
|
|
||||||
result := AppDataDir()
|
result := AppDataDir()
|
||||||
expected := "/home/user/.local/share/caddy"
|
expected := "/home/user/.local/share/caddy"
|
||||||
@ -344,8 +367,11 @@ func TestAppDataDir_Fallback(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Clear all relevant environment variables
|
// Unset all relevant environment variables instead of clearing everything
|
||||||
os.Clearenv()
|
envVarsToUnset := []string{"XDG_DATA_HOME", "AppData", "HOME", "home"}
|
||||||
|
for _, envVar := range envVarsToUnset {
|
||||||
|
os.Unsetenv(envVar)
|
||||||
|
}
|
||||||
|
|
||||||
result := AppDataDir()
|
result := AppDataDir()
|
||||||
expected := "./caddy"
|
expected := "./caddy"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user