gitlab.com/polyapp-open-source/poly@v0.0.0-20200304172929-90b164ae7520/cmd/test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"os/exec"
     8  
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  // testCmd represents the test command
    13  //nolint
    14  var testCmd = &cobra.Command{
    15  	Use:   "test",
    16  	Short: "Runs 'go test' across all main.go files",
    17  	Long: `go test is run normally and also run with GOOS=js and GOARCH=wasm.
    18  GOOS=js and GOARCH=wasm uses the default go_js_wasm_exec program. If
    19  no such program is installed the user is notified and wasmbrowsertest is recommended.
    20  GOOS=js and GOARCH=wasm makes an effort to not test code which could rely on non-js syscalls.
    21  Results are collected and printed sequentially. Example output:
    22  
    23  $ poly test
    24  ----- Command: C:\Go\bin\go.exe test gitlab.com/polyapp-open-source/poly/testdata/cmd ----- GOOS: windows GOARCH:  -----
    25  ?       gitlab.com/polyapp-open-source/poly/testdata/cmd        [no test files]
    26  ----- Command: C:\Go\bin\go.exe test gitlab.com/polyapp-open-source/poly/testdata/cmd/hello ----- GOOS: windows GOARCH:  -----
    27  ok      gitlab.com/polyapp-open-source/poly/testdata/cmd/hello  (cached)
    28  ----- Command: C:\Go\bin\go.exe test gitlab.com/polyapp-open-source/poly/testdata/cmd/wasm1 ----- GOOS: js GOARCH: wasm -----
    29  ?       gitlab.com/polyapp-open-source/poly/testdata/cmd/wasm1  [no test files]
    30  ----- Command: C:\Go\bin\go.exe test gitlab.com/polyapp-open-source/poly/testdata/cmd/wasm2 ----- GOOS: js GOARCH: wasm -----
    31  ?       gitlab.com/polyapp-open-source/poly/testdata/cmd/wasm2  [no test files]
    32  ----- Command: C:\Go\bin\go.exe test gitlab.com/polyapp-open-source/poly/testdata/cmd/wasm3 ----- GOOS: js GOARCH: wasm -----
    33  ?       gitlab.com/polyapp-open-source/poly/testdata/cmd/wasm3  [no test files]
    34  `,
    35  	Run: func(cmd *cobra.Command, args []string) {
    36  		exitCode := Test()
    37  		os.Exit(exitCode)
    38  	},
    39  }
    40  
    41  func init() {
    42  	rootCmd.AddCommand(testCmd)
    43  }
    44  
    45  // Test performs the work of 'poly test'.
    46  func Test() (exitCode int) {
    47  	defaultTargets, JSWASMTargets := GetTargets()
    48  
    49  	defaultProcs := make([]*procInfo, len(defaultTargets))
    50  	for i, targetPath := range defaultTargets {
    51  		defaultProc := procInfo{
    52  			Cmd: exec.Command("go", "test", targetPath),
    53  		}
    54  		defaultProc.Cmd.Stdout = &defaultProc.Stdout
    55  		defaultProc.Cmd.Stderr = &defaultProc.Stderr
    56  		defaultProc.StartError = defaultProc.Cmd.Start()
    57  		defaultProcs[i] = &defaultProc
    58  	}
    59  
    60  	JSWASMProcs := make([]*procInfo, len(JSWASMTargets))
    61  	for i, targetPath := range JSWASMTargets {
    62  		JSWASMProc := procInfo{
    63  			Cmd: exec.Command("go", "test", targetPath),
    64  		}
    65  		JSWASMProc.Cmd.Env = append(os.Environ(), "GOOS=js", "GOARCH=wasm")
    66  		JSWASMProc.Cmd.Stdout = &JSWASMProc.Stdout
    67  		JSWASMProc.Cmd.Stderr = &JSWASMProc.Stderr
    68  		JSWASMProc.StartError = JSWASMProc.Cmd.Start()
    69  		JSWASMProcs[i] = &JSWASMProc
    70  	}
    71  
    72  	largestExitCode := 0
    73  	for _, proc := range defaultProcs {
    74  		proc.WaitError = proc.Cmd.Wait()
    75  		proc.print()
    76  		osExitValue := proc.getExitValue()
    77  		if osExitValue > largestExitCode {
    78  			largestExitCode = osExitValue
    79  		}
    80  	}
    81  	for _, proc := range JSWASMProcs {
    82  		if bytes.Contains(proc.Stdout.Bytes(), []byte("fork/exec")) {
    83  			fmt.Println("----- Likely User Error Detected -----")
    84  			fmt.Printf("Didn't see a go_js_wasm_exec. Run these commands:\n" +
    85  				"go get github.com/agnivade/wasmbrowsertest\ncd $GOPATH/bin\n" +
    86  				"mv wasmbrowsertest go_js_wasm_exec\n# add $GOBIN to $PATH if not already done\n" +
    87  				"# Then this application should pass")
    88  		}
    89  		proc.WaitError = proc.Cmd.Wait()
    90  		proc.print()
    91  		osExitValue := proc.getExitValue()
    92  		if osExitValue > largestExitCode {
    93  			largestExitCode = osExitValue
    94  		}
    95  	}
    96  
    97  	return largestExitCode
    98  }