MCPcopy Create free account
hub / github.com/coder/agentapi / TestServerCmd_StatePersistenceFlags

Function TestServerCmd_StatePersistenceFlags

cmd/server/server_test.go:482–600  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

480}
481
482func TestServerCmd_StatePersistenceFlags(t *testing.T) {
483 // NOTE: These tests use --exit flag to test flag parsing and defaults.
484 // Runtime validation that happens in runServer (e.g., "--load-state requires --state-file")
485 // would call os.Exit(1) which terminates the test process, so those validations
486 // are tested through integration/E2E tests instead.
487
488 t.Run("state-file with defaults", func(t *testing.T) {
489 isolateViper(t)
490
491 serverCmd := CreateServerCmd()
492 setupCommandOutput(t, serverCmd)
493 serverCmd.SetArgs([]string{"--state-file", "/tmp/state.json", "--exit", "dummy-command"})
494 err := serverCmd.Execute()
495 require.NoError(t, err)
496
497 assert.Equal(t, "/tmp/state.json", viper.GetString(FlagStateFile))
498 // load-state and save-state default to true when state-file is set (validated in runServer)
499 })
500
501 t.Run("state-file with explicit load-state=false", func(t *testing.T) {
502 isolateViper(t)
503
504 serverCmd := CreateServerCmd()
505 setupCommandOutput(t, serverCmd)
506 serverCmd.SetArgs([]string{"--state-file", "/tmp/state.json", "--load-state=false", "--exit", "dummy-command"})
507 err := serverCmd.Execute()
508 require.NoError(t, err)
509
510 assert.Equal(t, "/tmp/state.json", viper.GetString(FlagStateFile))
511 assert.Equal(t, false, viper.GetBool(FlagLoadState))
512 })
513
514 t.Run("state-file with explicit save-state=false", func(t *testing.T) {
515 isolateViper(t)
516
517 serverCmd := CreateServerCmd()
518 setupCommandOutput(t, serverCmd)
519 serverCmd.SetArgs([]string{"--state-file", "/tmp/state.json", "--save-state=false", "--exit", "dummy-command"})
520 err := serverCmd.Execute()
521 require.NoError(t, err)
522
523 assert.Equal(t, "/tmp/state.json", viper.GetString(FlagStateFile))
524 assert.Equal(t, false, viper.GetBool(FlagSaveState))
525 })
526
527 t.Run("state-file with explicit load-state=true and save-state=true", func(t *testing.T) {
528 isolateViper(t)
529
530 serverCmd := CreateServerCmd()
531 setupCommandOutput(t, serverCmd)
532 serverCmd.SetArgs([]string{
533 "--state-file", "/tmp/state.json",
534 "--load-state=true",
535 "--save-state=true",
536 "--exit", "dummy-command",
537 })
538 err := serverCmd.Execute()
539 require.NoError(t, err)

Callers

nothing calls this directly

Calls 3

isolateViperFunction · 0.85
CreateServerCmdFunction · 0.85
setupCommandOutputFunction · 0.85

Tested by

no test coverage detected