44 "fmt"
55 "go/build"
66 "runtime"
7+ "strings"
78)
89
910func main () {
@@ -26,28 +27,121 @@ func main() {
2627 }
2728 fmt .Printf ("build.Default.ReleaseTags count = %d\n " , len (ctx .ReleaseTags ))
2829
29- // Test 2: build.Import with FindOnly
30+ // Validate GOOS and GOARCH are set
31+ if ctx .GOOS == "" {
32+ panic ("expected build.Default.GOOS to be non-empty" )
33+ }
34+ if ctx .GOARCH == "" {
35+ panic ("expected build.Default.GOARCH to be non-empty" )
36+ }
37+ fmt .Printf ("build.Default.GOOS = %q, GOARCH = %q\n " , ctx .GOOS , ctx .GOARCH )
38+
39+ // Test 2: Import standard library package with FindOnly
3040 pkg , err := build .Import ("fmt" , "" , build .FindOnly )
3141 if err != nil {
32- panic (fmt .Sprintf ("build.Import failed: %v" , err ))
42+ panic (fmt .Sprintf ("build.Import(\" fmt\" ) failed: %v" , err ))
43+ }
44+ if pkg .ImportPath != "fmt" {
45+ panic (fmt .Sprintf ("expected ImportPath \" fmt\" , got %q" , pkg .ImportPath ))
46+ }
47+ if ! pkg .Goroot {
48+ panic ("expected fmt package to be in GOROOT" )
49+ }
50+ fmt .Printf ("build.Import(\" fmt\" ): ImportPath=%s, Goroot=%v\n " , pkg .ImportPath , pkg .Goroot )
51+
52+ // Test 3: Import nested standard library package
53+ osPkg , err := build .Import ("os/exec" , "" , build .FindOnly )
54+ if err != nil {
55+ panic (fmt .Sprintf ("build.Import(\" os/exec\" ) failed: %v" , err ))
56+ }
57+ if osPkg .ImportPath != "os/exec" {
58+ panic (fmt .Sprintf ("expected ImportPath \" os/exec\" , got %q" , osPkg .ImportPath ))
59+ }
60+ if ! osPkg .Goroot {
61+ panic ("expected os/exec package to be in GOROOT" )
62+ }
63+ fmt .Printf ("build.Import(\" os/exec\" ): ImportPath=%s, Goroot=%v\n " , osPkg .ImportPath , osPkg .Goroot )
64+
65+ // Test 4: Import internal package (should succeed with FindOnly)
66+ internalPkg , err := build .Import ("internal/cpu" , "" , build .FindOnly )
67+ if err != nil {
68+ panic (fmt .Sprintf ("build.Import(\" internal/cpu\" ) failed: %v" , err ))
69+ }
70+ if internalPkg .ImportPath != "internal/cpu" {
71+ panic (fmt .Sprintf ("expected ImportPath \" internal/cpu\" , got %q" , internalPkg .ImportPath ))
72+ }
73+ fmt .Printf ("build.Import(\" internal/cpu\" ): ImportPath=%s\n " , internalPkg .ImportPath )
74+
75+ // Test 5: Import with srcDir parameter
76+ runtimePkg , err := build .Import ("runtime" , "" , build .FindOnly )
77+ if err != nil {
78+ panic (fmt .Sprintf ("build.Import(\" runtime\" ) failed: %v" , err ))
79+ }
80+ if runtimePkg .ImportPath != "runtime" {
81+ panic (fmt .Sprintf ("expected ImportPath \" runtime\" , got %q" , runtimePkg .ImportPath ))
3382 }
34- fmt .Printf ("build.Import(\" fmt\" ): %s\n " , pkg .ImportPath )
83+ if runtimePkg .Dir == "" {
84+ panic ("expected runtime package Dir to be non-empty" )
85+ }
86+ fmt .Printf ("build.Import(\" runtime\" ): ImportPath=%s, Dir exists=%v\n " , runtimePkg .ImportPath , runtimePkg .Dir != "" )
3587
36- // Test 3: build. ImportDir
88+ // Test 6: ImportDir with current directory
3789 dirPkg , err := build .ImportDir ("." , build .FindOnly )
3890 if err != nil {
39- panic (fmt .Sprintf ("build.ImportDir failed: %v" , err ))
91+ panic (fmt .Sprintf ("build.ImportDir(\" .\" ) failed: %v" , err ))
92+ }
93+ // Note: Name might be empty with FindOnly mode as it doesn't read source files
94+ fmt .Printf ("build.ImportDir(\" .\" ): Dir=%s, ImportPath=%s\n " , dirPkg .Dir , dirPkg .ImportPath )
95+
96+ // Test 7: IsLocalImport with various paths
97+ testCases := []struct {
98+ path string
99+ expected bool
100+ }{
101+ {"./foo" , true },
102+ {"../bar" , true },
103+ {"./" , true },
104+ {"fmt" , false },
105+ {"github.com/user/repo" , false },
106+ {"" , false },
107+ }
108+ for _ , tc := range testCases {
109+ result := build .IsLocalImport (tc .path )
110+ if result != tc .expected {
111+ panic (fmt .Sprintf ("build.IsLocalImport(%q): expected %v, got %v" , tc .path , tc .expected , result ))
112+ }
113+ }
114+ fmt .Printf ("build.IsLocalImport: all test cases passed\n " )
115+
116+ // Test 8: Verify Context has expected fields
117+ if ctx .GOPATH == "" && ctx .GOROOT == "" {
118+ panic ("expected either GOPATH or GOROOT to be set" )
119+ }
120+ fmt .Printf ("build.Default.GOROOT exists = %v\n " , ctx .GOROOT != "" )
121+
122+ // Test 9: Import with AllowBinary flag
123+ binaryPkg , err := build .Import ("fmt" , "" , build .FindOnly | build .AllowBinary )
124+ if err != nil {
125+ panic (fmt .Sprintf ("build.Import with AllowBinary failed: %v" , err ))
126+ }
127+ if binaryPkg .ImportPath != "fmt" {
128+ panic (fmt .Sprintf ("expected ImportPath \" fmt\" , got %q" , binaryPkg .ImportPath ))
40129 }
41- fmt .Printf ("build.ImportDir (\" . \" ): %s \n " , dirPkg . Name )
130+ fmt .Printf ("build.Import (\" fmt \" ) with AllowBinary: success \n " )
42131
43- // Test 4: build.IsLocalImport
44- if ! build .IsLocalImport ("./foo" ) {
45- panic ("expected \" ./foo\" to be a local import" )
132+ // Test 10: Verify compiler tag in build context
133+ hasCompilerTag := false
134+ for _ , tag := range ctx .ReleaseTags {
135+ if strings .HasPrefix (tag , "go1." ) {
136+ hasCompilerTag = true
137+ break
138+ }
46139 }
47- if build . IsLocalImport ( "fmt" ) {
48- panic ("expected \" fmt \" not to be a local import " )
140+ if ! hasCompilerTag {
141+ panic ("expected at least one go1.x release tag " )
49142 }
50- fmt .Printf ("build.IsLocalImport works correctly \n " )
143+ fmt .Printf ("build.Default.ReleaseTags: contains go1.x tags = %v \n " , hasCompilerTag )
51144
52- fmt .Printf ("Success! All go/build public functions work with llgo\n " )
145+ fmt .Printf ("\n Success! All go/build public functions work correctly with llgo\n " )
146+ fmt .Printf ("Total tests passed: 10\n " )
53147}
0 commit comments