1package cmd
2
3import (
4 "strings"
5 "testing"
6)
7
8func TestIsWrappedCommand(t *testing.T) {
9 t.Parallel()
10
11 tests := []struct {
12 name string
13 cmd string
14 want bool
15 }{
16 {name: "backup is wrapped", cmd: "backup", want: true},
17 {name: "restore is wrapped", cmd: "restore", want: true},
18 {name: "snapshots is wrapped", cmd: "snapshots", want: true},
19 {name: "cat is not wrapped", cmd: "cat", want: false},
20 {name: "ls is not wrapped", cmd: "ls", want: false},
21 {name: "empty string", cmd: "", want: false},
22 }
23
24 for _, tt := range tests {
25 t.Run(tt.name, func(t *testing.T) {
26 t.Parallel()
27
28 got := isWrappedCommand(tt.cmd)
29 if got != tt.want {
30 t.Errorf("isWrappedCommand(%q) = %v, want %v", tt.cmd, got, tt.want)
31 }
32 })
33 }
34}
35
36func TestSubcommandWithArgsStaysNonInteractive(t *testing.T) {
37 // Not parallel: mutates package-level flag vars via
38 // setRootFlagValuesForTest and environment via t.Setenv.
39 configFile := setupCommandConfig(t)
40 setRootFlagValuesForTest(t, "home@cloud", true, configFile)
41 t.Setenv("KELD_CONFIG_FILE", "")
42 t.Setenv("KELD_DRYRUN", "")
43
44 backup := lookupSubcommand(t, "backup")
45 out, err := captureStdout(t, func() error {
46 return backup.RunE(backup, []string{"--tag", "daily", "/src"})
47 })
48 if err != nil {
49 t.Fatalf("subcommand with args returned error: %v", err)
50 }
51
52 // Should produce dry-run output containing the command.
53 if !strings.Contains(out, `"backup"`) {
54 t.Fatalf("expected dry-run output to contain backup command, got:\n%s", out)
55 }
56 if !strings.Contains(out, `"--tag" "daily"`) {
57 t.Fatalf("expected dry-run output to contain --tag daily, got:\n%s", out)
58 }
59}