From fa361dbfbdc2cd29b21c1a6cff07e28d7c51368a Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Thu, 20 Apr 2023 23:02:56 +0200 Subject: [PATCH] backend: use generic implementation for ParseConfig tests --- internal/backend/azure/config_test.go | 25 ++++---------- internal/backend/b2/config_test.go | 25 ++++---------- internal/backend/gs/config_test.go | 25 ++++---------- internal/backend/rclone/config_test.go | 45 +++++++++----------------- internal/backend/rest/config_test.go | 30 +++++------------ internal/backend/s3/config_test.go | 21 +++--------- internal/backend/sftp/config_test.go | 21 +++--------- internal/backend/swift/config_test.go | 25 ++++---------- internal/backend/test/config.go | 28 ++++++++++++++++ 9 files changed, 88 insertions(+), 157 deletions(-) create mode 100644 internal/backend/test/config.go diff --git a/internal/backend/azure/config_test.go b/internal/backend/azure/config_test.go index a57542e77..0d76c98ee 100644 --- a/internal/backend/azure/config_test.go +++ b/internal/backend/azure/config_test.go @@ -1,11 +1,12 @@ package azure -import "testing" +import ( + "testing" -var configTests = []struct { - s string - cfg Config -}{ + "github.com/restic/restic/internal/backend/test" +) + +var configTests = []test.ConfigTestData[Config]{ {"azure:container-name:/", Config{ Container: "container-name", Prefix: "", @@ -24,17 +25,5 @@ var configTests = []struct { } func TestParseConfig(t *testing.T) { - for i, test := range configTests { - cfg, err := ParseConfig(test.s) - if err != nil { - t.Errorf("test %d:%s failed: %v", i, test.s, err) - continue - } - - if cfg != test.cfg { - t.Errorf("test %d:\ninput:\n %s\n wrong config, want:\n %v\ngot:\n %v", - i, test.s, test.cfg, cfg) - continue - } - } + test.ParseConfigTester(t, ParseConfig, configTests) } diff --git a/internal/backend/b2/config_test.go b/internal/backend/b2/config_test.go index 4194cb62c..157dfc085 100644 --- a/internal/backend/b2/config_test.go +++ b/internal/backend/b2/config_test.go @@ -1,11 +1,12 @@ package b2 -import "testing" +import ( + "testing" -var configTests = []struct { - s string - cfg Config -}{ + "github.com/restic/restic/internal/backend/test" +) + +var configTests = []test.ConfigTestData[Config]{ {"b2:bucketname", Config{ Bucket: "bucketname", Prefix: "", @@ -39,19 +40,7 @@ var configTests = []struct { } func TestParseConfig(t *testing.T) { - for _, test := range configTests { - t.Run("", func(t *testing.T) { - cfg, err := ParseConfig(test.s) - if err != nil { - t.Fatalf("%s failed: %v", test.s, err) - } - - if cfg != test.cfg { - t.Fatalf("input: %s\n wrong config, want:\n %#v\ngot:\n %#v", - test.s, test.cfg, cfg) - } - }) - } + test.ParseConfigTester(t, ParseConfig, configTests) } var invalidConfigTests = []struct { diff --git a/internal/backend/gs/config_test.go b/internal/backend/gs/config_test.go index fb2774d25..927be5788 100644 --- a/internal/backend/gs/config_test.go +++ b/internal/backend/gs/config_test.go @@ -1,11 +1,12 @@ package gs -import "testing" +import ( + "testing" -var configTests = []struct { - s string - cfg Config -}{ + "github.com/restic/restic/internal/backend/test" +) + +var configTests = []test.ConfigTestData[Config]{ {"gs:bucketname:/", Config{ Bucket: "bucketname", Prefix: "", @@ -27,17 +28,5 @@ var configTests = []struct { } func TestParseConfig(t *testing.T) { - for i, test := range configTests { - cfg, err := ParseConfig(test.s) - if err != nil { - t.Errorf("test %d:%s failed: %v", i, test.s, err) - continue - } - - if cfg != test.cfg { - t.Errorf("test %d:\ninput:\n %s\n wrong config, want:\n %v\ngot:\n %v", - i, test.s, test.cfg, cfg) - continue - } - } + test.ParseConfigTester(t, ParseConfig, configTests) } diff --git a/internal/backend/rclone/config_test.go b/internal/backend/rclone/config_test.go index 923555136..ada9df293 100644 --- a/internal/backend/rclone/config_test.go +++ b/internal/backend/rclone/config_test.go @@ -1,37 +1,24 @@ package rclone import ( - "reflect" "testing" + + "github.com/restic/restic/internal/backend/test" ) -func TestParseConfig(t *testing.T) { - var tests = []struct { - s string - cfg Config - }{ - { - "rclone:local:foo:/bar", - Config{ - Remote: "local:foo:/bar", - Program: defaultConfig.Program, - Args: defaultConfig.Args, - Connections: defaultConfig.Connections, - Timeout: defaultConfig.Timeout, - }, +var configTests = []test.ConfigTestData[Config]{ + { + "rclone:local:foo:/bar", + Config{ + Remote: "local:foo:/bar", + Program: defaultConfig.Program, + Args: defaultConfig.Args, + Connections: defaultConfig.Connections, + Timeout: defaultConfig.Timeout, }, - } - - for _, test := range tests { - t.Run("", func(t *testing.T) { - cfg, err := ParseConfig(test.s) - if err != nil { - t.Fatal(err) - } - - if !reflect.DeepEqual(cfg, test.cfg) { - t.Fatalf("wrong config, want:\n %v\ngot:\n %v", test.cfg, cfg) - } - }) - } + }, +} + +func TestParseConfig(t *testing.T) { + test.ParseConfigTester(t, ParseConfig, configTests) } diff --git a/internal/backend/rest/config_test.go b/internal/backend/rest/config_test.go index 2d8e32a73..8cfc78407 100644 --- a/internal/backend/rest/config_test.go +++ b/internal/backend/rest/config_test.go @@ -2,8 +2,9 @@ package rest import ( "net/url" - "reflect" "testing" + + "github.com/restic/restic/internal/backend/test" ) func parseURL(s string) *url.URL { @@ -15,20 +16,17 @@ func parseURL(s string) *url.URL { return u } -var configTests = []struct { - s string - cfg Config -}{ +var configTests = []test.ConfigTestData[Config]{ { - s: "rest:http://localhost:1234", - cfg: Config{ + S: "rest:http://localhost:1234", + Cfg: Config{ URL: parseURL("http://localhost:1234/"), Connections: 5, }, }, { - s: "rest:http://localhost:1234/", - cfg: Config{ + S: "rest:http://localhost:1234/", + Cfg: Config{ URL: parseURL("http://localhost:1234/"), Connections: 5, }, @@ -36,17 +34,5 @@ var configTests = []struct { } func TestParseConfig(t *testing.T) { - for _, test := range configTests { - t.Run("", func(t *testing.T) { - cfg, err := ParseConfig(test.s) - if err != nil { - t.Fatalf("%s failed: %v", test.s, err) - } - - if !reflect.DeepEqual(cfg, test.cfg) { - t.Fatalf("\ninput: %s\n wrong config, want:\n %v\ngot:\n %v", - test.s, test.cfg, cfg) - } - }) - } + test.ParseConfigTester(t, ParseConfig, configTests) } diff --git a/internal/backend/s3/config_test.go b/internal/backend/s3/config_test.go index 821fbc244..24315d159 100644 --- a/internal/backend/s3/config_test.go +++ b/internal/backend/s3/config_test.go @@ -3,12 +3,11 @@ package s3 import ( "strings" "testing" + + "github.com/restic/restic/internal/backend/test" ) -var configTests = []struct { - s string - cfg Config -}{ +var configTests = []test.ConfigTestData[Config]{ {"s3://eu-central-1/bucketname", Config{ Endpoint: "eu-central-1", Bucket: "bucketname", @@ -100,19 +99,7 @@ var configTests = []struct { } func TestParseConfig(t *testing.T) { - for i, test := range configTests { - cfg, err := ParseConfig(test.s) - if err != nil { - t.Errorf("test %d:%s failed: %v", i, test.s, err) - continue - } - - if cfg != test.cfg { - t.Errorf("test %d:\ninput:\n %s\n wrong config, want:\n %v\ngot:\n %v", - i, test.s, test.cfg, cfg) - continue - } - } + test.ParseConfigTester(t, ParseConfig, configTests) } func TestParseError(t *testing.T) { diff --git a/internal/backend/sftp/config_test.go b/internal/backend/sftp/config_test.go index 3772c038b..ae2494791 100644 --- a/internal/backend/sftp/config_test.go +++ b/internal/backend/sftp/config_test.go @@ -2,12 +2,11 @@ package sftp import ( "testing" + + "github.com/restic/restic/internal/backend/test" ) -var configTests = []struct { - in string - cfg Config -}{ +var configTests = []test.ConfigTestData[Config]{ // first form, user specified sftp://user@host/dir { "sftp://user@host/dir/subdir", @@ -77,19 +76,7 @@ var configTests = []struct { } func TestParseConfig(t *testing.T) { - for i, test := range configTests { - cfg, err := ParseConfig(test.in) - if err != nil { - t.Errorf("test %d:%s failed: %v", i, test.in, err) - continue - } - - if cfg != test.cfg { - t.Errorf("test %d:\ninput:\n %s\n wrong config, want:\n %v\ngot:\n %v", - i, test.in, test.cfg, cfg) - continue - } - } + test.ParseConfigTester(t, ParseConfig, configTests) } var configTestsInvalid = []string{ diff --git a/internal/backend/swift/config_test.go b/internal/backend/swift/config_test.go index 57002a924..57551efa5 100644 --- a/internal/backend/swift/config_test.go +++ b/internal/backend/swift/config_test.go @@ -1,11 +1,12 @@ package swift -import "testing" +import ( + "testing" -var configTests = []struct { - s string - cfg Config -}{ + "github.com/restic/restic/internal/backend/test" +) + +var configTests = []test.ConfigTestData[Config]{ { "swift:cnt1:/", Config{ @@ -31,19 +32,7 @@ var configTests = []struct { } func TestParseConfig(t *testing.T) { - for _, test := range configTests { - t.Run("", func(t *testing.T) { - cfg, err := ParseConfig(test.s) - if err != nil { - t.Fatalf("parsing %q failed: %v", test.s, err) - } - - if cfg != test.cfg { - t.Fatalf("wrong output for %q, want:\n %#v\ngot:\n %#v", - test.s, test.cfg, cfg) - } - }) - } + test.ParseConfigTester(t, ParseConfig, configTests) } var configTestsInvalid = []string{ diff --git a/internal/backend/test/config.go b/internal/backend/test/config.go new file mode 100644 index 000000000..77172dc86 --- /dev/null +++ b/internal/backend/test/config.go @@ -0,0 +1,28 @@ +package test + +import ( + "fmt" + "reflect" + "testing" +) + +type ConfigTestData[C comparable] struct { + S string + Cfg C +} + +func ParseConfigTester[C comparable](t *testing.T, parser func(s string) (C, error), tests []ConfigTestData[C]) { + for i, test := range tests { + t.Run(fmt.Sprint(i), func(t *testing.T) { + cfg, err := parser(test.S) + if err != nil { + t.Fatalf("%s failed: %v", test.S, err) + } + + if !reflect.DeepEqual(cfg, test.Cfg) { + t.Fatalf("input: %s\n wrong config, want:\n %#v\ngot:\n %#v", + test.S, test.Cfg, cfg) + } + }) + } +}