Skip to content

Commit 42dba2d

Browse files
authored
Add testscripts that tests installing 'hello' (#547)
## Summary Add testscripts that tests installing 'hello' ## How was it tested? `go test -v`
1 parent 4d212ec commit 42dba2d

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

internal/nix/run.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ func RunScript(nixShellFilePath string, projectDir string, cmdWithArgs string, a
3434
}
3535
}
3636

37-
cmd := exec.Command("sh", "-c", cmdWithArgs)
37+
// Try to find sh in the PATH, if not, default to a well known absolute path.
38+
shPath, err := exec.LookPath("sh")
39+
if err != nil {
40+
shPath = "/bin/sh"
41+
}
42+
cmd := exec.Command(shPath, "-c", cmdWithArgs)
3843
cmd.Env = append(nixEnv, additionalEnv...)
3944
cmd.Dir = projectDir
4045
cmd.Stdin = os.Stdin
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Devbox should be able to install a very simple package like 'hello'
2+
# and it should work.
3+
4+
# Ensure hello is not found anywhere in the environment
5+
! exec hello
6+
! exec devbox run hello
7+
8+
# Initialize devbox
9+
exec devbox init
10+
11+
# Add the package and run hello with devbox
12+
exec devbox add hello
13+
! exec hello
14+
15+
# Run hello and check it prints the right output
16+
exec devbox run hello
17+
stdout 'Hello, world!'
18+
19+
# Once we have better progress output, we should check that stderr is empty, with:
20+
# ! stderr .+ # No stderr output
21+
# As is, we always print 'Ensuring packages are installed'.

testscripts/testscripts_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/rogpeppe/go-internal/testscript"
1515
"github.com/stretchr/testify/require"
1616
"go.jetpack.io/devbox/internal/boxcli"
17+
"go.jetpack.io/devbox/internal/xdg"
1718
)
1819

1920
func TestScripts(t *testing.T) {
@@ -75,6 +76,34 @@ func getTestscriptParams(dir string) testscript.Params {
7576
oldPath := env.Getenv("PATH")
7677
newPath := strings.Split(oldPath, ":")[0]
7778
env.Setenv("PATH", newPath)
79+
80+
// Both devbox itself and nix occasionally create some files in
81+
// XDG_CACHE_HOME (which defaults to ~/.cache). For purposes of this
82+
// test set it to a location within the test's working directory:
83+
cacheHome := filepath.Join(env.WorkDir, ".cache")
84+
env.Setenv("XDG_CACHE_HOME", cacheHome)
85+
err := os.MkdirAll(cacheHome, 0755) // Ensure dir exists.
86+
if err != nil {
87+
return err
88+
}
89+
90+
// There is one directory we do want to share across tests: nix's cache.
91+
// Without it tests are very slow, and nix would end up re-downloading
92+
// nixpkgs every time.
93+
// Here we create a shared location for nix's cache, and symlink from
94+
// the test's working directory.
95+
err = os.MkdirAll(xdg.CacheSubpath("devbox-tests/nix"), 0755) // Ensure dir exists.
96+
if err != nil {
97+
return err
98+
}
99+
err = os.Symlink(xdg.CacheSubpath("devbox-tests/nix"), filepath.Join(cacheHome, "nix"))
100+
if err != nil {
101+
return err
102+
}
103+
104+
// Enable new `devbox run` so we can use it in tests. This is temporary,
105+
// and should be removed once we enable this feature flag.
106+
env.Setenv("DEVBOX_FEATURE_STRICT_RUN", "1")
78107
return nil
79108
},
80109
Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){

0 commit comments

Comments
 (0)