mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2026-04-06 13:49:18 +00:00
ai testing
This commit is contained in:
@@ -91,3 +91,30 @@ func TestBuildRestoreCommand(t *testing.T) {
|
||||
expected := []string{"restore", "--target", "to", "--tag", "ar:location:foo", "snapshot", "options"}
|
||||
assertSliceEqual(t, result, expected)
|
||||
}
|
||||
|
||||
func TestLocationBackupWithMock(t *testing.T) {
|
||||
// Backup original
|
||||
originalExecutor := DefaultExecutor
|
||||
defer func() { DefaultExecutor = originalExecutor }()
|
||||
|
||||
// Inject mock
|
||||
mock := &MockExecutor{
|
||||
ExecuteResticFunc: func(options ExecuteOptions, args ...string) (int, string, error) {
|
||||
assertEqual(t, args[0], "backup")
|
||||
return 0, "success", nil
|
||||
},
|
||||
}
|
||||
DefaultExecutor = mock
|
||||
|
||||
loc := Location{
|
||||
name: "test-location",
|
||||
To: []string{"test-backend"},
|
||||
From: []string{"/"},
|
||||
Type: "local",
|
||||
}
|
||||
|
||||
errs := loc.Backup(false, false, "")
|
||||
if len(errs) != 0 {
|
||||
t.Errorf("expected no error, got %v", errs)
|
||||
}
|
||||
}
|
||||
|
||||
20
internal/mock_executor_test.go
Normal file
20
internal/mock_executor_test.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package internal
|
||||
|
||||
type MockExecutor struct {
|
||||
ExecuteFunc func(options ExecuteOptions, args ...string) (int, string, error)
|
||||
ExecuteResticFunc func(options ExecuteOptions, args ...string) (int, string, error)
|
||||
}
|
||||
|
||||
func (m *MockExecutor) Execute(options ExecuteOptions, args ...string) (int, string, error) {
|
||||
if m.ExecuteFunc != nil {
|
||||
return m.ExecuteFunc(options, args...)
|
||||
}
|
||||
return 0, "", nil
|
||||
}
|
||||
|
||||
func (m *MockExecutor) ExecuteRestic(options ExecuteOptions, args ...string) (int, string, error) {
|
||||
if m.ExecuteResticFunc != nil {
|
||||
return m.ExecuteResticFunc(options, args...)
|
||||
}
|
||||
return 0, "", nil
|
||||
}
|
||||
@@ -39,7 +39,14 @@ func (w ColoredWriter) Write(p []byte) (n int, err error) {
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func ExecuteCommand(options ExecuteOptions, args ...string) (int, string, error) {
|
||||
type Executor interface {
|
||||
Execute(options ExecuteOptions, args ...string) (int, string, error)
|
||||
ExecuteRestic(options ExecuteOptions, args ...string) (int, string, error)
|
||||
}
|
||||
|
||||
type RealExecutor struct{}
|
||||
|
||||
func (e *RealExecutor) Execute(options ExecuteOptions, args ...string) (int, string, error) {
|
||||
cmd := exec.Command(options.Command, args...)
|
||||
env := os.Environ()
|
||||
for k, v := range options.Envs {
|
||||
@@ -76,12 +83,22 @@ func ExecuteCommand(options ExecuteOptions, args ...string) (int, string, error)
|
||||
return 0, out.String(), nil
|
||||
}
|
||||
|
||||
func ExecuteResticCommand(options ExecuteOptions, args ...string) (int, string, error) {
|
||||
func (e *RealExecutor) ExecuteRestic(options ExecuteOptions, args ...string) (int, string, error) {
|
||||
options.Command = flags.RESTIC_BIN
|
||||
var c = GetConfig()
|
||||
var optionsAsString = getOptions(c.Global, []string{"all"})
|
||||
args = append(optionsAsString, args...)
|
||||
return ExecuteCommand(options, args...)
|
||||
return e.Execute(options, args...)
|
||||
}
|
||||
|
||||
var DefaultExecutor Executor = &RealExecutor{}
|
||||
|
||||
func ExecuteCommand(options ExecuteOptions, args ...string) (int, string, error) {
|
||||
return DefaultExecutor.Execute(options, args...)
|
||||
}
|
||||
|
||||
func ExecuteResticCommand(options ExecuteOptions, args ...string) (int, string, error) {
|
||||
return DefaultExecutor.ExecuteRestic(options, args...)
|
||||
}
|
||||
|
||||
func CopyFile(from, to string) error {
|
||||
|
||||
27
internal/utils_test.go
Normal file
27
internal/utils_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExecuteCommandWithMock(t *testing.T) {
|
||||
// Backup original
|
||||
originalExecutor := DefaultExecutor
|
||||
defer func() { DefaultExecutor = originalExecutor }()
|
||||
|
||||
// Inject mock
|
||||
mock := &MockExecutor{
|
||||
ExecuteFunc: func(options ExecuteOptions, args ...string) (int, string, error) {
|
||||
assert.Equal(t, "docker", options.Command)
|
||||
return 0, "mock output", nil
|
||||
},
|
||||
}
|
||||
DefaultExecutor = mock
|
||||
|
||||
code, out, err := ExecuteCommand(ExecuteOptions{Command: "docker"}, "info")
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 0, code)
|
||||
assert.Equal(t, "mock output", out)
|
||||
}
|
||||
Reference in New Issue
Block a user