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

@@ -23,6 +23,7 @@ import (
"github.com/goplus/llgo/compiler/cmd/internal/base"
"github.com/goplus/llgo/compiler/internal/build"
"github.com/goplus/llgo/compiler/internal/mockable"
)
// llgo build
@@ -47,6 +48,6 @@ func runCmd(cmd *base.Command, args []string) {
_, err := build.Do(args, conf)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
mockable.Exit(1)
}
}

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)
}

View File

@@ -23,6 +23,7 @@ import (
"github.com/goplus/llgo/compiler/cmd/internal/base"
"github.com/goplus/llgo/compiler/internal/build"
"github.com/goplus/llgo/compiler/internal/mockable"
)
// llgo install
@@ -40,6 +41,6 @@ func runCmd(cmd *base.Command, args []string) {
_, err := build.Do(args, conf)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
mockable.Exit(1)
}
}

View File

@@ -25,6 +25,7 @@ import (
"github.com/goplus/llgo/compiler/cmd/internal/base"
"github.com/goplus/llgo/compiler/internal/build"
"github.com/goplus/llgo/compiler/internal/mockable"
)
var (
@@ -68,7 +69,7 @@ func runCmdEx(_ *base.Command, args []string, mode build.Mode) {
_, err = build.Do(args, conf)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
mockable.Exit(1)
}
}