fix: support Windows builds and tests

Replace Unix-only hub test fixtures with a cross-platform helper executable. Document Windows installation and usage, and ignore the generated .exe artifact while retaining Linux build guidance.
This commit is contained in:
2026-09-06 19:23:15 +01:00
parent 761424b2d5
commit ca53874b48
3 changed files with 99 additions and 9 deletions
+53 -8
View File
@@ -2,14 +2,63 @@ package main
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)
func TestMain(m *testing.M) {
if os.Getenv("STACK_SYNC_TEST_HELPER") == "hub" {
marker := os.Getenv("STACK_SYNC_TEST_MARKER")
if err := os.WriteFile(marker, []byte(strings.Join(os.Args[1:], " ")), 0o644); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Exit(0)
}
os.Exit(m.Run())
}
func installFakeHub(t *testing.T, dir string) string {
t.Helper()
source, err := os.Executable()
if err != nil {
t.Fatal(err)
}
name := "hub"
if runtime.GOOS == "windows" {
name += ".exe"
}
destination := filepath.Join(dir, name)
if err := os.Link(source, destination); err == nil {
return destination
}
input, err := os.Open(source)
if err != nil {
t.Fatal(err)
}
defer input.Close()
output, err := os.OpenFile(destination, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o755)
if err != nil {
t.Fatal(err)
}
if _, err := io.Copy(output, input); err != nil {
_ = output.Close()
t.Fatal(err)
}
if err := output.Close(); err != nil {
t.Fatal(err)
}
return destination
}
func git(t *testing.T, dir string, args ...string) {
t.Helper()
cmd := exec.Command("git", append([]string{"-C", dir}, args...)...)
@@ -128,11 +177,9 @@ func TestSyncRechecksAndSkipsNewlyDirtyRepository(t *testing.T) {
t.Fatal(err)
}
marker := filepath.Join(root, "hub-was-called")
script := "#!/bin/sh\ntouch \"$STACK_SYNC_TEST_MARKER\"\n"
if err := os.WriteFile(filepath.Join(bin, "hub"), []byte(script), 0o755); err != nil {
t.Fatal(err)
}
installFakeHub(t, bin)
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
t.Setenv("STACK_SYNC_TEST_HELPER", "hub")
t.Setenv("STACK_SYNC_TEST_MARKER", marker)
results := syncAll(root, []repo{planned}, 1, time.Minute, io.Discard, true)
@@ -155,11 +202,9 @@ func TestSyncInvokesHubForCleanRepository(t *testing.T) {
t.Fatal(err)
}
marker := filepath.Join(root, "hub-was-called")
script := "#!/bin/sh\nprintf '%s' \"$*\" > \"$STACK_SYNC_TEST_MARKER\"\n"
if err := os.WriteFile(filepath.Join(bin, "hub"), []byte(script), 0o755); err != nil {
t.Fatal(err)
}
installFakeHub(t, bin)
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
t.Setenv("STACK_SYNC_TEST_HELPER", "hub")
t.Setenv("STACK_SYNC_TEST_MARKER", marker)
results := syncAll(root, []repo{planned}, 1, time.Minute, io.Discard, true)