-
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathhelp_test.go
More file actions
109 lines (96 loc) · 2.64 KB
/
help_test.go
File metadata and controls
109 lines (96 loc) · 2.64 KB
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
package main
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
"github.com/alecthomas/kong"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.abhg.dev/gs/internal/cli"
)
//go:generate go test -run ^TestHelp$ -update
func TestHelp(t *testing.T) {
oldName := cli.Name()
cli.SetName("gs")
defer cli.SetName(oldName)
// Build Kong parser with the same configuration as main
var cmd mainCmd
parser, err := kong.New(&cmd,
kong.Name("gs"),
kong.Description("git-spice is a command line tool for stacking Git branches."),
kong.Help(helpPrinter),
kong.Exit(func(int) {}), // Don't actually exit in tests
kong.Vars{
"defaultPrompt": "true",
},
)
require.NoError(t, err)
// Collect all commands to test
var commands []struct {
name string
path []string
}
// Add root help
commands = append(commands, struct {
name string
path []string
}{
name: "gs",
path: []string{},
})
// Collect all leaf commands (exclude hidden)
for _, node := range parser.Model.Leaves(true) {
// Build the command path
var path []string
for n := node; n != nil && n.Type == kong.CommandNode; n = n.Parent {
path = append([]string{n.Name}, path...)
}
if len(path) > 0 {
commands = append(commands, struct {
name string
path []string
}{
name: strings.Join(path, " "),
path: path,
})
}
}
for _, tc := range commands {
t.Run(tc.name, func(t *testing.T) {
// Generate help output by creating a parser with a buffer
var helpBuf bytes.Buffer
testParser, err := kong.New(&cmd,
kong.Name("gs"),
kong.Description("git-spice is a command line tool for stacking Git branches."),
kong.ConfigureHelp(kong.HelpOptions{Compact: true}),
kong.Help(helpPrinter),
kong.Exit(func(int) {}),
kong.Writers(&helpBuf, &helpBuf),
kong.Vars{
"defaultPrompt": "true",
},
)
require.NoError(t, err)
_, _ = testParser.Parse(append(tc.path, "--help"))
actual := helpBuf.String()
// Determine golden file path
filename := strings.ReplaceAll(tc.name, " ", "_") + ".txt"
goldenPath := filepath.Join("testdata", "help", filename)
if *_update {
// Update mode: write actual output to golden file
err := os.MkdirAll(filepath.Dir(goldenPath), 0o755)
require.NoError(t, err)
err = os.WriteFile(goldenPath, []byte(actual), 0o644)
require.NoError(t, err)
t.Logf("Updated golden file: %s", goldenPath)
} else {
// Test mode: compare against golden file
expected, err := os.ReadFile(goldenPath)
require.NoError(t, err, "failed to read golden file: %s", goldenPath)
assert.Equal(t, string(expected), actual)
}
})
}
}