1
1
package builder
2
2
3
3
import (
4
+ "regexp"
4
5
"runtime"
5
6
"testing"
6
7
"time"
@@ -55,26 +56,7 @@ func TestBinarySize(t *testing.T) {
55
56
t .Parallel ()
56
57
57
58
// Build the binary.
58
- options := compileopts.Options {
59
- Target : tc .target ,
60
- Opt : "z" ,
61
- Semaphore : sema ,
62
- InterpTimeout : 60 * time .Second ,
63
- Debug : true ,
64
- VerifyIR : true ,
65
- }
66
- target , err := compileopts .LoadTarget (& options )
67
- if err != nil {
68
- t .Fatal ("could not load target:" , err )
69
- }
70
- config := & compileopts.Config {
71
- Options : & options ,
72
- Target : target ,
73
- }
74
- result , err := Build (tc .path , "" , t .TempDir (), config )
75
- if err != nil {
76
- t .Fatal ("could not build:" , err )
77
- }
59
+ result := buildBinary (t , tc .target , tc .path )
78
60
79
61
// Check whether the size of the binary matches the expected size.
80
62
sizes , err := loadProgramSize (result .Executable , nil )
@@ -90,3 +72,69 @@ func TestBinarySize(t *testing.T) {
90
72
})
91
73
}
92
74
}
75
+
76
+ // Check that the -size=full flag attributes binary size to the correct package
77
+ // without filesystem paths and things like that.
78
+ func TestSizeFull (t * testing.T ) {
79
+ tests := []string {
80
+ "microbit" ,
81
+ "wasip1" ,
82
+ }
83
+
84
+ libMatch := regexp .MustCompile (`^C [a-z -]+$` ) // example: "C interrupt vector"
85
+ pkgMatch := regexp .MustCompile (`^[a-z/]+$` ) // example: "internal/task"
86
+
87
+ for _ , target := range tests {
88
+ target := target
89
+ t .Run (target , func (t * testing.T ) {
90
+ t .Parallel ()
91
+
92
+ // Build the binary.
93
+ result := buildBinary (t , target , "examples/serial" )
94
+
95
+ // Check whether the binary doesn't contain any unexpected package
96
+ // names.
97
+ sizes , err := loadProgramSize (result .Executable , result .PackagePathMap )
98
+ if err != nil {
99
+ t .Fatal ("could not read program size:" , err )
100
+ }
101
+ for _ , pkg := range sizes .sortedPackageNames () {
102
+ if pkg == "(padding)" || pkg == "(unknown)" {
103
+ // TODO: correctly attribute all unknown binary size.
104
+ continue
105
+ }
106
+ if libMatch .MatchString (pkg ) {
107
+ continue
108
+ }
109
+ if pkgMatch .MatchString (pkg ) {
110
+ continue
111
+ }
112
+ t .Error ("unexpected package name in size output:" , pkg )
113
+ }
114
+ })
115
+ }
116
+ }
117
+
118
+ func buildBinary (t * testing.T , targetString , pkgName string ) BuildResult {
119
+ options := compileopts.Options {
120
+ Target : targetString ,
121
+ Opt : "z" ,
122
+ Semaphore : sema ,
123
+ InterpTimeout : 60 * time .Second ,
124
+ Debug : true ,
125
+ VerifyIR : true ,
126
+ }
127
+ target , err := compileopts .LoadTarget (& options )
128
+ if err != nil {
129
+ t .Fatal ("could not load target:" , err )
130
+ }
131
+ config := & compileopts.Config {
132
+ Options : & options ,
133
+ Target : target ,
134
+ }
135
+ result , err := Build (pkgName , "" , t .TempDir (), config )
136
+ if err != nil {
137
+ t .Fatal ("could not build:" , err )
138
+ }
139
+ return result
140
+ }
0 commit comments