forked from getgauge/gauge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
137 lines (123 loc) · 4.53 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2015 ThoughtWorks, Inc.
// This file is part of Gauge.
// Gauge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Gauge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Gauge. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"os"
"strings"
"github.com/getgauge/gauge/env"
"github.com/getgauge/gauge/execution"
"github.com/getgauge/gauge/execution/rerun"
"github.com/getgauge/gauge/filter"
"github.com/getgauge/gauge/logger"
"github.com/getgauge/gauge/order"
"github.com/getgauge/gauge/reporter"
"github.com/getgauge/gauge/track"
"github.com/getgauge/gauge/util"
"github.com/getgauge/gauge/validation"
"github.com/spf13/cobra"
)
var (
runCmd = &cobra.Command{
Use: "run [flags] [args]",
Short: "Run specs",
Long: `Run specs.`,
Example: ` gauge run specs/
gauge run --tags "login" -s -p specs/`,
Run: func(cmd *cobra.Command, args []string) {
setGlobalFlags()
if err := isValidGaugeProject(args); err != nil {
logger.Fatalf(err.Error())
}
if failed {
loadLastState(cmd)
return
}
execute(args)
},
DisableAutoGenTag: true,
}
verbose bool
simpleConsole bool
failed bool
parallel bool
sort bool
environment string
tags string
rows string
strategy string
streams int
group int
)
func init() {
GaugeCmd.AddCommand(runCmd)
runCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Enable step level reporting on console, default being scenario level")
runCmd.Flags().BoolVarP(&simpleConsole, "simple-console", "", false, "Removes colouring and simplifies the console output")
runCmd.Flags().StringVarP(&environment, "env", "e", "default", "Specifies the environment to use")
runCmd.Flags().StringVarP(&tags, "tags", "t", "", "Executes the specs and scenarios tagged with given tags")
runCmd.Flags().StringVarP(&rows, "table-rows", "r", "", "Executes the specs and scenarios only for the selected rows. It can be specified by range as 2-4 or as list 2,4")
runCmd.Flags().BoolVarP(¶llel, "parallel", "p", false, "Execute specs in parallel")
runCmd.Flags().IntVarP(&streams, "n", "n", util.NumberOfCores(), "Specify number of parallel execution streams")
runCmd.Flags().IntVarP(&group, "group", "g", -1, "Specify which group of specification to execute based on -n flag")
runCmd.Flags().StringVarP(&strategy, "strategy", "", "lazy", "Set the parallelization strategy for execution. Possible options are: `eager`, `lazy`")
runCmd.Flags().BoolVarP(&sort, "sort", "s", false, "Run specs in Alphabetical Order")
runCmd.Flags().BoolVarP(&failed, "failed", "f", false, "Run only the scenarios failed in previous run")
}
func loadLastState(cmd *cobra.Command) {
lastState, err := rerun.GetLastState()
if err != nil {
logger.Fatalf(err.Error())
}
logger.Infof("Executing => gauge %s\n", strings.Join(lastState, " "))
cmd.Parent().SetArgs(lastState)
os.Args = append([]string{"gauge"}, lastState...)
resetFlags()
cmd.Execute()
}
func resetFlags() {
verbose, simpleConsole, failed, parallel, sort = false, false, false, false, false
environment, tags, rows, strategy, logLevel, dir = "default", "", "", "lazy", "info", "."
streams, group = util.NumberOfCores(), -1
}
func execute(args []string) {
specs := getSpecsDir(args)
rerun.SaveState(os.Args[1:], specs)
track.Execution(parallel, tags != "", sort, simpleConsole, verbose, strategy)
initPackageFlags()
if e := env.LoadEnv(environment); e != nil {
logger.Fatalf(e.Error())
}
exitCode := execution.ExecuteSpecs(specs)
os.Exit(exitCode)
}
func initPackageFlags() {
if parallel {
simpleConsole = true
reporter.IsParallel = true
}
reporter.SimpleConsoleOutput = simpleConsole
reporter.Verbose = verbose
execution.ExecuteTags = tags
execution.SetTableRows(rows)
validation.TableRows = rows
execution.NumberOfExecutionStreams = streams
execution.InParallel = parallel
execution.Strategy = strategy
filter.ExecuteTags = tags
order.Sorted = sort
filter.Distribute = group
filter.NumberOfExecutionStreams = streams
reporter.NumberOfExecutionStreams = streams
if group != -1 {
execution.Strategy = execution.Eager
}
}