| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | @@ -80,9 +80,15 @@ func (o *options) apply(opts []Opt) error { |
| 6 | |
| 7 | type Opt func(*options) error |
| 8 | |
| 9 | -// WithCache enables caching using filepath.Join(os.UserCacheDir(), "lima") as the cache dir. |
| 10 | +// WithCache uses Cage's environment volume when configured, or the user's Lima cache. |
| 11 | func WithCache() Opt { |
| 12 | return func(o *options) error { |
| 13 | + if dir := os.Getenv("CAGE_LIMA_CACHE_DIR"); dir != "" { |
| 14 | + if !filepath.IsAbs(dir) { |
| 15 | + return fmt.Errorf("CAGE_LIMA_CACHE_DIR must be absolute") |
| 16 | + } |
| 17 | + return WithCacheDir(dir)(o) |
| 18 | + } |
| 19 | ucd, err := os.UserCacheDir() |
| 20 | if err != nil { |
| 21 | return err |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | @@ -37,10 +37,14 @@ func TestPinnedDownloadResumesInterruptedBody(t *testing.T) { |
| 27 | })) |
| 28 | defer s.Close() |
| 29 | cache := t.TempDir() |
| 30 | - r, err := Download(t.Context(), "", s.URL, WithCacheDir(cache), WithExpectedDigest(digest.FromString(data))) |
| 31 | + t.Setenv("CAGE_LIMA_CACHE_DIR", cache) |
| 32 | + r, err := Download(t.Context(), "", s.URL, WithCache(), WithExpectedDigest(digest.FromString(data))) |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | + if rel, err := filepath.Rel(cache, r.CachePath); err != nil || !filepath.IsLocal(rel) { |
| 37 | + t.Fatalf("download landed outside chosen cache: %q", r.CachePath) |
| 38 | + } |
| 39 | got, err := os.ReadFile(r.CachePath) |
| 40 | if err != nil || string(got) != data || calls.Load() != 2 { |
| 41 | t.Fatalf("result %q, calls %d, error %v", got, calls.Load(), err) |