@@ -13,68 +13,66 @@ import (
13
13
"go.jetpack.io/devbox/internal/xdg"
14
14
)
15
15
16
- func setupTestEnv (t * testing.T , envs * testscript.Env ) error {
17
- setupPATH (envs )
18
-
19
- setupHome (t , envs )
20
-
21
- err := setupCacheHome (envs )
22
- if err != nil {
23
- return err
24
- }
25
-
26
- propagateEnvVars (envs ,
16
+ // setupTestEnv configures env for devbox tests.
17
+ func setupTestEnv (env * testscript.Env ) error {
18
+ setupPATH (env )
19
+ setupHome (env )
20
+ setupCacheHome (env )
21
+ propagateEnvVars (env ,
27
22
debug .DevboxDebug , // to enable extra logging
28
23
"SSL_CERT_FILE" , // so HTTPS works with Nix-installed certs
29
24
)
30
25
return nil
31
26
}
32
27
33
- func setupHome (t * testing.T , envs * testscript.Env ) {
34
- // We set a HOME env-var because:
35
- // 1. testscripts overrides it to /no-home, presumably to improve isolation
36
- // 2. but many language tools rely on a $HOME being set, and break due to 1.
37
- // examples include ~/.dotnet folder and GOCACHE=$HOME/Library/Caches/go-build
38
- envs .Setenv (envir .Home , t .TempDir ())
28
+ // setupHome sets the test's HOME to a unique temp directory. The testscript
29
+ // package sets it to /no-home by default (presumably to improve isolation), but
30
+ // this breaks most programs.
31
+ func setupHome (env * testscript.Env ) {
32
+ env .Setenv (envir .Home , env .T ().(testing.TB ).TempDir ())
39
33
}
40
34
41
- func setupPATH (envs * testscript.Env ) {
42
- // Ensure path is empty so that we rely only on the PATH set by devbox
43
- // itself.
44
- // The one entry we need to keep is the /bin directory in the testing directory.
45
- // That directory is setup by the testing framework itself, and it's what allows
46
- // us to call our own custom "devbox" command.
47
- oldPath := envs .Getenv (envir .Path )
48
- newPath := strings .Split (oldPath , ":" )[0 ]
49
- envs .Setenv (envir .Path , newPath )
35
+ // setupPATH removes all directories from the test's PATH to ensure that it only
36
+ // uses the PATH set by devbox. The one exception is the testscript's bin
37
+ // directory, which contains the commands given to testscript.RunMain
38
+ // (such as devbox itself).
39
+ func setupPATH (env * testscript.Env ) {
40
+ s , _ , _ := strings .Cut (env .Getenv (envir .Path ), string (filepath .ListSeparator ))
41
+ env .Setenv (envir .Path , s )
50
42
}
51
43
52
- func setupCacheHome (envs * testscript.Env ) error {
53
- // Both devbox itself and nix occasionally create some files in
54
- // XDG_CACHE_HOME (which defaults to ~/.cache). For purposes of this
55
- // test set it to a location within the test's working directory:
56
- cacheHome := filepath .Join (envs .WorkDir , ".cache" )
57
- envs .Setenv (envir .XDGCacheHome , cacheHome )
58
- err := os .MkdirAll (cacheHome , 0o755 ) // Ensure dir exists.
59
- if err != nil {
60
- return err
61
- }
44
+ // setupCacheHome sets the test's XDG_CACHE_HOME to a unique temp directory so
45
+ // that it doesn't share caches with other tests or the user's system. For
46
+ // programs where this would make tests too slow, it symlinks specific cache
47
+ // subdirectories to a shared location that persists between test runs. For
48
+ // example, $WORK/.cache/nix would symlink to $XDG_CACHE_HOME/devbox-tests/nix
49
+ // so that Nix doesn't re-download tarballs for every test.
50
+ func setupCacheHome (env * testscript.Env ) {
51
+ t := env .T ().(testing.TB ) //nolint:varnamelen
62
52
63
- // There is one directory we do want to share across tests: nix's cache.
64
- // Without it tests are very slow, and nix would end up re-downloading
65
- // nixpkgs every time.
66
- // Here we create a shared location for nix's cache, and symlink from
67
- // the test's working directory.
68
- err = os .MkdirAll (xdg .CacheSubpath ("devbox-tests/nix" ), 0o755 ) // Ensure dir exists.
53
+ cacheHome := filepath .Join (env .WorkDir , ".cache" )
54
+ env .Setenv (envir .XDGCacheHome , cacheHome )
55
+ err := os .MkdirAll (cacheHome , 0o755 )
69
56
if err != nil {
70
- return err
71
- }
72
- err = os .Symlink (xdg .CacheSubpath ("devbox-tests/nix" ), filepath .Join (cacheHome , "nix" ))
73
- if err != nil {
74
- return err
57
+ t .Fatal ("create XDG_CACHE_HOME for test:" , err )
75
58
}
76
59
77
- return nil
60
+ // Symlink cache subdirectories that we want to share and persist
61
+ // between tests.
62
+ sharedCacheDir := xdg .CacheSubpath ("devbox-tests" )
63
+ for _ , subdir := range []string {"nix" , "pip" } {
64
+ sharedSubdir := filepath .Join (sharedCacheDir , subdir )
65
+ err := os .MkdirAll (sharedSubdir , 0o755 )
66
+ if err != nil {
67
+ t .Fatal ("create shared XDG_CACHE_HOME subdir:" , err )
68
+ }
69
+
70
+ testSubdir := filepath .Join (cacheHome , subdir )
71
+ err = os .Symlink (sharedSubdir , testSubdir )
72
+ if err != nil {
73
+ t .Fatal ("symlink test's XDG_CACHE_HOME subdir to shared XDG_CACHE_HOME subdir:" , err )
74
+ }
75
+ }
78
76
}
79
77
80
78
// propagateEnvVars propagates the values of environment variables to the test
0 commit comments