github.com/goplus/gossa@v0.3.25/cmd/internal/test/test.go (about)

     1  /*
     2   Copyright 2021 The GoPlus Authors (goplus.org)
     3  
     4   Licensed under the Apache License, Version 2.0 (the "License");
     5   you may not use this file except in compliance with the License.
     6   You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10   Unless required by applicable law or agreed to in writing, software
    11   distributed under the License is distributed on an "AS IS" BASIS,
    12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   See the License for the specific language governing permissions and
    14   limitations under the License.
    15  */
    16  
    17  // Package test implements the ``gossa test'' command.
    18  package test
    19  
    20  import (
    21  	"flag"
    22  	"fmt"
    23  	"log"
    24  	"strings"
    25  	"time"
    26  
    27  	"github.com/goplus/gossa"
    28  	"github.com/goplus/gossa/cmd/internal/base"
    29  	_ "github.com/goplus/gossa/pkg/testing"
    30  )
    31  
    32  // Cmd - gossa test
    33  var Cmd = &base.Command{
    34  	UsageLine: "gossa test [-v] package",
    35  	Short:     "test package",
    36  }
    37  
    38  func init() {
    39  	Cmd.Run = runCmd
    40  }
    41  
    42  func runCmd(cmd *base.Command, args []string) {
    43  	cf := &Cmd.Flag
    44  	cf.String("bench", "", "")
    45  	cf.Bool("benchmem", false, "")
    46  	cf.Duration("benchtime", 1*time.Second, "")
    47  	cf.String("blockprofile", "", "")
    48  	cf.Int("blockprofilerate", 1, "")
    49  	cf.Int("count", 1, "")
    50  	cf.String("coverprofile", "", "")
    51  	cf.String("cpu", "", "")
    52  	cf.String("cpuprofile", "", "")
    53  	cf.Bool("failfast", false, "")
    54  	cf.String("list", "", "")
    55  	cf.String("memprofile", "", "")
    56  	cf.Int("memprofilerate", 0, "")
    57  	cf.String("mutexprofile", "", "")
    58  	cf.Int("mutexprofilefraction", 1, "")
    59  	cf.String("outputdir", "", "")
    60  	cf.Int("parallel", 4, "")
    61  	cf.String("run", "", "")
    62  	cf.Bool("short", false, "")
    63  	cf.Duration("timeout", 10*time.Minute, "")
    64  	cf.String("trace", "", "")
    65  	cf.Bool("v", false, "")
    66  	for name, _ := range passFlagToTest {
    67  		cf.Var(cf.Lookup(name).Value, "test."+name, "")
    68  	}
    69  
    70  	cf.Parse(args)
    71  	pkgs := cf.Args()
    72  	if len(pkgs) == 0 {
    73  		pkgs = []string{"."}
    74  	}
    75  
    76  	var testArgs []string
    77  	cf.VisitAll(func(f *flag.Flag) {
    78  		if strings.HasPrefix(f.Name, "test.") && f.Value.String() != f.DefValue {
    79  			if typ, ok := passFlagToTest[f.Name[5:]]; ok {
    80  				switch typ {
    81  				case String:
    82  					testArgs = append(testArgs, fmt.Sprintf("-%v=%v", f.Name, f.Value))
    83  				default:
    84  					testArgs = append(testArgs, fmt.Sprintf("-%v=%v", f.Name, f.Value))
    85  				}
    86  			}
    87  		}
    88  	})
    89  	for _, pkg := range pkgs {
    90  		if err := gossa.RunTest(pkg, testArgs, 0); err != nil {
    91  			log.Println("gossa test failed:", pkg, err)
    92  		}
    93  	}
    94  }