Skip to content

Commit 2312441

Browse files
committed
fix: finch run panic without arguments
Signed-off-by: Arjun Raja Yogidas <arjunry@amazon.com>
1 parent b7e4eec commit 2312441

2 files changed

Lines changed: 99 additions & 2 deletions

File tree

cmd/finch/nerdctl_remote.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,12 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error {
9494
case "container run", "exec", "compose":
9595
// check if an option flag is present; immediately following the command
9696
switch {
97-
case args[0] == "run" && strings.HasPrefix(args[1], "-"):
97+
case len(args) > 1 && args[0] == "run" && strings.HasPrefix(args[1], "-"):
9898
firstOptPos = 1
99-
case strings.HasPrefix(args[0], "-"):
99+
case len(args) > 0 && strings.HasPrefix(args[0], "-"):
100+
firstOptPos = 0
101+
case len(args) == 0:
102+
args = append(args, "--help")
100103
firstOptPos = 0
101104
default:
102105
firstOptPos = -1

cmd/finch/nerdctl_remote_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/stretchr/testify/assert"
1515
"go.uber.org/mock/gomock"
1616

17+
"github.com/runfinch/finch/pkg/command"
1718
"github.com/runfinch/finch/pkg/config"
1819
"github.com/runfinch/finch/pkg/mocks"
1920
)
@@ -132,3 +133,96 @@ func TestNerdctlCommand_withVMErrors(t *testing.T) {
132133
})
133134
}
134135
}
136+
137+
func TestNerdctlCommand_withEmptyArgs(t *testing.T) {
138+
t.Parallel()
139+
140+
testCases := []struct {
141+
name string
142+
cmdName string
143+
args []string
144+
}{
145+
{
146+
name: "finch run with no arguments",
147+
cmdName: "container run",
148+
args: []string{},
149+
},
150+
{
151+
name: "finch compose with no arguments",
152+
cmdName: "compose",
153+
args: []string{},
154+
},
155+
{
156+
name: "finch exec with no arguments",
157+
cmdName: "exec",
158+
args: []string{},
159+
},
160+
}
161+
162+
for _, tc := range testCases {
163+
t.Run(tc.name, func(t *testing.T) {
164+
t.Parallel()
165+
166+
ctrl := gomock.NewController(t)
167+
ecc := mocks.NewCommandCreator(ctrl)
168+
ncc := mocks.NewNerdctlCmdCreator(ctrl)
169+
ncsd := mocks.NewNerdctlCommandSystemDeps(ctrl)
170+
logger := mocks.NewLogger(ctrl)
171+
fs := afero.NewMemMapFs()
172+
fc := &config.Finch{}
173+
174+
// Mock VM status check to return running
175+
getVMStatusC := mocks.NewCommand(ctrl)
176+
ncc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC)
177+
getVMStatusC.EXPECT().Output().Return([]byte("Running"), nil)
178+
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
179+
180+
// Mock environment variable lookups (these happen in the run method)
181+
envVars := []string{
182+
"COSIGN_PASSWORD", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY",
183+
"AWS_SESSION_TOKEN", "COMPOSE_FILE", "SOURCE_DATE_EPOCH",
184+
"AWS_ECR_DISABLE_CACHE", "AWS_ECR_CACHE_DIR", "AWS_ECR_IGNORE_CREDS_STORAGE",
185+
}
186+
for _, envVar := range envVars {
187+
ncsd.EXPECT().LookupEnv(envVar).Return("", false).AnyTimes()
188+
}
189+
190+
// Mock Windows-specific system calls (GetCmdArgs method calls these)
191+
ncsd.EXPECT().GetWd().Return("C:\\test", nil).AnyTimes()
192+
ncsd.EXPECT().FilePathAbs(gomock.Any()).DoAndReturn(func(path string) (string, error) {
193+
return path, nil
194+
}).AnyTimes()
195+
ncsd.EXPECT().FilePathToSlash(gomock.Any()).DoAndReturn(func(path string) string {
196+
return path
197+
}).AnyTimes()
198+
ncsd.EXPECT().FilePathJoin(gomock.Any()).DoAndReturn(func(elem ...string) string {
199+
return "/mnt/c/test"
200+
}).AnyTimes()
201+
202+
// Mock the RunWithReplacingStdout method (used when --help is present)
203+
ncc.EXPECT().RunWithReplacingStdout(gomock.Any(), gomock.Any()).DoAndReturn(
204+
func(replacements []command.Replacement, args ...string) error {
205+
// Verify that --help was added to the arguments
206+
found := false
207+
for _, arg := range args {
208+
if arg == "--help" {
209+
found = true
210+
break
211+
}
212+
}
213+
assert.True(t, found, "Expected --help to be added to arguments")
214+
215+
// Verify the replacement is correct (nerdctl -> finch)
216+
assert.Len(t, replacements, 1, "Expected one replacement")
217+
assert.Equal(t, "nerdctl", replacements[0].Source, "Expected source to be 'nerdctl'")
218+
assert.Equal(t, "finch", replacements[0].Target, "Expected target to be 'finch'")
219+
220+
return nil
221+
})
222+
223+
// This should not panic and should complete successfully
224+
err := newNerdctlCommand(ncc, ecc, ncsd, logger, fs, fc).run(tc.cmdName, tc.args)
225+
assert.NoError(t, err)
226+
})
227+
}
228+
}

0 commit comments

Comments
 (0)