Conversation
Given the path to an exercise directory on the filesystem, determine the root, the track, and the exercise slug.
This normalizes the file sent to the server to use forward slashes.
0066dfe to
0385ab8
Compare
|
I've rebased this onto master. |
|
Thanks @kytrinyx. I’ll have time to review later today. |
workspace/document.go
Outdated
| // Path is the normalized path. | ||
| // It uses forward slashes regardless of the operating system. | ||
| func (doc Document) Path() string { | ||
| path := strings.Replace(doc.Filepath, doc.Root, "", 1) |
There was a problem hiding this comment.
Are these two lines doing the same thing as path, _ := filepath.Rel(doc.Root, doc.Filepath)? Ignoring error right now.
There was a problem hiding this comment.
Oh. My. Godness.
I didn't know about filepath.Rel. That's exactly what I need.
workspace/document.go
Outdated
| Filepath string | ||
| } | ||
|
|
||
| // NewDocument creates a document from a filepath. |
There was a problem hiding this comment.
Lets add a comment on what is Root and what is Filepath. It seems like Filepath is more of a relative path to the file.
cmd/submit.go
Outdated
| continue | ||
| } | ||
| paths = append(paths, file) | ||
| exercise.Documents = append(exercise.Documents, exercise.NewDocument(file)) |
There was a problem hiding this comment.
Would it be clearer here if it was workspace.NewDocument(exercise.Filepath, file)
There was a problem hiding this comment.
I don't feel strongly either way.
workspace/exercise.go
Outdated
| } | ||
|
|
||
| // NewDocument creates a document relative to the exercise. | ||
| func (e Exercise) NewDocument(file string) Document { |
There was a problem hiding this comment.
Method on this type really feels like it should be a single function or just deleted as workspace.NewDocument does the same thing.
There was a problem hiding this comment.
I'll go ahead and make the change--I really don't have a preference one way or the other.
workspace/document.go
Outdated
| func NewDocument(root, file string) Document { | ||
| return Document{ | ||
| Root: root, | ||
| Filepath: file, |
There was a problem hiding this comment.
It's a path relative to the root. So for clojure exercises it would be src/anagram.clj
cmd/submit.go
Outdated
| for _, path := range paths { | ||
| file, err := os.Open(path) | ||
| for _, doc := range exercise.Documents { | ||
| file, err := os.Open(doc.Filepath) |
There was a problem hiding this comment.
I haven’t verified this yet. But I think we are losing the ability to submit via full path here.
doc.Filepath is a relative path so if I submit outside of my track calling open here may result in file not exist error. We should add a test TestSubmitWithFullPaths
There was a problem hiding this comment.
Good catch! I'll add a test.
| filename := pieces[len(pieces)-1] | ||
|
|
||
| part, err := writer.CreateFormFile("files[]", filename) | ||
| part, err := writer.CreateFormFile("files[]", doc.Path()) |
There was a problem hiding this comment.
doc.Path() now returns an error as a second value which now needs to be handled
There was a problem hiding this comment.
Dang. Why did this compile?
There was a problem hiding this comment.
I guess maybe it didn't but I was sure I had run all the tests...
ca4f60b to
916aa59
Compare
916aa59 to
e5a7345
Compare
|
I think I got this sorted out. I've moved the |
There was a problem hiding this comment.
Nicely done! This works as intended when submitting using full and relative paths.
I requested a change on the documentation for NewDocument which has an unwanted behavior if path is relative. It is not an issue for the commands because we are always passing the absolute path. I provided code to give context. If the documentation is clear we may not need to change anything. It really depends on how flexible we want the code to be.
workspace/document.go
Outdated
|
|
||
| // NewDocument creates a document from a relative filepath. | ||
| // The root is typically the root of the exercise, and | ||
| // path is the relative path to the file within the root directory. |
There was a problem hiding this comment.
Path is no longer a relative path. Path needs to be an absolute path, rooted with root, or filepath.Rel will return an error.
calling filepath.Abs before calling filepath.Rel in this method is an option but it can result in unwanted behavior. https://play.golang.org/p/cLE1R9dsRcH
I think a filepath.HasPrefix(root, path) check before .Rel would catch it.
I actually went back and forth on that a bit, but concluded that I don't want the flexibility until we find that we need it. Every time I get myself in trouble with complexity it's because I thought we needed flexibility that we really didn't need. I'll fix the doc comment! |
Good call! Comment is good and will save us time in the future if we need to revisit. |
|
Yay, thanks so much for your patience working through this one. I'll cut a release tonight. |
Use a normalized path relative to the exercise directory and using forward slashes when submitting to the server. This is converted to a proper filepath on the file system, using
/or\depending on the operating system.This builds on top of the pull request in #703 -- too look at just the difference between the two branches, compare no-leading-slash to path-vs-filepath.
Closes #698
Closes #665