aboutsummaryrefslogtreecommitdiffhomepage
path: root/builder/sizes_test.go
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2024-11-20 09:04:29 +0100
committerRon Evans <[email protected]>2024-11-26 12:41:12 +0100
commitee6fcd76f461543c67849a3b5b6cd5f8d16914d8 (patch)
treefdf7d25d4cbf275319f4210e3eb3ab9e29481e5f /builder/sizes_test.go
parentca80c52df19855074b8503165acf04bbe64e26bf (diff)
downloadtinygo-ee6fcd76f461543c67849a3b5b6cd5f8d16914d8.tar.gz
tinygo-ee6fcd76f461543c67849a3b5b6cd5f8d16914d8.zip
builder: add testing for -size=full
This helps to make sure this feature continues to work and we won't accidentally introduce regressions.
Diffstat (limited to 'builder/sizes_test.go')
-rw-r--r--builder/sizes_test.go88
1 files changed, 68 insertions, 20 deletions
diff --git a/builder/sizes_test.go b/builder/sizes_test.go
index bda5e0760..a1be28f27 100644
--- a/builder/sizes_test.go
+++ b/builder/sizes_test.go
@@ -1,6 +1,7 @@
package builder
import (
+ "regexp"
"runtime"
"testing"
"time"
@@ -55,26 +56,7 @@ func TestBinarySize(t *testing.T) {
t.Parallel()
// Build the binary.
- options := compileopts.Options{
- Target: tc.target,
- Opt: "z",
- Semaphore: sema,
- InterpTimeout: 60 * time.Second,
- Debug: true,
- VerifyIR: true,
- }
- target, err := compileopts.LoadTarget(&options)
- if err != nil {
- t.Fatal("could not load target:", err)
- }
- config := &compileopts.Config{
- Options: &options,
- Target: target,
- }
- result, err := Build(tc.path, "", t.TempDir(), config)
- if err != nil {
- t.Fatal("could not build:", err)
- }
+ result := buildBinary(t, tc.target, tc.path)
// Check whether the size of the binary matches the expected size.
sizes, err := loadProgramSize(result.Executable, nil)
@@ -90,3 +72,69 @@ func TestBinarySize(t *testing.T) {
})
}
}
+
+// Check that the -size=full flag attributes binary size to the correct package
+// without filesystem paths and things like that.
+func TestSizeFull(t *testing.T) {
+ tests := []string{
+ "microbit",
+ "wasip1",
+ }
+
+ libMatch := regexp.MustCompile(`^C [a-z -]+$`) // example: "C interrupt vector"
+ pkgMatch := regexp.MustCompile(`^[a-z/]+$`) // example: "internal/task"
+
+ for _, target := range tests {
+ target := target
+ t.Run(target, func(t *testing.T) {
+ t.Parallel()
+
+ // Build the binary.
+ result := buildBinary(t, target, "examples/serial")
+
+ // Check whether the binary doesn't contain any unexpected package
+ // names.
+ sizes, err := loadProgramSize(result.Executable, result.PackagePathMap)
+ if err != nil {
+ t.Fatal("could not read program size:", err)
+ }
+ for _, pkg := range sizes.sortedPackageNames() {
+ if pkg == "(padding)" || pkg == "(unknown)" {
+ // TODO: correctly attribute all unknown binary size.
+ continue
+ }
+ if libMatch.MatchString(pkg) {
+ continue
+ }
+ if pkgMatch.MatchString(pkg) {
+ continue
+ }
+ t.Error("unexpected package name in size output:", pkg)
+ }
+ })
+ }
+}
+
+func buildBinary(t *testing.T, targetString, pkgName string) BuildResult {
+ options := compileopts.Options{
+ Target: targetString,
+ Opt: "z",
+ Semaphore: sema,
+ InterpTimeout: 60 * time.Second,
+ Debug: true,
+ VerifyIR: true,
+ }
+ target, err := compileopts.LoadTarget(&options)
+ if err != nil {
+ t.Fatal("could not load target:", err)
+ }
+ config := &compileopts.Config{
+ Options: &options,
+ Target: target,
+ }
+ result, err := Build(pkgName, "", t.TempDir(), config)
+ if err != nil {
+ t.Fatal("could not build:", err)
+ }
+ return result
+}