-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_files.go
164 lines (141 loc) · 4.39 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package venom
import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/mattn/go-zglob"
"github.com/rockbears/yaml"
"github.com/ovh/cds/sdk/interpolate"
"github.com/pkg/errors"
)
func getFilesPath(path []string) ([]string, error) {
filePaths := make([]string, 0)
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() {
//check if *.yml or *.yaml files exists in the path
p = p + string(os.PathSeparator) + "*.y*ml"
}
fpaths, err := zglob.Glob(p)
if err != nil {
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 YAML (*.yml or *.yaml) file found or defined")
}
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
}
func (v *Venom) readFiles(ctx context.Context, filesPath []string) (err error) {
for _, filePath := range filesPath {
Info(ctx, "Reading %v", filePath)
btes, err := os.ReadFile(filePath)
if err != nil {
return errors.Wrapf(err, "unable to read file %q", filePath)
}
varCloned := v.variables.Clone()
fromPartial, err := getVarFromPartialYML(ctx, btes)
if err != nil {
return errors.Wrapf(err, "unable to get vars from file %q", filePath)
}
var varsFromPartial map[string]string
if len(fromPartial) > 0 {
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 k == "" {
continue
}
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.Add(k, valueInterpolated)
}
}
var vars map[string]string
if len(varCloned) > 0 {
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 testSuiteInput TestSuiteInput
if err := yaml.Unmarshal([]byte(content), &testSuiteInput); err != nil {
Error(context.Background(), "file content: %s", content)
return errors.Wrapf(err, "error while unmarshal file %q", filePath)
}
ts := TestSuite{
Name: testSuiteInput.Name,
Description: testSuiteInput.Description,
TestCases: make([]TestCase, len(testSuiteInput.TestCases)),
Vars: testSuiteInput.Vars,
Secrets: testSuiteInput.Secrets,
}
for i := range testSuiteInput.TestCases {
ts.TestCases[i] = TestCase{
TestCaseInput: testSuiteInput.TestCases[i],
}
}
Info(ctx, "Has %d Secrets", len(ts.Secrets))
// Default workdir is testsuite directory
ts.WorkDir, err = filepath.Abs(filepath.Dir(filePath))
if runtime.GOOS == "windows" {
// Replace backslashes with forward slashes for Windows
ts.WorkDir = strings.ReplaceAll(ts.WorkDir, "\\", "/")
}
if err != nil {
return errors.Wrapf(err, "Unable to get testsuite's working directory")
}
// ../foo/a.yml
ts.Filepath = filePath
// a
ts.ShortName = strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath))
// a.yml
ts.Filename = filepath.Base(filePath)
ts.Vars = varCloned
ts.Vars.Add("venom.testsuite.workdir", ts.WorkDir)
ts.Vars.Add("venom.testsuite.name", ts.Name)
ts.Vars.Add("venom.testsuite.shortName", ts.ShortName)
ts.Vars.Add("venom.testsuite.filename", ts.Filename)
ts.Vars.Add("venom.testsuite.filepath", ts.Filepath)
ts.Vars.Add("venom.datetime", time.Now().Format(time.RFC3339))
ts.Vars.Add("venom.timestamp", fmt.Sprintf("%d", time.Now().Unix()))
v.Tests.TestSuites = append(v.Tests.TestSuites, ts)
}
return nil
}