Skip to content

Commit fcf8046

Browse files
committed
Merge pull request #16 from nf/master
fix submit with relative path name
2 parents d26185b + 28c938d commit fcf8046

4 files changed

Lines changed: 42 additions & 21 deletions

File tree

src/exercism/api.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"fmt"
88
"io/ioutil"
99
"net/http"
10-
"os"
11-
"path/filepath"
1210
)
1311

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

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

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

85-
workingDirectory, err := os.Getwd()
86-
if err != nil {
87-
return
88-
}
89-
90-
fullFilePath := filepath.Join(workingDirectory, filepath.Clean(filePath))
91-
code, err := ioutil.ReadFile(fullFilePath)
92-
93-
if err != nil {
94-
return
95-
}
96-
9783
submission := submitRequest{Key: apiKey, Code: string(code), Path: filePath}
9884
submissionJson, err := json.Marshal(submission)
9985
if err != nil {

src/exercism/api_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ var submitHandler = func(rw http.ResponseWriter, r *http.Request) {
121121
filePath := submission.Path
122122

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

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

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

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

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

171173
assert.Error(t, err)
172174
assert.Nil(t, response)

src/fixtures/submit/ruby/bob/bob.rb

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/main/exercism.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import (
44
"exercism"
55
"fmt"
66
"github.com/codegangsta/cli"
7+
"io/ioutil"
78
"os"
9+
"path/filepath"
10+
"strings"
811
)
912

1013
func main() {
@@ -122,14 +125,33 @@ func main() {
122125

123126
filename := c.Args()[0]
124127

128+
// Make filename relative to config.ExercismDirectory.
129+
absPath, err := absolutePath(filename)
130+
if err != nil {
131+
fmt.Printf("Couldn't find %v: %v\n", filename, err)
132+
return
133+
}
134+
exDir := config.ExercismDirectory + string(filepath.Separator)
135+
if !strings.HasPrefix(absPath, exDir) {
136+
fmt.Printf("%v is not under your exercism project path (%v)\n", absPath, exDir)
137+
return
138+
}
139+
filename = absPath[len(exDir):]
140+
125141
if exercism.IsTest(filename) {
126142
fmt.Println("It looks like this is a test, please enter an example file name.")
127143
return
128144
}
129145

130-
response, err := exercism.SubmitAssignment(config.Hostname, config.ApiKey, filename)
146+
code, err := ioutil.ReadFile(absPath)
131147
if err != nil {
132-
fmt.Printf("There was an issue with your submission: %s\n", err)
148+
fmt.Printf("Error reading %v: %v\n", absPath, err)
149+
return
150+
}
151+
152+
response, err := exercism.SubmitAssignment(config.Hostname, config.ApiKey, filename, code)
153+
if err != nil {
154+
fmt.Printf("There was an issue with your submission: %v\n", err)
133155
return
134156
}
135157

@@ -183,10 +205,22 @@ func askForConfigInfo() (c exercism.Config) {
183205
if err != nil && err.Error() != "unexpected newline" {
184206
panic(err)
185207
}
208+
dir, err = absolutePath(dir)
209+
if err != nil {
210+
panic(err)
211+
}
186212

187213
if dir == "" {
188214
dir = currentDir
189215
}
190216

191217
return exercism.Config{un, key, exercism.ReplaceTilde(dir), "http://exercism.io"}
192218
}
219+
220+
func absolutePath(path string) (string, error) {
221+
path, err := filepath.Abs(path)
222+
if err != nil {
223+
return "", err
224+
}
225+
return filepath.EvalSymlinks(path)
226+
}

0 commit comments

Comments
 (0)