Skip to content

Commit a1edfb1

Browse files
committed
assert profile history in TestScripts/multi
1 parent d295708 commit a1edfb1

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

testscripts/rm/multi.test.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ exec devbox rm vim hello
1010

1111
json.superset devbox.json expected.json
1212

13+
# Check that profile history was cleaned up. There should only be
14+
# default and default-N-link.
15+
glob -count=2 .devbox/nix/profile/*
16+
1317
-- expected.json --
1418
{
1519
"packages": []

testscripts/testrunner/testrunner.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,54 @@ func copyFileCmd(script *testscript.TestScript, neg bool, args []string) {
8080
script.Check(err)
8181
}
8282

83+
func globCmd(script *testscript.TestScript, neg bool, args []string) {
84+
count := -1
85+
if neg {
86+
count = 0
87+
}
88+
if len(args) != 0 {
89+
after, ok := strings.CutPrefix(args[0], "-count=")
90+
if ok {
91+
var err error
92+
count, err = strconv.Atoi(after)
93+
if err != nil {
94+
script.Fatalf("invalid -count=: %v", err)
95+
}
96+
if count < 1 {
97+
script.Fatalf("invalid -count=: must be at least 1")
98+
}
99+
args = args[1:]
100+
}
101+
}
102+
if len(args) == 0 {
103+
script.Fatalf("usage: glob [-count=N] pattern")
104+
}
105+
106+
var matches []string
107+
for _, a := range args {
108+
glob := script.MkAbs(a)
109+
m, err := filepath.Glob(glob)
110+
if err != nil {
111+
script.Fatalf("invalid glob pattern: %v", err)
112+
}
113+
for _, match := range m {
114+
script.Logf("glob %q matched: %s", glob, match)
115+
}
116+
matches = append(matches, m...)
117+
}
118+
119+
// -1 means that no -count= was given, so we want at least 1 match.
120+
if count == -1 {
121+
if len(matches) == 0 && !neg {
122+
script.Fatalf("no matches for globs %q, want at least 1", strings.Join(args, " "))
123+
}
124+
return
125+
}
126+
if len(matches) != count {
127+
script.Fatalf("got %d matches for globs %q, want %d", len(matches), strings.Join(args, " "), count)
128+
}
129+
}
130+
83131
func getTestscriptParams(dir string) testscript.Params {
84132
return testscript.Params{
85133
Dir: dir,
@@ -91,6 +139,7 @@ func getTestscriptParams(dir string) testscript.Params {
91139
"devboxjson.packages.contains": assertDevboxJSONPackagesContains,
92140
"devboxlock.packages.contains": assertDevboxLockPackagesContains,
93141
"env.path.len": assertPathLength,
142+
"glob": globCmd,
94143
"json.superset": assertJSONSuperset,
95144
"path.order": assertPathOrder,
96145
"source.path": sourcePath,

0 commit comments

Comments
 (0)