github.com/goplus/igop@v0.25.0/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 “igop test” command.
    18  package test
    19  
    20  import (
    21  	"flag"
    22  	"fmt"
    23  	"os"
    24  	"strings"
    25  	"time"
    26  
    27  	"github.com/goplus/igop"
    28  	"github.com/goplus/igop/cmd/internal/base"
    29  )
    30  
    31  // Cmd - igop test
    32  var Cmd = &base.Command{
    33  	UsageLine: "igop test [build/test flags] [package]",
    34  	Short:     "test a package",
    35  }
    36  
    37  func init() {
    38  	Cmd.Run = runCmd
    39  	base.AddBuildFlags(Cmd, base.OmitModFlag|base.OmitSSAFlag|base.OmitSSATraceFlag|
    40  		base.OmitVFlag|base.OmitExperimentalGCFlag)
    41  }
    42  
    43  func runCmd(cmd *base.Command, args []string) {
    44  	cf := &Cmd.Flag
    45  	cf.String("bench", "", "")
    46  	cf.Bool("benchmem", false, "")
    47  	cf.Duration("benchtime", 1*time.Second, "")
    48  	cf.String("blockprofile", "", "")
    49  	cf.Int("blockprofilerate", 1, "")
    50  	cf.Int("count", 1, "")
    51  	cf.String("coverprofile", "", "")
    52  	cf.String("cpu", "", "")
    53  	cf.String("cpuprofile", "", "")
    54  	cf.Bool("failfast", false, "")
    55  	cf.String("list", "", "")
    56  	cf.String("memprofile", "", "")
    57  	cf.Int("memprofilerate", 0, "")
    58  	cf.String("mutexprofile", "", "")
    59  	cf.Int("mutexprofilefraction", 1, "")
    60  	cf.String("outputdir", "", "")
    61  	cf.Int("parallel", 4, "")
    62  	cf.String("run", "", "")
    63  	cf.Bool("short", false, "")
    64  	cf.Duration("timeout", 10*time.Minute, "")
    65  	cf.String("trace", "", "")
    66  	for name, _ := range passFlagToTest {
    67  		cf.Var(cf.Lookup(name).Value, "test."+name, "")
    68  	}
    69  
    70  	err := cf.Parse(args)
    71  	if err != nil {
    72  		os.Exit(2)
    73  		return
    74  	}
    75  	paths := cf.Args()
    76  	if len(paths) == 0 {
    77  		paths = []string{"."}
    78  	}
    79  	path := paths[0]
    80  	var testArgs []string
    81  	cf.VisitAll(func(f *flag.Flag) {
    82  		if strings.HasPrefix(f.Name, "test.") && f.Value.String() != f.DefValue {
    83  			if typ, ok := passFlagToTest[f.Name[5:]]; ok {
    84  				switch typ {
    85  				case String:
    86  					testArgs = append(testArgs, fmt.Sprintf("-%v=%v", f.Name, f.Value))
    87  				default:
    88  					testArgs = append(testArgs, fmt.Sprintf("-%v=%v", f.Name, f.Value))
    89  				}
    90  			}
    91  		}
    92  	})
    93  	var mode igop.Mode
    94  	if base.BuildSSA {
    95  		mode |= igop.EnableDumpInstr
    96  	}
    97  	if base.DebugSSATrace {
    98  		mode |= igop.EnableTracing
    99  	}
   100  	if base.BuildX {
   101  		mode |= igop.EnableDumpImports
   102  	}
   103  	if base.ExperimentalGC {
   104  		mode |= igop.ExperimentalSupportGC
   105  	}
   106  	ctx := igop.NewContext(mode)
   107  	ctx.BuildContext = base.BuildContext
   108  
   109  	pkg, err := ctx.LoadDir(path, true)
   110  	if err != nil {
   111  		fmt.Fprintln(os.Stderr, err)
   112  		os.Exit(2)
   113  	}
   114  
   115  	err = ctx.TestPkg(pkg, path, testArgs)
   116  	if err != nil {
   117  		os.Exit(2)
   118  	}
   119  }