From d00e78174c23697945d4956e696d171ca86abad9 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Wed, 23 Feb 2022 08:05:24 -0800 Subject: [PATCH] start adding some tests --- go.mod | 5 ++- go.sum | 3 +- restic.go | 28 ++++++++--------- restic_test.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 16 deletions(-) create mode 100644 restic_test.go diff --git a/go.mod b/go.mod index 2142995..7ce04fd 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module git.iamthefij.com/iamthefij/restic-scheduler go 1.17 -require github.com/hashicorp/hcl/v2 v2.11.1 +require ( + github.com/go-test/deep v1.0.8 + github.com/hashicorp/hcl/v2 v2.11.1 +) require ( github.com/agext/levenshtein v1.2.1 // indirect diff --git a/go.sum b/go.sum index 5e58289..cc4e4e8 100644 --- a/go.sum +++ b/go.sum @@ -7,8 +7,9 @@ github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6 github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68= github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= +github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM= +github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= diff --git a/restic.go b/restic.go index 36c3618..717bd8b 100644 --- a/restic.go +++ b/restic.go @@ -42,8 +42,12 @@ func (glo ResticGlobalOpts) ToArgs() (args []string) { args = append(args, "--cache-dir", glo.CacheDir) } - if glo.CleanupCache { - args = append(args, "--cleanup-cache") + if glo.PasswordFile != "" { + args = append(args, "--password-file", glo.PasswordFile) + } + + if glo.TLSClientCertFile != "" { + args = append(args, "--tls-client-cert", glo.TLSClientCertFile) } if glo.LimitDownload > 0 { @@ -54,6 +58,14 @@ func (glo ResticGlobalOpts) ToArgs() (args []string) { args = append(args, "--limit-upload", fmt.Sprint(glo.LimitUpload)) } + if glo.VerboseLevel > 0 { + args = append(args, "--verbose", fmt.Sprint(glo.VerboseLevel)) + } + + if glo.CleanupCache { + args = append(args, "--cleanup-cache") + } + if glo.NoCache { args = append(args, "--no-cache") } @@ -62,18 +74,6 @@ func (glo ResticGlobalOpts) ToArgs() (args []string) { args = append(args, "--no-lock") } - if glo.PasswordFile != "" { - args = append(args, "--password-file", glo.PasswordFile) - } - - if glo.TLSClientCertFile != "" { - args = append(args, "--tls-client-cert", glo.TLSClientCertFile) - } - - if glo.VerboseLevel > 0 { - args = append(args, "--verbose", fmt.Sprint(glo.VerboseLevel)) - } - return args } diff --git a/restic_test.go b/restic_test.go new file mode 100644 index 0000000..13f251a --- /dev/null +++ b/restic_test.go @@ -0,0 +1,85 @@ +package main_test + +import ( + "os" + "testing" + + main "git.iamthefij.com/iamthefij/restic-scheduler" + "github.com/go-test/deep" +) + +func TestGlobalOptions(t *testing.T) { + t.Parallel() + + args := main.ResticGlobalOpts{ + CaCertFile: "file", + CacheDir: "directory", + PasswordFile: "file", + TLSClientCertFile: "file", + LimitDownload: 1, + LimitUpload: 1, + VerboseLevel: 1, + CleanupCache: true, + NoCache: true, + NoLock: true, + }.ToArgs() + + expected := []string{ + "--cacert", "file", + "--cache-dir", "directory", + "--password-file", "file", + "--tls-client-cert", "file", + "--limit-download", "1", + "--limit-upload", "1", + "--verbose", "1", + "--cleanup-cache", + "--no-cache", + "--no-lock", + } + + if diff := deep.Equal(args, expected); diff != nil { + t.Errorf("args didn't match %v", diff) + } +} + +func TestBuildEnv(t *testing.T) { + t.Parallel() + + cases := []struct { + name string + cmd main.ResticCmd + expected []string + }{ + { + name: "No Env", + cmd: main.ResticCmd{}, // nolint:exhaustivestruct + expected: os.Environ(), + }, + { + name: "SetEnv", + cmd: main.ResticCmd{ // nolint:exhaustivestruct + Env: map[string]string{"TestKey": "Value"}, + }, + expected: append(os.Environ(), "TestKey=Value"), + }, + { + name: "SetEnv", + cmd: main.ResticCmd{ // nolint:exhaustivestruct + Passphrase: "Shhhhhhhh!!", + }, + expected: append(os.Environ(), "RESTIC_PASSWORD=Shhhhhhhh!!"), + }, + } + + for _, c := range cases { + c := c + + t.Run(c.name, func(t *testing.T) { + t.Parallel() + + if diff := deep.Equal(c.expected, c.cmd.BuildEnv()); diff != nil { + t.Error(diff) + } + }) + } +}