Files
magpie/internal/update/com_build_test.go
T
yetone 6b253dbb3f windows: magpie.com is written again only when the launcher changes, not on every update
lucky_eagle_38410 on Discord (#general, 10-28): every run of the command
line wrote a new magpie.com. magpie.com is the console launcher from
2afacd7d1 (#3737): PowerShell and cmd run magpie.com before magpie.exe
(PATHEXT), and wait for it, so a prompt, a password and the exit code
reach the terminal; the GUI-linked magpie.exe alone gives the prompt back
at once. It is needed, and writeCom already writes it only when its bytes
differ.

They differed on every release. `make release-windows` built the
launcher from the git checkout with Go's default -buildvcs, which stamps
vcs.revision and vcs.time into the binary, so each release carried a
different magpie.com though the launcher's source hadn't changed, and the
first `magpie` after each update (several a day) wrote it again, moving a
running one aside as magpie.com.old. The launcher is now built with
-buildvcs=false, so magpie.com changes only when the launcher does.

Run:
- Windows box (xiaomi-gpu, PowerShell 5.1 over a ConPTY, sandbox
  USERPROFILE/APPDATA, a folder of its own on PATH), the app run as
  magpie.exe, then through magpie.com, then the next version copied over
  magpie.exe and run again:
  - real releases v0.1.1861 -> v0.1.1862: magpie.com 5F55B8D4... ->
    2E6AD7D9..., rewritten on the update.
  - make release-windows at origin/main 6e080ca04, at two commits of the
    same tree: B26668C1... -> 15AD7512..., rewritten.
  - make release-windows with this change, the same two commits:
    782517C6... both times, mtime unchanged.
- mote: go test -tags nogui ./internal/update/...: ok. GOOS=windows and
  GOOS=linux go vet ./internal/update/: ok. gofmt -l: clean.
- win-run: TestLauncherSameAtEveryCommit, TestWriteCom, TestComOwner,
  TestEnsureCompanion, TestFindCLIPrefersCom pass on Windows.

Broken by hand: the Makefile without -buildvcs=false makes
TestLauncherSameAtEveryCommit red ("the launcher built at the next
commit differs ([-trimpath -ldflags=-s -w])"). The test builds a
program with the Makefile's launcher flags in a scratch git repo at two
commits and compares the bytes.

Not tried:
- A real release build in magpie-releases; the next tag is the first.
  The first update to it still writes magpie.com once (its bytes change
  from the stamped one); after that only a launcher change does.
- The arm64 build on a real ARM machine.
- The full go test ./...; only internal/update changed (a Makefile flag,
  a comment and a test).
2026-10-28 17:32:28 +08:00

94 lines
3.0 KiB
Go

package update
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/yetone/magpie/internal/proc"
)
// launcherFlags are the go build flags `make release-windows` builds the
// launcher with: what stands between "go build" and "-o" on the Makefile
// line that builds ./internal/update/launcher.
func launcherFlags(t *testing.T) []string {
t.Helper()
mk, err := os.ReadFile(filepath.Join("..", "..", "Makefile"))
if err != nil {
t.Fatal(err)
}
for _, line := range strings.Split(string(mk), "\n") {
if !strings.Contains(line, "./internal/update/launcher") {
continue
}
_, rest, ok := strings.Cut(line, "go build ")
if !ok {
continue
}
rest, _, _ = strings.Cut(rest, " -o ")
var flags []string
for _, f := range regexp.MustCompile(`(?:[^\s"]+|"[^"]*")+`).FindAllString(rest, -1) {
flags = append(flags, strings.ReplaceAll(f, `"`, ""))
}
return flags
}
t.Fatal("no line in the Makefile builds ./internal/update/launcher")
return nil
}
// The launcher is built into every Windows release and written out as
// magpie.com wherever its bytes differ from the one there (writeCom). Built
// from a git checkout with Go's default -buildvcs, it carried the release's
// commit and time, so every update wrote a new magpie.com though the
// launcher hadn't changed (lucky_eagle_38410 on Discord, 10-28). Built as
// the Makefile builds it, the same source at two commits is the same bytes.
func TestLauncherSameAtEveryCommit(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("no git")
}
flags := launcherFlags(t)
dir := t.TempDir()
for name, text := range map[string]string{
"go.mod": "module launcher\n\ngo 1.26\n",
"main.go": "package main\n\nfunc main() {}\n",
} {
if err := os.WriteFile(filepath.Join(dir, name), []byte(text), 0o644); err != nil {
t.Fatal(err)
}
}
run := func(name string, args ...string) {
t.Helper()
cmd := proc.Command(name, args...)
cmd.Dir = dir
cmd.Env = append(os.Environ(), "GOWORK=off", "GOFLAGS=", "GOTOOLCHAIN=local",
"CGO_ENABLED=0", "GOOS=windows", "GOARCH=amd64",
"GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@t.invalid", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@t.invalid")
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("%s %q: %v\n%s", name, args, err, out)
}
}
build := func(out string) []byte {
t.Helper()
run("go", append(append([]string{"build"}, flags...), "-o", out, ".")...)
b, err := os.ReadFile(out)
if err != nil {
t.Fatal(err)
}
return b
}
run("git", "init", "-q")
run("git", "add", "go.mod", "main.go")
run("git", "commit", "-q", "-m", "one")
// outside the checkout, so the build sees no untracked file
out := t.TempDir()
first := build(filepath.Join(out, "1.com"))
run("git", "commit", "-q", "--allow-empty", "-m", "two")
if second := build(filepath.Join(out, "2.com")); !bytes.Equal(first, second) {
t.Fatalf("the launcher built at the next commit differs (%v): magpie.com would be written again on every update", flags)
}
}