|
1 | 1 | package cli |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
4 | 8 | "slices" |
5 | 9 | "strings" |
6 | 10 | "testing" |
| 11 | + |
| 12 | + "github.com/spf13/cobra" |
7 | 13 | ) |
8 | 14 |
|
| 15 | +func writeExecutable(t *testing.T, path, body string) { |
| 16 | + t.Helper() |
| 17 | + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 18 | + t.Fatal(err) |
| 19 | + } |
| 20 | + if err := os.WriteFile(path, []byte(body), 0o755); err != nil { |
| 21 | + t.Fatal(err) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func TestRunInstallerUpdatesInvokedBinary(t *testing.T) { |
| 26 | + if runtime.GOOS == "windows" { |
| 27 | + t.Skip("shell installer test") |
| 28 | + } |
| 29 | + |
| 30 | + dir := t.TempDir() |
| 31 | + executable := filepath.Join(dir, ".flashduty", "bin", "fduty") |
| 32 | + writeExecutable(t, executable, `#!/bin/sh |
| 33 | +if [ "$1" = "version" ] && [ "$2" = "--json" ]; then |
| 34 | + echo '{"version":"1.3.6"}' |
| 35 | +else |
| 36 | + echo 'flashduty version 1.3.6 (old) built old' |
| 37 | +fi |
| 38 | +`) |
| 39 | + |
| 40 | + fakeBin := filepath.Join(dir, "fake-bin") |
| 41 | + writeExecutable(t, filepath.Join(fakeBin, "sh"), `#!/bin/sh |
| 42 | +cat > "$FLASHDUTY_INSTALL_DIR/$INSTALLED_NAME" <<'EOF' |
| 43 | +#!/bin/sh |
| 44 | +if [ "$1" = "version" ] && [ "$2" = "--json" ]; then |
| 45 | + echo '{"version":"1.3.24"}' |
| 46 | +else |
| 47 | + echo 'flashduty version 1.3.24 (new) built now' |
| 48 | +fi |
| 49 | +EOF |
| 50 | +chmod +x "$FLASHDUTY_INSTALL_DIR/$INSTALLED_NAME" |
| 51 | +`) |
| 52 | + t.Setenv("PATH", fakeBin+string(os.PathListSeparator)+os.Getenv("PATH")) |
| 53 | + t.Setenv("FLASHDUTY_INSTALL_DIR", filepath.Join(dir, "wrong-dir")) |
| 54 | + t.Setenv("INSTALLED_NAME", "wrong-name") |
| 55 | + |
| 56 | + var stdout, stderr bytes.Buffer |
| 57 | + cmd := &cobra.Command{} |
| 58 | + cmd.SetOut(&stdout) |
| 59 | + cmd.SetErr(&stderr) |
| 60 | + if err := runInstaller(cmd, executable, "v1.3.24"); err != nil { |
| 61 | + t.Fatalf("runInstaller: %v; stderr=%s", err, stderr.String()) |
| 62 | + } |
| 63 | + |
| 64 | + out, err := os.ReadFile(executable) |
| 65 | + if err != nil { |
| 66 | + t.Fatal(err) |
| 67 | + } |
| 68 | + if !strings.Contains(string(out), "version 1.3.24") { |
| 69 | + t.Fatalf("active executable was not replaced: %s", out) |
| 70 | + } |
| 71 | + if !strings.Contains(stdout.String(), "Update complete") { |
| 72 | + t.Fatalf("stdout = %q, want update success", stdout.String()) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestRunInstallerRejectsShadowedInstall(t *testing.T) { |
| 77 | + if runtime.GOOS == "windows" { |
| 78 | + t.Skip("shell installer test") |
| 79 | + } |
| 80 | + |
| 81 | + dir := t.TempDir() |
| 82 | + executable := filepath.Join(dir, ".flashduty", "bin", "fduty") |
| 83 | + writeExecutable(t, executable, `#!/bin/sh |
| 84 | +if [ "$1" = "version" ] && [ "$2" = "--json" ]; then |
| 85 | + echo '{"version":"1.3.6"}' |
| 86 | +else |
| 87 | + echo 'flashduty version 1.3.6 (old) built old' |
| 88 | +fi |
| 89 | +`) |
| 90 | + |
| 91 | + fakeBin := filepath.Join(dir, "fake-bin") |
| 92 | + writeExecutable(t, filepath.Join(fakeBin, "sh"), "#!/bin/sh\nexit 0\n") |
| 93 | + t.Setenv("PATH", fakeBin+string(os.PathListSeparator)+os.Getenv("PATH")) |
| 94 | + |
| 95 | + var stdout bytes.Buffer |
| 96 | + cmd := &cobra.Command{} |
| 97 | + cmd.SetOut(&stdout) |
| 98 | + err := runInstaller(cmd, executable, "v1.3.24") |
| 99 | + if err == nil || !strings.Contains(err.Error(), executable) || !strings.Contains(err.Error(), "1.3.6") { |
| 100 | + t.Fatalf("runInstaller error = %v, want active path and stale version", err) |
| 101 | + } |
| 102 | + if strings.Contains(stdout.String(), "Update complete") { |
| 103 | + t.Fatalf("reported success for stale executable: %q", stdout.String()) |
| 104 | + } |
| 105 | +} |
| 106 | + |
9 | 107 | func TestInstallerCommandSpecPassesInstallerURLAsArgument(t *testing.T) { |
10 | 108 | shellURL := `https://mirror.example.com/fduty/install.sh; echo injected` |
11 | 109 | psURL := `https://mirror.example.com/fduty/install.ps1; Write-Host injected` |
|
0 commit comments