-
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add error message if submitting outside exercism dir
- Loading branch information
Showing
5 changed files
with
103 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,5 +50,4 @@ func TestIsTest(t *testing.T) { | |
t.Fatalf("Expected isTest(%s) to be %t", tt.name, tt.isTest) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters