gitlink-cli/cmd/interactive/all_commands_test.go

71 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package interactive
import (
"strings"
"testing"
"github.com/gitlink-org/gitlink-cli/shortcuts"
)
// TestAllCommandsExecuteWithoutPanic 验证所有注册的命令都能安全执行:
// 不 panic、恢复 stdout、且产生输出或合理的错误。
// 这是回归测试,防止之前 "选择命令后无输出" 的 nil-Client panic 复发。
func TestAllCommandsExecuteWithoutPanic(t *testing.T) {
all := shortcuts.GetAllShortcuts()
if len(all) == 0 {
t.Fatal("GetAllShortcuts returned empty registry")
}
exec := &Executor{}
totalCommands := 0
silentFailures := 0
for group, shortcuts := range all {
for _, s := range shortcuts {
if s.Run == nil {
continue
}
totalCommands++
// 用空参数执行;需要 owner/repo/必填参数的命令会返回错误,这是正常的
out, err := exec.Execute(s, map[string]string{})
// 断言1不能既无输出又无错误静默失败
if strings.TrimSpace(out) == "" && err == nil {
silentFailures++
t.Errorf("[%s +%s] 静默失败:无输出且无错误", group, s.Name)
}
// 断言2错误信息不应包含 "panic"(说明 recover 被触发,命令有 bug
if err != nil && strings.Contains(err.Error(), "panic") {
t.Errorf("[%s +%s] 命令 panic: %v", group, s.Name, err)
}
}
}
t.Logf("测试了 %d 个命令,%d 个静默失败", totalCommands, silentFailures)
if totalCommands < 50 {
t.Errorf("命令总数 %d 偏少,预期至少 50 个", totalCommands)
}
}
// TestAllCommandsHaveMetadata 验证所有命令都有名称、描述和 Run 函数。
func TestAllCommandsHaveMetadata(t *testing.T) {
all := shortcuts.GetAllShortcuts()
for group, shortcuts := range all {
if len(shortcuts) == 0 {
t.Errorf("命令组 %q 没有任何子命令", group)
}
for _, s := range shortcuts {
if s.Name == "" {
t.Errorf("[%s] 有命令名称为空", group)
}
if s.Description == "" {
t.Errorf("[%s +%s] 描述为空", group, s.Name)
}
if s.Run == nil {
t.Errorf("[%s +%s] Run 函数为空", group, s.Name)
}
}
}
}