From b469fc990fd71011e458b386a2c30b37a4ce78ee Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 24 Jun 2025 11:57:49 +0800 Subject: [PATCH 1/3] fix(build):test all pkg with llgo test ./... --- internal/build/build.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/build/build.go b/internal/build/build.go index 3c45dac1..1c480b25 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -252,7 +252,7 @@ func Do(args []string, conf *Config) ([]Package, error) { output := conf.OutFile != "" export, err := crosscompile.Use(conf.Goos, conf.Goarch, IsWasiThreadsEnabled(), IsRpathChangeEnabled()) 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) check(err) 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 } @@ -342,6 +346,8 @@ type context struct { buildConf *Config crossCompile crosscompile.Export + + testFail bool } 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 { exitCode := s.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: args := make([]string, 0, len(conf.RunArgs)+1) From 3df783de13caab7fdb4f65a2530ca57d403ded44 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 24 Jun 2025 15:23:12 +0800 Subject: [PATCH 2/3] test(build):case with llgo test ./... --- .github/workflows/llgo.yml | 3 +-- .github/workflows/llgo_test.sh | 17 +++++++++++++++++ internal/build/build_test.go | 1 + test/bar/bar.go | 5 +++++ test/bar/bar_test.go | 13 +++++++++++++ test/bar/barinner/barinner.go | 5 +++++ test/bar/barinner/barinner_test.go | 13 +++++++++++++ test/foo/foo.go | 5 +++++ test/foo/foo_test.go | 13 +++++++++++++ 9 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/llgo_test.sh create mode 100644 test/bar/bar.go create mode 100644 test/bar/bar_test.go create mode 100644 test/bar/barinner/barinner.go create mode 100644 test/bar/barinner/barinner_test.go create mode 100644 test/foo/foo.go create mode 100644 test/foo/foo_test.go diff --git a/.github/workflows/llgo.yml b/.github/workflows/llgo.yml index f3f4ed7b..1c5cbb9b 100644 --- a/.github/workflows/llgo.yml +++ b/.github/workflows/llgo.yml @@ -137,8 +137,7 @@ jobs: go-version: ${{matrix.go}} - name: run llgo test - run: | - llgo test ./... + run: bash .github/workflows/llgo_test.sh hello: continue-on-error: true diff --git a/.github/workflows/llgo_test.sh b/.github/workflows/llgo_test.sh new file mode 100644 index 00000000..6137fd37 --- /dev/null +++ b/.github/workflows/llgo_test.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +set -e + +output=$(llgo test ./... 2>&1) +echo "$output" + +pass_count=$(echo "$output" | grep -c "^PASS$") +echo "llgo test pass count: $pass_count" + +if [ "$pass_count" -gt 1 ]; then + echo "llgo test ./... passed" + exit 0 +else + echo "llgo test ./... failed: PASS count is not greater than 1" + exit 1 +fi diff --git a/internal/build/build_test.go b/internal/build/build_test.go index e7e599fd..a7d3cf38 100644 --- a/internal/build/build_test.go +++ b/internal/build/build_test.go @@ -58,6 +58,7 @@ func TestRun(t *testing.T) { } func TestTest(t *testing.T) { + // fixme:with builtin package test in a llgo test ./... will cause duplicate symbol error mockRun([]string{"../../cl/_testgo/runtest"}, &Config{Mode: ModeTest}) } diff --git a/test/bar/bar.go b/test/bar/bar.go new file mode 100644 index 00000000..5730a5c5 --- /dev/null +++ b/test/bar/bar.go @@ -0,0 +1,5 @@ +package bar + +func Bar() int { + return 2 +} diff --git a/test/bar/bar_test.go b/test/bar/bar_test.go new file mode 100644 index 00000000..b3e25c2f --- /dev/null +++ b/test/bar/bar_test.go @@ -0,0 +1,13 @@ +package bar_test + +import ( + "testing" + + "github.com/goplus/llgo/test/bar" +) + +func TestBar(t *testing.T) { + if bar.Bar() != 2 { + t.Fatal("Bar() != 2") + } +} diff --git a/test/bar/barinner/barinner.go b/test/bar/barinner/barinner.go new file mode 100644 index 00000000..b37a5d6d --- /dev/null +++ b/test/bar/barinner/barinner.go @@ -0,0 +1,5 @@ +package barinner + +func BarInner() int { + return 2 +} diff --git a/test/bar/barinner/barinner_test.go b/test/bar/barinner/barinner_test.go new file mode 100644 index 00000000..d2d79d03 --- /dev/null +++ b/test/bar/barinner/barinner_test.go @@ -0,0 +1,13 @@ +package barinner_test + +import ( + "testing" + + "github.com/goplus/llgo/test/bar/barinner" +) + +func TestBarInner(t *testing.T) { + if barinner.BarInner() != 2 { + t.Fatal("BarInner() != 2") + } +} diff --git a/test/foo/foo.go b/test/foo/foo.go new file mode 100644 index 00000000..96b79fa4 --- /dev/null +++ b/test/foo/foo.go @@ -0,0 +1,5 @@ +package foo + +func Foo() int { + return 1 +} diff --git a/test/foo/foo_test.go b/test/foo/foo_test.go new file mode 100644 index 00000000..aa11438a --- /dev/null +++ b/test/foo/foo_test.go @@ -0,0 +1,13 @@ +package foo_test + +import ( + "testing" + + "github.com/goplus/llgo/test/foo" +) + +func TestFoo(t *testing.T) { + if foo.Foo() != 1 { + t.Fatal("Foo() != 1") + } +} From 6e8f3d1d191e43dee0a9d17f771592ec94c7019e Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Wed, 25 Jun 2025 14:46:44 +0800 Subject: [PATCH 3/3] test(build):collect llgo test ./... output to test --- .github/workflows/llgo.yml | 3 +- .github/workflows/llgo_test.sh | 17 ----------- {test => cl/_testgo/runextest}/bar/bar.go | 0 .../_testgo/runextest}/bar/bar_test.go | 2 +- .../runextest}/bar/barinner/barinner.go | 0 .../runextest}/bar/barinner/barinner_test.go | 2 +- {test => cl/_testgo/runextest}/foo/foo.go | 0 .../_testgo/runextest}/foo/foo_test.go | 2 +- cl/_testgo/runextest/main.go | 16 ++++++++++ cl/_testgo/runextest/main_test.go | 11 +++++++ cl/_testgo/runextest/out.ll | 1 + internal/build/build_test.go | 30 ++++++++++++++++++- 12 files changed, 62 insertions(+), 22 deletions(-) delete mode 100644 .github/workflows/llgo_test.sh rename {test => cl/_testgo/runextest}/bar/bar.go (100%) rename {test => cl/_testgo/runextest}/bar/bar_test.go (70%) rename {test => cl/_testgo/runextest}/bar/barinner/barinner.go (100%) rename {test => cl/_testgo/runextest}/bar/barinner/barinner_test.go (70%) rename {test => cl/_testgo/runextest}/foo/foo.go (100%) rename {test => cl/_testgo/runextest}/foo/foo_test.go (70%) create mode 100644 cl/_testgo/runextest/main.go create mode 100644 cl/_testgo/runextest/main_test.go create mode 100644 cl/_testgo/runextest/out.ll diff --git a/.github/workflows/llgo.yml b/.github/workflows/llgo.yml index 1c5cbb9b..f3f4ed7b 100644 --- a/.github/workflows/llgo.yml +++ b/.github/workflows/llgo.yml @@ -137,7 +137,8 @@ jobs: go-version: ${{matrix.go}} - name: run llgo test - run: bash .github/workflows/llgo_test.sh + run: | + llgo test ./... hello: continue-on-error: true diff --git a/.github/workflows/llgo_test.sh b/.github/workflows/llgo_test.sh deleted file mode 100644 index 6137fd37..00000000 --- a/.github/workflows/llgo_test.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash - -set -e - -output=$(llgo test ./... 2>&1) -echo "$output" - -pass_count=$(echo "$output" | grep -c "^PASS$") -echo "llgo test pass count: $pass_count" - -if [ "$pass_count" -gt 1 ]; then - echo "llgo test ./... passed" - exit 0 -else - echo "llgo test ./... failed: PASS count is not greater than 1" - exit 1 -fi diff --git a/test/bar/bar.go b/cl/_testgo/runextest/bar/bar.go similarity index 100% rename from test/bar/bar.go rename to cl/_testgo/runextest/bar/bar.go diff --git a/test/bar/bar_test.go b/cl/_testgo/runextest/bar/bar_test.go similarity index 70% rename from test/bar/bar_test.go rename to cl/_testgo/runextest/bar/bar_test.go index b3e25c2f..8d4c0438 100644 --- a/test/bar/bar_test.go +++ b/cl/_testgo/runextest/bar/bar_test.go @@ -3,7 +3,7 @@ package bar_test import ( "testing" - "github.com/goplus/llgo/test/bar" + "github.com/goplus/llgo/cl/_testgo/runextest/bar" ) func TestBar(t *testing.T) { diff --git a/test/bar/barinner/barinner.go b/cl/_testgo/runextest/bar/barinner/barinner.go similarity index 100% rename from test/bar/barinner/barinner.go rename to cl/_testgo/runextest/bar/barinner/barinner.go diff --git a/test/bar/barinner/barinner_test.go b/cl/_testgo/runextest/bar/barinner/barinner_test.go similarity index 70% rename from test/bar/barinner/barinner_test.go rename to cl/_testgo/runextest/bar/barinner/barinner_test.go index d2d79d03..12c543cd 100644 --- a/test/bar/barinner/barinner_test.go +++ b/cl/_testgo/runextest/bar/barinner/barinner_test.go @@ -3,7 +3,7 @@ package barinner_test import ( "testing" - "github.com/goplus/llgo/test/bar/barinner" + "github.com/goplus/llgo/cl/_testgo/runextest/bar/barinner" ) func TestBarInner(t *testing.T) { diff --git a/test/foo/foo.go b/cl/_testgo/runextest/foo/foo.go similarity index 100% rename from test/foo/foo.go rename to cl/_testgo/runextest/foo/foo.go diff --git a/test/foo/foo_test.go b/cl/_testgo/runextest/foo/foo_test.go similarity index 70% rename from test/foo/foo_test.go rename to cl/_testgo/runextest/foo/foo_test.go index aa11438a..5c2125e7 100644 --- a/test/foo/foo_test.go +++ b/cl/_testgo/runextest/foo/foo_test.go @@ -3,7 +3,7 @@ package foo_test import ( "testing" - "github.com/goplus/llgo/test/foo" + "github.com/goplus/llgo/cl/_testgo/runextest/foo" ) func TestFoo(t *testing.T) { diff --git a/cl/_testgo/runextest/main.go b/cl/_testgo/runextest/main.go new file mode 100644 index 00000000..b3fd473c --- /dev/null +++ b/cl/_testgo/runextest/main.go @@ -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()) +} diff --git a/cl/_testgo/runextest/main_test.go b/cl/_testgo/runextest/main_test.go new file mode 100644 index 00000000..385de527 --- /dev/null +++ b/cl/_testgo/runextest/main_test.go @@ -0,0 +1,11 @@ +package main + +import ( + "testing" +) + +func TestZoo(t *testing.T) { + if Zoo() != 3 { + t.Fatal("Zoo() != 3") + } +} diff --git a/cl/_testgo/runextest/out.ll b/cl/_testgo/runextest/out.ll new file mode 100644 index 00000000..1c8a0e79 --- /dev/null +++ b/cl/_testgo/runextest/out.ll @@ -0,0 +1 @@ +; \ No newline at end of file diff --git a/internal/build/build_test.go b/internal/build/build_test.go index a7d3cf38..f2f8730f 100644 --- a/internal/build/build_test.go +++ b/internal/build/build_test.go @@ -4,7 +4,9 @@ package build import ( + "bytes" "fmt" + "io" "os" "testing" @@ -58,10 +60,36 @@ func TestRun(t *testing.T) { } func TestTest(t *testing.T) { - // fixme:with builtin package test in a llgo test ./... will cause duplicate symbol error + // FIXME(zzy): with builtin package test in a llgo test ./... will cause duplicate symbol error 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) { mockRun([]string{"../../cl/_testgo/runtest"}, &Config{Mode: ModeCmpTest}) }