-
Notifications
You must be signed in to change notification settings - Fork 132
/
list_csv.go
68 lines (60 loc) · 1.5 KB
/
list_csv.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
package main
import (
"encoding/csv"
"io"
"path/filepath"
"sort"
"strings"
)
// listCSV writes the testsuite as CSV, with the columns "Feature", "ID", "Desc", and
// "Test Path".
func listCSV(w io.Writer, featuredir string, ts testsuite) error {
rootdir := filepath.Dir(featuredir)
cw := csv.NewWriter(w)
heading := []string{"Feature", "ID", "Desc", "Test Path"}
if err := cw.Write(heading); err != nil {
return err
}
keys := sortedByTestPlanID(ts)
for _, k := range keys {
reldir, err := filepath.Rel(rootdir, k)
if err != nil {
reldir = k
}
feature := featureFromTestDir(reldir)
feature = strings.TrimPrefix(feature, "feature/")
md := ts[k].markdown
row := []string{feature, md.PlanId, md.Description, reldir}
if err := cw.Write(row); err != nil {
break
}
}
cw.Flush()
return cw.Error()
}
// featureFromTestDir extracts the feature path from a test directory.
func featureFromTestDir(testdir string) string {
kinddir := filepath.Dir(testdir)
kind := filepath.Base(kinddir)
if !isTestKind(kind) {
return kinddir
}
return filepath.Dir(kinddir)
}
// sortedByTestPlanID returns the testsuite keys sorted by the test plan ID of the test
// cases.
func sortedByTestPlanID(ts testsuite) []string {
keys := []string{}
for k := range ts {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool {
idi := ts[keys[i]].markdown.PlanId
idj := ts[keys[j]].markdown.PlanId
if idi == idj {
return keys[i] < keys[j]
}
return lessVersion(idi, idj)
})
return keys
}