Skip to content

Commit b5c8991

Browse files
authored
Add test that iterates through testdata/ (#82)
## Summary Add test that iterates through `testdata/`. If there's a `plan.json` it uses it as a golden file, otherwise it just checks that plans succeeds and that `devPackages` is not empty. ## How was it tested? `go test -v ./...`
1 parent cf90fba commit b5c8991

File tree

6 files changed

+54
-7
lines changed

6 files changed

+54
-7
lines changed

devbox_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package devbox
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/bmatcuk/doublestar/v4"
10+
"github.com/stretchr/testify/assert"
11+
"go.jetpack.io/devbox/planner"
12+
)
13+
14+
func TestPlan(t *testing.T) {
15+
testPaths, err := doublestar.FilepathGlob("./testdata/**/devbox.json")
16+
assert.NoError(t, err, "Reading testdata/ should not fail")
17+
assert.Greater(t, len(testPaths), 0, "testdata/ should contain at least 1 test")
18+
19+
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()
29+
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")
33+
34+
expected := &planner.Plan{}
35+
err = json.Unmarshal(data, &expected)
36+
assert.NoError(err, "plan.json should parse correctly")
37+
38+
assert.Equal(expected, plan)
39+
}
40+
})
41+
}
42+
}
43+
44+
func fileExists(path string) bool {
45+
_, err := os.Stat(path)
46+
return err == nil
47+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.19
44

55
require (
66
cuelang.org/go v0.4.3
7+
github.com/bmatcuk/doublestar/v4 v4.2.0
78
github.com/denisbrodbeck/machineid v1.0.1
89
github.com/imdario/mergo v0.3.13
910
github.com/pelletier/go-toml/v2 v2.0.5

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
cuelang.org/go v0.4.3 h1:W3oBBjDTm7+IZfCKZAmC8uDG0eYfJL4Pp/xbbCMKaVo=
22
cuelang.org/go v0.4.3/go.mod h1:7805vR9H+VoBNdWFdI7jyDR3QLUPp4+naHfbcgp55HI=
3+
github.com/bmatcuk/doublestar/v4 v4.2.0 h1:Qu+u9wR3Vd89LnlLMHvnZ5coJMWKQamqdz9/p5GNthA=
4+
github.com/bmatcuk/doublestar/v4 v4.2.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
35
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
46
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
57
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=

planner/python_poetry_planner.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func (g *PythonPoetryPlanner) Name() string {
2525
}
2626

2727
func (g *PythonPoetryPlanner) IsRelevant(srcDir string) bool {
28-
return fileExists(filepath.Join(srcDir, "poetry.lock"))
28+
return fileExists(filepath.Join(srcDir, "poetry.lock")) ||
29+
fileExists(filepath.Join(srcDir, "pyproject.toml"))
2930
}
3031

3132
func (g *PythonPoetryPlanner) GetPlan(srcDir string) *Plan {
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
{
2-
"packages": [
3-
"poetry"
4-
]
2+
"packages": []
53
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
{
2-
"packages": [
3-
"poetry"
4-
]
2+
"packages": []
53
}

0 commit comments

Comments
 (0)