-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
71 lines (62 loc) · 1.98 KB
/
main_test.go
File metadata and controls
71 lines (62 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"regexp"
"testing"
)
func TestCommandGeneration(t *testing.T) {
pkgName := "testpkg"
tests := []struct {
pmName string
action string // Install, Uninstall, Search, etc.
expectedTemplate string
expectedCmd string
}{
{"dnf", "install", "dnf install -y x", "dnf install -y testpkg"},
{"pacman", "install", "pacman -S --noconfirm x", "pacman -S --noconfirm testpkg"},
{"yum", "search", "yum search x", "yum search testpkg"},
{"zypper", "info", "zypper info x", "zypper info testpkg"},
{"apk", "upgrade", "apk add --upgrade x", "apk add --upgrade testpkg"},
{"xbps", "uninstall", "xbps-remove -y x", "xbps-remove -y testpkg"},
{"nix-env", "install", "nix-env -iA nixpkgs.x", "nix-env -iA nixpkgs.testpkg"},
// Add checks for multi-word commands or flags
{"apt", "list", "apt list --installed", "apt list --installed"},
}
for _, tt := range tests {
cmds, ok := pm_commands[tt.pmName]
if !ok {
t.Errorf("Package manager %s not found in pm_commands", tt.pmName)
continue
}
var template string
switch tt.action {
case "install":
template = cmds.Install
case "uninstall":
template = cmds.Uninstall
case "search":
template = cmds.Search
case "info":
template = cmds.Info
case "upgrade":
template = cmds.Upgrade
case "list":
template = cmds.ListInstalled
}
if template != tt.expectedTemplate {
t.Errorf("[%s] %s template mismatch: got %q, want %q", tt.pmName, tt.action, template, tt.expectedTemplate)
}
re := regexp.MustCompile(`\bx\b`)
// Use ReplaceAllStringFunc to avoid interpreting $ in pkgName
cmdStr := re.ReplaceAllStringFunc(template, func(s string) string {
return pkgName
})
// Fix expectation for list if we passed empty string
expected := tt.expectedCmd
if tt.action == "list" {
expected = "apt list --installed" // no change
}
if cmdStr != expected {
t.Errorf("[%s] %s command mismatch: got %q, want %q", tt.pmName, tt.action, cmdStr, expected)
}
}
}