-
Notifications
You must be signed in to change notification settings - Fork 33
/
basket.go
268 lines (241 loc) · 6.46 KB
/
basket.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"fmt"
"regexp"
"strings"
"github.com/nokia/ntt/internal/env"
"github.com/nokia/ntt/project"
"github.com/spf13/pflag"
)
// BasketFlags returns a flagset with all flags for filtering objects.
//
// BasketFlags are regular expressions to filter objects. If you pass multiple regular
// expressions, all of them must match (AND). Example:
//
// $ cat example.ttcn3
// testcase foo() ...
// testcase bar() ...
// testcase foobar() ...
// ...
//
// $ ntt list --regex=foo --regex=bar
// example.foobar
//
// $ ntt list --regex='foo|bar'
// example.foo
// example.bar
// example.foobar
//
//
// Similarly, you can also specify regular expressions for documentation tags.
// Example:
//
// $ cat example.ttcn3
// // @one
// // @two some-value
// testcase foo() ...
//
// // @two: some-other-value
// testcase bar() ...
// ...
//
// $ ntt list --tags-regex=@one --tags-regex=@two
// example.foo
//
// $ ntt list --tags-regex='@two: some'
// example.foo
// example.bar
//
func BasketFlags() *pflag.FlagSet {
fs := pflag.NewFlagSet("basket", pflag.ContinueOnError)
fs.StringSliceP("regex", "r", nil, "list objects matching regular * expression.")
fs.StringSliceP("exclude", "x", nil, "exclude objects matching regular * expresion.")
fs.StringSliceP("tags-regex", "R", nil, "list objects with tags matching regular * expression")
fs.StringSliceP("tags-exclude", "X", nil, "exclude objects with tags matching * regular expression")
return fs
}
// A Basket is a filter for objects. It can be used to filter objects by name
// and tags.
//
// Baskets are also filters defined by environment variables of the form:
//
// NTT_LIST_BASKETS_<name> = <filters>
//
// For example, to define a basket "stable" which excludes all objects with @wip
// or @flaky tags:
//
// export NTT_LIST_BASKETS_stable="-X @wip|@flaky"
//
// Baskets become active when they are listed in colon separated environment
// variable NTT_LIST_BASKETS. If you specify multiple baskets, at least of them
// must match (OR).
//
// Rule of thumb: all baskets are ORed, all explicit filter options are ANDed.
// Example:
//
// $ export NTT_LIST_BASKETS_stable="--tags-exclude @wip|@flaky"
// $ export NTT_LIST_BASKETS_ipv6="--tags-regex @ipv6"
// $ NTT_LIST_BASKETS=stable:ipv6 ntt list -R @flaky
//
//
// Above example will output all tests with a @flaky tag and either @wip or @ipv6 tag.
//
// If a basket is not defined by an environment variable, it's equivalent to a
// "--tags-regex" filter. For example, to lists all tests, which have either a
// @flaky or a @wip tag:
//
// # Note, flaky and wip baskets are not specified explicitly.
// $ NTT_LIST_BASKETS=flaky:wip ntt list
//
// # This does the same:
// $ ntt list --tags-regex="@wip|@flaky"
//
type Basket struct {
// Name is the name of the basket. The basket is used to filter objects
// by tag, if no explicit filters are given.
Name string
// Regular expressions the object name must match.
NameRegex []string
// Regular expressions the object name must not match.
NameExclude []string
// Regular expressions the object tags must match.
TagsRegex []string
// Regular expressions the object tags must not match.
TagsExclude []string
// Baskets are sub-baskets to be ORed.
Baskets []Basket
}
// NewBasket creates a new basket and parses the given arguments.
func NewBasket(name string, args ...string) (Basket, error) {
fs := pflag.NewFlagSet(name, pflag.ContinueOnError)
fs.AddFlagSet(BasketFlags())
if err := fs.Parse(args); err != nil {
return Basket{}, err
}
return NewBasketWithFlags(name, fs)
}
func NewBasketWithFlags(name string, fs *pflag.FlagSet) (Basket, error) {
b := Basket{Name: name}
var err error
b.NameRegex, err = fs.GetStringSlice("regex")
if err != nil {
return b, err
}
b.NameExclude, err = fs.GetStringSlice("exclude")
if err != nil {
return b, err
}
b.TagsRegex, err = fs.GetStringSlice("tags-regex")
if err != nil {
return b, err
}
b.TagsExclude, err = fs.GetStringSlice("tags-exclude")
if err != nil {
return b, err
}
return b, nil
}
// Load baskets from given environment variable from environment
// or from configuration.
func (b *Basket) LoadFromEnvOrConfig(c *project.Config, key string) error {
get := func(name string) string {
if s, ok := env.LookupEnv(name); ok {
return s
}
if c != nil {
if c.Variables[name] != "" {
return c.Variables[name]
}
if strings.HasPrefix(name, "NTT") {
return c.Variables[strings.Replace(name, "NTT", "K3", 1)]
}
}
return ""
}
s := get(key)
if s == "" {
return nil
}
for _, name := range strings.Split(s, ":") {
// Ignore empty fields
if name == "" {
continue
}
args := strings.Fields(get(fmt.Sprintf("%s_%s", key, name)))
if len(args) == 0 {
args = []string{"-R", "@" + name}
}
sb, err := NewBasket(name, args...)
if err != nil {
return err
}
b.Baskets = append(b.Baskets, sb)
}
return nil
}
// Match returns true if the given name and tags match the basket or sub-basket filters.
func (b *Basket) Match(name string, tags [][]string) bool {
ok := b.match(name, tags)
if len(b.Baskets) == 0 {
return ok
}
for _, basket := range b.Baskets {
if basket.Match(name, tags) && ok {
return true
}
}
return false
}
// match returns true if the given name and tags match the basket filters.
func (b *Basket) match(name string, tags [][]string) bool {
if !b.matchAll(b.NameRegex, name) {
return false
}
if len(b.NameExclude) > 0 && b.matchAll(b.NameExclude, name) {
return false
}
if len(b.TagsRegex) > 0 {
if len(tags) == 0 {
return false
}
if !b.matchAllTags(b.TagsRegex, tags) {
return false
}
}
if len(b.TagsExclude) > 0 && b.matchAllTags(b.TagsExclude, tags) {
return false
}
return true
}
// matchAll returns true if all regular expressions match the given string.
func (b *Basket) matchAll(regexes []string, s string) bool {
for _, r := range regexes {
if ok, _ := regexp.Match(r, []byte(s)); !ok {
return false
}
}
return true
}
// matchAllTags returns true if all regular expressions match the all given tags.
func (b *Basket) matchAllTags(regexes []string, tags [][]string) bool {
next:
for _, r := range regexes {
f := strings.SplitN(r, ":", 2)
for i := range f {
f[i] = strings.TrimSpace(f[i])
}
for _, tag := range tags {
if ok, _ := regexp.Match(f[0], []byte(tag[0])); !ok {
continue
}
if len(f) > 1 {
if ok, _ := regexp.Match(f[1], []byte(tag[1])); !ok {
continue
}
}
continue next
}
return false
}
return true
}