diff --git a/internal/repository/memory.go b/internal/repository/memory.go index e7870a07a1..3537f58523 100644 --- a/internal/repository/memory.go +++ b/internal/repository/memory.go @@ -1,13 +1,13 @@ package repository import ( - "fyne.io/fyne/v2" - "fyne.io/fyne/v2/storage" - "fyne.io/fyne/v2/storage/repository" - "fmt" "io" "strings" + + "fyne.io/fyne/v2" + "fyne.io/fyne/v2/storage" + "fyne.io/fyne/v2/storage/repository" ) // declare conformance to interfaces @@ -297,7 +297,7 @@ func (m *InMemoryRepository) List(u fyne.URI) ([]fyne.URI, error) { // does not have one. pSplit := strings.Split(p, "/") ncomp := len(pSplit) - if p[len(p)-1] == '/' { + if len(p) > 0 && p[len(p)-1] == '/' { ncomp-- } diff --git a/internal/repository/memory_test.go b/internal/repository/memory_test.go index 17bb5d0eff..b61b59c1da 100644 --- a/internal/repository/memory_test.go +++ b/internal/repository/memory_test.go @@ -68,6 +68,10 @@ func TestInMemoryRepositoryParsing(t *testing.T) { baz, _ := storage.ParseURI("mem:///baz") assert.Nil(t, err) assert.NotNil(t, baz) + + empty, _ := storage.ParseURI("mem:") + assert.Nil(t, err) + assert.NotNil(t, empty) } func TestInMemoryRepositoryExists(t *testing.T) { @@ -328,6 +332,7 @@ func TestInMemoryRepositoryListing(t *testing.T) { // set up our repository - it's OK if we already registered it m := NewInMemoryRepository("mem") repository.Register("mem", m) + m.Data[""] = []byte{1, 2, 3} m.Data["/foo"] = []byte{1, 2, 3} m.Data["/foo/bar"] = []byte{1, 2, 3} m.Data["/foo/baz/"] = []byte{1, 2, 3} @@ -346,6 +351,11 @@ func TestInMemoryRepositoryListing(t *testing.T) { stringListing = append(stringListing, u.String()) } assert.ElementsMatch(t, []string{"mem:///foo/bar", "mem:///foo/baz/"}, stringListing) + + empty, _ := storage.ParseURI("mem:") // invalid path + canList, err = storage.CanList(empty) + assert.NotNil(t, err) + assert.False(t, canList) } func TestInMemoryRepositoryCreateListable(t *testing.T) {