Skip to content

Commit

Permalink
Add error message if submitting outside exercism dir
Browse files Browse the repository at this point in the history
  • Loading branch information
kytrinyx committed Oct 5, 2014
1 parent 16bbc12 commit 12672c4
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 11 deletions.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ func Submit(url string, iter *Iteration) (*Submission, error) {
}

body, err := ioutil.ReadAll(res.Body)
defer res.Body.Close()
if err != nil {
return nil, err
}
defer res.Body.Close()

ps := &PayloadSubmission{}
err = json.Unmarshal(body, ps)
Expand Down
49 changes: 45 additions & 4 deletions api/iteration.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,51 @@
package api

import (
"fmt"
"path/filepath"
"strings"
)

const (
msgUnidentifiable = "unable to identify track and problem"
)

// Iteration represents a version of a particular exercise.
// This gets submitted to the API.
type Iteration struct {
Key string `json:"key"`
Code string `json:"code"`
Path string `json:"path"`
Dir string `json:"dir"`
Key string `json:"key"`
Code string `json:"code"`
Path string `json:"path"`
Dir string `json:"dir"`
File string `json:"-"`
Language string `json:"-"`
Problem string `json:"-"`
}

func (iter *Iteration) RelativePath() string {
if iter.Path != "" {
return iter.Path
}

if len(iter.Dir) > len(iter.File) {
return ""
}
iter.Path = iter.File[len(iter.Dir):]
return iter.Path
}

func (iter *Iteration) Identify() error {
if !strings.HasPrefix(iter.File, iter.Dir) {
return fmt.Errorf(msgUnidentifiable)
}

segments := strings.Split(iter.RelativePath(), string(filepath.Separator))
// file is always the absolute path, so the first segment will be empty
if len(segments) < 4 {
return fmt.Errorf(msgUnidentifiable)
}

iter.Language = segments[1]
iter.Problem = segments[2]
return nil
}
46 changes: 46 additions & 0 deletions api/iteration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package api

import "testing"

func TestIdentify(t *testing.T) {
testCases := []struct {
file string
ok bool
}{
{
file: "/Users/me/exercism/ruby/bob/totally/fine/deep/path/src/bob.rb",
ok: true,
},
{
file: "/Users/me/exercism/ruby/bob/bob.rb",
ok: true,
},
{
file: "/Users/me/exercism/bob.rb",
ok: false,
},
{
file: "/Users/me/bob.rb",
ok: false,
},
{
file: "/tmp/bob.rb",
ok: false,
},
}

for _, tt := range testCases {
iter := &Iteration{
File: tt.file,
Dir: "/Users/me/exercism",
}
err := iter.Identify()
if !tt.ok && err == nil {
t.Errorf("Expected %s to fail.", tt.file)
}

if tt.ok && !(iter.Language == "ruby" && iter.Problem == "bob") {
t.Errorf("Language: %s, Problem: %s\nPath: %s\n", iter.Language, iter.Problem, tt.file)
}
}
}
1 change: 0 additions & 1 deletion handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,4 @@ func TestIsTest(t *testing.T) {
t.Fatalf("Expected isTest(%s) to be %t", tt.name, tt.isTest)
}
}

}
16 changes: 11 additions & 5 deletions handlers/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func Submit(ctx *cli.Context) {
log.Fatal("Please submit the solution, not the test file.")
}

path, err := filepath.Abs(filename)
file, err := filepath.Abs(filename)
if err != nil {
log.Fatal(err)
}
path, err = filepath.EvalSymlinks(path)
file, err = filepath.EvalSymlinks(file)
if err != nil {
log.Fatal(err)
}
Expand All @@ -46,7 +46,7 @@ func Submit(ctx *cli.Context) {
log.Fatal(err)
}

code, err := ioutil.ReadFile(path)
code, err := ioutil.ReadFile(file)
if err != nil {
log.Fatalf("Cannot read the contents of %s - %s\n", filename, err)
}
Expand All @@ -56,8 +56,14 @@ func Submit(ctx *cli.Context) {
iteration := &api.Iteration{
Key: c.APIKey,
Code: string(code),
Path: path[len(dir):],
Dir: c.Dir,
File: file,
Dir: dir,
}

err = iteration.Identify()
if err != nil {
msg := `Please leave the solution within the problem directory that was created by 'exercism fetch'`
log.Fatalf("Cannot submit - %s.\n\n%s", err, msg)
}

submission, err := api.Submit(url, iteration)
Expand Down

0 comments on commit 12672c4

Please sign in to comment.