From a0ce524ee8f0fe6bb72c0f3fe12286852c510abf Mon Sep 17 00:00:00 2001 From: Forest Eckhardt Date: Tue, 24 Mar 2020 15:00:26 -0400 Subject: [PATCH] Adds corrected support for stacks in summary Signed-off-by: Ryan Moran --- .../jam/commands/fakes/buildpack_inspector.go | 13 ++++++------ cargo/jam/commands/fakes/formatter.go | 14 +++++++------ cargo/jam/commands/summarize.go | 8 ++++---- cargo/jam/commands/summarize_test.go | 18 ++++++++++++----- cargo/jam/internal/buildpack_inspector.go | 18 ++++++++++------- .../jam/internal/buildpack_inspector_test.go | 20 +++++++++++++------ 6 files changed, 57 insertions(+), 34 deletions(-) diff --git a/cargo/jam/commands/fakes/buildpack_inspector.go b/cargo/jam/commands/fakes/buildpack_inspector.go index a2066e19..6ebb01b8 100644 --- a/cargo/jam/commands/fakes/buildpack_inspector.go +++ b/cargo/jam/commands/fakes/buildpack_inspector.go @@ -14,15 +14,16 @@ 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++ @@ -30,5 +31,5 @@ func (f *BuildpackInspector) Dependencies(param1 string) ([]cargo.ConfigMetadata 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 } diff --git a/cargo/jam/commands/fakes/formatter.go b/cargo/jam/commands/fakes/formatter.go index 32e2dfe6..3dd6d5ee 100644 --- a/cargo/jam/commands/fakes/formatter.go +++ b/cargo/jam/commands/fakes/formatter.go @@ -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) } } diff --git a/cargo/jam/commands/summarize.go b/cargo/jam/commands/summarize.go index ef05dd23..eb0d1283 100644 --- a/cargo/jam/commands/summarize.go +++ b/cargo/jam/commands/summarize.go @@ -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 { @@ -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) } diff --git a/cargo/jam/commands/summarize_test.go b/cargo/jam/commands/summarize_test.go index 18a7ca0f..b06c2aae 100644 --- a/cargo/jam/commands/summarize_test.go +++ b/cargo/jam/commands/summarize_test.go @@ -25,7 +25,7 @@ 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", @@ -33,10 +33,14 @@ func testSummarize(t *testing.T, context spec.G, it spec.S) { }, } - 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) @@ -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() { @@ -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", })) }) @@ -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() { diff --git a/cargo/jam/internal/buildpack_inspector.go b/cargo/jam/internal/buildpack_inspector.go index ced3761d..99b533ac 100644 --- a/cargo/jam/internal/buildpack_inspector.go +++ b/cargo/jam/internal/buildpack_inspector.go @@ -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() @@ -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") } diff --git a/cargo/jam/internal/buildpack_inspector_test.go b/cargo/jam/internal/buildpack_inspector_test.go index af56a58d..1d92517e 100644 --- a/cargo/jam/internal/buildpack_inspector_test.go +++ b/cargo/jam/internal/buildpack_inspector_test.go @@ -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{ @@ -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{ { @@ -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"))) }) }) @@ -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"))) }) @@ -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 '%'"))) }) }) @@ -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"))) }) })