2025-02-22 09:21:31 +08:00
|
|
|
//go:build !llgo
|
|
|
|
|
// +build !llgo
|
|
|
|
|
|
2025-02-17 15:04:41 +08:00
|
|
|
package build
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2025-04-03 15:52:18 +08:00
|
|
|
"github.com/goplus/llgo/internal/mockable"
|
2025-02-17 15:04:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func mockRun(args []string, cfg *Config) {
|
2025-02-18 10:58:45 +08:00
|
|
|
const maxAttempts = 3
|
|
|
|
|
var lastErr error
|
|
|
|
|
var lastPanic interface{}
|
|
|
|
|
for attempt := 0; attempt < maxAttempts; attempt++ {
|
|
|
|
|
mockable.EnableMock()
|
|
|
|
|
func() {
|
|
|
|
|
defer func() {
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
if r != "exit" {
|
|
|
|
|
lastPanic = r
|
|
|
|
|
} else {
|
|
|
|
|
exitCode := mockable.ExitCode()
|
|
|
|
|
if (exitCode != 0) != false {
|
|
|
|
|
lastPanic = fmt.Errorf("got exit code %d", exitCode)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-17 15:04:41 +08:00
|
|
|
}
|
2025-02-18 10:58:45 +08:00
|
|
|
}()
|
|
|
|
|
file, _ := os.CreateTemp("", "llgo-*")
|
|
|
|
|
cfg.OutFile = file.Name()
|
|
|
|
|
file.Close()
|
|
|
|
|
defer os.Remove(cfg.OutFile)
|
|
|
|
|
_, err := Do(args, cfg)
|
|
|
|
|
if err == nil {
|
|
|
|
|
return // Success, return immediately from the inner function
|
2025-02-17 15:04:41 +08:00
|
|
|
}
|
2025-02-18 10:58:45 +08:00
|
|
|
lastErr = err
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
if lastPanic == nil && lastErr == nil {
|
|
|
|
|
return // Success, return from mockRun
|
2025-02-17 15:04:41 +08:00
|
|
|
}
|
2025-02-18 10:58:45 +08:00
|
|
|
// Continue to next attempt if this one failed
|
|
|
|
|
}
|
|
|
|
|
// If we get here, all attempts failed
|
|
|
|
|
if lastPanic != nil {
|
|
|
|
|
panic(lastPanic)
|
|
|
|
|
}
|
|
|
|
|
panic(fmt.Errorf("all %d attempts failed, last error: %v", maxAttempts, lastErr))
|
2025-02-17 15:04:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRun(t *testing.T) {
|
2025-02-18 10:22:21 +08:00
|
|
|
mockRun([]string{"-v", "../../cl/_testgo/print"}, &Config{Mode: ModeRun})
|
2025-02-17 15:04:41 +08:00
|
|
|
}
|
|
|
|
|
|
2025-04-03 16:20:09 +08:00
|
|
|
func TestTest(t *testing.T) {
|
|
|
|
|
mockRun([]string{"-v", "../../cl/_testgo/runtest"}, &Config{Mode: ModeTest})
|
2025-02-17 15:04:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCmpTest(t *testing.T) {
|
2025-04-03 16:20:09 +08:00
|
|
|
mockRun([]string{"-v", "../../cl/_testgo/runtest"}, &Config{Mode: ModeCmpTest})
|
2025-02-17 15:04:41 +08:00
|
|
|
}
|