2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-11 05:42:29 +00:00
restic/internal/backend/location/location_test.go

77 lines
1.7 KiB
Go
Raw Normal View History

package location_test
2015-12-28 14:51:24 +00:00
import (
"testing"
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/backend/location"
"github.com/restic/restic/internal/test"
2015-12-28 14:51:24 +00:00
)
type testConfig struct {
loc string
}
2016-02-21 14:24:37 +00:00
func testFactory() location.Factory {
return location.NewHTTPBackendFactory[testConfig, backend.Backend](
"local",
func(s string) (*testConfig, error) {
return &testConfig{loc: s}, nil
}, nil, nil, nil,
)
2016-02-21 14:24:37 +00:00
}
func TestParse(t *testing.T) {
registry := location.NewRegistry()
registry.Register(testFactory())
path := "local:example"
u, err := location.Parse(registry, path)
test.OK(t, err)
test.Equals(t, "local", u.Scheme)
test.Equals(t, &testConfig{loc: path}, u.Config)
}
func TestParseFallback(t *testing.T) {
fallbackTests := []string{
"dir1/dir2",
"/dir1/dir2",
"/dir1:foobar/dir2",
`\dir1\foobar\dir2`,
`c:\dir1\foobar\dir2`,
`C:\Users\appveyor\AppData\Local\Temp\1\restic-test-879453535\repo`,
`c:/dir1/foobar/dir2`,
}
2015-12-28 14:51:24 +00:00
registry := location.NewRegistry()
registry.Register(testFactory())
2015-12-28 14:51:24 +00:00
for _, path := range fallbackTests {
t.Run(path, func(t *testing.T) {
u, err := location.Parse(registry, path)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
test.Equals(t, "local", u.Scheme)
test.Equals(t, "local:"+path, u.Config.(*testConfig).loc)
})
2015-12-28 14:51:24 +00:00
}
}
func TestInvalidScheme(t *testing.T) {
registry := location.NewRegistry()
var invalidSchemes = []string{
"foobar:xxx",
"foobar:/dir/dir2",
}
for _, s := range invalidSchemes {
t.Run(s, func(t *testing.T) {
_, err := location.Parse(registry, s)
if err == nil {
t.Fatalf("error for invalid location %q not found", s)
}
})
}
}