-
Notifications
You must be signed in to change notification settings - Fork 34
/
run_profile_test.go
101 lines (90 loc) · 2.44 KB
/
run_profile_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"bytes"
"errors"
"testing"
"github.com/creativeprojects/resticprofile/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestStartProfileOrGroup(t *testing.T) {
// Sample configuration
configContent := `version = "2"
[profiles.default]
repository = "test-repo"
[profiles.profile1]
inherit = "default"
[profiles.profile2]
inherit = "default"
[groups.group_undefined]
profiles = ["profile1", "profile2"]
[groups.group_true]
profiles = ["profile1", "profile2"]
continue-on-error = true
[groups.group_false]
profiles = ["profile1", "profile2"]
continue-on-error = false
`
// Load configuration
cfg, err := config.Load(bytes.NewBufferString(configContent), config.FormatTOML)
require.NoError(t, err)
// Mock context
ctx := &Context{
config: cfg,
global: &config.Global{},
request: Request{
profile: "profile1",
},
}
t.Run("ProfileNotFound", func(t *testing.T) {
ctx.request.profile = "unknown"
err := startProfileOrGroup(ctx, nil)
assert.Error(t, err)
assert.ErrorIs(t, err, ErrProfileNotFound)
})
t.Run("ProfileExists", func(t *testing.T) {
ctx.request.profile = "profile1"
err := startProfileOrGroup(ctx, func(ctx *Context) error {
return nil
})
assert.NoError(t, err)
})
t.Run("ProfileGroupExists", func(t *testing.T) {
ctx.request.profile = "group_undefined"
err := startProfileOrGroup(ctx, func(ctx *Context) error {
return nil
})
assert.NoError(t, err)
})
t.Run("ProfileGroupGlobalContinueOnErrorTrue", func(t *testing.T) {
calls := 0
ctx.request.profile = "group_undefined"
ctx.global.GroupContinueOnError = true
err := startProfileOrGroup(ctx, func(ctx *Context) error {
calls++
return errors.New("error")
})
assert.NoError(t, err)
assert.Equal(t, 2, calls)
})
t.Run("ProfileGroupContinueOnErrorTrue", func(t *testing.T) {
calls := 0
ctx.request.profile = "group_true"
err := startProfileOrGroup(ctx, func(ctx *Context) error {
calls++
return errors.New("error")
})
assert.NoError(t, err)
assert.Equal(t, 2, calls)
})
t.Run("ProfileGroupContinueOnErrorFalse", func(t *testing.T) {
calls := 0
ctx.request.profile = "group_false"
err := startProfileOrGroup(ctx, func(ctx *Context) error {
calls++
return errors.New("error")
})
assert.Error(t, err)
assert.Equal(t, 1, calls)
})
}