-
-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #705 from exercism/path-vs-filepath
Distinguish between path and filepath
- Loading branch information
Showing
6 changed files
with
113 additions
and
16 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
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,34 @@ | ||
package workspace | ||
|
||
import "path/filepath" | ||
|
||
// Document is a file in a directory. | ||
type Document struct { | ||
Root string | ||
RelativePath string | ||
} | ||
|
||
// NewDocument creates a document from the filepath. | ||
// The root is typically the root of the exercise, and | ||
// path is the absolute path to the file. | ||
func NewDocument(root, path string) (Document, error) { | ||
path, err := filepath.Rel(root, path) | ||
if err != nil { | ||
return Document{}, err | ||
} | ||
return Document{ | ||
Root: root, | ||
RelativePath: path, | ||
}, nil | ||
} | ||
|
||
// Filepath is the absolute path to the document on the filesystem. | ||
func (doc Document) Filepath() string { | ||
return filepath.Join(doc.Root, doc.RelativePath) | ||
} | ||
|
||
// Path is the normalized path. | ||
// It uses forward slashes regardless of the operating system. | ||
func (doc Document) Path() string { | ||
return filepath.ToSlash(doc.RelativePath) | ||
} |
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,43 @@ | ||
package workspace | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNormalizedDocumentPath(t *testing.T) { | ||
root, err := ioutil.TempDir("", "docpath") | ||
assert.NoError(t, err) | ||
defer os.RemoveAll(root) | ||
|
||
err = os.MkdirAll(filepath.Join(root, "subdirectory"), os.FileMode(0755)) | ||
assert.NoError(t, err) | ||
|
||
testCases := []struct { | ||
filepath string | ||
path string | ||
}{ | ||
{ | ||
filepath: filepath.Join(root, "file.txt"), | ||
path: "file.txt", | ||
}, | ||
{ | ||
filepath: filepath.Join(root, "subdirectory", "file.txt"), | ||
path: "subdirectory/file.txt", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
err = ioutil.WriteFile(tc.filepath, []byte("a file"), os.FileMode(0600)) | ||
assert.NoError(t, err) | ||
|
||
doc, err := NewDocument(root, tc.filepath) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, doc.Path(), tc.path) | ||
} | ||
} |
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