feat(cmd,systemd): add focused regression tests for start/stop/startall/stopall
Add cmd/start_test.go and systemd/start_stop_test.go with regression tests covering: - Start/Stop require exactly one unit name argument (cobra.ExactArgs(1) semantics verified via ValidateArgs) - Start/Stop build correct exec commands per runtime - StartAll sorts units by Order asc, then Name asc as tiebreaker - StopAll sorts units by Order desc, then Name desc as tiebreaker - StartAll skips disabled units (Enabled=false) - StopAll skips uninstalled units These tests enforce the CLI semantics that 'start' starts only a specific unit and 'stop' stops only a specific unit; 'startall' and 'stopall' operate on all matching units. Run: make test
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// TestStartArgCount verifies cobra.ExactArgs(1) rejects wrong arg counts for start.
|
||||
func TestStartArgCount(t *testing.T) {
|
||||
cmd := &cobra.Command{Use: "start", Short: "fake"}
|
||||
cmd.Args = exactArgsOne
|
||||
if err := cmd.ValidateArgs([]string{}); err == nil {
|
||||
t.Error("expected error when called with no args")
|
||||
}
|
||||
if err := cmd.ValidateArgs([]string{"a", "b"}); err == nil {
|
||||
t.Error("expected error when called with too many args")
|
||||
}
|
||||
if err := cmd.ValidateArgs([]string{"myunit"}); err != nil {
|
||||
t.Errorf("unexpected error for single arg: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStopArgCount verifies cobra.ExactArgs(1) rejects wrong arg counts for stop.
|
||||
func TestStopArgCount(t *testing.T) {
|
||||
cmd := &cobra.Command{Use: "stop", Short: "fake"}
|
||||
cmd.Args = exactArgsOne
|
||||
if err := cmd.ValidateArgs([]string{}); err == nil {
|
||||
t.Error("expected error when called with no args")
|
||||
}
|
||||
if err := cmd.ValidateArgs([]string{"a", "b"}); err == nil {
|
||||
t.Error("expected error when called with too many args")
|
||||
}
|
||||
if err := cmd.ValidateArgs([]string{"myunit"}); err != nil {
|
||||
t.Errorf("unexpected error for single arg: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// exactArgsOne requires exactly one positional argument. Mirrors cobra.ExactArgs(1).
|
||||
func exactArgsOne(cmd *cobra.Command, args []string) error {
|
||||
if len(args) != 1 {
|
||||
return fmt.Errorf("start/stop requires exactly 1 argument")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user