Skip to content

Commit 48c6cd5

Browse files
added the condition for args [""] and no args[]
1 parent 5d0687f commit 48c6cd5

2 files changed

Lines changed: 99 additions & 4 deletions

File tree

pkg/client/inspect_image.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package client
22

33
import (
44
"context"
5+
"fmt"
56
"strings"
67

78
"github.com/Masterminds/semver"
@@ -88,6 +89,11 @@ const (
8889
windowsPrefix = "c:"
8990
)
9091

92+
type EnhancedProcess struct {
93+
launch.Process
94+
ArgsDisplay string
95+
}
96+
9197
// InspectImage reads the Label metadata of an image. It initializes a ImageInfo object
9298
// using this metadata, and returns it.
9399
// If daemon is true, first the local registry will be searched for the image.
@@ -173,16 +179,25 @@ func (c *Client) InspectImage(name string, daemon bool) (*ImageInfo, error) {
173179
}
174180

175181
var processDetails ProcessDetails
182+
183+
// Update to how processes are handled to include enhanced argument information.
176184
for _, proc := range buildMD.Processes {
177-
proc := proc
185+
enhancedProc := EnhancedProcess{
186+
Process: proc,
187+
ArgsDisplay: formatArgsDisplay(proc.Args), // Utilize the new formatArgsDisplay to enhance arg visibility
188+
}
189+
178190
if proc.WorkingDirectory == "" {
179-
proc.WorkingDirectory = workingDir
191+
enhancedProc.WorkingDirectory = workingDir
180192
}
193+
181194
if proc.Type == defaultProcessType {
182-
processDetails.DefaultProcess = &proc
195+
defaultProc := launch.Process(enhancedProc.Process)
196+
processDetails.DefaultProcess = &defaultProc
183197
continue
184198
}
185-
processDetails.OtherProcesses = append(processDetails.OtherProcesses, proc)
199+
200+
processDetails.OtherProcesses = append(processDetails.OtherProcesses, launch.Process(enhancedProc.Process))
186201
}
187202

188203
var stackCompat files.Stack
@@ -229,3 +244,20 @@ func getRebasableLabel(labeled dist.Labeled) (bool, error) {
229244

230245
return rebasableOutput, nil
231246
}
247+
248+
func formatArgsDisplay(args []string) string {
249+
if len(args) == 0 {
250+
return "<NONE>" // Indicates no arguments are present.
251+
}
252+
253+
var result strings.Builder
254+
result.WriteString(fmt.Sprintf("(count = %d) ", len(args)))
255+
for _, arg := range args {
256+
if arg == "" {
257+
result.WriteString(`"" `) // Represent empty string arguments visibly.
258+
} else {
259+
result.WriteString(fmt.Sprintf("%q ", arg))
260+
}
261+
}
262+
return strings.TrimSpace(result.String())
263+
}

pkg/client/inspect_image_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,3 +1023,66 @@ func testInspectImage(t *testing.T, when spec.G, it spec.S) {
10231023
})
10241024
})
10251025
}
1026+
1027+
func TestArgumentParsing(t *testing.T) {
1028+
spec.Run(t, "Argument Parsing", testArgumentParsing, spec.Parallel(), spec.Report(report.Terminal{}))
1029+
}
1030+
1031+
func testArgumentParsing(t *testing.T, when spec.G, it spec.S) {
1032+
var (
1033+
mockController *gomock.Controller
1034+
mockImageFetcher *testmocks.MockImageFetcher
1035+
subject *Client
1036+
out bytes.Buffer
1037+
)
1038+
1039+
it.Before(func() {
1040+
mockController = gomock.NewController(t)
1041+
mockImageFetcher = testmocks.NewMockImageFetcher(mockController)
1042+
1043+
var err error
1044+
subject, err = NewClient(WithLogger(logging.NewLogWithWriters(&out, &out)), WithFetcher(mockImageFetcher))
1045+
h.AssertNil(t, err)
1046+
})
1047+
1048+
it.After(func() {
1049+
mockController.Finish()
1050+
})
1051+
1052+
when("inspecting images with different argument configurations", func() {
1053+
it("properly displays the arguments", func() {
1054+
images := map[string]string{
1055+
"imageNoArgs": `[]`,
1056+
"imageEmptyArgs": `[""]`,
1057+
"imageNonEmptyArgs": `["-p", "8080"]`,
1058+
}
1059+
1060+
for imageName, args := range images {
1061+
mockImage := testmocks.NewImage(imageName, "", nil)
1062+
h.AssertNil(t, mockImage.SetLabel("io.buildpacks.build.metadata", fmt.Sprintf(`{
1063+
"processes": [
1064+
{
1065+
"type": "web",
1066+
"command": "/start/web-process",
1067+
"args": %s,
1068+
"direct": false
1069+
}
1070+
]
1071+
}`, args)))
1072+
1073+
mockImageFetcher.EXPECT().Fetch(gomock.Any(), imageName, gomock.Any()).Return(mockImage, nil).Times(1)
1074+
1075+
info, err := subject.InspectImage(imageName, true)
1076+
h.AssertNil(t, err)
1077+
1078+
if args == `[]` {
1079+
h.AssertEq(t, info.Processes.DefaultProcess.Args, []string{})
1080+
} else if args == `[""]` {
1081+
h.AssertEq(t, info.Processes.DefaultProcess.Args, []string{""})
1082+
} else {
1083+
h.AssertEq(t, info.Processes.DefaultProcess.Args, []string{"-p", "8080"})
1084+
}
1085+
}
1086+
})
1087+
})
1088+
}

0 commit comments

Comments
 (0)