noninteractive_errors_test.go

  1package cmd
  2
  3import (
  4	"os"
  5	"path/filepath"
  6	"strings"
  7	"testing"
  8)
  9
 10func TestBackupNonInteractiveMissingPaths(t *testing.T) {
 11	// Not parallel: mutates package-level isStdinTerminal.
 12
 13	original := isStdinTerminal
 14	t.Cleanup(func() {
 15		isStdinTerminal = original
 16	})
 17	isStdinTerminal = func() bool { return false }
 18
 19	// Config with a valid preset but no backup paths.
 20	dir := t.TempDir()
 21	cfg := filepath.Join(dir, "config.toml")
 22	err := os.WriteFile(cfg, []byte(`
 23[global]
 24repo = "/repos/default"
 25
 26["home@"]
 27tag = "home"
 28
 29["@cloud"]
 30repo = "/repos/cloud"
 31`), 0o600)
 32	if err != nil {
 33		t.Fatalf("writing fixture: %v", err)
 34	}
 35	t.Setenv("HOME", dir)
 36
 37	setRootFlagValuesForTest(t, "home@cloud", true, cfg)
 38	t.Setenv("KELD_CONFIG_FILE", "")
 39	t.Setenv("KELD_DRYRUN", "")
 40
 41	backup := lookupSubcommand(t, "backup")
 42	_, err = captureStdout(t, func() error {
 43		return backup.RunE(backup, []string{})
 44	})
 45
 46	if err == nil {
 47		t.Fatal("expected error for backup with no paths, got nil")
 48	}
 49
 50	errMsg := err.Error()
 51	// Should mention paths specifically.
 52	if !strings.Contains(errMsg, "path") {
 53		t.Fatalf("expected error to mention paths, got: %v", err)
 54	}
 55}
 56
 57func TestBackupNonInteractiveMultiplePresetsNoSelection(t *testing.T) {
 58	// Not parallel: mutates package-level isStdinTerminal.
 59
 60	original := isStdinTerminal
 61	t.Cleanup(func() {
 62		isStdinTerminal = original
 63	})
 64	isStdinTerminal = func() bool { return false }
 65
 66	// Config with multiple presets (home@cloud, work@cloud).
 67	dir := t.TempDir()
 68	cfg := filepath.Join(dir, "config.toml")
 69	err := os.WriteFile(cfg, []byte(`
 70[global]
 71repo = "/repos/default"
 72
 73["home@"]
 74tag = "home"
 75
 76["home@".backup]
 77_arguments = "/home"
 78
 79["work@"]
 80tag = "work"
 81
 82["work@".backup]
 83_arguments = "/work"
 84
 85["@cloud"]
 86repo = "/repos/cloud"
 87`), 0o600)
 88	if err != nil {
 89		t.Fatalf("writing fixture: %v", err)
 90	}
 91	t.Setenv("HOME", dir)
 92
 93	// No --preset specified.
 94	setRootFlagValuesForTest(t, "", true, cfg)
 95	t.Setenv("KELD_CONFIG_FILE", "")
 96	t.Setenv("KELD_DRYRUN", "")
 97
 98	backup := lookupSubcommand(t, "backup")
 99	_, err = captureStdout(t, func() error {
100		return backup.RunE(backup, []string{})
101	})
102
103	if err == nil {
104		t.Fatal("expected error for multiple presets with no selection, got nil")
105	}
106
107	errMsg := err.Error()
108	// Should mention multiple presets.
109	if !strings.Contains(errMsg, "preset") {
110		t.Fatalf("expected error to mention presets, got: %v", err)
111	}
112}