-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
141 lines (122 loc) · 2.99 KB
/
main.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
138
139
140
141
package main
import (
"cotton/internal/clock"
"cotton/internal/config"
"cotton/internal/directory"
"cotton/internal/executable"
"cotton/internal/httphelper"
"cotton/internal/logger"
"cotton/internal/reader"
"cotton/internal/reporter"
"cotton/internal/testcase"
"flag"
"fmt"
"os"
)
var Version = "1.0.0"
func main() {
var debug bool
var detailedDebug bool
var compact bool
var help bool
var ver bool
var insecure bool
var stopWhenFailed bool
var customBaseDir string
var reporterType string
flag.Usage = usage
flag.BoolVar(&compact, "c", false, "compact mode")
flag.BoolVar(&debug, "d", false, "debug mode")
flag.BoolVar(&detailedDebug, "p", false, "paranoid mode")
flag.BoolVar(&insecure, "i", false, "disable certificate verification")
flag.BoolVar(&stopWhenFailed, "s", false, "stop when test failed")
flag.BoolVar(&ver, "v", false, "display cotton version")
flag.BoolVar(&help, "h", false, "display this help")
flag.StringVar(&customBaseDir, "b", "", "set base directory path")
flag.StringVar(&reporterType, "r", "", "set reporter type, html or ctrf")
flag.Parse()
testPath := flag.Arg(0)
if testPath == "" {
testPath = "./"
}
dir := directory.New()
testBaseDir, err := dir.DirectoryOf(testPath)
baseDir := ""
if customBaseDir == "" {
if err == nil {
baseDir = testBaseDir
} else {
baseDir, _ = os.Getwd()
}
} else {
baseDir = customBaseDir
}
config := &config.Config{
BaseDir: baseDir,
}
level := logger.Verbose
if compact {
level = logger.Compact
}
if debug {
level = logger.Debug
}
if detailedDebug {
level = logger.DetailedDebug
}
if ver {
fmt.Printf("cotton %s\n", Version)
os.Exit(0)
}
if help {
usage()
os.Exit(0)
}
log := logger.NewTerminalLogger(level)
reader := reader.New(os.ReadFile)
reqParser := &httphelper.HTTPRequestParser{}
clockWrapper := clock.New()
exOptions := &executable.ParserOptions{
Configurator: config,
FileReader: reader,
RequestParser: reqParser,
Logger: log,
ClockWrapper: clockWrapper,
InsecureRequest: insecure,
}
exParser := executable.NewParser(exOptions)
tcOptions := &testcase.ParserOptions{
Configurator: config,
FileReader: reader,
RequestParser: reqParser,
Logger: log,
ExecutableParser: exParser,
ClockWrapper: clockWrapper,
InsecureRequest: insecure,
}
options := &testcase.TestsuiteOptions{
TestcaseParserOption: tcOptions,
StopWhenFailed: stopWhenFailed,
Logger: log,
ClockWrapper: clockWrapper,
AppVersion: Version,
TestReporter: reporter.NewReporter(reporter.ReporterType(reporterType)),
}
ts, err := testcase.NewTestsuite(testPath, options)
if err != nil {
os.Exit(1)
}
_, err = ts.Execute()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func usage() {
fmt.Fprintf(flag.CommandLine.Output(), `Usage of cotton:
cotton [-d] [-c] [-p] [-i] [-b <basedir>] [-r <reporttype>] <testpath|testdir>
cotton -v
cotton --help
`)
flag.PrintDefaults()
}