Skip to content

Commit

Permalink
Only rely on shasum for dependency cache hit
Browse files Browse the repository at this point in the history
  • Loading branch information
c0d1ngm0nk3y committed Jul 28, 2023
1 parent eed720d commit df3b845
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
21 changes: 8 additions & 13 deletions dependency_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ type RequestModifierFunc func(request *http.Request) (*http.Request, error)
func (d *DependencyCache) Artifact(dependency BuildModuleDependency, mods ...RequestModifierFunc) (*os.File, error) {

var (
actual BuildModuleDependency
artifact string
file string
uri = dependency.URI
Expand All @@ -190,29 +189,25 @@ func (d *DependencyCache) Artifact(dependency BuildModuleDependency, mods ...Req
}

file = filepath.Join(d.CachePath, fmt.Sprintf("%s.toml", dependency.SHA256))
b, err := os.ReadFile(file)
if err != nil && !os.IsNotExist(err) {
exists, err := sherpa.Exists(file)

if err != nil {
return nil, fmt.Errorf("unable to read %s\n%w", file, err)
}
if err := toml.Unmarshal(b, &actual); err != nil {
return nil, fmt.Errorf("unable to decode download metadata %s\n%w", file, err)
}

if dependency.Equals(actual) {
if exists {
d.Logger.Bodyf("%s cached download from buildpack", color.GreenString("Reusing"))
return os.Open(filepath.Join(d.CachePath, dependency.SHA256, filepath.Base(uri)))
}

file = filepath.Join(d.DownloadPath, fmt.Sprintf("%s.toml", dependency.SHA256))
b, err = os.ReadFile(file)
if err != nil && !os.IsNotExist(err) {
exists, err = sherpa.Exists(file)

if err != nil {
return nil, fmt.Errorf("unable to read %s\n%w", file, err)
}
if err := toml.Unmarshal(b, &actual); err != nil {
return nil, fmt.Errorf("unable to decode download metadata %s\n%w", file, err)
}

if dependency.Equals(actual) {
if exists {
d.Logger.Bodyf("%s previously cached download", color.GreenString("Reusing"))
return os.Open(filepath.Join(d.DownloadPath, dependency.SHA256, filepath.Base(uri)))
}
Expand Down
22 changes: 22 additions & 0 deletions dependency_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,17 @@ func testDependencyCache(t *testing.T, context spec.G, it spec.S) {
Expect(io.ReadAll(a)).To(Equal([]byte("test-fixture")))
})

it("returns from cache path even with updated metadata", func() {
copyFile(filepath.Join("testdata", "test-file"), filepath.Join(cachePath, dependency.SHA256, "test-path"))
dependency.DeprecationDate = time.Now()
writeTOML(filepath.Join(cachePath, fmt.Sprintf("%s.toml", dependency.SHA256)), dependency)

a, err := dependencyCache.Artifact(dependency)
Expect(err).NotTo(HaveOccurred())

Expect(io.ReadAll(a)).To(Equal([]byte("test-fixture")))
})

it("returns from download path", func() {
copyFile(filepath.Join("testdata", "test-file"), filepath.Join(downloadPath, dependency.SHA256, "test-path"))
writeTOML(filepath.Join(downloadPath, fmt.Sprintf("%s.toml", dependency.SHA256)), dependency)
Expand All @@ -244,6 +255,17 @@ func testDependencyCache(t *testing.T, context spec.G, it spec.S) {
Expect(io.ReadAll(a)).To(Equal([]byte("test-fixture")))
})

it("returns from download path even with updated metadata", func() {
copyFile(filepath.Join("testdata", "test-file"), filepath.Join(downloadPath, dependency.SHA256, "test-path"))
dependency.DeprecationDate = time.Now()
writeTOML(filepath.Join(downloadPath, fmt.Sprintf("%s.toml", dependency.SHA256)), dependency)

a, err := dependencyCache.Artifact(dependency)
Expect(err).NotTo(HaveOccurred())

Expect(io.ReadAll(a)).To(Equal([]byte("test-fixture")))
})

it("downloads", func() {
server.AppendHandlers(ghttp.CombineHandlers(
ghttp.VerifyRequest(http.MethodGet, "/test-path", ""),
Expand Down

0 comments on commit df3b845

Please sign in to comment.