Skip to content

Commit

Permalink
refactor (validate): move validations to cargo package
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Oct 19, 2022
1 parent 04028c1 commit 4fc91da
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 248 deletions.
5 changes: 0 additions & 5 deletions internal/commands/testdata/validate/floating-release/Kilnfile

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

5 changes: 0 additions & 5 deletions internal/commands/testdata/validate/missing-lock/Kilnfile

This file was deleted.

6 changes: 0 additions & 6 deletions internal/commands/testdata/validate/pinned-release/Kilnfile

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

51 changes: 4 additions & 47 deletions internal/commands/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"strings"

"github.com/Masterminds/semver"
"github.com/go-git/go-billy/v5"
"github.com/pivotal-cf/jhanda"

Expand Down Expand Up @@ -34,28 +33,14 @@ func (v Validate) Execute(args []string) error {
return err
}

kilnfile, lock, err := v.Options.Standard.LoadKilnfiles(v.FS, nil)
kf, lock, err := v.Options.Standard.LoadKilnfiles(v.FS, nil)
if err != nil {
return fmt.Errorf("failed to load kilnfiles: %w", err)
}

var releaseErrors errorList

for index, release := range kilnfile.Releases {
releaseLock, err := lock.FindReleaseWithName(release.Name)
if err != nil {
releaseErrors = append(releaseErrors,
fmt.Errorf("release %q not found in lock", release.Name))
continue
}

if err := validateRelease(release, releaseLock, index); err != nil {
releaseErrors = append(releaseErrors, err)
}
}

if len(releaseErrors) > 0 {
return releaseErrors
errs := cargo.Validate(kf, lock)
if len(errs) > 0 {
return errorList(errs)
}

return nil
Expand All @@ -71,34 +56,6 @@ func (list errorList) Error() string {
return strings.Join(messages, "\n")
}

func validateRelease(release cargo.ComponentSpec, lock cargo.ComponentLock, index int) error {
if release.Name == "" {
return fmt.Errorf("release at index %d missing name", index)
}

if release.Version != "" {
c, err := semver.NewConstraint(release.Version)
if err != nil {
return fmt.Errorf("release %s (index %d in Kilnfile) has invalid version constraint: %w",
release.Name, index, err)
}

v, err := semver.NewVersion(lock.Version)
if err != nil {
return fmt.Errorf("release %s (index %d in Kilnfile.lock) has invalid lock version %q: %w",
release.Name, index, lock.Version, err)
}

matches, errs := c.Validate(v)
if !matches {
return fmt.Errorf("release %s version in lock %q does not match constraint %q: %v",
release.Name, lock.Version, release.Version, errs)
}
}

return nil
}

func (v Validate) Usage() jhanda.Usage {
return jhanda.Usage{
Description: "Validate checks for common Kilnfile and Kilnfile.lock mistakes",
Expand Down
137 changes: 0 additions & 137 deletions internal/commands/validate_test.go

This file was deleted.

59 changes: 59 additions & 0 deletions pkg/cargo/kilnfile_validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cargo

import (
"fmt"

"github.com/Masterminds/semver"
)

func Validate(spec Kilnfile, lock KilnfileLock) []error {
var result []error

for index, componentSpec := range spec.Releases {
if componentSpec.Name == "" {
result = append(result, fmt.Errorf("spec at index %d missing name", index))
continue
}

componentLock, err := lock.FindReleaseWithName(componentSpec.Name)
if err != nil {
result = append(result,
fmt.Errorf("component spec for release %q not found in lock", componentSpec.Name))
continue
}

if err := checkComponentVersionsAndConstraint(componentSpec, componentLock, index); err != nil {
result = append(result, err)
}
}

if len(result) > 0 {
return result
}

return nil
}

func checkComponentVersionsAndConstraint(spec ComponentSpec, lock ComponentLock, index int) error {
v, err := semver.NewVersion(lock.Version)
if err != nil {
return fmt.Errorf("spec %s (index %d in Kilnfile.lock) has invalid lock version %q: %w",
spec.Name, index, lock.Version, err)
}

if spec.Version != "" {
c, err := semver.NewConstraint(spec.Version)
if err != nil {
return fmt.Errorf("spec %s (index %d in Kilnfile) has invalid version constraint: %w",
spec.Name, index, err)
}

matches, errs := c.Validate(v)
if !matches {
return fmt.Errorf("spec %s version in lock %q does not match constraint %q: %v",
spec.Name, lock.Version, spec.Version, errs)
}
}

return nil
}
Loading

0 comments on commit 4fc91da

Please sign in to comment.