|
| 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 | +} |
0 commit comments