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
16 changes: 1 addition & 15 deletions src/exercism/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
)

const VERSION = "1.0.0"
Expand Down Expand Up @@ -77,23 +75,11 @@ func FetchAssignments(host string, path string, apiKey string) (as []Assignment,
return fr.Assignments, err
}

func SubmitAssignment(host, apiKey, filePath string) (r *submitResponse, err error) {
func SubmitAssignment(host, apiKey, filePath string, code []byte) (r *submitResponse, err error) {
path := "api/v1/user/assignments"

url := fmt.Sprintf("%s/%s", host, path)

workingDirectory, err := os.Getwd()
if err != nil {
return
}

fullFilePath := filepath.Join(workingDirectory, filepath.Clean(filePath))
code, err := ioutil.ReadFile(fullFilePath)

if err != nil {
return
}

submission := submitRequest{Key: apiKey, Code: string(code), Path: filePath}
submissionJson, err := json.Marshal(submission)
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions src/exercism/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ var submitHandler = func(rw http.ResponseWriter, r *http.Request) {
filePath := submission.Path

codeMatches := string(code) == "My source code\n"
filePathMatches := filePath == "../fixtures/submit/ruby/bob/bob.rb"
filePathMatches := filePath == "ruby/bob/bob.rb"

if !filePathMatches {
fmt.Printf("FilePathMismatch: File Path: %s\n", filePath)
Expand Down Expand Up @@ -153,7 +153,8 @@ func TestSubmitWithKey(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(submitHandler))
defer server.Close()

response, err := SubmitAssignment(server.URL, "myApiKey", "../fixtures/submit/ruby/bob/bob.rb")
var code = []byte("My source code\n")
response, err := SubmitAssignment(server.URL, "myApiKey", "ruby/bob/bob.rb", code)
assert.NoError(t, err)

assert.Equal(t, response.Status, "saved")
Expand All @@ -166,7 +167,8 @@ func TestSubmitWithIncorrectKey(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(submitHandler))
defer server.Close()

response, err := SubmitAssignment(server.URL, "myWrongApiKey", "ruby/bob/bob.rb")
var code = []byte("My source code\n")
response, err := SubmitAssignment(server.URL, "myWrongApiKey", "ruby/bob/bob.rb", code)

assert.Error(t, err)
assert.Nil(t, response)
Expand Down
1 change: 0 additions & 1 deletion src/fixtures/submit/ruby/bob/bob.rb

This file was deleted.

38 changes: 36 additions & 2 deletions src/main/exercism.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"exercism"
"fmt"
"github.com/codegangsta/cli"
"io/ioutil"
"os"
"path/filepath"
"strings"
)

func main() {
Expand Down Expand Up @@ -122,14 +125,33 @@ func main() {

filename := c.Args()[0]

// Make filename relative to config.ExercismDirectory.
absPath, err := absolutePath(filename)
if err != nil {
fmt.Printf("Couldn't find %v: %v\n", filename, err)
return
}
exDir := config.ExercismDirectory + string(filepath.Separator)
if !strings.HasPrefix(absPath, exDir) {
fmt.Printf("%v is not under your exercism project path (%v)\n", absPath, exDir)
return
}
filename = absPath[len(exDir):]

if exercism.IsTest(filename) {
fmt.Println("It looks like this is a test, please enter an example file name.")
return
}

response, err := exercism.SubmitAssignment(config.Hostname, config.ApiKey, filename)
code, err := ioutil.ReadFile(absPath)
if err != nil {
fmt.Printf("There was an issue with your submission: %s\n", err)
fmt.Printf("Error reading %v: %v\n", absPath, err)
return
}

response, err := exercism.SubmitAssignment(config.Hostname, config.ApiKey, filename, code)
if err != nil {
fmt.Printf("There was an issue with your submission: %v\n", err)
return
}

Expand Down Expand Up @@ -183,10 +205,22 @@ func askForConfigInfo() (c exercism.Config) {
if err != nil && err.Error() != "unexpected newline" {
panic(err)
}
dir, err = absolutePath(dir)
if err != nil {
panic(err)
}

if dir == "" {
dir = currentDir
}

return exercism.Config{un, key, exercism.ReplaceTilde(dir), "http://exercism.io"}
}

func absolutePath(path string) (string, error) {
path, err := filepath.Abs(path)
if err != nil {
return "", err
}
return filepath.EvalSymlinks(path)
}