From 19ddee02410d53ed1c5eb03d762f3a2a4eadfaed Mon Sep 17 00:00:00 2001 From: Haolan Date: Fri, 29 Aug 2025 17:48:34 +0800 Subject: [PATCH] test: add compile test --- internal/crosscompile/compile/compile.go | 7 +- internal/crosscompile/compile/compile_test.go | 142 ++++++++++++++++++ internal/crosscompile/fetch_test.go | 2 +- 3 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 internal/crosscompile/compile/compile_test.go diff --git a/internal/crosscompile/compile/compile.go b/internal/crosscompile/compile/compile.go index 5165b39f..20b52aa1 100644 --- a/internal/crosscompile/compile/compile.go +++ b/internal/crosscompile/compile/compile.go @@ -43,7 +43,7 @@ func (g CompileGroup) Compile(outputDir, cc, linkerName string, extraCCFlags, ex compiler := clang.NewCompiler(cfg) - compiler.Verbose = true + compiler.Verbose = false archive := filepath.Join(outputDir, g.OutputFileName) fmt.Fprintf(os.Stderr, "Start to compile group %s to %s...\n", g.OutputFileName, archive) @@ -75,8 +75,9 @@ func (g CompileGroup) Compile(outputDir, cc, linkerName string, extraCCFlags, ex llvmAr := filepath.Join(ccDir, "llvm-ar") cmd := exec.Command(llvmAr, args...) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + // TODO(MeteorsLiu): support verbose + // cmd.Stdout = os.Stdout + // cmd.Stderr = os.Stderr err = cmd.Run() return } diff --git a/internal/crosscompile/compile/compile_test.go b/internal/crosscompile/compile/compile_test.go new file mode 100644 index 00000000..9995c555 --- /dev/null +++ b/internal/crosscompile/compile/compile_test.go @@ -0,0 +1,142 @@ +package compile + +import ( + "os" + "strings" + "testing" + + "github.com/goplus/llgo/xtool/nm" +) + +func TestIsCompile(t *testing.T) { + t.Run("IsCompile Not Exists", func(t *testing.T) { + cfg := CompileConfig{ + Groups: []CompileGroup{ + { + OutputFileName: "fakefile1.a", + }, + }, + } + + if cfg.IsCompiled(".") || cfg.Groups[0].IsCompiled(".") { + t.Errorf("unexpected result: should false") + } + }) + t.Run("IsCompile Exists", func(t *testing.T) { + tmpFile, err := os.CreateTemp(".", "test*.a") + if err != nil { + t.Error(err) + return + } + defer os.Remove(tmpFile.Name()) + cfg := CompileConfig{ + Groups: []CompileGroup{ + { + OutputFileName: tmpFile.Name(), + }, + }, + } + + if !cfg.IsCompiled(".") && !cfg.Groups[0].IsCompiled(".") { + t.Errorf("unexpected result: should true") + } + }) +} + +func TestCompile(t *testing.T) { + t.Run("Skip compile", func(t *testing.T) { + tmpFile, err := os.CreateTemp(".", "test*.a") + if err != nil { + t.Error(err) + return + } + defer os.Remove(tmpFile.Name()) + group := CompileGroup{ + OutputFileName: tmpFile.Name(), + } + err = group.Compile(".", "clang", "lld", nil, nil) + if err != nil { + t.Errorf("unexpected result: should nil") + } + }) + + t.Run("TmpDir Fail", func(t *testing.T) { + err := os.Mkdir("test-compile", 0) + if err != nil { + t.Error(err) + return + } + defer os.RemoveAll("test-compile") + + os.Setenv("TMPDIR", "test-compile") + defer os.Unsetenv("TMPDIR") + + group := CompileGroup{ + OutputFileName: "nop.a", + } + err = group.Compile(".", "clang", "lld", nil, nil) + if err == nil { + t.Errorf("unexpected result: should not nil") + } + }) + + t.Run("Compile", func(t *testing.T) { + tmpFile, err := os.CreateTemp("", "test*.c") + if err != nil { + t.Error(err) + return + } + defer os.Remove(tmpFile.Name()) + + _, err = tmpFile.Write([]byte(`#include + void Foo() { + double x = 2.0; + double y = sqrt(x); + (void) y ; + } + `)) + if err != nil { + t.Error(err) + return + } + + group := CompileGroup{ + OutputFileName: "nop.a", + Files: []string{tmpFile.Name()}, + } + err = group.Compile(".", "clang", "lld", []string{"-nostdinc"}, nil) + if err == nil { + t.Errorf("unexpected result: should not nil") + } + err = group.Compile(".", "clang", "lld", nil, nil) + if err != nil { + t.Errorf("unexpected result: should not nil") + } + if _, err := os.Stat("nop.a"); os.IsNotExist(err) { + t.Error("unexpected result: compiled nop.a not found") + return + } + defer os.Remove("nop.a") + + items, err := nm.New("").List("nop.a") + if err != nil { + t.Error(err) + return + } + + want := "Foo" + found := false + loop: + for _, item := range items { + for _, sym := range item.Symbols { + if strings.Contains(sym.Name, want) { + found = true + break loop + } + } + } + if !found { + t.Errorf("cannot find symbol Foo") + } + }) +} diff --git a/internal/crosscompile/fetch_test.go b/internal/crosscompile/fetch_test.go index 9bd2c496..7f5d2af9 100644 --- a/internal/crosscompile/fetch_test.go +++ b/internal/crosscompile/fetch_test.go @@ -287,7 +287,7 @@ func TestDownloadAndExtractArchive(t *testing.T) { func TestDownloadAndExtractArchiveUnsupportedFormat(t *testing.T) { server := createTestServer(t, map[string]string{ - "test.zip": "fake zip content", + "test.7z": "fake zip content", }) defer server.Close()