2023-06-08 11:04:34 +00:00
|
|
|
package location_test
|
2015-12-28 14:51:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-06-08 11:04:34 +00:00
|
|
|
"github.com/restic/restic/internal/backend/location"
|
2023-06-08 13:05:07 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
"github.com/restic/restic/internal/test"
|
2015-12-28 14:51:24 +00:00
|
|
|
)
|
|
|
|
|
2023-06-08 13:05:07 +00:00
|
|
|
type testConfig struct {
|
|
|
|
loc string
|
|
|
|
}
|
2016-02-21 14:24:37 +00:00
|
|
|
|
2023-06-08 13:05:07 +00:00
|
|
|
func testFactory() location.Factory {
|
|
|
|
return location.NewHTTPBackendFactory[testConfig, restic.Backend](
|
|
|
|
func(s string) (*testConfig, error) {
|
|
|
|
return &testConfig{loc: s}, nil
|
|
|
|
}, nil, nil, nil,
|
|
|
|
)
|
2016-02-21 14:24:37 +00:00
|
|
|
}
|
|
|
|
|
2023-06-08 13:05:07 +00:00
|
|
|
func TestParse(t *testing.T) {
|
|
|
|
registry := location.NewRegistry()
|
|
|
|
registry.Register("test", testFactory())
|
|
|
|
|
|
|
|
path := "test:example"
|
|
|
|
u, err := location.Parse(registry, path)
|
|
|
|
test.OK(t, err)
|
|
|
|
test.Equals(t, "test", u.Scheme)
|
|
|
|
test.Equals(t, &testConfig{loc: path}, u.Config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseFallback(t *testing.T) {
|
|
|
|
fallbackTests := []string{
|
2017-03-25 12:27:14 +00:00
|
|
|
"dir1/dir2",
|
|
|
|
"/dir1/dir2",
|
2017-06-30 18:40:27 +00:00
|
|
|
"/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`,
|
2023-06-08 13:05:07 +00:00
|
|
|
}
|
2015-12-28 14:51:24 +00:00
|
|
|
|
2023-06-08 13:05:07 +00:00
|
|
|
registry := location.NewRegistry()
|
|
|
|
registry.Register("local", testFactory())
|
2015-12-28 14:51:24 +00:00
|
|
|
|
2023-06-08 13:05:07 +00:00
|
|
|
for _, path := range fallbackTests {
|
|
|
|
t.Run(path, func(t *testing.T) {
|
|
|
|
u, err := location.Parse(registry, path)
|
2017-03-25 12:27:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2023-06-08 13:05:07 +00:00
|
|
|
test.Equals(t, "local", u.Scheme)
|
|
|
|
test.Equals(t, "local:"+path, u.Config.(*testConfig).loc)
|
2017-03-25 12:27:14 +00:00
|
|
|
})
|
2015-12-28 14:51:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-30 18:40:27 +00:00
|
|
|
|
|
|
|
func TestInvalidScheme(t *testing.T) {
|
2023-06-08 13:05:07 +00:00
|
|
|
registry := location.NewRegistry()
|
2017-06-30 18:40:27 +00:00
|
|
|
var invalidSchemes = []string{
|
|
|
|
"foobar:xxx",
|
|
|
|
"foobar:/dir/dir2",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range invalidSchemes {
|
|
|
|
t.Run(s, func(t *testing.T) {
|
2023-06-08 13:05:07 +00:00
|
|
|
_, err := location.Parse(registry, s)
|
2017-06-30 18:40:27 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("error for invalid location %q not found", s)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|