Debian Codegraph @

integration/help_quality_test.go

raw symbols SCIP
1 // +build integration
2
3 package integration
4
5 import (
6 "encoding/json"
7 "fmt"
8 "io/ioutil"
9 "sort"
10 "strings"
11 "testing"
12
13 "github.com/smallstep/assert"
14 "github.com/smallstep/cli/usage"
15 )
16
17 func TestHelpQuality(t *testing.T) {
18 cmd := NewCLICommand().setCommand("../bin/step help").setFlag("html", "./html").setFlag("report", "")
19 cmd.run()
20
21 raw, _ := ioutil.ReadFile("./html/report.json")
22 var report *usage.Report
23 json.Unmarshal([]byte(raw), &report)
24
25 expectations := make(map[string]usage.Section)
26 expectations["COMMANDS"] = usage.Section{Name: "COMMANDS", Words: 0, Lines: 0}
27 expectations["COPYRIGHT"] = usage.Section{Name: "COPYRIGHT", Words: 5, Lines: 1}
28 expectations["DESCRIPTION"] = usage.Section{Name: "DESCRIPTION", Words: 8, Lines: 1}
29 expectations["EXAMPLES"] = usage.Section{Name: "EXAMPLES", Words: 10, Lines: 1}
30 expectations["EXIT CODES"] = usage.Section{Name: "EXIT CODES", Words: 12, Lines: 1}
31 expectations["ONLINE"] = usage.Section{Name: "ONLINE", Words: 7, Lines: 1}
32 expectations["OPTIONS"] = usage.Section{Name: "OPTIONS", Words: 6, Lines: 2}
33 expectations["POSITIONAL ARGUMENTS"] = usage.Section{Name: "POSITIONAL ARGUMENTS", Words: 6, Lines: 2}
34 expectations["PRINTING"] = usage.Section{Name: "PRINTING", Words: 23, Lines: 1}
35 expectations["SECURITY CONSIDERATIONS"] = usage.Section{Name: "SECURITY CONSIDERATIONS", Words: 220, Lines: 25}
36 expectations["STANDARDS"] = usage.Section{Name: "STANDARDS", Words: 45, Lines: 10}
37 expectations["USAGE"] = usage.Section{Name: "USAGE", Words: 2, Lines: 1}
38 expectations["VERSION"] = usage.Section{Name: "VERSION", Words: 3, Lines: 1}
39
40 t.Run("Headlines consistency", func(t *testing.T) {
41 var headlines []string
42 for _, top := range report.Report {
43 for _, section := range top.Sections {
44 headlines = append(headlines, section.Name)
45 }
46 }
47 sort.Strings(headlines)
48
49 grouped := make(map[string]int)
50 for _, line := range headlines {
51 grouped[line]++
52 }
53
54 for item, count := range grouped {
55 fmt.Printf("%s: %d\n", item, count)
56
57 // Let's say only upper case considered headlines
58 if strings.ToUpper(item) == item {
59
60 _, ok := expectations[item]
61 msg := fmt.Sprintf("Unexpected headline %s might lead to inconsistent docs", item)
62 assert.Equals(t, ok, true, msg)
63 }
64 }
65 })
66
67 t.Run("Thresholds", func(t *testing.T) {
68 for _, expected := range expectations {
69 entries := report.PerHeadline(expected.Name)
70
71 for _, entry := range entries {
72 msgw := fmt.Sprintf("Short on words (%d < %d) in %s (%s)", entry.Words, expected.Words, entry.Command, expected.Name)
73 msgl := fmt.Sprintf("Short on lines (%d < %d) in %s (%s)", entry.Lines, expected.Lines, entry.Command, expected.Name)
74 assert.True(t, entry.Words >= expected.Words, msgw)
75 assert.True(t, entry.Lines >= expected.Lines, msgl)
76 }
77 }
78 })
79
80 t.Run("No TODOs", func(t *testing.T) {
81 for _, top := range report.Report {
82 for _, section := range top.Sections {
83 msg := fmt.Sprintf("TODO found in %s (%s)", section.Command, section.Name)
84 assert.False(t, strings.Contains(strings.ToUpper(section.Text), "TODO"), msg)
85 }
86 }
87 })
88 }