Skip to content

Commit 20267d3

Browse files
Jacalzandydotxyz
authored andcommitted
Fix URI extension and name to be correct on Windows
Many thanks to @mbaklor for figuring this out. Should unblock #5958 failing Windows tests.
1 parent 899bedc commit 20267d3

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

storage/repository/uri.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package repository
33
import (
44
"bufio"
55
"mime"
6-
"path/filepath"
6+
"path"
77
"strings"
88
"unicode/utf8"
99

@@ -40,11 +40,11 @@ type uri struct {
4040
}
4141

4242
func (u *uri) Extension() string {
43-
return filepath.Ext(u.path)
43+
return path.Ext(u.path)
4444
}
4545

4646
func (u *uri) Name() string {
47-
return filepath.Base(u.path)
47+
return path.Base(u.path)
4848
}
4949

5050
func (u *uri) MimeType() string {

storage/repository/uri_test.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package repository
22

3-
import "testing"
3+
import (
4+
"runtime"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
49

510
var benchString string
611

@@ -16,3 +21,39 @@ func BenchmarkURIString(b *testing.B) {
1621

1722
benchString = str
1823
}
24+
25+
func TestURIExtension(t *testing.T) {
26+
uri := NewFileURI("file")
27+
assert.Equal(t, "", uri.Extension())
28+
29+
uri = NewFileURI("../file")
30+
assert.Equal(t, "", uri.Extension())
31+
32+
uri = NewFileURI("file.txt")
33+
assert.Equal(t, ".txt", uri.Extension())
34+
35+
uri = NewFileURI("file.tar.gz")
36+
assert.Equal(t, ".gz", uri.Extension())
37+
}
38+
39+
func TestURIName(t *testing.T) {
40+
uri := NewFileURI("file")
41+
assert.Equal(t, "file", uri.Name())
42+
43+
uri = NewFileURI("file.txt")
44+
assert.Equal(t, "file.txt", uri.Name())
45+
46+
uri = NewFileURI("/somewhere/file.txt")
47+
assert.Equal(t, "file.txt", uri.Name())
48+
49+
if runtime.GOOS == "windows" {
50+
uri = NewFileURI("C://somewhere/file.txt")
51+
assert.Equal(t, "file.txt", uri.Name())
52+
53+
uri = NewFileURI("C://somewhere")
54+
assert.Equal(t, "somewhere", uri.Name())
55+
56+
uri = NewFileURI("C://")
57+
assert.Equal(t, "C:", uri.Name())
58+
}
59+
}

0 commit comments

Comments
 (0)