Skip to content

Commit a07287d

Browse files
jcchavezsdeadprogram
authored andcommitted
fix: fixes tinygo test ./... syntax.
1 parent 1b2e764 commit a07287d

File tree

6 files changed

+86
-8
lines changed

6 files changed

+86
-8
lines changed

builder/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
193193
defer machine.Dispose()
194194

195195
// Load entire program AST into memory.
196-
lprogram, err := loader.Load(config, []string{pkgName}, config.ClangHeaders, types.Config{
196+
lprogram, err := loader.Load(config, pkgName, config.ClangHeaders, types.Config{
197197
Sizes: compiler.Sizes(machine),
198198
})
199199
if err != nil {

compiler/compiler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func TestCompiler(t *testing.T) {
109109
defer machine.Dispose()
110110

111111
// Load entire program AST into memory.
112-
lprogram, err := loader.Load(config, []string{"./testdata/" + tc.file}, config.ClangHeaders, types.Config{
112+
lprogram, err := loader.Load(config, "./testdata/"+tc.file, config.ClangHeaders, types.Config{
113113
Sizes: Sizes(machine),
114114
})
115115
if err != nil {

loader/loader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ type EmbedFile struct {
104104
// Load loads the given package with all dependencies (including the runtime
105105
// package). Call .Parse() afterwards to parse all Go files (including CGo
106106
// processing, if necessary).
107-
func Load(config *compileopts.Config, inputPkgs []string, clangHeaders string, typeChecker types.Config) (*Program, error) {
107+
func Load(config *compileopts.Config, inputPkg string, clangHeaders string, typeChecker types.Config) (*Program, error) {
108108
goroot, err := GetCachedGoroot(config)
109109
if err != nil {
110110
return nil, err
@@ -133,7 +133,7 @@ func Load(config *compileopts.Config, inputPkgs []string, clangHeaders string, t
133133
if config.TestConfig.CompileTestBinary {
134134
extraArgs = append(extraArgs, "-test")
135135
}
136-
cmd, err := List(config, extraArgs, inputPkgs)
136+
cmd, err := List(config, extraArgs, []string{inputPkg})
137137
if err != nil {
138138
return nil, err
139139
}

main.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bufio"
45
"bytes"
56
"context"
67
"encoding/json"
@@ -1252,6 +1253,35 @@ func parseGoLinkFlag(flagsString string) (map[string]map[string]string, error) {
12521253
return map[string]map[string]string(globalVarValues), nil
12531254
}
12541255

1256+
// getListOfPackages returns a standard list of packages for a given list that might
1257+
// include wildards using `go list`.
1258+
// For example [./...] => ["pkg1", "pkg1/pkg12", "pkg2"]
1259+
func getListOfPackages(pkgs []string, options *compileopts.Options) ([]string, error) {
1260+
config, err := builder.NewConfig(options)
1261+
if err != nil {
1262+
return nil, err
1263+
}
1264+
cmd, err := loader.List(config, nil, pkgs)
1265+
if err != nil {
1266+
return nil, fmt.Errorf("failed to run `go list`: %w", err)
1267+
}
1268+
outputBuf := bytes.NewBuffer(nil)
1269+
cmd.Stdout = outputBuf
1270+
cmd.Stderr = os.Stderr
1271+
err = cmd.Run()
1272+
if err != nil {
1273+
return nil, err
1274+
}
1275+
1276+
var pkgNames []string
1277+
sc := bufio.NewScanner(outputBuf)
1278+
for sc.Scan() {
1279+
pkgNames = append(pkgNames, sc.Text())
1280+
}
1281+
1282+
return pkgNames, nil
1283+
}
1284+
12551285
func main() {
12561286
if len(os.Args) < 2 {
12571287
fmt.Fprintln(os.Stderr, "No command-line arguments supplied.")
@@ -1487,14 +1517,21 @@ func main() {
14871517
if len(pkgNames) == 0 {
14881518
pkgNames = []string{"."}
14891519
}
1490-
if outpath != "" && len(pkgNames) > 1 {
1520+
1521+
explicitPkgNames, err := getListOfPackages(pkgNames, options)
1522+
if err != nil {
1523+
fmt.Printf("cannot resolve packages: %v\n", err)
1524+
os.Exit(1)
1525+
}
1526+
1527+
if outpath != "" && len(explicitPkgNames) > 1 {
14911528
fmt.Println("cannot use -o flag with multiple packages")
14921529
os.Exit(1)
14931530
}
14941531

14951532
fail := make(chan struct{}, 1)
14961533
var wg sync.WaitGroup
1497-
bufs := make([]testOutputBuf, len(pkgNames))
1534+
bufs := make([]testOutputBuf, len(explicitPkgNames))
14981535
for i := range bufs {
14991536
bufs[i].done = make(chan struct{})
15001537
}
@@ -1520,7 +1557,7 @@ func main() {
15201557
// Build and run the tests concurrently.
15211558
// This uses an additional semaphore to reduce the memory usage.
15221559
testSema := make(chan struct{}, cap(options.Semaphore))
1523-
for i, pkgName := range pkgNames {
1560+
for i, pkgName := range explicitPkgNames {
15241561
pkgName := pkgName
15251562
buf := &bufs[i]
15261563
testSema <- struct{}{}

main_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"io/ioutil"
1414
"os"
1515
"os/exec"
16+
"reflect"
1617
"regexp"
1718
"runtime"
1819
"strings"
@@ -520,6 +521,46 @@ func ioLogger(t *testing.T, wg *sync.WaitGroup) io.WriteCloser {
520521
return w
521522
}
522523

524+
func TestGetListOfPackages(t *testing.T) {
525+
opts := optionsFromTarget("", sema)
526+
tests := []struct {
527+
pkgs []string
528+
expectedPkgs []string
529+
expectesError bool
530+
}{
531+
{
532+
pkgs: []string{"./tests/testing/recurse/..."},
533+
expectedPkgs: []string{
534+
"github.com/tinygo-org/tinygo/tests/testing/recurse",
535+
"github.com/tinygo-org/tinygo/tests/testing/recurse/subdir",
536+
},
537+
},
538+
{
539+
pkgs: []string{"./tests/testing/pass"},
540+
expectedPkgs: []string{
541+
"github.com/tinygo-org/tinygo/tests/testing/pass",
542+
},
543+
},
544+
{
545+
pkgs: []string{"./tests/testing"},
546+
expectesError: true,
547+
},
548+
}
549+
550+
for _, test := range tests {
551+
actualPkgs, err := getListOfPackages(test.pkgs, &opts)
552+
if err != nil && !test.expectesError {
553+
t.Errorf("unexpected error: %v", err)
554+
} else if err == nil && test.expectesError {
555+
t.Error("expected error, but got none")
556+
}
557+
558+
if !reflect.DeepEqual(test.expectedPkgs, actualPkgs) {
559+
t.Errorf("expected two slices to be equal, expected %v got %v", test.expectedPkgs, actualPkgs)
560+
}
561+
}
562+
}
563+
523564
// This TestMain is necessary because TinyGo may also be invoked to run certain
524565
// LLVM tools in a separate process. Not capturing these invocations would lead
525566
// to recursive tests.

transform/transform_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func compileGoFileForTesting(t *testing.T, filename string) llvm.Module {
146146
defer machine.Dispose()
147147

148148
// Load entire program AST into memory.
149-
lprogram, err := loader.Load(config, []string{filename}, config.ClangHeaders, types.Config{
149+
lprogram, err := loader.Load(config, filename, config.ClangHeaders, types.Config{
150150
Sizes: compiler.Sizes(machine),
151151
})
152152
if err != nil {

0 commit comments

Comments
 (0)