Skip to content

Commit

Permalink
Fix panic when submit not given args
Browse files Browse the repository at this point in the history
Previously, submit panics when not given args due when trying to index
an empty array:

    exercise, err := ctx.exercise(submitPaths[0])
  • Loading branch information
jdsutherland committed Jan 21, 2019
1 parent a86f829 commit 6f8b1be
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ type submitValidator struct {

// filesExistAndNotADir checks that each file exists and is not a directory.
func (s submitValidator) filesExistAndNotADir(submitPaths []string) error {
if len(submitPaths) == 0 {
return fmt.Errorf("usage: %s submit FILE1 [FILE2 ...]", BinaryName)
}
for _, path := range submitPaths {
path, err := filepath.Abs(path)
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions cmd/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,30 @@ func TestSubmitWithoutWorkspace(t *testing.T) {
}
}

func TestSubmitWithoutArgs(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "submit-no-args")
defer os.RemoveAll(tmpDir)
assert.NoError(t, err)

v := viper.New()
v.Set("token", "abc123")
v.Set("workspace", tmpDir)
v.Set("apibaseurl", "http://api.example.com")

cfg := config.Config{
Persister: config.InMemoryPersister{},
UserViperConfig: v,
DefaultBaseURL: "http://example.com",
}

var noCLIArguments []string

err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), noCLIArguments)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "usage")
}
}

func TestSubmitNonExistentFile(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "submit-no-such-file")
defer os.RemoveAll(tmpDir)
Expand Down

0 comments on commit 6f8b1be

Please sign in to comment.