Skip to content

Commit 8f62edb

Browse files
authored
Improve devbox plan test for go and python (#85)
## Summary Improve the devbox plan tests. Add tests for each go version we support. Add `plan.json` to both go and python examples. ## How was it tested? `go test -v ./...`
1 parent b5c8991 commit 8f62edb

File tree

16 files changed

+152
-21
lines changed

16 files changed

+152
-21
lines changed

devbox_test.go

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,49 @@ import (
1111
"go.jetpack.io/devbox/planner"
1212
)
1313

14-
func TestPlan(t *testing.T) {
14+
func TestDevboxPlan(t *testing.T) {
1515
testPaths, err := doublestar.FilepathGlob("./testdata/**/devbox.json")
1616
assert.NoError(t, err, "Reading testdata/ should not fail")
17-
assert.Greater(t, len(testPaths), 0, "testdata/ should contain at least 1 test")
17+
18+
examplePaths, err := doublestar.FilepathGlob("./examples/**/devbox.json")
19+
assert.NoError(t, err, "Reading examples/ should not fail")
20+
21+
testPaths = append(testPaths, examplePaths...)
22+
assert.Greater(t, len(testPaths), 0, "testdata/ and examples/ should contain at least 1 test")
1823

1924
for _, testPath := range testPaths {
20-
baseDir := filepath.Dir(testPath)
21-
t.Run(baseDir, func(t *testing.T) {
22-
assert := assert.New(t)
23-
goldenFile := filepath.Join(baseDir, "plan.json")
24-
hasGoldenFile := fileExists(goldenFile)
25-
26-
box, err := Open(baseDir)
27-
assert.NoErrorf(err, "%s should be a valid devbox project", baseDir)
28-
plan := box.Plan()
25+
testIndividualPlan(t, testPath)
26+
}
27+
}
28+
29+
func testIndividualPlan(t *testing.T, testPath string) {
30+
baseDir := filepath.Dir(testPath)
31+
t.Run(baseDir, func(t *testing.T) {
32+
assert := assert.New(t)
33+
goldenFile := filepath.Join(baseDir, "plan.json")
34+
hasGoldenFile := fileExists(goldenFile)
35+
36+
box, err := Open(baseDir)
37+
assert.NoErrorf(err, "%s should be a valid devbox project", baseDir)
38+
plan := box.Plan()
39+
40+
if !hasGoldenFile {
2941
assert.NotEmpty(plan.DevPackages, "the plan should have dev packages")
30-
if hasGoldenFile {
31-
data, err := os.ReadFile(goldenFile)
32-
assert.NoError(err, "plan.json should be readable")
42+
return
43+
}
3344

34-
expected := &planner.Plan{}
35-
err = json.Unmarshal(data, &expected)
36-
assert.NoError(err, "plan.json should parse correctly")
45+
data, err := os.ReadFile(goldenFile)
46+
assert.NoError(err, "plan.json should be readable")
3747

38-
assert.Equal(expected, plan)
39-
}
40-
})
41-
}
48+
expected := &planner.Plan{}
49+
err = json.Unmarshal(data, &expected)
50+
assert.NoError(err, "plan.json should parse correctly")
51+
expected.Errors = nil
52+
53+
// For now we only compare the DevPackages and RuntimePackages fields:
54+
assert.ElementsMatch(expected.DevPackages, plan.DevPackages, "DevPackages should match")
55+
assert.ElementsMatch(expected.RuntimePackages, plan.RuntimePackages, "RuntimePackages should match")
56+
})
4257
}
4358

4459
func fileExists(path string) bool {

testdata/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Test Suite for many different languages
2+
3+
`testdata/` contains a test-suite of projects using different programming
4+
languages and frameworks. `devbox_test.go` automatically loops through every
5+
project in here that has a `devbox.json` and runs `devbox plan`.
6+
7+
It then checks that `DevPackages` and `RuntimePackages` match the stated values
8+
in `plan.json`.
9+
10+
To add a new test:
11+
+ Create a new example
12+
+ Initialize it with a `devbox.json`. If you are trying to test that planners are
13+
automatically adding the right packages, your `devbox.json` should probably be
14+
empty. If you are trying to test the interaction between what a planner does, and
15+
the packages a user might have declared, then your `devbox.json` should include
16+
the user packages.
17+
+ Add a `plan.json` containing the expected values of `DevPackages` and `RuntimePackages`.

testdata/go/go-1.17/devbox.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"packages": []
3+
}

testdata/go/go-1.17/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module example
2+
3+
go 1.17

testdata/go/go-1.17/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
)
7+
8+
func main() {
9+
expected := "go1.17"
10+
goVersion := runtime.Version()
11+
fmt.Printf("Go version: %s\n", goVersion)
12+
if goVersion != expected {
13+
panic(fmt.Errorf("expected version: %s, got: %s", expected, goVersion))
14+
}
15+
}

testdata/go/go-1.17/plan.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"dev_packages": [
3+
"go_1_17"
4+
],
5+
"runtime_packages": []
6+
}

testdata/go/go-1.18/devbox.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"packages": []
3+
}

testdata/go/go-1.18/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module example
2+
3+
go 1.18

testdata/go/go-1.18/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
)
7+
8+
func main() {
9+
expected := "go1.18"
10+
goVersion := runtime.Version()
11+
fmt.Printf("Go version: %s\n", goVersion)
12+
if goVersion != expected {
13+
panic(fmt.Errorf("expected version: %s, got: %s", expected, goVersion))
14+
}
15+
}

testdata/go/go-1.18/plan.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"dev_packages": [
3+
"go"
4+
],
5+
"runtime_packages": []
6+
}

0 commit comments

Comments
 (0)