130 lines
3.9 KiB
Go
130 lines
3.9 KiB
Go
package selfupdate
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func write(t *testing.T, path, body string) {
|
|
t.Helper()
|
|
if err := os.WriteFile(path, []byte(body), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func read(t *testing.T, path string) string {
|
|
t.Helper()
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
func TestSwapAndCleanup(t *testing.T) {
|
|
dir := t.TempDir()
|
|
exe := filepath.Join(dir, "reims-go.exe")
|
|
newExe := filepath.Join(t.TempDir(), "reims-go-v9.9.9-windows-amd64.exe")
|
|
write(t, exe, "old build")
|
|
write(t, newExe, "new build")
|
|
write(t, exe+".old", "left over from an earlier update")
|
|
|
|
if err := Swap(exe, exe, newExe); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := read(t, exe); got != "new build" {
|
|
t.Fatalf("exe = %q, want the new build at the original path", got)
|
|
}
|
|
if got := read(t, exe+".old"); got != "old build" {
|
|
t.Fatalf(".old = %q, want the replaced build", got)
|
|
}
|
|
if _, err := os.Stat(exe + ".new"); !os.IsNotExist(err) {
|
|
t.Fatal("staged .new must not remain")
|
|
}
|
|
|
|
Cleanup(exe)
|
|
if _, err := os.Stat(exe + ".old"); !os.IsNotExist(err) {
|
|
t.Fatal("Cleanup must remove .old")
|
|
}
|
|
if got := read(t, exe); got != "new build" {
|
|
t.Fatal("Cleanup must not touch the executable")
|
|
}
|
|
}
|
|
|
|
func TestSwapLeavesExeOnFailure(t *testing.T) {
|
|
dir := t.TempDir()
|
|
exe := filepath.Join(dir, "reims-go.exe")
|
|
write(t, exe, "old build")
|
|
if err := Swap(exe, exe, filepath.Join(dir, "missing.exe")); err == nil {
|
|
t.Fatal("a missing download must fail")
|
|
}
|
|
if got := read(t, exe); got != "old build" {
|
|
t.Fatalf("exe changed after a failed swap: %q", got)
|
|
}
|
|
for _, p := range []string{exe + ".new", exe + ".old"} {
|
|
if _, err := os.Stat(p); !os.IsNotExist(err) {
|
|
t.Errorf("%s left behind after a failed swap", filepath.Base(p))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWritable(t *testing.T) {
|
|
if err := Writable(t.TempDir()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := Writable(filepath.Join(t.TempDir(), "missing")); err == nil {
|
|
t.Fatal("a missing folder is not writable")
|
|
}
|
|
}
|
|
|
|
func TestTarget(t *testing.T) {
|
|
dir := t.TempDir()
|
|
asset := "reims-go-v0.2.6-windows-amd64.exe"
|
|
cases := []struct{ running, want string }{
|
|
{"reims-go-v0.2.5-windows-amd64.exe", asset}, // release name: follow the version
|
|
{"Reims-Go-V0.2.5-Windows-AMD64.exe", asset}, // case does not matter
|
|
{"reims-go.exe", "reims-go.exe"}, // own name: keep it for shortcuts
|
|
{"reims-go-v0.2.5-windows-amd64 (1).exe", "reims-go-v0.2.5-windows-amd64 (1).exe"},
|
|
}
|
|
for _, c := range cases {
|
|
if got := Target(filepath.Join(dir, c.running), asset); got != filepath.Join(dir, c.want) {
|
|
t.Errorf("Target(%s) = %s, want %s", c.running, filepath.Base(got), c.want)
|
|
}
|
|
}
|
|
if got := Target(filepath.Join(dir, "reims-go-v0.2.5-windows-amd64.exe"), "odd.exe"); filepath.Base(got) != "reims-go-v0.2.5-windows-amd64.exe" {
|
|
t.Errorf("an asset without a release name must not rename, got %s", filepath.Base(got))
|
|
}
|
|
}
|
|
|
|
func TestSwapRenamesReleaseFile(t *testing.T) {
|
|
dir := t.TempDir()
|
|
exe := filepath.Join(dir, "reims-go-v0.2.5-windows-amd64.exe")
|
|
newExe := filepath.Join(t.TempDir(), "reims-go-v0.2.6-windows-amd64.exe")
|
|
write(t, exe, "v0.2.5")
|
|
write(t, newExe, "v0.2.6")
|
|
target := Target(exe, filepath.Base(newExe))
|
|
|
|
if err := Swap(exe, target, newExe); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := read(t, target); got != "v0.2.6" {
|
|
t.Fatalf("new name holds %q", got)
|
|
}
|
|
if _, err := os.Stat(exe); !os.IsNotExist(err) {
|
|
t.Fatal("the old release name must be gone, not left beside the new one")
|
|
}
|
|
if got := read(t, exe+".old"); got != "v0.2.5" {
|
|
t.Fatalf("old build moved aside = %q", got)
|
|
}
|
|
|
|
// The new version's first start removes the old release's .old file.
|
|
Cleanup(target)
|
|
if _, err := os.Stat(exe + ".old"); !os.IsNotExist(err) {
|
|
t.Fatal("Cleanup must remove the earlier release's .old file")
|
|
}
|
|
if got := read(t, target); got != "v0.2.6" {
|
|
t.Fatal("Cleanup must not touch the running executable")
|
|
}
|
|
}
|