Skip to content

Commit 1669ae6

Browse files
committed
test: lookup recursively for docs in e2e test
1 parent 66601c2 commit 1669ae6

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

tests/e2e/main_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
"bytes"
2222
"flag"
2323
"fmt"
24+
"io/fs"
2425
"log"
2526
"os"
26-
"path"
2727
"path/filepath"
2828
"regexp"
2929
"sort"
@@ -145,12 +145,6 @@ func TestDocumentation(t *testing.T) {
145145
func getLabelsDocumentation() (map[string][]string, error) {
146146
documentedMetrics := map[string][]string{}
147147

148-
docPath := "../../docs/"
149-
docFiles, err := os.ReadDir(docPath)
150-
if err != nil {
151-
return nil, fmt.Errorf("failed to read documentation directory: %w", err)
152-
}
153-
154148
// Match file names such as daemonset-metrics.md
155149
fileRe := regexp.MustCompile(`^([a-z]*)-metrics.md$`)
156150
// Match doc lines such as | kube_node_created | Gauge | `node`=<node-address>| STABLE |
@@ -160,15 +154,16 @@ func getLabelsDocumentation() (map[string][]string, error) {
160154
// Match wildcard patterns for dynamic labels such as label_CRONJOB_LABEL
161155
patternRe := regexp.MustCompile(`_[A-Z_]+`)
162156

163-
for _, file := range docFiles {
164-
if file.IsDir() || !fileRe.MatchString(file.Name()) {
165-
continue
157+
err := filepath.WalkDir("../../docs", func(p string, d fs.DirEntry, err error) error {
158+
159+
if d.IsDir() || !fileRe.MatchString(d.Name()) {
160+
// Ignore the entry
161+
return nil
166162
}
167163

168-
filePath := path.Join(docPath, file.Name())
169-
f, err := os.Open(filepath.Clean(filePath))
170-
if err != nil {
171-
return nil, fmt.Errorf("cannot read file %s: %w", filePath, err)
164+
f, e := os.Open(filepath.Clean(p))
165+
if e != nil {
166+
return fmt.Errorf("cannot read file %s: %w", p, e)
172167
}
173168
scanner := bufio.NewScanner(f)
174169
for scanner.Scan() {
@@ -183,14 +178,19 @@ func getLabelsDocumentation() (map[string][]string, error) {
183178
labelPatterns := make([]string, len(labels))
184179
for i, l := range labels {
185180
if len(l) <= 1 {
186-
return nil, fmt.Errorf("Label documentation %s did not match regex", labelsDoc)
181+
return fmt.Errorf("label documentation %s did not match regex", labelsDoc)
187182
}
188183
labelPatterns[i] = patternRe.ReplaceAllString(l[1], "_.*")
189184
}
190185

191186
documentedMetrics[metric] = labelPatterns
192187
}
188+
return nil
189+
})
190+
if err != nil {
191+
log.Fatalf("impossible to walk directories: %s", err)
193192
}
193+
194194
return documentedMetrics, nil
195195
}
196196

0 commit comments

Comments
 (0)