Skip to content

Small test improvements #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
85 changes: 37 additions & 48 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"
"runtime"
"sort"
"strings"
"testing"

"github.com/tinygo-org/tinygo/loader"
Expand All @@ -38,65 +39,52 @@ 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)
targets := []string{""} // always test the native host

t.Log("running tests on host...")
for _, path := range matches {
t.Run(path, func(t *testing.T) {
runTest(path, tmpdir, "", t)
})
if !testing.Short() {
targets = append(targets, "qemu") // emulated cortex-m3
}

if testing.Short() {
return
if !testing.Short() && runtime.GOOS == "linux" {
targets = append(targets,
"arm--linux-gnueabihf", // emulated linux/arm
"aarch64--linux-gnu", // "linux/arm"
"wasm", // "WebAssembly"
)
}

t.Log("running tests for emulated cortex-m3...")
for _, path := range matches {
t.Run(path, func(t *testing.T) {
runTest(path, tmpdir, "qemu", t)
})
}

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, tmpdir, "arm--linux-gnueabihf", t)
for _, target := range targets {
testName := filepath.Join(target, path)
t.Run(testName, func(t *testing.T) {
runTest(path, target, 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, tmpdir, "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, tmpdir, "wasm", t)
})
}
func runTest(path string, target string, t *testing.T) {
t.Parallel()

if target == "arm--linux-gnueabihf" && strings.HasPrefix(path, "testdata/cgo/") {
t.Skip("TODO: improve CGo")
}

if target == "aarch64--linux-gnu" && strings.HasPrefix(path, "testdata/cgo/") {
t.Skip("TODO: improve CGo")
}
}

func runTest(path, tmpdir string, target string, t *testing.T) {
if target == "wasm" && strings.HasPrefix(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)
}
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 {
Expand All @@ -120,7 +108,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 {
Expand Down