From e059ce838af8edfc4f250c7e0a3c3e02d354d6d1 Mon Sep 17 00:00:00 2001 From: Emily Casey Date: Tue, 22 Oct 2019 14:57:24 -0400 Subject: [PATCH] fix inspect-image --bom when image is missing Signed-off-by: Emily Casey Signed-off-by: Javier Romero --- commands/inspect_image.go | 11 +++++++++-- commands/inspect_image_test.go | 14 +++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/commands/inspect_image.go b/commands/inspect_image.go index 8dfd92a23..ce705463b 100644 --- a/commands/inspect_image.go +++ b/commands/inspect_image.go @@ -55,9 +55,16 @@ type bom struct { } func logBOM(remote *pack.ImageInfo, local *pack.ImageInfo, logger logging.Logger) error { + var remoteBOM, localBOM interface{} + if remote != nil { + remoteBOM = remote.BOM + } + if local != nil { + localBOM = local.BOM + } rawBOM, err := json.Marshal(bom{ - Remote: remote.BOM, - Local: local.BOM, + Remote: remoteBOM, + Local: localBOM, }) if err != nil { return errors.Wrapf(err, "writing bill of materials") diff --git a/commands/inspect_image_test.go b/commands/inspect_image_test.go index 7de6c1572..93e699e2f 100644 --- a/commands/inspect_image_test.go +++ b/commands/inspect_image_test.go @@ -53,14 +53,26 @@ func testInspectImageCommand(t *testing.T, when spec.G, it spec.S) { when("#InspectImage", func() { when("image cannot be found", func() { - it("logs 'Not present'", func() { + it.Before(func() { mockClient.EXPECT().InspectImage("some/image", false).Return(nil, nil) mockClient.EXPECT().InspectImage("some/image", true).Return(nil, nil) + }) + it("logs 'Not present'", func() { h.AssertNil(t, command.Execute()) h.AssertContains(t, outBuf.String(), "REMOTE:\n(not present)\n\nLOCAL:\n(not present)\n") }) + + when("--bom", func() { + it("adds nulls for missing images", func() { + command.SetArgs([]string{"some/image", "--bom"}) + h.AssertNil(t, command.Execute()) + h.AssertEq(t, + outBuf.String(), + `{"remote":null,"local":null}`+"\n") + }) + }) }) when("inspector returns an error", func() {