Skip to content

Commit ca4f60b

Browse files
author
Katrina Owen
committed
Replace gnarly logic with standard library call
1 parent efe66bd commit ca4f60b

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

workspace/document.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package workspace
22

3-
import (
4-
"os"
5-
"path/filepath"
6-
"strings"
7-
)
3+
import "path/filepath"
84

95
// Document is a file in a directory.
106
type Document struct {
@@ -24,8 +20,10 @@ func NewDocument(root, path string) Document {
2420

2521
// Path is the normalized path.
2622
// It uses forward slashes regardless of the operating system.
27-
func (doc Document) Path() string {
28-
path := strings.Replace(doc.Filepath, doc.Root, "", 1)
29-
path = strings.TrimLeft(path, string(os.PathSeparator))
30-
return filepath.ToSlash(path)
23+
func (doc Document) Path() (string, error) {
24+
path, err := filepath.Rel(doc.Root, doc.Filepath)
25+
if err != nil {
26+
return "", err
27+
}
28+
return filepath.ToSlash(path), nil
3129
}

workspace/document_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ func TestNormalizedDocumentPath(t *testing.T) {
2525

2626
for _, tc := range testCases {
2727
doc := NewDocument(root, tc.filepath)
28-
assert.Equal(t, tc.path, doc.Path())
28+
path, err := doc.Path()
29+
assert.NoError(t, err)
30+
assert.Equal(t, tc.path, path)
2931
}
3032
}

0 commit comments

Comments
 (0)