start adding some tests

This commit is contained in:
IamTheFij 2022-02-23 08:05:24 -08:00
parent 048e062102
commit d00e78174c
4 changed files with 105 additions and 16 deletions

5
go.mod
View File

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

3
go.sum
View File

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

View File

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

85
restic_test.go Normal file
View File

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