forked from jetify-com/devbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devbox_test.go
84 lines (65 loc) · 2.64 KB
/
devbox_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package devbox
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/bmatcuk/doublestar/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.jetpack.io/devbox/planner/plansdk"
)
func TestDevbox(t *testing.T) {
t.Setenv("TMPDIR", "/tmp")
testPaths, err := doublestar.FilepathGlob("./testdata/**/devbox.json")
assert.NoError(t, err, "Reading testdata/ should not fail")
examplePaths, err := doublestar.FilepathGlob("./examples/**/devbox.json")
assert.NoError(t, err, "Reading examples/ should not fail")
testPaths = append(testPaths, examplePaths...)
assert.Greater(t, len(testPaths), 0, "testdata/ and examples/ should contain at least 1 test")
for _, testPath := range testPaths {
testShell(t, testPath)
}
}
func testShell(t *testing.T, testPath string) {
currentDir, err := os.Getwd()
require.New(t).NoError(err)
baseDir := filepath.Dir(testPath)
testName := fmt.Sprintf("%s_shell_plan", baseDir)
t.Run(testName, func(t *testing.T) {
assert := assert.New(t)
shellPlanFile := filepath.Join(baseDir, "shell_plan.json")
hasShellPlanFile := fileExists(shellPlanFile)
box, err := Open(baseDir, os.Stdout)
assert.NoErrorf(err, "%s should be a valid devbox project", baseDir)
// Just for tests, we make configDir be a relative path so that the paths in plan.json
// of various test cases have relative paths. Absolute paths are a no-go because they'd
// be of the form `/Users/savil/...`, which are not generalized and cannot be checked in.
box.configDir, err = filepath.Rel(currentDir, box.configDir)
assert.NoErrorf(err, "expect to construct relative path from %s relative to base %s", box.configDir, currentDir)
shellPlan, err := box.ShellPlan()
assert.NoError(err, "devbox shell plan should not fail")
err = box.generateShellFiles()
assert.NoError(err, "devbox generate should not fail")
if !hasShellPlanFile {
assert.NotEmpty(shellPlan.DevPackages, "the plan should have dev packages")
return
}
data, err := os.ReadFile(shellPlanFile)
assert.NoError(err, "shell_plan.json should be readable")
expected := &plansdk.ShellPlan{}
err = json.Unmarshal(data, &expected)
assert.NoError(err, "plan.json should parse correctly")
assertShellPlansMatch(t, expected, shellPlan)
})
}
func assertShellPlansMatch(t *testing.T, expected *plansdk.ShellPlan, actual *plansdk.ShellPlan) {
assert := assert.New(t)
assert.ElementsMatch(expected.DevPackages, actual.DevPackages, "DevPackages should match")
assert.ElementsMatch(expected.NixOverlays, actual.NixOverlays, "NixOverlays should match")
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}