test: enhance demo to test multiple go/build public functions

Enhanced the demo test to cover comprehensive import scenarios:
- Test 1: build.Default context with GOOS/GOARCH validation
- Test 2: Standard library package import (fmt)
- Test 3: Nested standard library package (os/exec)
- Test 4: Internal package import (internal/cpu)
- Test 5: Runtime package with Dir validation
- Test 6: ImportDir with current directory
- Test 7: IsLocalImport with multiple path formats
- Test 8: Context GOPATH/GOROOT validation
- Test 9: Import with AllowBinary flag
- Test 10: Release tags validation

All tests include proper result validation to ensure go/build
patches work correctly across different use cases.

🤖 Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: luoliwoshang <luoliwoshang@users.noreply.github.com>
This commit is contained in:
xgopilot
2025-10-16 11:55:02 +00:00
parent d09ce613c8
commit 0a94a54772

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"go/build"
"runtime"
"strings"
)
func main() {
@@ -26,28 +27,121 @@ func main() {
}
fmt.Printf("build.Default.ReleaseTags count = %d\n", len(ctx.ReleaseTags))
// Test 2: build.Import with FindOnly
// Validate GOOS and GOARCH are set
if ctx.GOOS == "" {
panic("expected build.Default.GOOS to be non-empty")
}
if ctx.GOARCH == "" {
panic("expected build.Default.GOARCH to be non-empty")
}
fmt.Printf("build.Default.GOOS = %q, GOARCH = %q\n", ctx.GOOS, ctx.GOARCH)
// Test 2: Import standard library package with FindOnly
pkg, err := build.Import("fmt", "", build.FindOnly)
if err != nil {
panic(fmt.Sprintf("build.Import failed: %v", err))
panic(fmt.Sprintf("build.Import(\"fmt\") failed: %v", err))
}
fmt.Printf("build.Import(\"fmt\"): %s\n", pkg.ImportPath)
if pkg.ImportPath != "fmt" {
panic(fmt.Sprintf("expected ImportPath \"fmt\", got %q", pkg.ImportPath))
}
if !pkg.Goroot {
panic("expected fmt package to be in GOROOT")
}
fmt.Printf("build.Import(\"fmt\"): ImportPath=%s, Goroot=%v\n", pkg.ImportPath, pkg.Goroot)
// Test 3: build.ImportDir
// Test 3: Import nested standard library package
osPkg, err := build.Import("os/exec", "", build.FindOnly)
if err != nil {
panic(fmt.Sprintf("build.Import(\"os/exec\") failed: %v", err))
}
if osPkg.ImportPath != "os/exec" {
panic(fmt.Sprintf("expected ImportPath \"os/exec\", got %q", osPkg.ImportPath))
}
if !osPkg.Goroot {
panic("expected os/exec package to be in GOROOT")
}
fmt.Printf("build.Import(\"os/exec\"): ImportPath=%s, Goroot=%v\n", osPkg.ImportPath, osPkg.Goroot)
// Test 4: Import internal package (should succeed with FindOnly)
internalPkg, err := build.Import("internal/cpu", "", build.FindOnly)
if err != nil {
panic(fmt.Sprintf("build.Import(\"internal/cpu\") failed: %v", err))
}
if internalPkg.ImportPath != "internal/cpu" {
panic(fmt.Sprintf("expected ImportPath \"internal/cpu\", got %q", internalPkg.ImportPath))
}
fmt.Printf("build.Import(\"internal/cpu\"): ImportPath=%s\n", internalPkg.ImportPath)
// Test 5: Import with srcDir parameter
runtimePkg, err := build.Import("runtime", "", build.FindOnly)
if err != nil {
panic(fmt.Sprintf("build.Import(\"runtime\") failed: %v", err))
}
if runtimePkg.ImportPath != "runtime" {
panic(fmt.Sprintf("expected ImportPath \"runtime\", got %q", runtimePkg.ImportPath))
}
if runtimePkg.Dir == "" {
panic("expected runtime package Dir to be non-empty")
}
fmt.Printf("build.Import(\"runtime\"): ImportPath=%s, Dir exists=%v\n", runtimePkg.ImportPath, runtimePkg.Dir != "")
// Test 6: ImportDir with current directory
dirPkg, err := build.ImportDir(".", build.FindOnly)
if err != nil {
panic(fmt.Sprintf("build.ImportDir failed: %v", err))
panic(fmt.Sprintf("build.ImportDir(\".\") failed: %v", err))
}
fmt.Printf("build.ImportDir(\".\"): %s\n", dirPkg.Name)
// Note: Name might be empty with FindOnly mode as it doesn't read source files
fmt.Printf("build.ImportDir(\".\"): Dir=%s, ImportPath=%s\n", dirPkg.Dir, dirPkg.ImportPath)
// Test 4: build.IsLocalImport
if !build.IsLocalImport("./foo") {
panic("expected \"./foo\" to be a local import")
// Test 7: IsLocalImport with various paths
testCases := []struct {
path string
expected bool
}{
{"./foo", true},
{"../bar", true},
{"./", true},
{"fmt", false},
{"github.com/user/repo", false},
{"", false},
}
if build.IsLocalImport("fmt") {
panic("expected \"fmt\" not to be a local import")
for _, tc := range testCases {
result := build.IsLocalImport(tc.path)
if result != tc.expected {
panic(fmt.Sprintf("build.IsLocalImport(%q): expected %v, got %v", tc.path, tc.expected, result))
}
fmt.Printf("build.IsLocalImport works correctly\n")
}
fmt.Printf("build.IsLocalImport: all test cases passed\n")
fmt.Printf("Success! All go/build public functions work with llgo\n")
// Test 8: Verify Context has expected fields
if ctx.GOPATH == "" && ctx.GOROOT == "" {
panic("expected either GOPATH or GOROOT to be set")
}
fmt.Printf("build.Default.GOROOT exists = %v\n", ctx.GOROOT != "")
// Test 9: Import with AllowBinary flag
binaryPkg, err := build.Import("fmt", "", build.FindOnly|build.AllowBinary)
if err != nil {
panic(fmt.Sprintf("build.Import with AllowBinary failed: %v", err))
}
if binaryPkg.ImportPath != "fmt" {
panic(fmt.Sprintf("expected ImportPath \"fmt\", got %q", binaryPkg.ImportPath))
}
fmt.Printf("build.Import(\"fmt\") with AllowBinary: success\n")
// Test 10: Verify compiler tag in build context
hasCompilerTag := false
for _, tag := range ctx.ReleaseTags {
if strings.HasPrefix(tag, "go1.") {
hasCompilerTag = true
break
}
}
if !hasCompilerTag {
panic("expected at least one go1.x release tag")
}
fmt.Printf("build.Default.ReleaseTags: contains go1.x tags = %v\n", hasCompilerTag)
fmt.Printf("\nSuccess! All go/build public functions work correctly with llgo\n")
fmt.Printf("Total tests passed: 10\n")
}