MCPcopy Index your code
hub / github.com/jetify-com/devbox / setupCacheHome

Function setupCacheHome

testscripts/testrunner/setupenv.go:50–76  ·  view source on GitHub ↗

setupCacheHome sets the test's XDG_CACHE_HOME to a unique temp directory so that it doesn't share caches with other tests or the user's system. For programs where this would make tests too slow, it symlinks specific cache subdirectories to a shared location that persists between test runs. For examp

(env *testscript.Env)

Source from the content-addressed store, hash-verified

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.
50func setupCacheHome(env *testscript.Env) {
51 t := env.T().(testing.TB) //nolint:varnamelen
52
53 cacheHome := filepath.Join(env.WorkDir, ".cache")
54 env.Setenv(envir.XDGCacheHome, cacheHome)
55 err := os.MkdirAll(cacheHome, 0o755)
56 if err != nil {
57 t.Fatal("create XDG_CACHE_HOME for test:", err)
58 }
59
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 }
76}
77
78// propagateEnvVars propagates the values of environment variables to the test
79// environment.

Callers 1

setupTestEnvFunction · 0.85

Calls 1

CacheSubpathFunction · 0.92

Tested by

no test coverage detected