(t *testing.T)
| 67 | func (f fakeProject) GetVars() []string { return nil } |
| 68 | |
| 69 | func TestArtifactCache_StoreAndFetch(t *testing.T) { |
| 70 | oldWorkspace := dirs.WorkspaceDir |
| 71 | tmpWorkspace := t.TempDir() |
| 72 | dirs.Init(tmpWorkspace) |
| 73 | t.Cleanup(func() { dirs.Init(oldWorkspace) }) |
| 74 | |
| 75 | cacheDir := filepath.Join(tmpWorkspace, "cache") |
| 76 | artifactCacheDir := filepath.Join(cacheDir, "artifacts-"+Version) |
| 77 | if err := os.MkdirAll(cacheDir, os.ModePerm); err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | |
| 81 | // creates a fresh cache entry per subtest so test cases |
| 82 | // stay isolated and do not depend on execution order. |
| 83 | setupArtifactFixture := func(t *testing.T) (artifactCache context.AritifactCache, nameVersion, meta, hash, packageDir string) { |
| 84 | t.Helper() |
| 85 | |
| 86 | fakeCtx := fakeContext{ |
| 87 | platform: "x86_64-linux", |
| 88 | project: "proj", |
| 89 | build: "release", |
| 90 | } |
| 91 | pkgCacheConfig := NewPkgCacheConfig(fakeCtx, cacheDir, true) |
| 92 | fakeCtx.pkgCacheConfig = pkgCacheConfig |
| 93 | pkgCacheConfig.ctx = fakeCtx |
| 94 | if err := pkgCacheConfig.Refresh(); err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | |
| 98 | nameVersion = "demo@1.0.0" |
| 99 | meta = "meta-data-for-test" |
| 100 | metaHash := sha256.Sum256([]byte(meta)) |
| 101 | hash = fmt.Sprintf("%x", metaHash) |
| 102 | |
| 103 | packageDir = filepath.Join(tmpWorkspace, "packages", "x86_64-linux", "proj", "release", "demo@1.0.0") |
| 104 | if err := os.MkdirAll(packageDir, os.ModePerm); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | if err := os.WriteFile(filepath.Join(packageDir, "a.txt"), []byte("hello"), os.ModePerm); err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | |
| 111 | artifactCache = pkgCacheConfig.GetArtifactCache() |
| 112 | if artifactCache == nil { |
| 113 | t.Fatal("artifact cache should not be nil") |
| 114 | } |
| 115 | if err := artifactCache.Store(packageDir, meta); err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | |
| 119 | return artifactCache, nameVersion, meta, hash, packageDir |
| 120 | } |
| 121 | |
| 122 | t.Run("artifact not exist", func(t *testing.T) { |
| 123 | artifactCache, nameVersion, _, _, _ := setupArtifactFixture(t) |
| 124 | destDir := filepath.Join(tmpWorkspace, "out-not-exist") |
| 125 | fromWhere, err := artifactCache.Restore(nameVersion, "not-exist-hash", destDir) |
| 126 | if err != nil { |
nothing calls this directly
no test coverage detected