auth: fix tests for Windows compatibility

- Set USERPROFILE env var on Windows (HOME on Unix)
- Add cross-platform DirectoryIsFile test for write failures
This commit is contained in:
ParthSareen
2026-01-07 17:19:47 -08:00
parent 106640b978
commit b978dc28d2
2 changed files with 24 additions and 10 deletions

View File

@@ -3,6 +3,7 @@ package auth
import (
"os"
"path/filepath"
"runtime"
"testing"
)
@@ -267,19 +268,27 @@ func TestConcurrentAccess(t *testing.T) {
// TestFileSystemEdgeCases tests filesystem-related edge cases
func TestFileSystemEdgeCases(t *testing.T) {
tmpDir := setupTestDir(t)
t.Run("DirectoryIsFile", func(t *testing.T) {
tmpDir := t.TempDir()
t.Run("ReadOnlyDirectory", func(t *testing.T) {
// Make .ollama directory read-only
ollamaDir := filepath.Join(tmpDir, ".ollama")
os.Chmod(ollamaDir, 0o500)
defer os.Chmod(ollamaDir, 0o755) // Restore for cleanup
// Create a file where .ollama directory should be
ollamaPath := filepath.Join(tmpDir, ".ollama")
if err := os.WriteFile(ollamaPath, []byte("not a directory"), 0o600); err != nil {
t.Fatalf("failed to create blocking file: %v", err)
}
// Try to write - should fail because can't create temp file
// Override home directory
if runtime.GOOS == "windows" {
t.Setenv("USERPROFILE", tmpDir)
} else {
t.Setenv("HOME", tmpDir)
}
// Try to write - should fail because .ollama is a file, not a directory
state := &SignInState{Name: "newuser", Email: "new@example.com"}
err := SetSignInState(state)
if err == nil {
t.Error("should fail to write in read-only directory")
t.Error("should fail when .ollama is a file instead of directory")
}
})
}

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"os"
"path/filepath"
"runtime"
"testing"
"time"
)
@@ -20,8 +21,12 @@ func setupTestDir(t *testing.T) string {
t.Fatalf("failed to create .ollama dir: %v", err)
}
// Override home directory for tests
t.Setenv("HOME", tmpDir)
// Override home directory for tests (platform-specific)
if runtime.GOOS == "windows" {
t.Setenv("USERPROFILE", tmpDir)
} else {
t.Setenv("HOME", tmpDir)
}
return tmpDir
}