| 1 |
package docs |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/spf13/cobra" |
| 7 |
"github.com/stretchr/testify/assert" |
| 8 |
|
| 9 |
"github.com/nicholas-fedor/shoutrrr/shoutrrr/cmd" |
| 10 |
) |
| 11 |
|
| 12 |
// Test_runDocs tests the runDocs function with various scenarios. |
| 13 |
func Test_runDocs(t *testing.T) { |
| 14 |
t.Parallel() |
| 15 |
|
| 16 |
tests := []struct { |
| 17 |
name string |
| 18 |
args []string |
| 19 |
formatFlag string |
| 20 |
wantExit int |
| 21 |
wantErrMsg string |
| 22 |
}{ |
| 23 |
{ |
| 24 |
name: "console format success with single service", |
| 25 |
args: []string{"discord"}, |
| 26 |
formatFlag: "console", |
| 27 |
wantExit: cmd.ExSuccess, |
| 28 |
wantErrMsg: "", |
| 29 |
}, |
| 30 |
{ |
| 31 |
name: "markdown format success with single service", |
| 32 |
args: []string{"slack"}, |
| 33 |
formatFlag: "markdown", |
| 34 |
wantExit: cmd.ExSuccess, |
| 35 |
wantErrMsg: "", |
| 36 |
}, |
| 37 |
{ |
| 38 |
name: "multiple services", |
| 39 |
args: []string{"discord", "slack"}, |
| 40 |
formatFlag: "console", |
| 41 |
wantExit: cmd.ExSuccess, |
| 42 |
wantErrMsg: "", |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "invalid format returns ExUsage", |
| 46 |
args: []string{"discord"}, |
| 47 |
formatFlag: "xml", |
| 48 |
wantExit: cmd.ExUsage, |
| 49 |
wantErrMsg: "invalid format", |
| 50 |
}, |
| 51 |
{ |
| 52 |
name: "unknown service returns ExUsage", |
| 53 |
args: []string{"unknownservice"}, |
| 54 |
formatFlag: "console", |
| 55 |
wantExit: cmd.ExUsage, |
| 56 |
wantErrMsg: "failed to init service", |
| 57 |
}, |
| 58 |
{ |
| 59 |
name: "empty service list handled by cobra args validation", |
| 60 |
args: []string{}, |
| 61 |
formatFlag: "console", |
| 62 |
wantExit: cmd.ExSuccess, |
| 63 |
wantErrMsg: "", |
| 64 |
}, |
| 65 |
} |
| 66 |
|
| 67 |
for _, tt := range tests { |
| 68 |
t.Run(tt.name, func(t *testing.T) { |
| 69 |
t.Parallel() |
| 70 |
|
| 71 |
// Create a cobra command with the format flag |
| 72 |
rootCmd := &cobra.Command{ |
| 73 |
Use: "docs", |
| 74 |
} |
| 75 |
rootCmd.Flags().StringP("format", "f", "console", "Output format") |
| 76 |
|
| 77 |
// Set the format flag value |
| 78 |
if err := rootCmd.Flags().Set("format", tt.formatFlag); err != nil { |
| 79 |
t.Fatalf("Failed to set format flag: %v", err) |
| 80 |
} |
| 81 |
|
| 82 |
got := runDocs(rootCmd, tt.args) |
| 83 |
|
| 84 |
assert.Equal(t, tt.wantExit, got.ExitCode, "Exit code mismatch") |
| 85 |
|
| 86 |
if tt.wantErrMsg != "" { |
| 87 |
assert.Contains(t, got.Message, tt.wantErrMsg, "Error message should contain expected text") |
| 88 |
} |
| 89 |
}) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// Test_runDocs_FlagError tests the error handling when flag retrieval fails. |
| 94 |
func Test_runDocs_FlagError(t *testing.T) { |
| 95 |
t.Parallel() |
| 96 |
|
| 97 |
// Create a cobra command without the format flag to simulate an error |
| 98 |
rootCmd := &cobra.Command{ |
| 99 |
Use: "docs", |
| 100 |
} |
| 101 |
// Do not add the format flag - this will cause GetString to return an error |
| 102 |
|
| 103 |
got := runDocs(rootCmd, []string{"discord"}) |
| 104 |
|
| 105 |
assert.Equal(t, 1, got.ExitCode, "Exit code should be 1 on flag error") |
| 106 |
assert.Contains(t, got.Message, "Error getting format flag", "Error message should indicate flag retrieval failure") |
| 107 |
} |
| 108 |
|
| 109 |
// Test_printDocs tests the printDocs function with various scenarios. |
| 110 |
func Test_printDocs(t *testing.T) { |
| 111 |
t.Parallel() |
| 112 |
|
| 113 |
tests := []struct { |
| 114 |
name string |
| 115 |
docFormat string |
| 116 |
services []string |
| 117 |
wantExit int |
| 118 |
wantErrMsg string |
| 119 |
}{ |
| 120 |
{ |
| 121 |
name: "console render success", |
| 122 |
docFormat: "console", |
| 123 |
services: []string{"discord"}, |
| 124 |
wantExit: cmd.ExSuccess, |
| 125 |
wantErrMsg: "", |
| 126 |
}, |
| 127 |
{ |
| 128 |
name: "markdown render success", |
| 129 |
docFormat: "markdown", |
| 130 |
services: []string{"slack"}, |
| 131 |
wantExit: cmd.ExSuccess, |
| 132 |
wantErrMsg: "", |
| 133 |
}, |
| 134 |
{ |
| 135 |
name: "multiple services console", |
| 136 |
docFormat: "console", |
| 137 |
services: []string{"discord", "slack"}, |
| 138 |
wantExit: cmd.ExSuccess, |
| 139 |
wantErrMsg: "", |
| 140 |
}, |
| 141 |
{ |
| 142 |
name: "multiple services markdown", |
| 143 |
docFormat: "markdown", |
| 144 |
services: []string{"smtp", "telegram"}, |
| 145 |
wantExit: cmd.ExSuccess, |
| 146 |
wantErrMsg: "", |
| 147 |
}, |
| 148 |
{ |
| 149 |
name: "invalid format returns ExUsage", |
| 150 |
docFormat: "xml", |
| 151 |
services: []string{"discord"}, |
| 152 |
wantExit: cmd.ExUsage, |
| 153 |
wantErrMsg: "invalid format", |
| 154 |
}, |
| 155 |
{ |
| 156 |
name: "unknown service returns ExUsage", |
| 157 |
docFormat: "console", |
| 158 |
services: []string{"notarealservice"}, |
| 159 |
wantExit: cmd.ExUsage, |
| 160 |
wantErrMsg: "failed to init service", |
| 161 |
}, |
| 162 |
{ |
| 163 |
name: "empty format defaults to console", |
| 164 |
docFormat: "", |
| 165 |
services: []string{"discord"}, |
| 166 |
wantExit: cmd.ExUsage, |
| 167 |
wantErrMsg: "invalid format", |
| 168 |
}, |
| 169 |
} |
| 170 |
|
| 171 |
for _, tt := range tests { |
| 172 |
t.Run(tt.name, func(t *testing.T) { |
| 173 |
t.Parallel() |
| 174 |
|
| 175 |
got := printDocs(tt.docFormat, tt.services) |
| 176 |
|
| 177 |
assert.Equal(t, tt.wantExit, got.ExitCode, "Exit code mismatch") |
| 178 |
|
| 179 |
if tt.wantErrMsg != "" { |
| 180 |
assert.Contains(t, got.Message, tt.wantErrMsg, "Error message should contain expected text") |
| 181 |
} |
| 182 |
}) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
// Test_printDocs_EdgeCases tests edge cases for printDocs. |
| 187 |
func Test_printDocs_EdgeCases(t *testing.T) { |
| 188 |
t.Parallel() |
| 189 |
|
| 190 |
tests := []struct { |
| 191 |
name string |
| 192 |
docFormat string |
| 193 |
services []string |
| 194 |
wantExit int |
| 195 |
}{ |
| 196 |
{ |
| 197 |
name: "empty services slice", |
| 198 |
docFormat: "console", |
| 199 |
services: []string{}, |
| 200 |
wantExit: cmd.ExSuccess, |
| 201 |
}, |
| 202 |
{ |
| 203 |
name: "case insensitive service lookup", |
| 204 |
docFormat: "console", |
| 205 |
services: []string{"DISCORD"}, |
| 206 |
wantExit: cmd.ExSuccess, |
| 207 |
}, |
| 208 |
} |
| 209 |
|
| 210 |
for _, tt := range tests { |
| 211 |
t.Run(tt.name, func(t *testing.T) { |
| 212 |
t.Parallel() |
| 213 |
|
| 214 |
got := printDocs(tt.docFormat, tt.services) |
| 215 |
assert.Equal(t, tt.wantExit, got.ExitCode, "Exit code mismatch") |
| 216 |
}) |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
// TestCmd_ValidArgs verifies that the Cmd has valid service arguments. |
| 221 |
func TestCmd_ValidArgs(t *testing.T) { |
| 222 |
t.Parallel() |
| 223 |
|
| 224 |
// Verify that services variable is populated |
| 225 |
assert.NotEmpty(t, services, "services list should not be empty") |
| 226 |
|
| 227 |
// Verify Cmd has ValidArgs set |
| 228 |
assert.NotEmpty(t, Cmd.ValidArgs, "Cmd.ValidArgs should not be empty") |
| 229 |
assert.Equal(t, services, Cmd.ValidArgs, "Cmd.ValidArgs should match services list") |
| 230 |
} |
| 231 |
|
| 232 |
// TestCmd_Flags verifies that the Cmd has the expected flags. |
| 233 |
func TestCmd_Flags(t *testing.T) { |
| 234 |
t.Parallel() |
| 235 |
|
| 236 |
// Check that the format flag exists |
| 237 |
formatFlag := Cmd.Flags().Lookup("format") |
| 238 |
assert.NotNil(t, formatFlag, "format flag should exist") |
| 239 |
assert.Equal(t, "console", formatFlag.DefValue, "format flag default should be 'console'") |
| 240 |
assert.Equal(t, "f", formatFlag.Shorthand, "format flag shorthand should be 'f'") |
| 241 |
} |