fix: macOS main job — /private/var symlink in test repo pin; bash 3.2-safe CI scripts (#7165)
* fix(git): canonicalize PinNoRepositoryUnderForTesting root so the pin matches on macOS
On macOS t.TempDir()/os.TempDir() live under /var/folders/..., but /var is
a symlink to /private/var and getcwd(2) reports /private/var/folders/....
underPinnedRootForTesting compared the raw pin against os.Getwd() with a
string prefix test, so the pin never matched there and
TestPinNoRepositoryUnderForTestingSurvivesResetCaches failed on every Main
macOS run (GetRepoRoot() returned the /private/var repo instead of "").
Store the pin both as given (absolute) and symlink-resolved, and match the
working directory in both raw and resolved form. Canonicalization can only
widen the fence, never narrow it (e.g. Bazel runfiles symlink trees keep
matching exactly as before).
TestPinNoRepositoryUnderForTestingMatchesThroughSymlinks reproduces the
macOS shape on any platform with a symlinked repo root; it fails without
this change.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(ci): make run-go-test-binary.sh and install-golangci-lint.sh parse under bash 3.2
The Main macOS job runs the scripts package tests with macOS's /bin/bash
3.2, where two constructs abort under `set -u`:
- run-go-test-binary.sh expanded the usually-empty PASSTHROUGH array bare;
bash < 4.4 treats that as an unbound variable ("PASSTHROUGH[*]: unbound
variable"), failing TestTestScriptPrebuiltTestBinaryContract and
TestRunGoTestBinaryDefaultsTimeoutWhenOmitted. Use the ${arr[@]+...}
guard.
- install-golangci-lint.sh used `declare -A`; bash 3.2 parses it as an
indexed array and `[amd64]=` dies as an arithmetic lookup ("amd64:
unbound variable"), failing
TestInstallGolangciLintFailsClosedOnChecksumMismatch. Replace it with a
case function and update TestGolangciLintInstallScriptPinned's pin check.
Reproduced and verified against a bash 3.2.57 build: all four tests fail
on origin/main and pass with this change.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
59dcd83bbd
commit
4b2852ce21
5 changed files with 121 additions and 15 deletions
|
|
@ -37,6 +37,10 @@ var (
|
|||
// ResetCaches: see that function's comment and PinNoRepositoryUnderForTesting's
|
||||
// doc for why the pin must outlive a cache reset to do its job.
|
||||
pinnedRootForTesting string
|
||||
// pinnedRootRawForTesting is the pin as given (absolute but not
|
||||
// symlink-resolved). underPinnedRootForTesting matches against both forms
|
||||
// so canonicalization only ever widens the fence, never narrows it.
|
||||
pinnedRootRawForTesting string
|
||||
)
|
||||
|
||||
// underPinnedRootForTesting reports whether wd is pinnedRootForTesting itself
|
||||
|
|
@ -44,14 +48,42 @@ var (
|
|||
// getGitContext lookup while a pin is active, so a test that chdirs outside
|
||||
// the pinned root (e.g. into its own t.TempDir() fixture) still gets real git
|
||||
// detection scoped to that fixture.
|
||||
//
|
||||
// Both sides are compared in canonical form (see canonicalPinPath): on macOS
|
||||
// t.TempDir() and os.TempDir() live under /var/folders/..., but /var is a
|
||||
// symlink to /private/var and getcwd(2) reports the resolved
|
||||
// /private/var/folders/... path, so a raw prefix test never matched and the
|
||||
// pin silently did nothing there.
|
||||
func underPinnedRootForTesting(wd string) bool {
|
||||
if pinnedRootForTesting == "" || wd == "" {
|
||||
return false
|
||||
}
|
||||
if wd == pinnedRootForTesting {
|
||||
return true
|
||||
canonicalWD := canonicalPinPath(wd)
|
||||
for _, root := range []string{pinnedRootForTesting, pinnedRootRawForTesting} {
|
||||
if root == "" {
|
||||
continue
|
||||
}
|
||||
for _, dir := range []string{wd, canonicalWD} {
|
||||
if dir == root || strings.HasPrefix(dir, root+string(filepath.Separator)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return strings.HasPrefix(wd, pinnedRootForTesting+string(filepath.Separator))
|
||||
return false
|
||||
}
|
||||
|
||||
// canonicalPinPath returns p as an absolute, symlink-resolved, cleaned path,
|
||||
// falling back to the best form available when resolution fails (e.g. the
|
||||
// path no longer exists), so the pin comparison is never stricter than the
|
||||
// raw strings.
|
||||
func canonicalPinPath(p string) string {
|
||||
if abs, err := filepath.Abs(p); err == nil {
|
||||
p = abs
|
||||
}
|
||||
if resolved, err := filepath.EvalSymlinks(p); err == nil {
|
||||
p = resolved
|
||||
}
|
||||
return filepath.Clean(p)
|
||||
}
|
||||
|
||||
// initGitContext populates the gitContext with a single git call.
|
||||
|
|
@ -556,11 +588,11 @@ var errPinnedNoRepository = errors.New("not a git repository (pinned for testing
|
|||
// WARNING: Not thread-safe, like ResetCaches. Only call before m.Run(),
|
||||
// before any goroutines that might read the git context are started.
|
||||
func PinNoRepositoryUnderForTesting(root string) {
|
||||
abs, err := filepath.Abs(root)
|
||||
if err == nil {
|
||||
root = abs
|
||||
pinnedRootRawForTesting = root
|
||||
if abs, err := filepath.Abs(root); err == nil {
|
||||
pinnedRootRawForTesting = abs
|
||||
}
|
||||
pinnedRootForTesting = root
|
||||
pinnedRootForTesting = canonicalPinPath(root)
|
||||
gitCtxOnce = sync.Once{}
|
||||
gitCtx = gitContext{}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,6 +68,7 @@ func TestPinNoRepositoryUnderForTestingSurvivesResetCaches(t *testing.T) {
|
|||
t.Fatalf("restore working directory: %v", err)
|
||||
}
|
||||
pinnedRootForTesting = ""
|
||||
pinnedRootRawForTesting = ""
|
||||
ResetCaches()
|
||||
})
|
||||
|
||||
|
|
@ -103,6 +104,67 @@ func TestPinNoRepositoryUnderForTestingSurvivesResetCaches(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestPinNoRepositoryUnderForTestingMatchesThroughSymlinks reproduces, on any
|
||||
// platform with symlinks, the macOS failure of the pin: there t.TempDir() is
|
||||
// /var/folders/... while getcwd(2) reports /private/var/folders/... (/var is
|
||||
// a symlink), so pinning the unresolved path and comparing it to os.Getwd()
|
||||
// never matched. Pinning through a symlink must still fence the real
|
||||
// directory, and pinning the real directory must still fence a working
|
||||
// directory reported through the symlink.
|
||||
func TestPinNoRepositoryUnderForTestingMatchesThroughSymlinks(t *testing.T) {
|
||||
realRepo, _ := setupTestRepo(t)
|
||||
realRepo, err := filepath.EvalSymlinks(realRepo)
|
||||
if err != nil {
|
||||
t.Fatalf("EvalSymlinks: %v", err)
|
||||
}
|
||||
link := filepath.Join(t.TempDir(), "linked-repo")
|
||||
if err := os.Symlink(realRepo, link); err != nil {
|
||||
t.Skipf("symlinks unavailable: %v", err)
|
||||
}
|
||||
subdir := filepath.Join(realRepo, "sub")
|
||||
if err := os.MkdirAll(subdir, 0o750); err != nil {
|
||||
t.Fatalf("mkdir: %v", err)
|
||||
}
|
||||
|
||||
origWD, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("Getwd: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if err := os.Chdir(origWD); err != nil {
|
||||
t.Fatalf("restore working directory: %v", err)
|
||||
}
|
||||
pinnedRootForTesting = ""
|
||||
pinnedRootRawForTesting = ""
|
||||
ResetCaches()
|
||||
})
|
||||
|
||||
for _, tc := range []struct{ name, pin, wd string }{
|
||||
{"pin via symlink, cwd real", link, realRepo},
|
||||
{"pin via symlink, cwd real subdir", link, subdir},
|
||||
{"pin real, cwd via symlink", realRepo, filepath.Join(link, "sub")},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if err := os.Chdir(tc.wd); err != nil {
|
||||
t.Fatalf("chdir: %v", err)
|
||||
}
|
||||
PinNoRepositoryUnderForTesting(tc.pin)
|
||||
if !underPinnedRootForTesting(tc.wd) {
|
||||
t.Errorf("underPinnedRootForTesting(%q) = false with pin %q (stored %q), want true", tc.wd, tc.pin, pinnedRootForTesting)
|
||||
}
|
||||
if got := GetRepoRoot(); got != "" {
|
||||
t.Errorf("GetRepoRoot() = %q under pinned root %q, want \"\"", got, tc.pin)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// A sibling that merely shares the string prefix is not under the pin.
|
||||
PinNoRepositoryUnderForTesting(realRepo)
|
||||
if underPinnedRootForTesting(realRepo + "-sibling") {
|
||||
t.Errorf("underPinnedRootForTesting(%q) = true, want false (prefix-only sibling)", realRepo+"-sibling")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetGitHooksDirTildeExpansion(t *testing.T) {
|
||||
// Use an explicit temporary home so tilde expansion is deterministic
|
||||
// regardless of the environment (CI, containers, overridden homes, etc.).
|
||||
|
|
|
|||
|
|
@ -18,10 +18,16 @@ readonly version="2.10.1"
|
|||
readonly max_attempts=3
|
||||
readonly retry_delay_seconds=5
|
||||
|
||||
declare -A sha256_by_arch=(
|
||||
[amd64]="dfa775874cf0561b404a02a8f4481fc69b28091da95aa697259820d429b09c99"
|
||||
[arm64]="6652b42ae02915eb2f9cb2a2e0cac99514c8eded8388d88ae3e06e1a52c00de8"
|
||||
)
|
||||
# A case function rather than `declare -A`: associative arrays need bash 4,
|
||||
# and macOS's /bin/bash 3.2 silently parses `declare -A` as an indexed array,
|
||||
# so `[amd64]=` becomes an arithmetic lookup that dies under `set -u` before
|
||||
# the Linux-only guard below can print its message.
|
||||
sha256_for_arch() {
|
||||
case "$1" in
|
||||
amd64) printf '%s' "dfa775874cf0561b404a02a8f4481fc69b28091da95aa697259820d429b09c99" ;;
|
||||
arm64) printf '%s' "6652b42ae02915eb2f9cb2a2e0cac99514c8eded8388d88ae3e06e1a52c00de8" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
: "${RUNNER_TEMP:?RUNNER_TEMP is required; this script is CI-only}"
|
||||
: "${GITHUB_PATH:?GITHUB_PATH is required; this script is CI-only}"
|
||||
|
|
@ -42,7 +48,8 @@ case "$arch" in
|
|||
;;
|
||||
esac
|
||||
|
||||
expected_sha256="${sha256_by_arch[$arch]:?no pinned sha256 for arch: $arch}"
|
||||
expected_sha256="$(sha256_for_arch "$arch")"
|
||||
: "${expected_sha256:?no pinned sha256 for arch: $arch}"
|
||||
readonly asset="golangci-lint-${version}-${os}-${arch}.tar.gz"
|
||||
readonly url="https://github.com/golangci/golangci-lint/releases/download/v${version}/${asset}"
|
||||
|
||||
|
|
|
|||
|
|
@ -146,6 +146,9 @@ if [[ -z "$BEADS_TEST_REPO_ROOT" ]]; then
|
|||
exit 1
|
||||
fi
|
||||
|
||||
echo "Running: $BIN (cwd=$PKGDIR_ABS) ${TEST_ARGS[*]} ${PASSTHROUGH[*]}" >&2
|
||||
# PASSTHROUGH is usually empty. Expand it with the ${arr[@]+...} guard: under
|
||||
# `set -u`, bash < 4.4 (macOS's /bin/bash is 3.2) treats a bare expansion of
|
||||
# an empty array as an unbound variable and aborts before the exec.
|
||||
echo "Running: $BIN (cwd=$PKGDIR_ABS) ${TEST_ARGS[*]} ${PASSTHROUGH[*]+${PASSTHROUGH[*]}}" >&2
|
||||
cd "$PKGDIR_ABS"
|
||||
exec "$BIN" "${TEST_ARGS[@]}" "${PASSTHROUGH[@]}"
|
||||
exec "$BIN" "${TEST_ARGS[@]}" ${PASSTHROUGH[@]+"${PASSTHROUGH[@]}"}
|
||||
|
|
|
|||
|
|
@ -148,7 +148,9 @@ func TestGolangciLintInstallScriptPinned(t *testing.T) {
|
|||
t.Errorf("install-golangci-lint.sh does not pin version %q", version)
|
||||
}
|
||||
for arch, sha := range wantSHA256 {
|
||||
if !strings.Contains(script, "["+arch+"]=\""+sha+"\"") {
|
||||
// The script maps arch to sha256 with a case arm, not `declare -A`,
|
||||
// so it still parses under macOS's bash 3.2.
|
||||
if !strings.Contains(script, arch+") printf '%s' \""+sha+"\"") {
|
||||
t.Errorf("install-golangci-lint.sh does not pin %s sha256 %q", arch, sha)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue