fix: test fixes for windows file restore

This commit is contained in:
garethgeorge
2024-08-26 22:28:15 -07:00
parent cc173aa7b1
commit 44585ede61
2 changed files with 27 additions and 14 deletions
+12 -7
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"path"
"runtime"
"slices"
"sort"
"strings"
@@ -305,13 +306,17 @@ func (r *RepoOrchestrator) Restore(ctx context.Context, snapshotId string, snaps
opts = append(opts, restic.WithFlags("--target", target))
if snapshotPath != "" {
dir := path.Dir(snapshotPath)
base := path.Base(snapshotPath)
if dir != "" {
snapshotId = snapshotId + ":" + dir
}
if base != "" {
opts = append(opts, restic.WithFlags("--include", base))
if runtime.GOOS == "windows" {
opts = append(opts, restic.WithFlags("--include", snapshotPath))
} else {
dir := path.Dir(snapshotPath)
base := path.Base(snapshotPath)
if dir != "" {
snapshotId = snapshotId + ":" + dir
}
if base != "" {
opts = append(opts, restic.WithFlags("--include", base))
}
}
}
+15 -7
View File
@@ -5,6 +5,7 @@ import (
"context"
"io/ioutil"
"os"
"path"
"runtime"
"slices"
"strings"
@@ -90,8 +91,8 @@ func TestBackup(t *testing.T) {
func TestRestore(t *testing.T) {
t.Parallel()
testFile := t.TempDir() + "/test.txt"
if err := ioutil.WriteFile(testFile, []byte("test"), 0644); err != nil {
testFile := path.Join(t.TempDir(), "test.txt")
if err := ioutil.WriteFile(testFile, []byte("lorum ipsum"), 0644); err != nil {
t.Fatalf("failed to create test file: %v", err)
}
@@ -124,19 +125,26 @@ func TestRestore(t *testing.T) {
// Restore the file
restoreDir := t.TempDir()
restoreSummary, err := orchestrator.Restore(context.Background(), summary.SnapshotId, testFile, restoreDir, nil)
snapshotPath := strings.ReplaceAll(testFile, ":", "") // remove the colon from the windows path e.g. C:\test.txt -> C\test.txt
restoreSummary, err := orchestrator.Restore(context.Background(), summary.SnapshotId, snapshotPath, restoreDir, nil)
if err != nil {
t.Fatalf("restore error: %v", err)
}
t.Logf("restore summary: %+v", restoreSummary)
if runtime.GOOS == "windows" {
return
}
if restoreSummary.FilesRestored != 1 {
t.Fatalf("expected 1 new file, got %d", restoreSummary.FilesRestored)
t.Errorf("expected 1 new file, got %d", restoreSummary.FilesRestored)
}
if restoreSummary.TotalFiles != 1 {
t.Fatalf("expected 1 total file, got %d", restoreSummary.TotalFiles)
t.Errorf("expected 1 total file, got %d", restoreSummary.TotalFiles)
}
// Check the restored file
restoredFile := restoreDir + "/test.txt"
restoredFile := path.Join(restoreDir, "test.txt")
if _, err := os.Stat(restoredFile); err != nil {
t.Fatalf("failed to stat restored file: %v", err)
}
@@ -144,7 +152,7 @@ func TestRestore(t *testing.T) {
if err != nil {
t.Fatalf("failed to read restored file: %v", err)
}
if string(restoredData) != "test" {
if string(restoredData) != "lorum ipsum" {
t.Fatalf("expected 'test', got '%s'", restoredData)
}
}