From f531ca3b483b26213cdcbd6b471875df28fa63a9 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Tue, 11 Apr 2017 22:04:04 +0200 Subject: [PATCH] layout: Fix corner cases --- src/restic/backend/layout_cloud.go | 6 +++++- src/restic/backend/layout_s3.go | 6 +++++- src/restic/backend/layout_test.go | 26 +++++++++++++++++++------- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/restic/backend/layout_cloud.go b/src/restic/backend/layout_cloud.go index dc79130a2..6331d4709 100644 --- a/src/restic/backend/layout_cloud.go +++ b/src/restic/backend/layout_cloud.go @@ -14,7 +14,11 @@ var cloudLayoutPaths = defaultLayoutPaths // Dirname returns the directory path for a given file type and name. func (l *CloudLayout) Dirname(h restic.Handle) string { - return l.URL + l.Join(l.Path, "/", cloudLayoutPaths[h.Type]) + if h.Type == restic.ConfigFile { + return l.URL + l.Join(l.Path, "/") + } + + return l.URL + l.Join(l.Path, "/", cloudLayoutPaths[h.Type]) + "/" } // Filename returns a path to a file, including its name. diff --git a/src/restic/backend/layout_s3.go b/src/restic/backend/layout_s3.go index d2a034a4c..9db8ae7f3 100644 --- a/src/restic/backend/layout_s3.go +++ b/src/restic/backend/layout_s3.go @@ -20,7 +20,11 @@ var s3LayoutPaths = map[restic.FileType]string{ // Dirname returns the directory path for a given file type and name. func (l *S3Layout) Dirname(h restic.Handle) string { - return l.URL + l.Join(l.Path, "/", s3LayoutPaths[h.Type]) + if h.Type == restic.ConfigFile { + return l.URL + l.Join(l.Path, "/") + } + + return l.URL + l.Join(l.Path, "/", s3LayoutPaths[h.Type]) + "/" } // Filename returns a path to a file, including its name. diff --git a/src/restic/backend/layout_test.go b/src/restic/backend/layout_test.go index 9b270164d..4fee713cb 100644 --- a/src/restic/backend/layout_test.go +++ b/src/restic/backend/layout_test.go @@ -149,47 +149,59 @@ func TestCloudLayout(t *testing.T) { func TestCloudLayoutURLs(t *testing.T) { var tests = []struct { - l Layout - h restic.Handle - fn string + l Layout + h restic.Handle + fn string + dir string }{ { &CloudLayout{URL: "https://hostname.foo", Path: "", Join: path.Join}, restic.Handle{Type: restic.DataFile, Name: "foobar"}, "https://hostname.foo/data/foobar", + "https://hostname.foo/data/", }, { &CloudLayout{URL: "https://hostname.foo:1234/prefix/repo", Path: "/", Join: path.Join}, restic.Handle{Type: restic.LockFile, Name: "foobar"}, "https://hostname.foo:1234/prefix/repo/locks/foobar", + "https://hostname.foo:1234/prefix/repo/locks/", }, { &CloudLayout{URL: "https://hostname.foo:1234/prefix/repo", Path: "/", Join: path.Join}, restic.Handle{Type: restic.ConfigFile, Name: "foobar"}, "https://hostname.foo:1234/prefix/repo/config", + "https://hostname.foo:1234/prefix/repo/", }, { &S3Layout{URL: "https://hostname.foo", Path: "/", Join: path.Join}, restic.Handle{Type: restic.DataFile, Name: "foobar"}, "https://hostname.foo/data/foobar", + "https://hostname.foo/data/", }, { &S3Layout{URL: "https://hostname.foo:1234/prefix/repo", Path: "", Join: path.Join}, restic.Handle{Type: restic.LockFile, Name: "foobar"}, "https://hostname.foo:1234/prefix/repo/lock/foobar", + "https://hostname.foo:1234/prefix/repo/lock/", }, { &S3Layout{URL: "https://hostname.foo:1234/prefix/repo", Path: "/", Join: path.Join}, restic.Handle{Type: restic.ConfigFile, Name: "foobar"}, "https://hostname.foo:1234/prefix/repo/config", + "https://hostname.foo:1234/prefix/repo/", }, } for _, test := range tests { - t.Run("cloud", func(t *testing.T) { - res := test.l.Filename(test.h) - if res != test.fn { - t.Fatalf("wrong filename, want %v, got %v", test.fn, res) + t.Run(fmt.Sprintf("%T", test.l), func(t *testing.T) { + fn := test.l.Filename(test.h) + if fn != test.fn { + t.Fatalf("wrong filename, want %v, got %v", test.fn, fn) + } + + dir := test.l.Dirname(test.h) + if dir != test.dir { + t.Fatalf("wrong dirname, want %v, got %v", test.dir, dir) } }) }