| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/sylvinus/agent-vm/internal/paths" |
| 10 | "github.com/sylvinus/agent-vm/internal/vm/vmtest" |
| 11 | ) |
| 12 | |
| 13 | func TestInstall(t *testing.T) { |
| 14 | home := t.TempDir() |
| 15 | t.Setenv("HOME", home) |
| 16 | |
| 17 | t.Setenv("USERPROFILE", home) |
| 18 | bin := filepath.Join(t.TempDir(), "bin") |
| 19 | t.Setenv("AGENT_VM_BIN_DIR", bin) |
| 20 | te := newTestEnv(t, vmtest.New()) |
| 21 | exe, _ := self() |
| 22 | link, err := binLink() |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | |
| 27 | if te.run("install") != 0 || !strings.Contains(te.out(), "Linked "+link+" -> "+exe) || !strings.Contains(te.out(), "is not on your PATH") { |
| 28 | t.Fatalf("install: %s", te.out()) |
| 29 | } |
| 30 | if !strings.Contains(te.out(), "agent-vm setup # build the base VM, once") { |
| 31 | t.Errorf("next step: %s", te.out()) |
| 32 | } |
| 33 | if t2, _ := os.Readlink(link); t2 != exe { |
| 34 | t.Errorf("link: %q", t2) |
| 35 | } |
| 36 | if te.run("install") != 0 || !strings.Contains(te.out(), "already linked") { |
| 37 | t.Errorf("again: %s", te.out()) |
| 38 | } |
| 39 | |
| 40 | |
| 41 | old := paths.Windows |
| 42 | paths.Windows = func() bool { return true } |
| 43 | if te.run("install"); !strings.Contains(te.out(), "Add it from PowerShell") || !strings.Contains(te.out(), "SetEnvironmentVariable('Path', '"+bin+";'") { |
| 44 | t.Errorf("Windows hint: %s", te.out()) |
| 45 | } |
| 46 | t.Setenv("PATH", strings.ToUpper(bin)+"/"+string(os.PathListSeparator)+os.Getenv("PATH")) |
| 47 | if te.run("install"); strings.Contains(te.out(), "not on your PATH") { |
| 48 | t.Errorf("on PATH, as Windows compares it: %s", te.out()) |
| 49 | } |
| 50 | paths.Windows = old |
| 51 | if te.run("install", "x") != 2 { |
| 52 | t.Error("an argument") |
| 53 | } |
| 54 | os.WriteFile(filepath.Join(home, ".zshrc"), []byte("source ~/agent-vm/agent-vm.sh\n"), 0o644) |
| 55 | if te.run("uninstall") != 0 || !strings.Contains(te.out(), "Removed "+link) || !strings.Contains(te.out(), "still sources agent-vm.sh") { |
| 56 | t.Errorf("uninstall: %s", te.out()) |
| 57 | } |
| 58 | if _, err := os.Lstat(link); err == nil { |
| 59 | t.Error("not removed") |
| 60 | } |
| 61 | |
| 62 | os.WriteFile(link, []byte("#!/bin/sh\n"), 0o755) |
| 63 | if te.run("install") != 1 || !strings.Contains(te.out(), "already exists and is not a link to") { |
| 64 | t.Errorf("foreign file, install: %s", te.out()) |
| 65 | } |
| 66 | if te.run("uninstall") != 0 || !strings.Contains(te.out(), "is not a link to this agent-vm: left alone.") { |
| 67 | t.Errorf("foreign file, uninstall: %s", te.out()) |
| 68 | } |
| 69 | if b, _ := os.ReadFile(link); string(b) != "#!/bin/sh\n" { |
| 70 | t.Error("the foreign file changed") |
| 71 | } |
| 72 | |
| 73 | os.Remove(link) |
| 74 | os.Symlink(filepath.Join(home, "gone"), link) |
| 75 | if te.run("install") != 1 || !strings.Contains(te.out(), "already exists and is not a link to") { |
| 76 | t.Errorf("dangling link: %s", te.out()) |
| 77 | } |
| 78 | |
| 79 | os.Remove(link) |
| 80 | os.Symlink(filepath.Join(home, "agent-vm", "agent-vm.sh"), link) |
| 81 | if te.run("install") != 0 || !strings.Contains(te.out(), "Replacing "+link) { |
| 82 | t.Errorf("0.2's link: %s", te.out()) |
| 83 | } |
| 84 | if t2, _ := os.Readlink(link); t2 != exe { |
| 85 | t.Errorf("link after 0.2's: %q", t2) |
| 86 | } |
| 87 | |
| 88 | |
| 89 | os.Remove(link) |
| 90 | os.WriteFile(link, []byte("old agent-vm"), 0o755) |
| 91 | os.MkdirAll(string(te.state), 0o755) |
| 92 | os.WriteFile(te.state.Path(installedCopy), []byte(sha256Of(link)+"\n"), 0o644) |
| 93 | if te.run("uninstall") != 0 || !strings.Contains(te.out(), "Removed "+link) { |
| 94 | t.Errorf("recorded copy, uninstall: %s", te.out()) |
| 95 | } |
| 96 | os.WriteFile(link, []byte("old agent-vm"), 0o755) |
| 97 | if te.run("install") != 0 || !strings.Contains(te.out(), "Replacing "+link) { |
| 98 | t.Errorf("recorded copy, install: %s", te.out()) |
| 99 | } |
| 100 | } |