-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
302 lines (244 loc) · 6.72 KB
/
list.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"github.com/nokia/ntt/internal/fs"
"github.com/nokia/ntt/project"
"github.com/nokia/ntt/ttcn3"
"github.com/nokia/ntt/ttcn3/doc"
"github.com/nokia/ntt/ttcn3/syntax"
"github.com/spf13/cobra"
)
var (
ListCommand = &cobra.Command{
Use: "list",
Short: "List modules, tests, imports, ...",
Long: `List various types of objects.
List control parts, modules, imports or control. The list command without any explicit
sub-commands will output control.
List will ignore imported directories when printing control. If you need to list all
tests from a testsuite you'll have to pass .ttcn3 files as arguments.
Example:
ntt list $(ntt show -- sources) $(find $(ntt show -- imports) -name \*.ttcn3)
Filtering
---------
You can use 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
Baskets
-------
Baskets are 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"
`,
// Listing tests is the default command
RunE: list,
}
w = bufio.NewWriter(os.Stdout)
showFiles = false
showTags = false
formatJSON = false
formatPlain = true
first = true
)
func init() {
flags := ListCommand.PersistentFlags()
flags.BoolVar(&showFiles, "with-filename", false, "Print the filename for each match.")
flags.BoolVar(&showTags, "with-tags", false, "Print documentation tags for each match.")
flags.BoolVarP(&showTags, "tags", "t", false, "Print documentation tags for each match.")
flags.MarkDeprecated("tags", "please use --with-tags instead")
flags.AddFlagSet(BasketFlags())
ListCommand.AddCommand(
&cobra.Command{Use: `tests`, RunE: list},
&cobra.Command{Use: `modules`, RunE: list},
&cobra.Command{Use: `imports`, RunE: list},
&cobra.Command{Use: `controls`, RunE: list},
&cobra.Command{Use: `modulepars`, RunE: list},
)
}
func list(cmd *cobra.Command, args []string) error {
basket, err := NewBasketWithFlags("list", cmd.Flags())
basket.LoadFromEnvOrConfig(Project, "NTT_LIST_BASKETS")
if err != nil {
return err
}
formatJSON, _ = cmd.Flags().GetBool("json")
formatPlain, _ = cmd.Flags().GetBool("plain")
if formatJSON {
fmt.Fprintln(w, "[")
}
files, err := filesOfInterest(cmd.Use, Project)
for _, f := range files {
tree := ttcn3.ParseFile(f)
if tree.Err != nil {
return tree.Err
}
var module string
tree.Inspect(func(n syntax.Node) bool {
if n != nil {
switch n := n.(type) {
case *syntax.Module:
module = syntax.Name(n.Name)
if cmd.Use == "modules" {
Print(basket, n, module)
return false
}
return true
case *syntax.FuncDecl:
if n.IsTest() && (cmd.Use == "list" || cmd.Use == "tests") {
Print(basket, n, module+"."+n.Name.String())
}
case *syntax.ImportDecl:
if cmd.Use == "imports" {
Print(basket, n, fmt.Sprintf("%s\t%s", module, n.Module.String()))
}
case *syntax.ControlPart:
if cmd.Use == "controls" {
Print(basket, n, module+".control")
}
case *syntax.Declarator:
if cmd.Use == "modulepars" {
Print(basket, n, module+"."+n.Name.String())
}
case *syntax.ValueDecl:
if n.Kind == nil && n.Kind.Kind() == syntax.MODULEPAR {
return true
}
case *syntax.NodeList, *syntax.ModuleDef, *syntax.GroupDecl, *syntax.ModuleParameterGroup:
return true
}
}
return false
})
}
if formatJSON {
fmt.Fprintln(w, "]")
}
w.Flush()
return err
}
type Match struct {
Filename string `json:"filename,omitempty"`
Line int `json:"line,omitempty"`
Column int `json:"column,omitempty"`
ID string `json:"id,omitempty"`
Tags []Tag `json:"tags,omitempty"`
}
type Tag struct {
Key string
Value string
}
func (t Tag) String() string {
if t.Value != "" {
return fmt.Sprintf("%s:%s", t.Key, t.Value)
}
return t.Key
}
func (t Tag) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String())
}
func Print(basket Basket, n syntax.Node, id string) {
tags := doc.FindAllTags(syntax.Doc(n))
if !basket.Match(id, tags) {
return
}
p := syntax.Begin(n)
filename := syntax.Filename(n)
var prettyTags []Tag
for _, tag := range tags {
t := Tag{Key: tag[0]}
if len(tag) > 1 {
t.Value = tag[1]
}
prettyTags = append(prettyTags, t)
}
switch {
case formatJSON:
if !first {
w.Write([]byte(",\n"))
}
first = false
b, err := json.Marshal(Match{
Filename: filename,
Line: p.Line,
Column: p.Column,
ID: id,
Tags: prettyTags,
})
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
w.Write(b)
default:
if showTags && len(tags) != 0 {
for _, tag := range prettyTags {
PrintMatch(filename, p, id, tag.String())
}
return
}
PrintMatch(filename, p, id)
}
}
func PrintMatch(filename string, pos syntax.Position, id string, tags ...string) {
if showFiles {
fmt.Fprintf(w, "%s:%d\t", filename, pos.Line)
}
fmt.Fprintf(w, id)
for _, t := range tags {
fmt.Fprintf(w, "\t%s", t)
}
fmt.Fprintln(w)
}
func filesOfInterest(cmd string, conf *project.Config) ([]string, error) {
switch cmd {
case "tests", "controls", "list":
return fs.TTCN3Files(conf.Sources...)
default:
return project.Files(conf)
}
}