backend: use generic implementation for ParseConfig tests

This commit is contained in:
Michael Eischer 2023-04-20 23:02:56 +02:00
parent 5260d38980
commit fa361dbfbd
9 changed files with 88 additions and 157 deletions

View File

@ -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)
}

View File

@ -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 {

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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) {

View File

@ -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{

View File

@ -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{

View File

@ -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)
}
})
}
}