Skip to content

Commit

Permalink
Adds corrected support for stacks in summary
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Moran <rmoran@pivotal.io>
  • Loading branch information
ForestEckhardt authored and Ryan Moran committed Mar 24, 2020
1 parent a66d41a commit a0ce524
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 34 deletions.
13 changes: 7 additions & 6 deletions cargo/jam/commands/fakes/buildpack_inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@ type BuildpackInspector struct {
Path string
}
Returns struct {
ConfigMetadataDependencySlice []cargo.ConfigMetadataDependency
MapStringString map[string]string
Error error
Dependencies []cargo.ConfigMetadataDependency
Defaults map[string]string
Stacks []string
Err error
}
Stub func(string) ([]cargo.ConfigMetadataDependency, map[string]string, error)
Stub func(string) ([]cargo.ConfigMetadataDependency, map[string]string, []string, error)
}
}

func (f *BuildpackInspector) Dependencies(param1 string) ([]cargo.ConfigMetadataDependency, map[string]string, error) {
func (f *BuildpackInspector) Dependencies(param1 string) ([]cargo.ConfigMetadataDependency, map[string]string, []string, error) {
f.DependenciesCall.Lock()
defer f.DependenciesCall.Unlock()
f.DependenciesCall.CallCount++
f.DependenciesCall.Receives.Path = param1
if f.DependenciesCall.Stub != nil {
return f.DependenciesCall.Stub(param1)
}
return f.DependenciesCall.Returns.ConfigMetadataDependencySlice, f.DependenciesCall.Returns.MapStringString, f.DependenciesCall.Returns.Error
return f.DependenciesCall.Returns.Dependencies, f.DependenciesCall.Returns.Defaults, f.DependenciesCall.Returns.Stacks, f.DependenciesCall.Returns.Err
}
14 changes: 8 additions & 6 deletions cargo/jam/commands/fakes/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ type Formatter struct {
sync.Mutex
CallCount int
Receives struct {
Dependencies []cargo.ConfigMetadataDependency
DefaultVersions map[string]string
Dependencies []cargo.ConfigMetadataDependency
Defaults map[string]string
Stacks []string
}
Stub func([]cargo.ConfigMetadataDependency, map[string]string)
Stub func([]cargo.ConfigMetadataDependency, map[string]string, []string)
}
}

func (f *Formatter) Markdown(param1 []cargo.ConfigMetadataDependency, param2 map[string]string) {
func (f *Formatter) Markdown(param1 []cargo.ConfigMetadataDependency, param2 map[string]string, param3 []string) {
f.MarkdownCall.Lock()
defer f.MarkdownCall.Unlock()
f.MarkdownCall.CallCount++
f.MarkdownCall.Receives.Dependencies = param1
f.MarkdownCall.Receives.DefaultVersions = param2
f.MarkdownCall.Receives.Defaults = param2
f.MarkdownCall.Receives.Stacks = param3
if f.MarkdownCall.Stub != nil {
f.MarkdownCall.Stub(param1, param2)
f.MarkdownCall.Stub(param1, param2, param3)
}
}
8 changes: 4 additions & 4 deletions cargo/jam/commands/summarize.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (

//go:generate faux --interface BuildpackInspector --output fakes/buildpack_inspector.go
type BuildpackInspector interface {
Dependencies(path string) ([]cargo.ConfigMetadataDependency, map[string]string, error)
Dependencies(path string) (dependencies []cargo.ConfigMetadataDependency, defaults map[string]string, stacks []string, err error)
}

//go:generate faux --interface Formatter --output fakes/formatter.go
type Formatter interface {
Markdown(dependencies []cargo.ConfigMetadataDependency, defaultVersions map[string]string)
Markdown(dependencies []cargo.ConfigMetadataDependency, defaults map[string]string, stacks []string)
}

type Summarize struct {
Expand Down Expand Up @@ -48,14 +48,14 @@ func (s Summarize) Execute(args []string) error {
return errors.New("missing required flag --buildpack")
}

dependencies, defaultVersions, err := s.buildpackInspector.Dependencies(buildpackTarballPath)
dependencies, defaults, stacks, err := s.buildpackInspector.Dependencies(buildpackTarballPath)
if err != nil {
return fmt.Errorf("failed to inspect buildpack dependencies: %w", err)
}

switch format {
case "markdown":
s.formatter.Markdown(dependencies, defaultVersions)
s.formatter.Markdown(dependencies, defaults, stacks)
default:
return fmt.Errorf("unknown format %q, please choose from the following formats (\"markdown\")", format)
}
Expand Down
18 changes: 13 additions & 5 deletions cargo/jam/commands/summarize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@ func testSummarize(t *testing.T, context spec.G, it spec.S) {
it.Before(func() {
buildpackInspector = &fakes.BuildpackInspector{}

buildpackInspector.DependenciesCall.Returns.ConfigMetadataDependencySlice = []cargo.ConfigMetadataDependency{
buildpackInspector.DependenciesCall.Returns.Dependencies = []cargo.ConfigMetadataDependency{
{
ID: "some-depency",
Version: "some-version",
Stacks: []string{"some-stack"},
},
}

buildpackInspector.DependenciesCall.Returns.MapStringString = map[string]string{
buildpackInspector.DependenciesCall.Returns.Defaults = map[string]string{
"some-dependency": "some-version",
}

buildpackInspector.DependenciesCall.Returns.Stacks = []string{
"some-stack",
}

formatter = &fakes.Formatter{}

command = commands.NewSummarize(buildpackInspector, formatter)
Expand All @@ -60,9 +64,13 @@ func testSummarize(t *testing.T, context spec.G, it spec.S) {
},
}))

Expect(formatter.MarkdownCall.Receives.DefaultVersions).To(Equal(map[string]string{
Expect(formatter.MarkdownCall.Receives.Defaults).To(Equal(map[string]string{
"some-dependency": "some-version",
}))

Expect(formatter.MarkdownCall.Receives.Stacks).To(Equal([]string{
"some-stack",
}))
})

context("when not given a --format flag", func() {
Expand All @@ -82,7 +90,7 @@ func testSummarize(t *testing.T, context spec.G, it spec.S) {
},
}))

Expect(formatter.MarkdownCall.Receives.DefaultVersions).To(Equal(map[string]string{
Expect(formatter.MarkdownCall.Receives.Defaults).To(Equal(map[string]string{
"some-dependency": "some-version",
}))
})
Expand All @@ -105,7 +113,7 @@ func testSummarize(t *testing.T, context spec.G, it spec.S) {

context("when buildpack inspector returns an error", func() {
it.Before(func() {
buildpackInspector.DependenciesCall.Returns.Error = errors.New("failed to get dependencies")
buildpackInspector.DependenciesCall.Returns.Err = errors.New("failed to get dependencies")
})

it("returns an error", func() {
Expand Down
18 changes: 11 additions & 7 deletions cargo/jam/internal/buildpack_inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ func NewBuildpackInspector() BuildpackInspector {
return BuildpackInspector{}
}

func (i BuildpackInspector) Dependencies(path string) ([]cargo.ConfigMetadataDependency, map[string]string, error) {
func (i BuildpackInspector) Dependencies(path string) ([]cargo.ConfigMetadataDependency, map[string]string, []string, error) {
file, err := os.Open(path)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

gr, err := gzip.NewReader(file)
if err != nil {
return nil, nil, fmt.Errorf("failed to open gzip reader: %w", err)
return nil, nil, nil, fmt.Errorf("failed to open gzip reader: %w", err)
}
defer gr.Close()

Expand All @@ -38,18 +38,22 @@ func (i BuildpackInspector) Dependencies(path string) ([]cargo.ConfigMetadataDep
break
}
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

if filepath.Base(hdr.Name) == "buildpack.toml" {
var config cargo.Config
err = cargo.DecodeConfig(tr, &config)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
return config.Metadata.Dependencies, config.Metadata.DefaultVersions, nil
var stacks []string
for _, s := range config.Stacks {
stacks = append(stacks, s.ID)
}
return config.Metadata.Dependencies, config.Metadata.DefaultVersions, stacks, nil
}
}

return nil, nil, errors.New("failed to find buildpack.toml in buildpack tarball")
return nil, nil, nil, errors.New("failed to find buildpack.toml in buildpack tarball")
}
20 changes: 14 additions & 6 deletions cargo/jam/internal/buildpack_inspector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ id = "some-buildpack"
id = "other-dependency"
stacks = ["other-stack"]
version = "other-version"
[[stacks]]
id = "some-stack"
[[stacks]]
id = "other-stack"
`)

err = tr.WriteHeader(&tar.Header{
Expand Down Expand Up @@ -73,7 +79,7 @@ id = "some-buildpack"

context("Dependencies", func() {
it("returns a list of dependencies", func() {
dependencies, defaultVersions, err := inspector.Dependencies(buildpack)
dependencies, defaults, stacks, err := inspector.Dependencies(buildpack)
Expect(err).NotTo(HaveOccurred())
Expect(dependencies).To(Equal([]cargo.ConfigMetadataDependency{
{
Expand All @@ -88,16 +94,18 @@ id = "some-buildpack"
},
}))

Expect(defaultVersions).To(Equal(map[string]string{
Expect(defaults).To(Equal(map[string]string{
"some-dependency": "1.2.x",
"other-dependency": "2.3.x",
}))

Expect(stacks).To(Equal([]string{"some-stack", "other-stack"}))
})

context("failure cases", func() {
context("when the tarball does not exist", func() {
it("returns an error", func() {
_, _, err := inspector.Dependencies("/tmp/no-such-file")
_, _, _, err := inspector.Dependencies("/tmp/no-such-file")
Expect(err).To(MatchError(ContainSubstring("no such file or directory")))
})
})
Expand All @@ -109,7 +117,7 @@ id = "some-buildpack"
})

it("returns an error", func() {
_, _, err := inspector.Dependencies(buildpack)
_, _, _, err := inspector.Dependencies(buildpack)
Expect(err).To(MatchError(ContainSubstring("failed to open gzip reader")))
Expect(err).To(MatchError(ContainSubstring("EOF")))
})
Expand Down Expand Up @@ -144,7 +152,7 @@ id = "some-buildpack"
})

it("returns an error", func() {
_, _, err := inspector.Dependencies(buildpack)
_, _, _, err := inspector.Dependencies(buildpack)
Expect(err).To(MatchError(ContainSubstring("bare keys cannot contain '%'")))
})
})
Expand Down Expand Up @@ -178,7 +186,7 @@ id = "some-buildpack"
})

it("returns an error", func() {
_, _, err := inspector.Dependencies(buildpack)
_, _, _, err := inspector.Dependencies(buildpack)
Expect(err).To(MatchError(ContainSubstring("failed to find buildpack.toml in buildpack tarball")))
})
})
Expand Down

0 comments on commit a0ce524

Please sign in to comment.