test: make cmd testable

This commit is contained in:
Li Jie
2025-01-14 10:50:43 +08:00
parent 25a3e19384
commit 8749923f1a
9 changed files with 186 additions and 249 deletions

View File

@@ -29,6 +29,7 @@ import (
"unicode/utf8"
"github.com/goplus/llgo/compiler/cmd/internal/base"
"github.com/goplus/llgo/compiler/internal/mockable"
)
// Help implements the 'help' command.
@@ -49,7 +50,7 @@ Args:
helpSuccess += " " + strings.Join(args[:i], " ")
}
fmt.Fprintf(os.Stderr, "llgo help %s: unknown help topic. Run '%s'.\n", strings.Join(args, " "), helpSuccess)
os.Exit(2)
mockable.Exit(2)
}
if len(cmd.Commands) > 0 {
@@ -98,7 +99,7 @@ func tmpl(w io.Writer, text string, data interface{}) {
if ew.err != nil {
// I/O error writing. Ignore write on closed pipe.
if strings.Contains(ew.err.Error(), "pipe") {
os.Exit(1)
mockable.Exit(1)
}
log.Fatalf("writing output: %v", ew.err)
}