Skip to content

Commit a23f475

Browse files
committed
Add test to ensure src snapshot upload --help prints its own usage text
1 parent 794e0b6 commit a23f475

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

cmd/src/cmd_test.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bytes"
55
"flag"
6+
"fmt"
67
"os"
78
"os/exec"
89
"testing"
@@ -206,7 +207,60 @@ func TestCommander_Run_HelpFlag(t *testing.T) {
206207
}
207208

208209
func TestCommander_Run_NestedHelpFlags(t *testing.T) {
209-
t.Skip("Complex nested help flag testing requires integration with actual src commands")
210+
if os.Getenv("TEST_SUBPROCESS") == "1" {
211+
testHomeDir = os.Getenv("TEST_TEMP_DIR")
212+
213+
uploadFlagSet := flag.NewFlagSet("upload", flag.ExitOnError)
214+
uploadCmd := &command{
215+
flagSet: uploadFlagSet,
216+
handler: func(args []string) error { return nil },
217+
usageFunc: func() {
218+
fmt.Fprint(flag.CommandLine.Output(), "upload usage text")
219+
},
220+
}
221+
222+
snapshotCommands := commander{uploadCmd}
223+
224+
snapshotFlagSet := flag.NewFlagSet("snapshot", flag.ExitOnError)
225+
snapshotCmd := &command{
226+
flagSet: snapshotFlagSet,
227+
handler: func(args []string) error {
228+
snapshotCommands.run(snapshotFlagSet, "src snapshot", "snapshot usage text", args)
229+
return nil
230+
},
231+
usageFunc: func() {
232+
fmt.Fprint(flag.CommandLine.Output(), "snapshot usage text")
233+
},
234+
}
235+
236+
cmdr := commander{snapshotCmd}
237+
flagSet := flag.NewFlagSet("test", flag.ExitOnError)
238+
args := []string{"snapshot", "upload", "--h"}
239+
cmdr.run(flagSet, "src", "root usage", args)
240+
return
241+
}
242+
243+
tempDir := t.TempDir()
244+
cmd := exec.Command(os.Args[0], "-test.run=^TestCommander_Run_NestedHelpFlags$")
245+
cmd.Env = append(os.Environ(), "TEST_SUBPROCESS=1", "TEST_TEMP_DIR="+tempDir)
246+
var stdout, stderr bytes.Buffer
247+
cmd.Stdout = &stdout
248+
cmd.Stderr = &stderr
249+
err := cmd.Run()
250+
251+
output := stdout.String() + stderr.String()
252+
253+
if err != nil {
254+
t.Errorf("expected success, got error: %v\noutput: %s", err, output)
255+
}
256+
257+
if !bytes.Contains([]byte(output), []byte("upload usage text")) {
258+
t.Errorf("expected output to contain 'upload usage text', got:\n%s", output)
259+
}
260+
261+
if bytes.Contains([]byte(output), []byte("snapshot usage text")) {
262+
t.Errorf("expected output NOT to contain 'snapshot usage text', got:\n%s", output)
263+
}
210264
}
211265

212266
func TestCommander_Run_InvalidSubcommand(t *testing.T) {

0 commit comments

Comments
 (0)