restic: RunPrune + runWithPump helper, refactor Forget/Init onto it

Add RunPrune for admin-credential prune invocations.  Extract
runWithPump to DRY the stdout+stderr pump pattern; refactor RunForget
and RunInit to delegate to it (RunInit preserves the "config file
already exists" soft-success sniff by wrapping the handler before the
call).  Add runner_test.go with TestRunPruneInvokesPrune.
This commit is contained in:
2026-05-03 22:20:48 +01:00
parent 11cbc2fb7f
commit 9b790bbade
2 changed files with 104 additions and 49 deletions
+53
View File
@@ -0,0 +1,53 @@
package restic
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
// setupScriptBin writes a small shell script to a temp directory,
// makes it executable, and returns its path. scriptBody is the
// complete script content (without the shebang line — that's added
// automatically).
func setupScriptBin(t *testing.T, scriptBody string) string {
t.Helper()
dir := t.TempDir()
p := filepath.Join(dir, "restic")
content := "#!/bin/sh\n" + scriptBody + "\n"
if err := os.WriteFile(p, []byte(content), 0o755); err != nil {
t.Fatalf("setupScriptBin: %v", err)
}
return p
}
// captureLines returns a LineHandler that appends "stream:line" into
// the returned slice pointer (safe for single-goroutine test use).
func captureLines() (*[]string, LineHandler) {
var lines []string
h := func(stream, line string, _ any) {
lines = append(lines, fmt.Sprintf("%s:%s", stream, line))
}
return &lines, h
}
// --- B1: RunPrune ---
func TestRunPruneInvokesPrune(t *testing.T) {
// Shell script that echoes its args; "prune" should appear in output.
bin := setupScriptBin(t, `echo "$@"`)
env := Env{Bin: bin}
lines, h := captureLines()
if err := env.RunPrune(context.Background(), h); err != nil {
t.Fatalf("RunPrune returned error: %v", err)
}
for _, l := range *lines {
if strings.Contains(l, "prune") {
return
}
}
t.Fatalf("expected 'prune' in captured output; got: %v", *lines)
}