From 7ec5599c57ecdff1b046cea4d6ce7587ae6c2b30 Mon Sep 17 00:00:00 2001 From: seph Date: Wed, 8 May 2019 22:18:09 -0400 Subject: [PATCH 1/4] Small test improvements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add an ENV the makefile will parse to add `-short` to the `go test` options Add llvm bin directory to the PATH Enable parallel tests. This required moving tmpfile creation into `runTest`, instead of being shared. Caveat — I’m on a osx laptop. I can run the short tests successfully, but I get clang errors no matter what I do. So this _probably_ doesn’t make things worse, but I can’t test all the permutations --- Makefile | 12 +++++++++++- main_test.go | 30 ++++++++++++++++-------------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index ea96730aa7..8e0ab4759a 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ tinygo: build/tinygo .PHONY: all tinygo build/tinygo test llvm-build llvm-source clean fmt gen-device gen-device-nrf gen-device-avr # Default build and source directories, as created by `make llvm-build`. +BASE_DIR ?= $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) LLVM_BUILDDIR ?= llvm-build CLANG_SRC ?= llvm/tools/clang LLD_SRC ?= llvm/tools/lld @@ -22,6 +23,9 @@ CLANG_LIBS = $(START_GROUP) $(abspath $(LLVM_BUILDDIR))/lib/libclang.a -lclangAn LLD_LIBS = $(START_GROUP) -llldCOFF -llldCommon -llldCore -llldDriver -llldELF -llldMachO -llldMinGW -llldReaderWriter -llldWasm -llldYAML $(END_GROUP) +# Set path to include the llvm build +PATH := $(PATH):$(BASE_DIR)/$(LLVM_BUILDDIR)/bin + # For static linking. CGO_CPPFLAGS=$(shell $(LLVM_BUILDDIR)/bin/llvm-config --cppflags) -I$(abspath $(CLANG_SRC))/include -I$(abspath $(LLD_SRC))/include @@ -81,8 +85,14 @@ build/tinygo: @if [ ! -f llvm-build/bin/llvm-config ]; then echo "Fetch and build LLVM first by running:\n make llvm-source\n make llvm-build"; exit 1; fi CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" go build -o build/tinygo -tags byollvm . + +# `TESTSHORT=1 make test` will run the tests in short mode. This is a +# ternary on whether $(TESTSHORT) is empty. There are no quotes here, +# because make syntax is weird +test: SHORTFLAG = $(if $(TESTSHORT), -short, ) test: - CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" go test -v -tags byollvm . + CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" go test -v $(SHORTFLAG) -tags byollvm . + release: build/tinygo gen-device @mkdir -p build/release/tinygo/bin diff --git a/main_test.go b/main_test.go index fa85a82c81..591228c17a 100644 --- a/main_test.go +++ b/main_test.go @@ -38,17 +38,10 @@ func TestCompiler(t *testing.T) { sort.Strings(matches) - // Create a temporary directory for test output files. - tmpdir, err := ioutil.TempDir("", "tinygo-test") - if err != nil { - t.Fatal("could not create temporary directory:", err) - } - defer os.RemoveAll(tmpdir) - t.Log("running tests on host...") for _, path := range matches { t.Run(path, func(t *testing.T) { - runTest(path, tmpdir, "", t) + runTest(path, "", t) }) } @@ -59,7 +52,7 @@ func TestCompiler(t *testing.T) { t.Log("running tests for emulated cortex-m3...") for _, path := range matches { t.Run(path, func(t *testing.T) { - runTest(path, tmpdir, "qemu", t) + runTest(path, "qemu", t) }) } @@ -70,7 +63,7 @@ func TestCompiler(t *testing.T) { continue // TODO: improve CGo } t.Run(path, func(t *testing.T) { - runTest(path, tmpdir, "arm--linux-gnueabihf", t) + runTest(path, "arm--linux-gnueabihf", t) }) } @@ -80,7 +73,7 @@ func TestCompiler(t *testing.T) { continue // TODO: improve CGo } t.Run(path, func(t *testing.T) { - runTest(path, tmpdir, "aarch64--linux-gnu", t) + runTest(path, "aarch64--linux-gnu", t) }) } @@ -90,13 +83,21 @@ func TestCompiler(t *testing.T) { continue // known to fail } t.Run(path, func(t *testing.T) { - runTest(path, tmpdir, "wasm", t) + runTest(path, "wasm", t) }) } } } -func runTest(path, tmpdir string, target string, t *testing.T) { +func runTest(path string, target string, t *testing.T) { + t.Parallel() + + tmpFile, err := ioutil.TempFile("", "tinygo-test-build") + if err != nil { + t.Fatal("could not create temporary directory:", err) + } + defer os.Remove(tmpFile.Name()) + // Get the expected output for this test. txtpath := path[:len(path)-3] + ".txt" if path[len(path)-1] == os.PathSeparator { @@ -120,7 +121,8 @@ func runTest(path, tmpdir string, target string, t *testing.T) { printSizes: "", wasmAbi: "js", } - binary := filepath.Join(tmpdir, "test") + + binary := tmpFile.Name() err = Build("./"+path, binary, target, config) if err != nil { if errLoader, ok := err.(loader.Errors); ok { From 2f7cdbed3a9da97fdf65014f064f0a26a452eba6 Mon Sep 17 00:00:00 2001 From: seph Date: Thu, 9 May 2019 00:55:33 -0400 Subject: [PATCH 2/4] fix error --- main_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main_test.go b/main_test.go index 591228c17a..28cfece1d4 100644 --- a/main_test.go +++ b/main_test.go @@ -94,7 +94,7 @@ func runTest(path string, target string, t *testing.T) { tmpFile, err := ioutil.TempFile("", "tinygo-test-build") if err != nil { - t.Fatal("could not create temporary directory:", err) + t.Fatal("could not create temporary file:", err) } defer os.Remove(tmpFile.Name()) From 39034bb2741f2bceaea0761393b9d4435aec7a9e Mon Sep 17 00:00:00 2001 From: seph Date: Thu, 9 May 2019 01:43:38 -0400 Subject: [PATCH 3/4] Change test target enumeration --- main_test.go | 68 +++++++++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 41 deletions(-) diff --git a/main_test.go b/main_test.go index 28cfece1d4..861a51bfe2 100644 --- a/main_test.go +++ b/main_test.go @@ -38,60 +38,46 @@ func TestCompiler(t *testing.T) { sort.Strings(matches) - t.Log("running tests on host...") - for _, path := range matches { - t.Run(path, func(t *testing.T) { - runTest(path, "", t) - }) - } + targets := []string{""} // always test the native host - if testing.Short() { - return + if !testing.Short() { + targets = append(targets, "qemu") // emulated cortex-m3 } - t.Log("running tests for emulated cortex-m3...") - for _, path := range matches { - t.Run(path, func(t *testing.T) { - runTest(path, "qemu", t) - }) + if !testing.Short() && runtime.GOOS == "linux" { + targets = append(targets, + "arm--linux-gnueabihf", // emulated linux/arm + "aarch64--linux-gnu", // "linux/arm" + "wasm", // "WebAssembly" + ) } - if runtime.GOOS == "linux" { - t.Log("running tests for linux/arm...") - for _, path := range matches { - if path == filepath.Join("testdata", "cgo")+string(filepath.Separator) { - continue // TODO: improve CGo - } - t.Run(path, func(t *testing.T) { - runTest(path, "arm--linux-gnueabihf", t) - }) - } - - t.Log("running tests for linux/arm64...") - for _, path := range matches { - if path == filepath.Join("testdata", "cgo")+string(filepath.Separator) { - continue // TODO: improve CGo - } - t.Run(path, func(t *testing.T) { - runTest(path, "aarch64--linux-gnu", t) - }) - } - - t.Log("running tests for WebAssembly...") - for _, path := range matches { - if path == filepath.Join("testdata", "gc.go") { - continue // known to fail - } - t.Run(path, func(t *testing.T) { - runTest(path, "wasm", t) + for _, path := range matches { + for _, target := range targets { + testName := filepath.Join(target, path) + t.Run(testName, func(t *testing.T) { + runTest(path, target, t) }) } } + } func runTest(path string, target string, t *testing.T) { t.Parallel() + if target == "arm--linux-gnueabihf" && path == "testdata/cgo/" { + t.Skip("TODO: improve CGo") + } + + if target == "aarch64--linux-gnu" && path == "testdata/cgo/" { + t.Skip("TODO: improve CGo") + } + + if target == "wasm" && path == "testdata/gc.go" { + t.Skip("known to fail") + } + tmpFile, err := ioutil.TempFile("", "tinygo-test-build") if err != nil { t.Fatal("could not create temporary file:", err) From 302564eb51c9dba0a0a5e9c0e03340ff1002d231 Mon Sep 17 00:00:00 2001 From: seph Date: Thu, 9 May 2019 10:02:15 -0400 Subject: [PATCH 4/4] Use HasPrefix for better test detection --- main_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/main_test.go b/main_test.go index 861a51bfe2..006707ac5e 100644 --- a/main_test.go +++ b/main_test.go @@ -12,6 +12,7 @@ import ( "path/filepath" "runtime" "sort" + "strings" "testing" "github.com/tinygo-org/tinygo/loader" @@ -66,15 +67,15 @@ func TestCompiler(t *testing.T) { func runTest(path string, target string, t *testing.T) { t.Parallel() - if target == "arm--linux-gnueabihf" && path == "testdata/cgo/" { + if target == "arm--linux-gnueabihf" && strings.HasPrefix(path, "testdata/cgo/") { t.Skip("TODO: improve CGo") } - if target == "aarch64--linux-gnu" && path == "testdata/cgo/" { + if target == "aarch64--linux-gnu" && strings.HasPrefix(path, "testdata/cgo/") { t.Skip("TODO: improve CGo") } - if target == "wasm" && path == "testdata/gc.go" { + if target == "wasm" && strings.HasPrefix(path, "testdata/gc.go") { t.Skip("known to fail") }