Merge pull request #1171 from luoliwoshang/internal/build/test

fix(build):test all pkg with llgo test ./...
This commit is contained in:
xushiwei
2025-06-25 18:17:41 +08:00
committed by GitHub
11 changed files with 121 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
package bar
func Bar() int {
return 2
}

View File

@@ -0,0 +1,13 @@
package bar_test
import (
"testing"
"github.com/goplus/llgo/cl/_testgo/runextest/bar"
)
func TestBar(t *testing.T) {
if bar.Bar() != 2 {
t.Fatal("Bar() != 2")
}
}

View File

@@ -0,0 +1,5 @@
package barinner
func BarInner() int {
return 2
}

View File

@@ -0,0 +1,13 @@
package barinner_test
import (
"testing"
"github.com/goplus/llgo/cl/_testgo/runextest/bar/barinner"
)
func TestBarInner(t *testing.T) {
if barinner.BarInner() != 2 {
t.Fatal("BarInner() != 2")
}
}

View File

@@ -0,0 +1,5 @@
package foo
func Foo() int {
return 1
}

View File

@@ -0,0 +1,13 @@
package foo_test
import (
"testing"
"github.com/goplus/llgo/cl/_testgo/runextest/foo"
)
func TestFoo(t *testing.T) {
if foo.Foo() != 1 {
t.Fatal("Foo() != 1")
}
}

View File

@@ -0,0 +1,16 @@
package main
import (
"github.com/goplus/llgo/cl/_testgo/runextest/bar"
"github.com/goplus/llgo/cl/_testgo/runextest/foo"
)
func Zoo() int {
return 3
}
func main() {
println("foo.Foo()", foo.Foo())
println("bar.Bar()", bar.Bar())
println("Zoo()", Zoo())
}

View File

@@ -0,0 +1,11 @@
package main
import (
"testing"
)
func TestZoo(t *testing.T) {
if Zoo() != 3 {
t.Fatal("Zoo() != 3")
}
}

View File

@@ -0,0 +1 @@
;

View File

@@ -252,7 +252,7 @@ func Do(args []string, conf *Config) ([]Package, error) {
output := conf.OutFile != "" output := conf.OutFile != ""
export, err := crosscompile.Use(conf.Goos, conf.Goarch, IsWasiThreadsEnabled(), IsRpathChangeEnabled()) export, err := crosscompile.Use(conf.Goos, conf.Goarch, IsWasiThreadsEnabled(), IsRpathChangeEnabled())
check(err) check(err)
ctx := &context{env, cfg, progSSA, prog, dedup, patches, make(map[string]none), initial, mode, 0, output, make(map[*packages.Package]bool), make(map[*packages.Package]bool), conf, export} ctx := &context{env, cfg, progSSA, prog, dedup, patches, make(map[string]none), initial, mode, 0, output, make(map[*packages.Package]bool), make(map[*packages.Package]bool), conf, export, false}
pkgs, err := buildAllPkgs(ctx, initial, verbose) pkgs, err := buildAllPkgs(ctx, initial, verbose)
check(err) check(err)
if mode == ModeGen { if mode == ModeGen {
@@ -282,6 +282,10 @@ func Do(args []string, conf *Config) ([]Package, error) {
} }
} }
if mode == ModeTest && ctx.testFail {
mockable.Exit(1)
}
return dpkg, nil return dpkg, nil
} }
@@ -342,6 +346,8 @@ type context struct {
buildConf *Config buildConf *Config
crossCompile crosscompile.Export crossCompile crosscompile.Export
testFail bool
} }
func (c *context) compiler() *clang.Cmd { func (c *context) compiler() *clang.Cmd {
@@ -587,7 +593,9 @@ func linkMainPkg(ctx *context, pkg *packages.Package, pkgs []*aPackage, global l
if s := cmd.ProcessState; s != nil { if s := cmd.ProcessState; s != nil {
exitCode := s.ExitCode() exitCode := s.ExitCode()
fmt.Fprintf(os.Stderr, "%s: exit code %d\n", app, exitCode) fmt.Fprintf(os.Stderr, "%s: exit code %d\n", app, exitCode)
mockable.Exit(exitCode) if !ctx.testFail && exitCode != 0 {
ctx.testFail = true
}
} }
case ModeRun: case ModeRun:
args := make([]string, 0, len(conf.RunArgs)+1) args := make([]string, 0, len(conf.RunArgs)+1)

View File

@@ -4,7 +4,9 @@
package build package build
import ( import (
"bytes"
"fmt" "fmt"
"io"
"os" "os"
"testing" "testing"
@@ -58,9 +60,36 @@ func TestRun(t *testing.T) {
} }
func TestTest(t *testing.T) { func TestTest(t *testing.T) {
// FIXME(zzy): with builtin package test in a llgo test ./... will cause duplicate symbol error
mockRun([]string{"../../cl/_testgo/runtest"}, &Config{Mode: ModeTest}) mockRun([]string{"../../cl/_testgo/runtest"}, &Config{Mode: ModeTest})
} }
func TestExtest(t *testing.T) {
originalStdout := os.Stdout
defer func() { os.Stdout = originalStdout }()
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("os.Pipe failed: %v", err)
}
os.Stdout = w
outputChan := make(chan string)
go func() {
var data bytes.Buffer
io.Copy(&data, r)
outputChan <- data.String()
}()
mockRun([]string{"../../cl/_testgo/runextest/..."}, &Config{Mode: ModeTest})
w.Close()
got := <-outputChan
expected := "PASS\nPASS\nPASS\nPASS\n"
if got != expected {
t.Errorf("Expected output %q, but got %q", expected, got)
}
}
func TestCmpTest(t *testing.T) { func TestCmpTest(t *testing.T) {
mockRun([]string{"../../cl/_testgo/runtest"}, &Config{Mode: ModeCmpTest}) mockRun([]string{"../../cl/_testgo/runtest"}, &Config{Mode: ModeCmpTest})
} }