-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmakefat_test.go
74 lines (66 loc) · 1.7 KB
/
makefat_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main_test
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
)
func TestMakeFat(t *testing.T) {
if runtime.GOOS != "darwin" {
t.Skip("works on darwin only")
}
// Make a directory to work in.
dir, err := ioutil.TempDir("", "makefat")
if err != nil {
t.Fatalf("could not create directory: %v", err)
}
defer os.RemoveAll(dir)
// List files we're working with.
src := filepath.Join(dir, "test.go")
amd64 := filepath.Join(dir, "amd64")
arm64 := filepath.Join(dir, "arm64")
fat := filepath.Join(dir, "fat")
// Create test source.
f, err := os.Create(src)
if err != nil {
t.Fatalf("could not create source file: %v", err)
}
f.Write([]byte(`
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
`))
f.Close()
// Compile test code in both amd64 and arm64.
cmd := exec.Command("go", "build", "-o", amd64, src)
cmd.Env = append(os.Environ(), "GOARCH=amd64")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("could not build amd64 target: %v\n%s\n", err, string(out))
}
cmd = exec.Command("go", "build", "-o", arm64, src)
cmd.Env = append(os.Environ(), "GOARCH=arm64")
out, err = cmd.CombinedOutput()
if err != nil {
t.Fatalf("could not build arm64 target: %v\n%s\n", err, string(out))
}
// Build fat binary.
cmd = exec.Command("go", "run", "makefat.go", fat, amd64, arm64)
out, err = cmd.CombinedOutput()
if err != nil {
t.Fatalf("could not build fat target: %v\n%s\n", err, string(out))
}
// Run fat binary.
cmd = exec.Command(fat)
out, err = cmd.CombinedOutput()
if err != nil {
t.Fatalf("could not run fat target: %v", err)
}
if string(out) != "hello world\n" {
t.Errorf("got=%s, want=hello world\n", string(out))
}
}