Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
creativeprojects committed Nov 5, 2023
1 parent a3f4919 commit 1157e07
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
13 changes: 13 additions & 0 deletions commands_display_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"fmt"
"runtime"
"strings"
"testing"

Expand Down Expand Up @@ -295,3 +296,15 @@ https://creativeprojects.github.io/resticprofile/
}
}
}

func TestDisplayVersionVerbose1(t *testing.T) {
buffer := &bytes.Buffer{}
displayVersion(buffer, commandContext{Context: Context{flags: commandLineFlags{verbose: true}}})
assert.True(t, strings.Contains(buffer.String(), runtime.GOOS))
}

func TestDisplayVersionVerbose2(t *testing.T) {
buffer := &bytes.Buffer{}
displayVersion(buffer, commandContext{Context: Context{request: Request{arguments: []string{"-v"}}}})
assert.True(t, strings.Contains(buffer.String(), runtime.GOOS))
}
7 changes: 7 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ type Context struct {
logTarget string // where to send the log output
}

// WithConfig sets the configuration and global values. It doesn't create a new context.
func (c *Context) WithConfig(cfg *config.Config, global *config.Global) *Context {
c.config = cfg
c.global = global
return c
}

// WithBinary sets the restic binary to use. It doesn't create a new context.
func (c *Context) WithBinary(resticBinary string) *Context {
c.binary = resticBinary
Expand Down
10 changes: 10 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import (
"github.com/stretchr/testify/assert"
)

func TestContextWithConfig(t *testing.T) {
ctx := &Context{
config: nil,
global: nil,
}
ctx = ctx.WithConfig(&config.Config{}, &config.Global{})
assert.NotNil(t, ctx.config)
assert.NotNil(t, ctx.global)
}

func TestContextWithBinary(t *testing.T) {
ctx := &Context{
binary: "test",
Expand Down
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,9 @@ func main() {
},
}
// try to load the config and setup logging for own command
configuration, global, err := loadConfig(flags, true)
cfg, global, err := loadConfig(flags, true)
if err == nil {
ctx.config = configuration
ctx.global = global
ctx = ctx.WithConfig(cfg, global)
}
closeLogger := setupLogging(ctx)
defer closeLogger()
Expand All @@ -167,7 +166,7 @@ func main() {
}
}

// Load the mandatory configuration and setup logging (before returning on error)
// Load the now mandatory configuration and setup logging (before returning an error)
ctx, err := loadContext(flags, false)
closeLogger := setupLogging(ctx)
defer closeLogger()
Expand Down

0 comments on commit 1157e07

Please sign in to comment.