fix(test): run the hub fixture on Windows
- replace the POSIX shell fixture with a compiled Go helper named hub or hub.exe - prepend the helper directory to PATH in TestMain so tests cannot invoke an installed hub binary - add PowerShell build, install, and Windows path examples to the README - ignore the generated stack-sync.exe binary
This commit is contained in:
+53
-8
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user