fix(cmd): use Fprintf for efficiency and handle Chdir error in tests
All checks were successful
CI / Test (push) Successful in 25s
CI / Lint (push) Successful in 37s
CI / Build (push) Successful in 15s

- Replace WriteString + Sprintf with Fprintf in harvestContext for better performance.
- Wrap deferred Chdir in scaffold tests with error logging to avoid silent failures.
This commit is contained in:
Gregory Gauthier 2026-03-03 15:50:30 +00:00
parent eadb4452a8
commit 0ef58cb5a4
2 changed files with 7 additions and 2 deletions

View File

@ -165,7 +165,8 @@ func harvestContext(filePath, lang string) string {
if len(content) > 2000 { if len(content) > 2000 {
content = content[:2000] content = content[:2000]
} }
sb.WriteString(fmt.Sprintf("=== %s ===\n%s\n\n", f.Name(), string(content))) // Fixed: use Fprintf instead of WriteString + Sprintf
fmt.Fprintf(&sb, "=== %s ===\n%s\n\n", f.Name(), string(content))
} }
} }

View File

@ -58,7 +58,11 @@ func TestScaffoldCmd_Live(t *testing.T) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
origDir, _ := os.Getwd() origDir, _ := os.Getwd()
require.NoError(t, os.Chdir(tmpDir)) require.NoError(t, os.Chdir(tmpDir))
defer os.Chdir(origDir) defer func() {
if err := os.Chdir(origDir); err != nil {
t.Logf("warning: failed to restore original directory: %v", err)
}
}()
rootCmd.SetArgs(tt.args) rootCmd.SetArgs(tt.args)
err := rootCmd.Execute() err := rootCmd.Execute()