-
Notifications
You must be signed in to change notification settings - Fork 5
/
allure.go
132 lines (110 loc) · 3.24 KB
/
allure.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
package allure
import (
"github.com/GabbyyLS/allure-go-common/beans"
"time"
"bytes"
"errors"
// "mime"
"code.google.com/p/go-uuid/uuid"
"io/ioutil"
"path/filepath"
"os"
"encoding/xml"
)
//
type Allure struct {
Suites []*beans.Suite
TargetDir string
}
//SetOptions()js -> New
func New(suites []*beans.Suite) *Allure {
return &Allure{Suites: suites, TargetDir: "allure-results"}
}
//getCurrentSuite -> 0
func (a *Allure) GetCurrentSuite() *beans.Suite {
return a.Suites[0]
}
func (a *Allure) StartSuite(name string,start time.Time) {
a.Suites = append(a.Suites,beans.NewSuite(name,start))
}
func (a *Allure) EndSuite (end time.Time) {
suite := a.GetCurrentSuite()
suite.End(end)
if(suite.HasTests()) {
writeSuite(a.TargetDir,suite)
}
//remove first/current suite
a.Suites = a.Suites[1:]
}
var currentState = map[*beans.Suite]*beans.TestCase{}
var currentStep = map[*beans.Suite]*beans.Step{}
func (a *Allure) StartCase (testName string, start time.Time) {
var (
test = beans.NewTestCase(testName, start)
step = beans.NewStep(testName, start)
suite = a.GetCurrentSuite()
)
currentState[suite] = test
//strange logic((((
currentStep[suite] = step
suite.AddTest(test)
}
func (a *Allure) EndCase (status string, err error, end time.Time) {
suite := a.GetCurrentSuite()
test, ok := currentState[suite]
if ok {
test.End(status,err,end)
currentState[suite]
}
}
func (a *Allure) CreateStep(name string, stepFunc func() ) {
status := `passed`
a.StartStep(name,time.Now())
// if test error
stepFunc()
//end
a.EndStep(status,time.Now())
}
func (a *Allure) StartStep (stepName string, start time.Time) {
var (
step = beans.NewStep(stepName, start)
suite = a.GetCurrentSuite()
)
step = currentStep[suite]
step.Parent.AddStep(step)
currentStep[suite] = step
}
func (a *Allure) EndStep (status string, end time.Time) {
suite := a.GetCurrentSuite()
currentStep[suite].End(status, end)
currentStep[suite] = currentStep[suite].Parent
}
func (a *Allure) AddAttachment (attachmentName, buf bytes.Buffer, typ string) {
mime,ext := getBufferInfo(buf,typ)
name,_ := writeBuffer(a.TargetDir,buf,ext)
currentState[a.GetCurrentSuite()].AddAttachment(beans.NewAttachment(attachmentName,mime,name,buf.Len()))
}
func (a *Allure) PendingCase(testName string, start time.Time) {
a.StartCase(testName, start)
a.EndCase("pending", errors.New("Test ignored"), start)
}
//utils
func getBufferInfo(buf bytes.Buffer, typ string) (string,string) {
// exts,err := mime.ExtensionsByType(typ)
// if err != nil {
// mime.ParseMediaType()
// }
return "text/plain","txt"
}
func writeBuffer(pathDir string,buf bytes.Buffer,ext string) (string,error) {
fileName := uuid.New()+`-attachment.`+ext
err := ioutil.WriteFile(filepath.Join(pathDir,fileName),buf.Bytes(),os.O_CREATE|os.O_WRONLY)
return fileName,err
}
func writeSuite(pathDir string,suite *beans.Suite) error {
bytes, err := xml.Marshal(suite)
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(pathDir,uuid.New()+`-testsuite.xml`),bytes,os.O_CREATE|os.O_WRONLY)
}