mirror of
https://github.com/taigrr/systemctl.git
synced 2026-03-09 00:14:38 -07:00
Allow callers to pass additional systemctl flags (e.g. --no-block, --force) via variadic string args on every exported function. This is backward-compatible: existing callers without extra args continue to work unchanged. Introduces a prepareArgs helper to centralize argument construction, replacing the duplicated args/UserMode pattern across all functions. Closes #2
63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
package systemctl
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestPrepareArgs(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
base string
|
|
opts Options
|
|
extra []string
|
|
expected []string
|
|
}{
|
|
{
|
|
name: "system mode no extra",
|
|
base: "start",
|
|
opts: Options{},
|
|
extra: nil,
|
|
expected: []string{"start", "--system"},
|
|
},
|
|
{
|
|
name: "user mode no extra",
|
|
base: "start",
|
|
opts: Options{UserMode: true},
|
|
extra: nil,
|
|
expected: []string{"start", "--user"},
|
|
},
|
|
{
|
|
name: "system mode with unit",
|
|
base: "start",
|
|
opts: Options{},
|
|
extra: []string{"nginx.service"},
|
|
expected: []string{"start", "--system", "nginx.service"},
|
|
},
|
|
{
|
|
name: "user mode with unit and extra args",
|
|
base: "restart",
|
|
opts: Options{UserMode: true},
|
|
extra: []string{"foo.service", "--no-block"},
|
|
expected: []string{"restart", "--user", "foo.service", "--no-block"},
|
|
},
|
|
{
|
|
name: "daemon-reload no extra",
|
|
base: "daemon-reload",
|
|
opts: Options{},
|
|
extra: nil,
|
|
expected: []string{"daemon-reload", "--system"},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := prepareArgs(tt.base, tt.opts, tt.extra...)
|
|
if !reflect.DeepEqual(got, tt.expected) {
|
|
t.Errorf("prepareArgs(%q, %+v, %v) = %v, want %v",
|
|
tt.base, tt.opts, tt.extra, got, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|