feat: 新增 disk usage 内置命令(跨平台 du 等价)

- 注册表新增 Variant.Builtin 内置动作,与 Argv 互斥并参与平台匹配
- disk.usage 支持位置参数与 --path/--path=,输出按大小降序并带合计
- Unix 用设备号实现 -x 不跨文件系统,Windows 降级为不跟随 reparse point
- list/help/explain 支持展示内置动作,补充单元与 CLI 测试、README
This commit is contained in:
2026-10-31 13:11:24 +08:00
parent 7d9852958f
commit 84f7361396
12 changed files with 672 additions and 9 deletions
+40 -1
View File
@@ -15,7 +15,7 @@ func TestBuiltinIsValid(t *testing.T) {
func TestBuiltinIDsStable(t *testing.T) {
reg := Builtin()
want := []string{"docker.clearsafe", "net.ports", "pkg.refreshindex"}
want := []string{"docker.clearsafe", "disk.usage", "net.ports", "pkg.refreshindex"}
if got := reg.IDs(); !reflect.DeepEqual(got, want) {
t.Fatalf("IDs() = %v, want %v", got, want)
}
@@ -132,6 +132,45 @@ func TestValidateErrors(t *testing.T) {
}
}
func TestValidateBuiltinVariants(t *testing.T) {
builtinOnly := plainCommand("a")
builtinOnly.Variants = []Variant{{Match: Match{OS: "linux"}, Builtin: "demo.action"}}
if err := (&Registry{Commands: []Command{builtinOnly}}).Validate(); err != nil {
t.Fatalf("builtin-only variant should be valid: %v", err)
}
both := plainCommand("a")
both.Variants = []Variant{{Match: Match{OS: "linux"}, Argv: []string{"x"}, Builtin: "demo.action"}}
err := (&Registry{Commands: []Command{both}}).Validate()
if err == nil || !strings.Contains(err.Error(), "mutually exclusive") {
t.Fatalf("err = %v, want mutual-exclusion error", err)
}
}
func TestDiskUsageIsBuiltinEverywhere(t *testing.T) {
reg := Builtin()
cmd, ok := reg.Lookup("disk.usage")
if !ok {
t.Fatal("disk.usage not found")
}
seen := map[string]bool{}
for _, variant := range cmd.Variants {
if variant.Builtin != "disk.usage" {
t.Errorf("variant for %s should use the disk.usage builtin, got %q", variant.Match.OS, variant.Builtin)
}
if len(variant.Argv) != 0 {
t.Errorf("builtin variant must not carry argv: %v", variant.Argv)
}
seen[variant.Match.OS] = true
}
for _, osName := range []string{"linux", "darwin", "windows"} {
if !seen[osName] {
t.Errorf("disk.usage lacks a %s variant", osName)
}
}
}
func TestMatchIsComparable(t *testing.T) {
a := Match{OS: "linux", Distro: "centos", DistroLike: []string{"rhel"}, Runtime: "docker"}
b := Match{OS: "linux", Distro: "centos", DistroLike: []string{"rhel"}, Runtime: "docker"}