@@ -19,29 +19,22 @@ import (
19
19
20
20
func TestRun (t * testing.T ) {
21
21
clangBin := clangBin (t )
22
- dir := mustWriteTempFile (t , "test.c" , minimalSocketFilter )
22
+ dir := t .TempDir ()
23
+ mustWriteFile (t , dir , "test.c" , minimalSocketFilter )
23
24
24
- cwd , err := os .Getwd ()
25
- if err != nil {
26
- t .Fatal (err )
27
- }
25
+ modRoot , err := filepath .Abs ("../.." )
26
+ qt .Assert (t , qt .IsNil (err ))
28
27
29
- modRoot := filepath .Clean (filepath .Join (cwd , "../.." ))
30
28
if _ , err := os .Stat (filepath .Join (modRoot , "go.mod" )); os .IsNotExist (err ) {
31
29
t .Fatal ("No go.mod file in" , modRoot )
32
30
}
33
31
34
- tmpDir , err := os .MkdirTemp ("" , "bpf2go-module-*" )
35
- if err != nil {
36
- t .Fatal (err )
37
- }
38
- defer os .RemoveAll (tmpDir )
39
-
32
+ modDir := t .TempDir ()
40
33
execInModule := func (name string , args ... string ) {
41
34
t .Helper ()
42
35
43
36
cmd := exec .Command (name , args ... )
44
- cmd .Dir = tmpDir
37
+ cmd .Dir = modDir
45
38
if out , err := cmd .CombinedOutput (); err != nil {
46
39
if out := string (out ); out != "" {
47
40
t .Log (out )
@@ -62,8 +55,15 @@ func TestRun(t *testing.T) {
62
55
fmt .Sprintf ("-replace=%s=%s" , module , modRoot ),
63
56
)
64
57
65
- err = run (io .Discard , "foo" , tmpDir , []string {
58
+ goarches := []string {
59
+ "amd64" , // little-endian
60
+ "arm64" ,
61
+ "s390x" , // big-endian
62
+ }
63
+
64
+ err = run (io .Discard , "main" , modDir , []string {
66
65
"-cc" , clangBin ,
66
+ "-target" , strings .Join (goarches , "," ),
67
67
"bar" ,
68
68
filepath .Join (dir , "test.c" ),
69
69
})
@@ -72,18 +72,24 @@ func TestRun(t *testing.T) {
72
72
t .Fatal ("Can't run:" , err )
73
73
}
74
74
75
- for _ , arch := range []string {
76
- "amd64" , // little-endian
77
- "s390x" , // big-endian
78
- } {
75
+ mustWriteFile (t , modDir , "main.go" ,
76
+ `
77
+ package main
78
+
79
+ func main() {
80
+ var obj barObjects
81
+ println(obj.Main)
82
+ }` )
83
+
84
+ for _ , arch := range goarches {
79
85
t .Run (arch , func (t * testing.T ) {
80
- goBin := exec .Command ("go" , "build" , "-mod=mod" )
81
- goBin .Dir = tmpDir
82
- goBin .Env = append (os .Environ (),
86
+ goBuild := exec .Command ("go" , "build" , "-mod=mod" , "-o" , "/dev/null " )
87
+ goBuild .Dir = modDir
88
+ goBuild .Env = append (os .Environ (),
83
89
"GOOS=linux" ,
84
90
"GOARCH=" + arch ,
85
91
)
86
- out , err := goBin .CombinedOutput ()
92
+ out , err := goBuild .CombinedOutput ()
87
93
if err != nil {
88
94
if out := string (out ); out != "" {
89
95
t .Log (out )
@@ -112,7 +118,8 @@ func TestErrorMentionsEnvVar(t *testing.T) {
112
118
}
113
119
114
120
func TestDisableStripping (t * testing.T ) {
115
- dir := mustWriteTempFile (t , "test.c" , minimalSocketFilter )
121
+ dir := t .TempDir ()
122
+ mustWriteFile (t , dir , "test.c" , minimalSocketFilter )
116
123
117
124
err := run (io .Discard , "foo" , dir , []string {
118
125
"-cc" , clangBin (t ),
@@ -213,7 +220,7 @@ func TestCollectTargetsErrors(t *testing.T) {
213
220
target string
214
221
}{
215
222
{"unknown" , "frood" },
216
- {"no linux target" , "mips64p32le " },
223
+ {"no linux target" , "mipsle " },
217
224
}
218
225
219
226
for _ , test := range tests {
@@ -228,7 +235,8 @@ func TestCollectTargetsErrors(t *testing.T) {
228
235
}
229
236
230
237
func TestConvertGOARCH (t * testing.T ) {
231
- tmp := mustWriteTempFile (t , "test.c" ,
238
+ tmp := t .TempDir ()
239
+ mustWriteFile (t , tmp , "test.c" ,
232
240
`
233
241
#ifndef __TARGET_ARCH_x86
234
242
#error __TARGET_ARCH_x86 is not defined
@@ -432,22 +440,41 @@ func TestParseArgs(t *testing.T) {
432
440
}
433
441
434
442
func TestGoarches (t * testing.T ) {
435
- exe , err := exec .LookPath ("go" )
436
- if errors .Is (err , exec .ErrNotFound ) {
437
- t .Skip ("go binary is not in PATH" )
438
- }
439
- qt .Assert (t , qt .IsNil (err ))
443
+ exe := goBin (t )
440
444
441
445
for goarch := range targetByGoArch {
442
446
t .Run (string (goarch ), func (t * testing.T ) {
443
447
goEnv := exec .Command (exe , "env" )
444
- goEnv .Env = []string {"GOOS=linux" , "GOARCH=" + string (goarch )}
448
+ goEnv .Env = []string {"GOROOT=/" , " GOOS=linux" , "GOARCH=" + string (goarch )}
445
449
output , err := goEnv .CombinedOutput ()
446
450
qt .Assert (t , qt .IsNil (err ), qt .Commentf ("go output is:\n %s" , string (output )))
447
451
})
448
452
}
449
453
}
450
454
455
+ func TestClangTargets (t * testing.T ) {
456
+ exe := goBin (t )
457
+
458
+ clangTargets := map [string ]struct {}{}
459
+ for _ , tgt := range targetByGoArch {
460
+ clangTargets [tgt .clang ] = struct {}{}
461
+ }
462
+
463
+ for target := range clangTargets {
464
+ for _ , env := range []string {"GOOS" , "GOARCH" } {
465
+ env += "=" + target
466
+ t .Run (env , func (t * testing.T ) {
467
+ goEnv := exec .Command (exe , "env" )
468
+ goEnv .Env = []string {"GOROOT=/" , env }
469
+ output , err := goEnv .CombinedOutput ()
470
+ t .Log ("go output is:" , string (output ))
471
+ qt .Assert (t , qt .IsNotNil (err ), qt .Commentf ("No clang target should be a valid build constraint" ))
472
+ })
473
+ }
474
+
475
+ }
476
+ }
477
+
451
478
func clangBin (t * testing.T ) string {
452
479
t .Helper ()
453
480
@@ -465,3 +492,15 @@ func clangBin(t *testing.T) string {
465
492
t .Log ("Testing against" , clang )
466
493
return clang
467
494
}
495
+
496
+ func goBin (t * testing.T ) string {
497
+ t .Helper ()
498
+
499
+ exe , err := exec .LookPath ("go" )
500
+ if errors .Is (err , exec .ErrNotFound ) {
501
+ t .Skip ("go binary is not in PATH" )
502
+ }
503
+ qt .Assert (t , qt .IsNil (err ))
504
+
505
+ return exe
506
+ }
0 commit comments