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:
2026-03-08 11:47:07 +00:00
parent d05ec0bd57
commit 6a5cad5700
2 changed files with 77 additions and 18 deletions
+24 -18
View File
@@ -59,24 +59,7 @@ func NewClaudeCodeComplete(cfg config.ClaudeCodeCfg, log *slog.Logger) coretypes
}
// Resolve working directory once at init time.
workDir := cfg.WorkingDir
if workDir == "" {
tmp, err := os.MkdirTemp("", "claude-agent-*")
if err != nil {
log.Error("claude-code: failed to create temp working dir", "err", err)
// Fall through — cmd.Dir will remain empty (inherits CWD).
} else {
workDir = tmp
log.Warn("claude-code working_dir is empty, using temporary directory",
"dir", workDir,
)
}
} else {
// Ensure configured directory exists.
if err := os.MkdirAll(workDir, 0o755); err != nil {
log.Error("claude-code: failed to create working dir", "dir", workDir, "err", err)
}
}
workDir := resolveWorkDir(cfg.WorkingDir, log)
return func(ctx context.Context, req coretypes.CompletionRequest) (coretypes.CompletionResponse, error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
@@ -141,6 +124,29 @@ func NewClaudeCodeComplete(cfg config.ClaudeCodeCfg, log *slog.Logger) coretypes
}
}
// resolveWorkDir determines the working directory for the claude subprocess.
// If configured is empty, it creates a temporary directory to avoid inheriting the launcher's CWD.
// If configured is non-empty, it ensures the directory exists.
func resolveWorkDir(configured string, log *slog.Logger) string {
if configured == "" {
tmp, err := os.MkdirTemp("", "claude-agent-*")
if err != nil {
log.Error("claude-code: failed to create temp working dir", "err", err)
return "" // Fall through — cmd.Dir will remain empty (inherits CWD).
}
log.Warn("claude-code working_dir is empty, using temporary directory",
"dir", tmp,
)
return tmp
}
// Ensure configured directory exists.
if err := os.MkdirAll(configured, 0o755); err != nil {
log.Error("claude-code: failed to create working dir", "dir", configured, "err", err)
}
return configured
}
// buildClaudeArgs constructs the CLI arguments for claude -p.
func buildClaudeArgs(cfg config.ClaudeCodeCfg, req coretypes.CompletionRequest) []string {
args := []string{"--print", "--output-format", "json"}