(t *testing.T)
| 13 | ) |
| 14 | |
| 15 | func TestDownloadCache_SaveAndFind(t *testing.T) { |
| 16 | oldWorkspace := dirs.WorkspaceDir |
| 17 | tmpWorkspace := t.TempDir() |
| 18 | dirs.Init(tmpWorkspace) |
| 19 | t.Cleanup(func() { dirs.Init(oldWorkspace) }) |
| 20 | |
| 21 | cacheDir := filepath.Join(tmpWorkspace, "cache") |
| 22 | if err := os.MkdirAll(cacheDir, os.ModePerm); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | |
| 26 | fakeCtx := fakeContext{ |
| 27 | platform: "x86_64-linux", |
| 28 | project: "proj", |
| 29 | build: "release", |
| 30 | } |
| 31 | pkgCacheConfig := NewPkgCacheConfig(fakeCtx, cacheDir, true) |
| 32 | fakeCtx.pkgCacheConfig = pkgCacheConfig |
| 33 | pkgCacheConfig.ctx = fakeCtx |
| 34 | if err := pkgCacheConfig.Refresh(); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | |
| 38 | cachedDownloadsDir := pkgCacheConfig.GetDir(context.PkgCacheDirDownloads) |
| 39 | chattrFS := fileio.NewChattrFS(pkgCacheConfig.GetDir(context.PkgCacheDirRoot)) |
| 40 | |
| 41 | t.Run("save and find cached download", func(t *testing.T) { |
| 42 | // Create a temporary source file to cache. |
| 43 | srcDir := t.TempDir() |
| 44 | srcFile := filepath.Join(srcDir, "test-tool-1.0.tar.gz") |
| 45 | content := []byte("download-cache-test-content") |
| 46 | if err := os.WriteFile(srcFile, content, os.ModePerm); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | |
| 50 | sha256 := fmt.Sprintf("%x", sha256.Sum256(content)) |
| 51 | fileName := "test-tool-1.0.tar.gz" |
| 52 | |
| 53 | // Save to cache. |
| 54 | cachedPath, err := fileio.SaveCachedFile(srcFile, cachedDownloadsDir, fileName, sha256, chattrFS) |
| 55 | if err != nil { |
| 56 | t.Fatalf("SaveCachedFile failed: %v", err) |
| 57 | } |
| 58 | |
| 59 | if !fileio.PathExists(cachedPath) { |
| 60 | t.Fatalf("expected cached file at %s", cachedPath) |
| 61 | } |
| 62 | |
| 63 | // Verify the downloads directory was created. |
| 64 | if !fileio.PathExists(cachedDownloadsDir) { |
| 65 | t.Fatal("expected downloads cache directory to exist") |
| 66 | } |
| 67 | |
| 68 | // Find the cached file. |
| 69 | foundPath, err := fileio.FindCachedFile(cachedDownloadsDir, fileName, sha256) |
| 70 | if err != nil { |
| 71 | t.Fatalf("FindCachedFile failed: %v", err) |
| 72 | } |
nothing calls this directly
no test coverage detected