fix(cmd): use Fprintf for efficiency and handle Chdir error in tests
- 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:
parent
eadb4452a8
commit
0ef58cb5a4
@ -165,7 +165,8 @@ func harvestContext(filePath, lang string) string {
|
||||
if len(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))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -58,7 +58,11 @@ func TestScaffoldCmd_Live(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
origDir, _ := os.Getwd()
|
||||
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)
|
||||
err := rootCmd.Execute()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user