Skip to content

Commit 6f8b1be

Browse files
committed
Fix panic when submit not given args
Previously, submit panics when not given args due when trying to index an empty array: exercise, err := ctx.exercise(submitPaths[0])
1 parent a86f829 commit 6f8b1be

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

cmd/submit.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ type submitValidator struct {
290290

291291
// filesExistAndNotADir checks that each file exists and is not a directory.
292292
func (s submitValidator) filesExistAndNotADir(submitPaths []string) error {
293+
if len(submitPaths) == 0 {
294+
return fmt.Errorf("usage: %s submit FILE1 [FILE2 ...]", BinaryName)
295+
}
293296
for _, path := range submitPaths {
294297
path, err := filepath.Abs(path)
295298
if err != nil {

cmd/submit_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@ func TestSubmitWithoutWorkspace(t *testing.T) {
4747
}
4848
}
4949

50+
func TestSubmitWithoutArgs(t *testing.T) {
51+
tmpDir, err := ioutil.TempDir("", "submit-no-args")
52+
defer os.RemoveAll(tmpDir)
53+
assert.NoError(t, err)
54+
55+
v := viper.New()
56+
v.Set("token", "abc123")
57+
v.Set("workspace", tmpDir)
58+
v.Set("apibaseurl", "http://api.example.com")
59+
60+
cfg := config.Config{
61+
Persister: config.InMemoryPersister{},
62+
UserViperConfig: v,
63+
DefaultBaseURL: "http://example.com",
64+
}
65+
66+
var noCLIArguments []string
67+
68+
err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), noCLIArguments)
69+
if assert.Error(t, err) {
70+
assert.Contains(t, err.Error(), "usage")
71+
}
72+
}
73+
5074
func TestSubmitNonExistentFile(t *testing.T) {
5175
tmpDir, err := ioutil.TempDir("", "submit-no-such-file")
5276
defer os.RemoveAll(tmpDir)

0 commit comments

Comments
 (0)