forked from dell/gopowermax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_test.go
107 lines (93 loc) · 2.56 KB
/
unit_test.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
/*
Copyright © 2020 Dell Inc. or its subsidiaries. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pmax
import (
"bufio"
"fmt"
"net/http/httptest"
"os"
"regexp"
"strings"
"testing"
"github.com/cucumber/godog"
"github.com/dell/gopowermax/v2/mock"
)
var mockServer *httptest.Server
func TestMain(m *testing.M) {
status := 0
var fileWriter *bufio.Writer
var outputFile os.File
format := "pretty"
filename := ""
testPaths := []string{"unittest"}
testTags := ""
runOptions := godog.Options{}
regex, _ := regexp.Compile("(\\S+)=(\\S+)")
// Read command line arguments
for _, it := range os.Args[1:] {
if allString := regex.FindAllStringSubmatch(it, 1); allString != nil {
name := allString[0][1]
value := allString[0][2]
switch name {
case "format":
format = value
case "outfile":
filename = value
case "test-paths":
testPaths = strings.Split(value, ",")
case "test-tags":
testTags = value
}
}
}
// Create appropriate file name extension based on 'format' value
if filename != "" {
switch format {
case "junit":
filename = fmt.Sprintf("%s.xml", filename)
case "cucumber":
filename = fmt.Sprintf("%s.json", filename)
case "pretty":
filename = fmt.Sprintf("%s.txt", filename)
case "progress":
filename = fmt.Sprintf("%s.txt", filename)
}
outputFile, err := os.Create(filename)
if err != nil {
fmt.Printf("Could not create output file %s - %v\n", filename, err)
os.Exit(1)
}
fileWriter = bufio.NewWriter(outputFile)
runOptions.Output = fileWriter
}
// Finalize the options
runOptions.Format = format
runOptions.Paths = testPaths
runOptions.Tags = testTags
// Start the mock server.
handler := mock.GetHandler()
mockServer = httptest.NewServer(handler)
fmt.Printf("mockServer listening on %s\n", mockServer.URL)
status = godog.RunWithOptions("godog", func(s *godog.Suite) {
UnitTestContext(s)
}, runOptions)
if st := m.Run(); st > status {
status = st
}
fmt.Printf("status %d\n", status)
if fileWriter != nil {
fileWriter.Flush()
}
outputFile.Close()
os.Exit(status)
}