From 9114a6dae0ac344bd91933104c461a1ae3bfb329 Mon Sep 17 00:00:00 2001 From: Ruben de Vries Date: Mon, 26 Apr 2021 10:42:33 +0200 Subject: [PATCH] allow test args to be specified at the end after a -- arg --- courtney.go | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/courtney.go b/courtney.go index e36d482..f9e3c8d 100644 --- a/courtney.go +++ b/courtney.go @@ -24,14 +24,31 @@ func main() { shortFlag := flag.Bool("short", false, "Pass the short flag to the go test command") timeoutFlag := flag.String("timeout", "", "Pass the timeout flag to the go test command") outputFlag := flag.String("o", "", "Override coverage file location") - argsFlag := new(argsValue) - flag.Var(argsFlag, "t", "Argument to pass to the 'go test' command. Can be used more than once.") + testArgsFlag := new(argsValue) + flag.Var(testArgsFlag, "t", "Argument to pass to the 'go test' command. Can be used more than once.") loadFlag := flag.String("l", "", "Load coverage file(s) instead of running 'go test'") excludeErrNoReturnParamFlag := flag.Bool("excludenoreturn", false, "Exclude error blocks in functions with no return params") excludePkgsFlag := new(argsValue) flag.Var(excludePkgsFlag, "ex", "Argument to exclude packages from the cover profile.") flag.Parse() + args := flag.Args() + + // any args after a "--" arg will be considered args for `go test` + testArgs := testArgsFlag.args + pkgArgs := make([]string, 0, len(args)) + foundTestArgs := false + for _, arg := range args { + if strings.TrimSpace(arg) == "--" { + foundTestArgs = true + continue + } + if foundTestArgs { + testArgs = append(testArgs, arg) + } else { + pkgArgs = append(pkgArgs, arg) + } + } setup := &shared.Setup{ Env: env, @@ -44,19 +61,19 @@ func main() { Options: shared.Options{ ExcludeErrNoReturnParam: *excludeErrNoReturnParamFlag, }, - TestArgs: argsFlag.args, + TestArgs: testArgs, ExcludePkgs: excludePkgsFlag.args, Load: *loadFlag, } - if err := Run(setup); err != nil { + if err := Run(setup, pkgArgs); err != nil { fmt.Printf("%+v", err) os.Exit(1) } } // Run initiates the command with the provided setup -func Run(setup *shared.Setup) error { - if err := setup.Parse(flag.Args()); err != nil { +func Run(setup *shared.Setup, pkgArgs []string) error { + if err := setup.Parse(pkgArgs); err != nil { return errors.Wrapf(err, "Parse") }