Skip to content

Commit

Permalink
small logic simplifications, new error case handled
Browse files Browse the repository at this point in the history
  • Loading branch information
Frankie Gallina-Jones authored and sophiewigmore committed Sep 22, 2022
1 parent e70b3f9 commit b5edac4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
10 changes: 6 additions & 4 deletions internal/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import (
type Dependency struct {
DeprecationDate string `json:"deprecation_date,omitempty"`
// The ID field should be the `name` from the dep-server
ID string `json:"name,omitempty"`
SHA256 string `json:"sha256,omitempty"`
Source string `json:"source,omitempty"`
ID string `json:"name,omitempty"`
// Deprecated: use Checksum instead.
SHA256 string `json:"sha256,omitempty"`
Source string `json:"source,omitempty"`
// Deprecated: use SourceChecksum instead.
SourceSHA256 string `json:"source_sha256,omitempty"`
Stacks []Stack `json:"stacks,omitempty"`
URI string `json:"uri,omitempty"`
Expand Down Expand Up @@ -105,7 +107,7 @@ func GetCargoDependenciesWithinConstraint(dependencies []cargo.ConfigMetadataDep
return nil, err
}

if !c.Check(depVersion) || dependency.ID != constraint.ID {
if dependency.ID != constraint.ID || !c.Check(depVersion) {
continue
}

Expand Down
17 changes: 9 additions & 8 deletions internal/dependency_cacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,20 @@ func (dc DependencyCacher) Cache(root string, deps []cargo.ConfigMetadataDepende
return nil, fmt.Errorf("failed to download dependency: %s", err)
}

var checksum string
var hash string
checksum := dep.Checksum
_, hash, _ := strings.Cut(dep.Checksum, ":")

if dep.SHA256 != "" {
if checksum == "" {
checksum = fmt.Sprintf("sha256:%s", dep.SHA256)
hash = dep.SHA256
dc.logger.Action("↳ dependencies/%s", dep.SHA256)
} else if dep.Checksum != "" {
checksum = dep.Checksum
hash = strings.SplitN(dep.Checksum, ":", 2)[1]
dc.logger.Action("↳ dependencies/%s", hash)
}

if checksum == "sha256:" {
return nil, fmt.Errorf("failed to create file for %s: no sha256 or checksum provided", dep.ID)
}

dc.logger.Action("↳ dependencies/%s", hash)

validatedSource := cargo.NewValidatedReader(source, checksum)

destination, err := os.Create(filepath.Join(dir, hash))
Expand Down
12 changes: 12 additions & 0 deletions internal/dependency_cacher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ func testDependencyCacher(t *testing.T, context spec.G, it spec.S) {
})
})

context("when the dependency has neither a SHA256 nor a checksum", func() {
it("returns a clear error", func() {
_, err := cacher.Cache(tmpDir, []cargo.ConfigMetadataDependency{
{
URI: "http://error-dep",
ID: "no-checksum-dep",
},
})
Expect(err).To(MatchError("failed to create file for no-checksum-dep: no sha256 or checksum provided"))
})
})

context("when the checksum does not match", func() {
it("returns an error", func() {
_, err := cacher.Cache(tmpDir, []cargo.ConfigMetadataDependency{
Expand Down

0 comments on commit b5edac4

Please sign in to comment.