MCPcopy Index your code
hub / github.com/coder/coder / TestPrintDeprecatedOptions

Function TestPrintDeprecatedOptions

cli/root_internal_test.go:243–382  ·  view source on GitHub ↗

nolint:tparallel,paralleltest // This test modifies environment variables.

(t *testing.T)

Source from the content-addressed store, hash-verified

241
242//nolint:tparallel,paralleltest // This test modifies environment variables.
243func TestPrintDeprecatedOptions(t *testing.T) {
244 newValue := serpent.StringOf(new(string))
245
246 // Both the "new" option and the deprecated option point at the
247 // same Value, mirroring how codersdk/deployment.go wires the
248 // CODER_EMAIL_* / CODER_NOTIFICATIONS_EMAIL_* pairs.
249 newOpt := serpent.Option{
250 Name: "new-option",
251 Flag: "new-option",
252 Env: "CODER_TEST_NEW_OPTION",
253 Value: newValue,
254 }
255 deprecatedOpt := serpent.Option{
256 Name: "old-option",
257 Flag: "old-option",
258 Env: "CODER_TEST_OLD_OPTION",
259 Value: newValue, // same pointer
260 UseInstead: serpent.OptionSet{newOpt},
261 }
262
263 makeCmd := func(opts serpent.OptionSet) *serpent.Command {
264 return &serpent.Command{
265 Use: "test",
266 Options: opts,
267 Middleware: PrintDeprecatedOptions(),
268 Handler: func(_ *serpent.Invocation) error {
269 return nil
270 },
271 }
272 }
273
274 t.Run("EnvOnlyNew_NoWarning", func(t *testing.T) {
275 t.Setenv("CODER_TEST_NEW_OPTION", "val")
276
277 cmd := makeCmd(serpent.OptionSet{newOpt, deprecatedOpt})
278 var stderr bytes.Buffer
279 inv := cmd.Invoke()
280 inv.Environ = serpent.ParseEnviron(os.Environ(), "")
281 inv.Stderr = &stderr
282 err := inv.Run()
283 require.NoError(t, err)
284 require.Empty(t, stderr.String(),
285 "setting only the new env var should not produce a deprecation warning")
286 })
287
288 t.Run("EnvOnlyOld_Warning", func(t *testing.T) {
289 t.Setenv("CODER_TEST_OLD_OPTION", "val")
290
291 cmd := makeCmd(serpent.OptionSet{newOpt, deprecatedOpt})
292 var stderr bytes.Buffer
293 inv := cmd.Invoke()
294 inv.Environ = serpent.ParseEnviron(os.Environ(), "")
295 inv.Stderr = &stderr
296 err := inv.Run()
297 require.NoError(t, err)
298 require.Contains(t, stderr.String(), "is deprecated",
299 "setting the deprecated env var should produce a warning")
300 })

Callers

nothing calls this directly

Calls 9

PrintDeprecatedOptionsFunction · 0.85
SetenvMethod · 0.80
RunMethod · 0.65
EnvironMethod · 0.65
InvokeMethod · 0.45
EmptyMethod · 0.45
StringMethod · 0.45
ContainsMethod · 0.45
OptionsMethod · 0.45

Tested by

no test coverage detected