Skip to content

Distinguish between path and filepath#705

Merged
kytrinyx merged 7 commits intomasterfrom
path-vs-filepath
Aug 21, 2018
Merged

Distinguish between path and filepath#705
kytrinyx merged 7 commits intomasterfrom
path-vs-filepath

Conversation

@kytrinyx
Copy link
Member

@kytrinyx kytrinyx commented Aug 11, 2018

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

Katrina Owen added 3 commits August 12, 2018 10:39
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.
@kytrinyx
Copy link
Member Author

I've rebased this onto master.

@nywilken
Copy link
Contributor

Thanks @kytrinyx. I’ll have time to review later today.

// 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these two lines doing the same thing as path, _ := filepath.Rel(doc.Root, doc.Filepath)? Ignoring error right now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. My. Godness.

I didn't know about filepath.Rel. That's exactly what I need.

Filepath string
}

// NewDocument creates a document from a filepath.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yepp, good call.

Copy link
Contributor

@nywilken nywilken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kytrinyx I left a few comments to help clarify a few items. I like the approach and the concept of exercise documents.

I’ll take another look in the am

cmd/submit.go Outdated
continue
}
paths = append(paths, file)
exercise.Documents = append(exercise.Documents, exercise.NewDocument(file))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be clearer here if it was workspace.NewDocument(exercise.Filepath, file)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel strongly either way.

}

// NewDocument creates a document relative to the exercise.
func (e Exercise) NewDocument(file string) Document {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method on this type really feels like it should be a single function or just deleted as workspace.NewDocument does the same thing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll go ahead and make the change--I really don't have a preference one way or the other.

func NewDocument(root, file string) Document {
return Document{
Root: root,
Filepath: file,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be Filename?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a path relative to the root. So for clojure exercises it would be src/anagram.clj

Copy link
Contributor

@nywilken nywilken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kytrinyx I took another look. We have an issue with the latest change and I think we might be losing functionality. I will confirm the latter once I get behind a machine.

cmd/submit.go Outdated
for _, path := range paths {
file, err := os.Open(path)
for _, doc := range exercise.Documents {
file, err := os.Open(doc.Filepath)
Copy link
Contributor

@nywilken nywilken Aug 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc.Path() now returns an error as a second value which now needs to be handled

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dang. Why did this compile?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess maybe it didn't but I was sure I had run all the tests...

@kytrinyx
Copy link
Member Author

I think I got this sorted out. I've moved the filepath.Rel logic into the NewDocument() function instead of keeping it in the normalized Path() method. @nywilken would you take another look at this?

Copy link
Contributor

@nywilken nywilken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.


// 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@kytrinyx
Copy link
Member Author

If the documentation is clear we may not need to change anything. It really depends on how flexible we want the code to be.

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!

@nywilken
Copy link
Contributor

nywilken commented Aug 20, 2018

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.

Good call! Comment is good and will save us time in the future if we need to revisit.

Copy link
Contributor

@nywilken nywilken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good to Go!

@kytrinyx kytrinyx merged commit 78cbd41 into master Aug 21, 2018
@kytrinyx kytrinyx deleted the path-vs-filepath branch August 21, 2018 19:27
@kytrinyx
Copy link
Member Author

Yay, thanks so much for your patience working through this one. I'll cut a release tonight.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants