| 1 | package desktoplauncher |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "path/filepath" |
| 6 | "reflect" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestShortcutRepairUsesLauncherRootAndExactInstallerPaths(t *testing.T) { |
| 11 | root := t.TempDir() |
| 12 | paths := []string{filepath.Join(t.TempDir(), "Reasonix.lnk"), filepath.Join(t.TempDir(), "Reasonix.LNK")} |
| 13 | called := false |
| 14 | err := repairInstallerShortcuts(paths, func() (string, error) { return root, nil }, func(gotRoot string, gotPaths []string) error { |
| 15 | called = true |
| 16 | if gotRoot != root || !reflect.DeepEqual(gotPaths, paths) { |
| 17 | t.Fatalf("repair = %q %v, want %q %v", gotRoot, gotPaths, root, paths) |
| 18 | } |
| 19 | return nil |
| 20 | }) |
| 21 | if err != nil || !called { |
| 22 | t.Fatalf("repair = %v, called=%t", err, called) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | func TestShortcutRepairRejectsInvalidArgumentsBeforeAnyWrite(t *testing.T) { |
| 27 | root := t.TempDir() |
| 28 | for _, paths := range [][]string{nil, {"relative.lnk"}, {filepath.Join(root, "Reasonix.exe")}, {filepath.Join(root, "good.lnk"), "bad.lnk"}} { |
| 29 | err := repairInstallerShortcuts(paths, func() (string, error) { return root, nil }, func(string, []string) error { |
| 30 | t.Fatal("invalid maintenance arguments reached the repair writer") |
| 31 | return nil |
| 32 | }) |
| 33 | if err == nil { |
| 34 | t.Fatalf("accepted invalid paths %v", paths) |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestShortcutRepairDoesNotFallThroughToDesktopLaunch(t *testing.T) { |
| 40 | if code := Run([]string{"--repair-shortcuts"}, "test"); code != 1 { |
| 41 | t.Fatalf("empty maintenance command exit = %d, want 1", code) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestShortcutRepairPropagatesErrors(t *testing.T) { |
| 46 | want := errors.New("access denied") |
| 47 | path := filepath.Join(t.TempDir(), "Reasonix.lnk") |
| 48 | err := repairInstallerShortcuts([]string{path}, func() (string, error) { return "", want }, func(string, []string) error { |
| 49 | t.Fatal("root failure reached writer") |
| 50 | return nil |
| 51 | }) |
| 52 | if !errors.Is(err, want) { |
| 53 | t.Fatalf("root error = %v", err) |
| 54 | } |
| 55 | err = repairInstallerShortcuts([]string{path}, func() (string, error) { return t.TempDir(), nil }, func(string, []string) error { return want }) |
| 56 | if !errors.Is(err, want) { |
| 57 | t.Fatalf("repair error = %v", err) |
| 58 | } |
| 59 | } |
| 60 |