diff --git a/internal/cmd/calendar/calendar.go b/internal/cmd/calendar/calendar.go index 671c76d..ee2f673 100644 --- a/internal/cmd/calendar/calendar.go +++ b/internal/cmd/calendar/calendar.go @@ -90,10 +90,13 @@ func newCmdSet() *cobra.Command { Short: "Replace calendar section subscriptions", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + if strings.TrimSpace(semesterID) == "" { + return fmt.Errorf("--semester-id is required for calendar set") + } return runCalendarBatch(cmd, args, openapi.CalendarSubscriptionBatchRequestSchemaActionSet, semesterID) }, } - cmd.Flags().StringVar(&semesterID, "semester-id", "", "Semester ID to narrow code matches") + cmd.Flags().StringVar(&semesterID, "semester-id", "", "Semester ID whose subscriptions will be replaced") return cmd } diff --git a/internal/cmd/calendar/calendar_test.go b/internal/cmd/calendar/calendar_test.go new file mode 100644 index 0000000..9cb40a5 --- /dev/null +++ b/internal/cmd/calendar/calendar_test.go @@ -0,0 +1,29 @@ +package calendar + +import ( + "strings" + "testing" +) + +func TestSetRequiresSemesterID(t *testing.T) { + for _, args := range [][]string{ + {"999999"}, + {"999999", "--semester-id", " "}, + } { + t.Run(strings.Join(args, " "), func(t *testing.T) { + t.Setenv("LIFE_USTC_CONFIG_DIR", t.TempDir()) + t.Setenv("LIFE_USTC_SERVER", "http://127.0.0.1:1") + + cmd := newCmdSet() + cmd.SetArgs(args) + + err := cmd.Execute() + if err == nil { + t.Fatal("expected calendar set without --semester-id to fail") + } + if !strings.Contains(err.Error(), "--semester-id is required") { + t.Fatalf("unexpected error: %v", err) + } + }) + } +}