-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_files.go
144 lines (120 loc) · 3.59 KB
/
process_files.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
142
143
144
package venom
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/ghodss/yaml"
"github.com/mattn/go-zglob"
"github.com/ovh/cds/sdk/interpolate"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
func getFilesPath(path []string) (filePaths []string, err error) {
for _, p := range path {
p = strings.TrimSpace(p)
// no need to check err on os.stat.
// if we put ./test/*.yml, it will fail and it's normal
fileInfo, _ := os.Stat(p)
if fileInfo != nil && fileInfo.IsDir() {
p = p + string(os.PathSeparator) + "*.yml"
}
fpaths, err := zglob.Glob(p)
if err != nil {
log.Errorf("error reading files on path %q err:%v", path, err)
return nil, errors.Wrapf(err, "error reading files on path %q", path)
}
for _, fp := range fpaths {
switch ext := filepath.Ext(fp); ext {
case ".yml", ".yaml":
filePaths = append(filePaths, fp)
}
}
}
if len(filePaths) == 0 {
return nil, fmt.Errorf("no yml file selected")
}
return uniq(filePaths), nil
}
func uniq(stringSlice []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range stringSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
type partialTestSuite struct {
Name string `json:"name" yaml:"name"`
Vars H `yaml:"vars" json:"vars"`
}
func (v *Venom) readFiles(ctx context.Context, filesPath []string) (err error) {
for _, f := range filesPath {
log.Info("Reading ", f)
btes, err := os.ReadFile(f)
if err != nil {
return errors.Wrapf(err, "unable to read file %q", f)
}
varCloned := v.variables.Clone()
fromPartial, err := getVarFromPartialYML(ctx, btes)
if err != nil {
return errors.Wrapf(err, "unable to get vars from file %q", f)
}
varsFromPartial, err := DumpStringPreserveCase(fromPartial)
if err != nil {
return errors.Wrapf(err, "unable to parse variables")
}
// we take default vars from the testsuite, only if it's not already is global vars
for k, value := range varsFromPartial {
if _, ok := varCloned[k]; !ok || (varCloned[k] == "{}" && varCloned["__Len__"] == "0") {
// we interpolate the value of vars here, to do it only once per ts
valueInterpolated, err := interpolate.Do(value, varsFromPartial)
if err != nil {
return errors.Wrapf(err, "unable to parse variable %q", k)
}
varCloned[k] = valueInterpolated
}
}
vars, err := DumpStringPreserveCase(varCloned)
if err != nil {
return errors.Wrapf(err, "unable to parse variables")
}
content, err := interpolate.Do(string(btes), vars)
if err != nil {
return err
}
var ts TestSuite
if err := yaml.Unmarshal([]byte(content), &ts); err != nil {
Error(context.Background(), "file content: %s", content)
return errors.Wrapf(err, "error while unmarshal file %q", f)
}
// Default workdir is testsuite directory
ts.WorkDir, err = filepath.Abs(filepath.Dir(f))
if err != nil {
return errors.Wrapf(err, "Unable to get testsuite's working directory")
}
ts.Package = f
ts.Filename = f
ts.Vars = varCloned
ts.Vars.Add("venom.testsuite.workdir", ts.WorkDir)
ts.Vars.Add("venom.testsuite.shortName", ts.Name)
ts.Vars.Add("venom.testsuite.filename", ts.Filename)
ts.Vars.Add("venom.datetime", time.Now().Format(time.RFC3339))
ts.Vars.Add("venom.timestamp", fmt.Sprintf("%d", time.Now().Unix()))
nSteps := 0
for _, tc := range ts.TestCases {
nSteps += len(tc.testSteps)
if len(tc.Skipped) >= 1 {
ts.Skipped += len(tc.Skipped)
}
}
ts.Total = len(ts.TestCases)
v.testsuites = append(v.testsuites, ts)
}
return nil
}