Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
if err != nil {
return err
}
if info.Size() >= int64(65535) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can turn this into an unxported constant const maxFileSize int64 = 65535

msg :=`

The file you are trying to submit is %d bytes. Please reduce the file to below 65535 bytes and try again.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small suggestion on the messaging

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

We can use the const here as well for printing and test explicitly for this message.

This error will also throw golint error because the error message starts with a capital letter, but that’s okay right now because of how we are printing this errors. We’ll solve that later for submit and download.


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

msg := `
Expand Down
42 changes: 42 additions & 0 deletions cmd/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,48 @@ 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.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s assert that we have an error assert.EqualError. I’ve seen cases in the past where the error check was skipped, and then resulted in a nil reference error when calling err.Error(). Which cluters the test output making it just a little longer to debug.

assert.Regexp(t, "Please reduce the file to below 65535 bytes and try again.", err.Error())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my suggestion above about the messaging and using a const variable.

}

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