Skip to content

Commit 37cde08

Browse files
authored
Add testscript tests for PATH setting (#646)
## Summary Tests that the PATH is built in the desired order. Had to add a `print` function and `path.order` assertion function. Let me know if you have any other ideas! ## How was it tested? ``` go test ./testscripts/testscripts_test.go ```
1 parent 446280a commit 37cde08

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

testscripts/assert/assert.test.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Tests assert functions (since some are relatively complex)
2+
3+
exec print a:b:c:d:e:fg:h
4+
path.order 'a' 'c' 'e'
5+
! path.order 'a' 'b' 'a'
6+
path.order 'a' 'f'
7+
! path.order 'a' 'c' 'h' 'h'

testscripts/run/path.test.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ exec devbox run echo '$PATH'
1919
! stdout '/some//dirty/../clean/path'
2020
stdout '/some/clean/path'
2121

22-
# Path contains path to installed nix packages
23-
stdout '-which-'
24-
2522
# Path contains plugin virtual env path
2623
stdout '.devbox/virtenv/bin'
2724

28-
# Path contains devbox-managed nix profiel path
25+
# Path contains devbox-managed nix profile path
2926
stdout '.devbox/nix/profile/default/bin'
3027

31-
# TODO: verify that paths appear in desired order.
28+
# Path contains path to installed nix packages
29+
stdout '-which-'
30+
31+
# Verify PATH is set in correct order: virtual env path, nix profile, nix packages, host path.
32+
path.order '.devbox/virtenv/bin' '.devbox/nix/profile/default/bin' '-which-' 'some/clean/path'
33+
3234
# TODO: verify that bashrc file prepends do not prepend before nix paths.

testscripts/testrunner/assert.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
var assertionMap = map[string]func(ts *testscript.TestScript, neg bool, args []string){
1414
"env.path.len": assertPathLength,
1515
"json.superset": assertJSONSuperset,
16+
"path.order": assertPathOrder,
1617
}
1718

1819
// Usage: env.path.len <number>
@@ -69,3 +70,49 @@ func assertJSONSuperset(script *testscript.TestScript, neg bool, args []string)
6970
}
7071
}
7172
}
73+
74+
// Usage: path.order 'a' 'b' 'c'
75+
// Checks that whatever is in stdout, P, is a string in PATH format (i.e. colon-separated strings), and that
76+
// every one of the arguments ('a', 'b', and 'c') are contained in separate subpaths of P, exactly once, and
77+
// in order.
78+
func assertPathOrder(script *testscript.TestScript, neg bool, args []string) {
79+
path := script.ReadFile("stdout")
80+
subpaths := strings.Split(strings.Replace(path, "\n", "", -1), ":")
81+
82+
allInOrder := containsInOrder(subpaths, args)
83+
if !neg && !allInOrder {
84+
script.Fatalf("Did not find all expected in order in subpaths.\n\nSubpaths: %v\nExpected: %v", subpaths, args)
85+
}
86+
if neg && allInOrder {
87+
script.Fatalf("Found all expected in subpaths.\n\nSubpaths: %v\nExpected: %v", subpaths, args)
88+
}
89+
}
90+
91+
func containsInOrder(subpaths []string, expected []string) bool {
92+
if len(expected) == 0 {
93+
return true // no parts passed in, assertion trivially holds.
94+
}
95+
96+
if len(subpaths) < len(expected) {
97+
return false
98+
}
99+
100+
i := 0
101+
j := 0
102+
outer:
103+
for j < len(expected) {
104+
currentExpected := expected[j]
105+
for i < len(subpaths) {
106+
if strings.Contains(subpaths[i], currentExpected) {
107+
j++
108+
i++
109+
continue outer // found expected, move on to the next expected
110+
} else {
111+
i++ // didn't find it, try the next subpath
112+
}
113+
}
114+
return false // ran out of subpaths, but not out of expected, so we fail.
115+
}
116+
117+
return true // if we're here, we found everything
118+
}

testscripts/testrunner/testrunner.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package testrunner
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67
"path/filepath"
8+
"strings"
79
"testing"
810

911
"github.com/bmatcuk/doublestar/v4"
@@ -18,6 +20,10 @@ func Main(m *testing.M) int {
1820
// Call the devbox CLI directly:
1921
return boxcli.Execute(context.Background(), os.Args[1:])
2022
},
23+
"print": func() int { // Not 'echo' because we don't expand variables
24+
fmt.Println(strings.Join(os.Args[1:], " "))
25+
return 0
26+
},
2127
}
2228
return testscript.RunMain(m, commands)
2329
}

0 commit comments

Comments
 (0)