Skip to content

Commit

Permalink
added the condition for args [""] and no args[]
Browse files Browse the repository at this point in the history
  • Loading branch information
keshavdalmia10 committed Aug 12, 2024
1 parent 5d0687f commit 48c6cd5
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 4 deletions.
40 changes: 36 additions & 4 deletions pkg/client/inspect_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"fmt"
"strings"

"github.com/Masterminds/semver"
Expand Down Expand Up @@ -88,6 +89,11 @@ const (
windowsPrefix = "c:"
)

type EnhancedProcess struct {
launch.Process
ArgsDisplay string
}

// InspectImage reads the Label metadata of an image. It initializes a ImageInfo object
// using this metadata, and returns it.
// If daemon is true, first the local registry will be searched for the image.
Expand Down Expand Up @@ -173,16 +179,25 @@ func (c *Client) InspectImage(name string, daemon bool) (*ImageInfo, error) {
}

var processDetails ProcessDetails

// Update to how processes are handled to include enhanced argument information.
for _, proc := range buildMD.Processes {
proc := proc
enhancedProc := EnhancedProcess{
Process: proc,
ArgsDisplay: formatArgsDisplay(proc.Args), // Utilize the new formatArgsDisplay to enhance arg visibility
}

if proc.WorkingDirectory == "" {
proc.WorkingDirectory = workingDir
enhancedProc.WorkingDirectory = workingDir
}

if proc.Type == defaultProcessType {
processDetails.DefaultProcess = &proc
defaultProc := launch.Process(enhancedProc.Process)

Check failure on line 195 in pkg/client/inspect_image.go

View workflow job for this annotation

GitHub Actions / test (macos)

unnecessary conversion (unconvert)

Check failure on line 195 in pkg/client/inspect_image.go

View workflow job for this annotation

GitHub Actions / test (linux)

unnecessary conversion (unconvert)

Check failure on line 195 in pkg/client/inspect_image.go

View workflow job for this annotation

GitHub Actions / test (windows-lcow)

unnecessary conversion (unconvert)

Check failure on line 195 in pkg/client/inspect_image.go

View workflow job for this annotation

GitHub Actions / test (windows-wcow)

unnecessary conversion (unconvert)
processDetails.DefaultProcess = &defaultProc
continue
}
processDetails.OtherProcesses = append(processDetails.OtherProcesses, proc)

processDetails.OtherProcesses = append(processDetails.OtherProcesses, launch.Process(enhancedProc.Process))

Check failure on line 200 in pkg/client/inspect_image.go

View workflow job for this annotation

GitHub Actions / test (macos)

unnecessary conversion (unconvert)

Check failure on line 200 in pkg/client/inspect_image.go

View workflow job for this annotation

GitHub Actions / test (linux)

unnecessary conversion (unconvert)

Check failure on line 200 in pkg/client/inspect_image.go

View workflow job for this annotation

GitHub Actions / test (windows-lcow)

unnecessary conversion (unconvert)

Check failure on line 200 in pkg/client/inspect_image.go

View workflow job for this annotation

GitHub Actions / test (windows-wcow)

unnecessary conversion (unconvert)
}

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

return rebasableOutput, nil
}

func formatArgsDisplay(args []string) string {
if len(args) == 0 {
return "<NONE>" // Indicates no arguments are present.
}

var result strings.Builder
result.WriteString(fmt.Sprintf("(count = %d) ", len(args)))
for _, arg := range args {
if arg == "" {
result.WriteString(`"" `) // Represent empty string arguments visibly.
} else {
result.WriteString(fmt.Sprintf("%q ", arg))
}
}
return strings.TrimSpace(result.String())
}
63 changes: 63 additions & 0 deletions pkg/client/inspect_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,3 +1023,66 @@ func testInspectImage(t *testing.T, when spec.G, it spec.S) {
})
})
}

func TestArgumentParsing(t *testing.T) {
spec.Run(t, "Argument Parsing", testArgumentParsing, spec.Parallel(), spec.Report(report.Terminal{}))
}

func testArgumentParsing(t *testing.T, when spec.G, it spec.S) {
var (
mockController *gomock.Controller
mockImageFetcher *testmocks.MockImageFetcher
subject *Client
out bytes.Buffer
)

it.Before(func() {
mockController = gomock.NewController(t)
mockImageFetcher = testmocks.NewMockImageFetcher(mockController)

var err error
subject, err = NewClient(WithLogger(logging.NewLogWithWriters(&out, &out)), WithFetcher(mockImageFetcher))
h.AssertNil(t, err)
})

it.After(func() {
mockController.Finish()
})

when("inspecting images with different argument configurations", func() {
it("properly displays the arguments", func() {
images := map[string]string{
"imageNoArgs": `[]`,
"imageEmptyArgs": `[""]`,
"imageNonEmptyArgs": `["-p", "8080"]`,
}

for imageName, args := range images {
mockImage := testmocks.NewImage(imageName, "", nil)
h.AssertNil(t, mockImage.SetLabel("io.buildpacks.build.metadata", fmt.Sprintf(`{
"processes": [
{
"type": "web",
"command": "/start/web-process",
"args": %s,
"direct": false
}
]
}`, args)))

mockImageFetcher.EXPECT().Fetch(gomock.Any(), imageName, gomock.Any()).Return(mockImage, nil).Times(1)

info, err := subject.InspectImage(imageName, true)
h.AssertNil(t, err)

if args == `[]` {

Check failure on line 1078 in pkg/client/inspect_image_test.go

View workflow job for this annotation

GitHub Actions / test (macos)

ifElseChain: rewrite if-else to switch statement (gocritic)

Check failure on line 1078 in pkg/client/inspect_image_test.go

View workflow job for this annotation

GitHub Actions / test (linux)

ifElseChain: rewrite if-else to switch statement (gocritic)

Check failure on line 1078 in pkg/client/inspect_image_test.go

View workflow job for this annotation

GitHub Actions / test (windows-lcow)

ifElseChain: rewrite if-else to switch statement (gocritic)

Check failure on line 1078 in pkg/client/inspect_image_test.go

View workflow job for this annotation

GitHub Actions / test (windows-wcow)

ifElseChain: rewrite if-else to switch statement (gocritic)
h.AssertEq(t, info.Processes.DefaultProcess.Args, []string{})
} else if args == `[""]` {
h.AssertEq(t, info.Processes.DefaultProcess.Args, []string{""})
} else {
h.AssertEq(t, info.Processes.DefaultProcess.Args, []string{"-p", "8080"})
}
}
})
})
}

0 comments on commit 48c6cd5

Please sign in to comment.