test: simplify allowed command checks using slices.Contains

This commit is contained in:
Ruslan Semagin 2026-02-24 09:22:46 +03:00
parent 0d63e18880
commit c45811f2d9
3 changed files with 27 additions and 24 deletions

View file

@ -1,6 +1,7 @@
package auth package auth
import ( import (
"slices"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -26,18 +27,18 @@ func TestNewAuthCommand(t *testing.T) {
assert.False(t, cmd.HasFlags()) assert.False(t, cmd.HasFlags())
assert.True(t, cmd.HasSubCommands()) assert.True(t, cmd.HasSubCommands())
allowedCommands := map[string]struct{}{ allowedCommands := []string{
"login": {}, "login",
"logout": {}, "logout",
"status": {}, "status",
"models": {}, "models",
} }
subcommands := cmd.Commands() subcommands := cmd.Commands()
assert.Len(t, subcommands, len(allowedCommands)) assert.Len(t, subcommands, len(allowedCommands))
for _, subcmd := range subcommands { for _, subcmd := range subcommands {
_, found := allowedCommands[subcmd.Name()] found := slices.Contains(allowedCommands, subcmd.Name())
assert.True(t, found, "unexpected subcommand %q", subcmd.Name()) assert.True(t, found, "unexpected subcommand %q", subcmd.Name())
assert.Len(t, subcmd.Aliases, 0) assert.Len(t, subcmd.Aliases, 0)

View file

@ -1,6 +1,7 @@
package cron package cron
import ( import (
"slices"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -28,19 +29,19 @@ func TestNewCronCommand(t *testing.T) {
assert.True(t, cmd.HasSubCommands()) assert.True(t, cmd.HasSubCommands())
allowedCommands := map[string]struct{}{ allowedCommands := []string{
"list": {}, "list",
"add": {}, "add",
"remove": {}, "remove",
"enable": {}, "enable",
"disable": {}, "disable",
} }
subcommands := cmd.Commands() subcommands := cmd.Commands()
assert.Len(t, subcommands, len(allowedCommands)) assert.Len(t, subcommands, len(allowedCommands))
for _, subcmd := range subcommands { for _, subcmd := range subcommands {
_, found := allowedCommands[subcmd.Name()] found := slices.Contains(allowedCommands, subcmd.Name())
assert.True(t, found, "unexpected subcommand %q", subcmd.Name()) assert.True(t, found, "unexpected subcommand %q", subcmd.Name())
assert.Len(t, subcmd.Aliases, 0) assert.Len(t, subcmd.Aliases, 0)

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"slices"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -26,23 +27,23 @@ func TestNewPicoclawCommand(t *testing.T) {
assert.Nil(t, cmd.PersistentPreRun) assert.Nil(t, cmd.PersistentPreRun)
assert.Nil(t, cmd.PersistentPostRun) assert.Nil(t, cmd.PersistentPostRun)
allowedCommands := map[string]struct{}{ allowedCommands := []string{
"agent": {}, "agent",
"auth": {}, "auth",
"cron": {}, "cron",
"gateway": {}, "gateway",
"migrate": {}, "migrate",
"onboard": {}, "onboard",
"skills": {}, "skills",
"status": {}, "status",
"version": {}, "version",
} }
subcommands := cmd.Commands() subcommands := cmd.Commands()
assert.Len(t, subcommands, len(allowedCommands)) assert.Len(t, subcommands, len(allowedCommands))
for _, subcmd := range subcommands { for _, subcmd := range subcommands {
_, found := allowedCommands[subcmd.Name()] found := slices.Contains(allowedCommands, subcmd.Name())
assert.True(t, found, "unexpected subcommand %q", subcmd.Name()) assert.True(t, found, "unexpected subcommand %q", subcmd.Name())
assert.False(t, subcmd.Hidden) assert.False(t, subcmd.Hidden)