Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
if err != nil {
return err
}
const maxFileSize int64 = 65535
if info.Size() >= maxFileSize {
msg :=`

The submitted file is larger than the max allowed file size of %d bytes. Please reduce the size of the file and try again.

`
return fmt.Errorf(msg, maxFileSize)
}
if info.Size() == 0 {

msg := `
Expand Down
46 changes: 46 additions & 0 deletions cmd/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,52 @@ func TestSubmitWithEmptyFile(t *testing.T) {
assert.Equal(t, "This is file 2.", submittedFiles["file-2.txt"])
}

func TestSubmitWithEnormousFile(t *testing.T) {
oldOut := Out
oldErr := Err
Out = ioutil.Discard
Err = ioutil.Discard
defer func() {
Out = oldOut
Err = oldErr
}()

// The fake endpoint will populate this when it receives the call from the command.
submittedFiles := map[string]string{}
ts := fakeSubmitServer(t, submittedFiles)
defer ts.Close()

tmpDir, err := ioutil.TempDir("", "enormous-file")
defer os.RemoveAll(tmpDir)
assert.NoError(t, err)

dir := filepath.Join(tmpDir, "bogus-track", "bogus-exercise")
os.MkdirAll(dir, os.FileMode(0755))

writeFakeSolution(t, dir, "bogus-track", "bogus-exercise")

v := viper.New()
v.Set("token", "abc123")
v.Set("workspace", tmpDir)
v.Set("apibaseurl", ts.URL)

cfg := config.Config{
Persister: config.InMemoryPersister{},
UserViperConfig: v,
}

file := filepath.Join(dir, "file.txt")
err = ioutil.WriteFile(file, make([]byte, 65535), os.FileMode(0755))
Comment thread
nywilken marked this conversation as resolved.
Comment thread
sfairchild marked this conversation as resolved.
if err != nil {
t.Fatal(err)
}

err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{file})

assert.Error(t, err)
assert.Regexp(t, "Please reduce the size of the file and try again.", err.Error())
}

func TestSubmitFilesForTeamExercise(t *testing.T) {
oldOut := Out
oldErr := Err
Expand Down