Skip to content

Commit

Permalink
Supports summarizing buildpackages
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Moran <[email protected]>
  • Loading branch information
ForestEckhardt authored and ryanmoran committed Jul 14, 2020
1 parent 0a90423 commit ff92ec0
Show file tree
Hide file tree
Showing 9 changed files with 1,164 additions and 234 deletions.
12 changes: 5 additions & 7 deletions cargo/jam/commands/fakes/buildpack_inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@ type BuildpackInspector struct {
Path string
}
Returns struct {
Dependencies []cargo.ConfigMetadataDependency
Defaults map[string]string
Stacks []string
Err error
ConfigSlice []cargo.Config
Error error
}
Stub func(string) ([]cargo.ConfigMetadataDependency, map[string]string, []string, error)
Stub func(string) ([]cargo.Config, error)
}
}

func (f *BuildpackInspector) Dependencies(param1 string) ([]cargo.ConfigMetadataDependency, map[string]string, []string, error) {
func (f *BuildpackInspector) Dependencies(param1 string) ([]cargo.Config, 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.Dependencies, f.DependenciesCall.Returns.Defaults, f.DependenciesCall.Returns.Stacks, f.DependenciesCall.Returns.Err
return f.DependenciesCall.Returns.ConfigSlice, f.DependenciesCall.Returns.Error
}
14 changes: 5 additions & 9 deletions cargo/jam/commands/fakes/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,18 @@ type Formatter struct {
sync.Mutex
CallCount int
Receives struct {
Dependencies []cargo.ConfigMetadataDependency
Defaults map[string]string
Stacks []string
Config cargo.Config
}
Stub func([]cargo.ConfigMetadataDependency, map[string]string, []string)
Stub func(cargo.Config)
}
}

func (f *Formatter) Markdown(param1 []cargo.ConfigMetadataDependency, param2 map[string]string, param3 []string) {
func (f *Formatter) Markdown(param1 cargo.Config) {
f.MarkdownCall.Lock()
defer f.MarkdownCall.Unlock()
f.MarkdownCall.CallCount++
f.MarkdownCall.Receives.Dependencies = param1
f.MarkdownCall.Receives.Defaults = param2
f.MarkdownCall.Receives.Stacks = param3
f.MarkdownCall.Receives.Config = param1
if f.MarkdownCall.Stub != nil {
f.MarkdownCall.Stub(param1, param2, param3)
f.MarkdownCall.Stub(param1)
}
}
12 changes: 7 additions & 5 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) (dependencies []cargo.ConfigMetadataDependency, defaults map[string]string, stacks []string, err error)
Dependencies(path string) ([]cargo.Config, error)
}

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

type Summarize struct {
Expand All @@ -37,7 +37,7 @@ func (s Summarize) Execute(args []string) error {
)

fset := flag.NewFlagSet("summarize", flag.ContinueOnError)
fset.StringVar(&buildpackTarballPath, "buildpack", "", "path to a buildpack tarball (required)")
fset.StringVar(&buildpackTarballPath, "buildpack", "", "path to a buildpackage tarball (required)")
fset.StringVar(&format, "format", "markdown", "format of output options are (markdown)")
err := fset.Parse(args)
if err != nil {
Expand All @@ -48,14 +48,16 @@ func (s Summarize) Execute(args []string) error {
return errors.New("missing required flag --buildpack")
}

dependencies, defaults, stacks, err := s.buildpackInspector.Dependencies(buildpackTarballPath)
configs, 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, defaults, stacks)
for _, config := range configs {
s.formatter.Markdown(config)
}
default:
return fmt.Errorf("unknown format %q, please choose from the following formats (\"markdown\")", format)
}
Expand Down
164 changes: 130 additions & 34 deletions cargo/jam/commands/summarize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,62 @@ func testSummarize(t *testing.T, context spec.G, it spec.S) {

buildpackInspector *fakes.BuildpackInspector
formatter *fakes.Formatter
configs []cargo.Config

command commands.Summarize
)

it.Before(func() {
buildpackInspector = &fakes.BuildpackInspector{}

buildpackInspector.DependenciesCall.Returns.Dependencies = []cargo.ConfigMetadataDependency{
buildpackInspector.DependenciesCall.Returns.ConfigSlice = []cargo.Config{
{
ID: "some-depency",
Version: "some-version",
Stacks: []string{"some-stack"},
Buildpack: cargo.ConfigBuildpack{
ID: "some-buildpack",
Version: "some-version",
},
Metadata: cargo.ConfigMetadata{
Dependencies: []cargo.ConfigMetadataDependency{
{
ID: "some-depency",
Version: "some-version",
Stacks: []string{"some-stack"},
},
},
DefaultVersions: map[string]string{
"some-dependency": "some-version",
},
},
Stacks: []cargo.ConfigStack{
{ID: "some-stack"},
},
},
{
Buildpack: cargo.ConfigBuildpack{
ID: "other-buildpack",
Version: "other-version",
},
Metadata: cargo.ConfigMetadata{
Dependencies: []cargo.ConfigMetadataDependency{
{
ID: "other-depency",
Version: "other-version",
Stacks: []string{"other-stack"},
},
},
DefaultVersions: map[string]string{
"other-dependency": "other-version",
},
},
Stacks: []cargo.ConfigStack{
{ID: "other-stack"},
},
},
}

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

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

formatter = &fakes.Formatter{}
formatter.MarkdownCall.Stub = func(config cargo.Config) {
configs = append(configs, config)
}

command = commands.NewSummarize(buildpackInspector, formatter)
})
Expand All @@ -56,20 +88,50 @@ func testSummarize(t *testing.T, context spec.G, it spec.S) {

Expect(buildpackInspector.DependenciesCall.Receives.Path).To(Equal("buildpack.tgz"))

Expect(formatter.MarkdownCall.Receives.Dependencies).To(Equal([]cargo.ConfigMetadataDependency{
Expect(formatter.MarkdownCall.CallCount).To(Equal(2))
Expect(configs).To(Equal([]cargo.Config{
{
ID: "some-depency",
Version: "some-version",
Stacks: []string{"some-stack"},
Buildpack: cargo.ConfigBuildpack{
ID: "some-buildpack",
Version: "some-version",
},
Metadata: cargo.ConfigMetadata{
Dependencies: []cargo.ConfigMetadataDependency{
{
ID: "some-depency",
Version: "some-version",
Stacks: []string{"some-stack"},
},
},
DefaultVersions: map[string]string{
"some-dependency": "some-version",
},
},
Stacks: []cargo.ConfigStack{
{ID: "some-stack"},
},
},
{
Buildpack: cargo.ConfigBuildpack{
ID: "other-buildpack",
Version: "other-version",
},
Metadata: cargo.ConfigMetadata{
Dependencies: []cargo.ConfigMetadataDependency{
{
ID: "other-depency",
Version: "other-version",
Stacks: []string{"other-stack"},
},
},
DefaultVersions: map[string]string{
"other-dependency": "other-version",
},
},
Stacks: []cargo.ConfigStack{
{ID: "other-stack"},
},
},
}))

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

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

Expand All @@ -82,16 +144,50 @@ func testSummarize(t *testing.T, context spec.G, it spec.S) {

Expect(buildpackInspector.DependenciesCall.Receives.Path).To(Equal("buildpack.tgz"))

Expect(formatter.MarkdownCall.Receives.Dependencies).To(Equal([]cargo.ConfigMetadataDependency{
Expect(formatter.MarkdownCall.CallCount).To(Equal(2))
Expect(configs).To(Equal([]cargo.Config{
{
ID: "some-depency",
Version: "some-version",
Stacks: []string{"some-stack"},
Buildpack: cargo.ConfigBuildpack{
ID: "some-buildpack",
Version: "some-version",
},
Metadata: cargo.ConfigMetadata{
Dependencies: []cargo.ConfigMetadataDependency{
{
ID: "some-depency",
Version: "some-version",
Stacks: []string{"some-stack"},
},
},
DefaultVersions: map[string]string{
"some-dependency": "some-version",
},
},
Stacks: []cargo.ConfigStack{
{ID: "some-stack"},
},
},
{
Buildpack: cargo.ConfigBuildpack{
ID: "other-buildpack",
Version: "other-version",
},
Metadata: cargo.ConfigMetadata{
Dependencies: []cargo.ConfigMetadataDependency{
{
ID: "other-depency",
Version: "other-version",
Stacks: []string{"other-stack"},
},
},
DefaultVersions: map[string]string{
"other-dependency": "other-version",
},
},
Stacks: []cargo.ConfigStack{
{ID: "other-stack"},
},
},
}))

Expect(formatter.MarkdownCall.Receives.Defaults).To(Equal(map[string]string{
"some-dependency": "some-version",
}))
})
})
Expand All @@ -113,7 +209,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.Err = errors.New("failed to get dependencies")
buildpackInspector.DependenciesCall.Returns.Error = errors.New("failed to get dependencies")
})

it("returns an error", func() {
Expand Down
Loading

0 comments on commit ff92ec0

Please sign in to comment.