test: extraer resolveWorkDir y tests unitarios de aislamiento
Extraer la logica de resolucion de working_dir a una funcion resolveWorkDir() separada para hacerla testeable. Tres tests cubren: - WorkingDir vacio → crea tmpdir con prefijo claude-agent-* - WorkingDir configurado → crea el directorio y lo usa - WorkingDir ya existente → lo usa sin error Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,9 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -318,6 +321,56 @@ func TestFilterEnv_PrefixSafety(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// ── resolveWorkDir ──────────────────────────────────────────────────────
|
||||
|
||||
func TestResolveWorkDir_EmptyCreatesTempDir(t *testing.T) {
|
||||
dir := resolveWorkDir("", discardLog)
|
||||
if dir == "" {
|
||||
t.Fatal("expected a temp directory, got empty string")
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
if !strings.Contains(dir, "claude-agent-") {
|
||||
t.Errorf("temp dir %q should contain 'claude-agent-' prefix", dir)
|
||||
}
|
||||
|
||||
info, err := os.Stat(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("temp dir should exist: %v", err)
|
||||
}
|
||||
if !info.IsDir() {
|
||||
t.Error("temp dir should be a directory")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveWorkDir_ConfiguredValueUsed(t *testing.T) {
|
||||
want := filepath.Join(t.TempDir(), "custom-workdir")
|
||||
|
||||
got := resolveWorkDir(want, discardLog)
|
||||
|
||||
if got != want {
|
||||
t.Errorf("got %q, want %q", got, want)
|
||||
}
|
||||
|
||||
info, err := os.Stat(got)
|
||||
if err != nil {
|
||||
t.Fatalf("configured dir should be created: %v", err)
|
||||
}
|
||||
if !info.IsDir() {
|
||||
t.Error("configured dir should be a directory")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveWorkDir_ConfiguredAlreadyExists(t *testing.T) {
|
||||
want := t.TempDir() // already exists
|
||||
|
||||
got := resolveWorkDir(want, discardLog)
|
||||
|
||||
if got != want {
|
||||
t.Errorf("got %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// ── helpers ──────────────────────────────────────────────────────────────
|
||||
|
||||
func contains(s, substr string) bool {
|
||||
|
||||
Reference in New Issue
Block a user