test: add real-hook push helpers for checkpoint sync (I-2) · Entire

test: add real-hook push helpers for checkpoint sync (I-2)

d40e0f1→main·Soph·1w ago·3 files·+150 added/-4 removed

The real git-invoked pre-push hook was never asserted end to end. Integration RunPrePush spawned the hook with Stdin=nil, and GitPush always passed --no-verify, so a completely broken hook would go unnoticed; e2e remote tests masked the same gap by always calling PushCheckpointRefs explicitly.

Integration TestEnv gains: - InstallRealPrePushHook + GitPushWithHooks: a plain git push (no --no-verify) that runs the installed hook exactly as git runs it — realistic stdin refspec lines and remote name/URL argv, inheriting the checkpoint-store env. - RunPrePush now feeds realistic stdin (defaultPrePushStdin) instead of nil, with RunPrePushWithStdin as an escape hatch for the empty-stdin no-op case. GitPush keeps --no-verify for setup plumbing.

A smoke test (seed of A1) proves GitPushWithHooks lands checkpoints on a bare remote with NO explicit checkpoint push, under both backends via ForEachBackend.

e2e gains AssertCheckpointsOnRemote(t, s, bareDir), asserting the backend-appropriate refs are present on the bare remote for later use by the section-3 e2e tests.

Sessions

51ee64d92120View transcript

Changes

3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

//go:build integration

package integration

import (
\t"testing"
)

// TestGitPushWithHooks_SyncsCheckpointsToRemote is the seed of test A1: a plain
// `git push` of a feature branch, running the installed pre-push hook exactly as
// git runs it (realistic stdin refspecs, remote name/URL argv), lands the
// committed checkpoints on the bare remote WITHOUT any explicit RunPrePush or
// PushCheckpointRefs. It runs under both checkpoint backends via ForEachBackend,
// validating the whole I-1/I-2 enabler stack: env injection selects the store,
// the real hook drains it, and the backend-aware assertion finds the result.
func TestGitPushWithHooks_SyncsCheckpointsToRemote(t *testing.T) {
\t t.Parallel()

\t ForEachBackend(t, func(t *testing.T, backend string) {
\t\t env := NewFeatureBranchEnv(t)
\t\t env.CheckpointStore = backend

\t\t bareDir := env.SetupBareRemote()

\t\t checkpointID := createCheckpointedCommit(t, env, "Add auth module", "auth.go", "package auth", "Add auth module")
\t\t if checkpointID == "" {
\t\t\t t.Fatal("should have a checkpoint ID after condensation")
\t\t }

\t\t // Sanity: checkpoint exists locally under the selected backend.
\t\t if !env.CheckpointsPresentLocally() {
\t\t\t t.Fatalf("[%s] checkpoint should exist locally after condensation", backend)
\t\t }

\t\t // Plain push through the real hook — no explicit checkpoint push.
\t\t env.GitPushWithHooks("origin", "HEAD")

\t\t if !env.CheckpointsPresentOnRemote(bareDir) {
\t\t\t t.Fatalf("[%s] checkpoints should be on remote after `git push` via the real pre-push hook", backend)
\t\t }
\t\t if !env.CheckpointExistsOnRemote(bareDir, checkpointID) {
\t\t\t t.Fatalf("[%s] checkpoint %s should be on remote after `git push` via the real pre-push hook", backend, checkpointID)
\t\t }
\t })
}

Acmd/entire/cli/integration_test/real_hook_push_test.go+45

1944 unmodified lines

1945
1946
1947
1948
1948
1949
1950
1951
1952
1953
1954
5 unmodified lines

1960
1961
1962
1960
1961
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
4 unmodified lines

2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
1976
2032
2033
2034
2035
2036
2037
3 unmodified lines

2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069

1944 unmodified lines
}

// GitPush pushes a branch to a remote. Fails the test on error.
// GitPush pushes a branch to a remote with --no-verify, bypassing the pre-push
// hook. Use this for setup plumbing (seeding remotes, pushing the user branch)
// where the checkpoint sync should NOT run. To exercise the real hook, use
// GitPushWithHooks. Fails the test on error.
func (env *TestEnv) GitPush(remote, refSpec string) {
\t env.T.Helper()

5 unmodified lines

}

// RunPrePush runs the pre-push hook via the CLI binary, consistent with how
// other CLI invocations (GitCommitWithShadowHooks, RunCLI) use env.cliEnv().
// InstallRealPrePushHook writes .git/hooks/pre-push so a plain `git push` (no
// --no-verify) runs the checkpoint sync exactly as git runs it: git invokes the
// hook with the remote name ($1) and URL ($2) as argv and feeds
// "<local-ref> <local-sha> <remote-ref> <remote-sha>" lines on stdin. The hook
// inherits the pushing process's environment, so the checkpoint-store and git
// isolation overrides from GitPushWithHooks propagate into it.
func (env *TestEnv) InstallRealPrePushHook() {
\t env.T.Helper()

\t hooksDir := filepath.Join(env.RepoDir, ".git", "hooks")
\t if err := os.MkdirAll(hooksDir, 0o755); err != nil {
\t\t env.T.Fatalf("failed to create hooks dir: %v", err)
\t }
\t // Quote the binary path so a temp path containing spaces still execs.
\t script := fmt.Sprintf("#!/bin/sh\nexec %%q hooks git pre-push \"$1\"\n", getTestBinary())
\t hookPath := filepath.Join(hooksDir, "pre-push")
\t if err := os.WriteFile(hookPath, []byte(script), 0o755); err != nil { //nolint:gosec // G306: hook scripts must be executable
\t\t env.T.Fatalf("failed to write pre-push hook: %v", err)
\t }
}

// GitPushWithHooks pushes a branch to a remote WITHOUT --no-verify, so the
// installed pre-push hook (see InstallRealPrePushHook) runs as part of the push.
// This is the real-git path: git feeds the hook realistic stdin refspec lines
// and the remote name/URL argv, so the checkpoint sync happens without any
// explicit RunPrePush. Fails the test on error.
func (env *TestEnv) GitPushWithHooks(remote, refSpec string) {
\t env.T.Helper()

\t env.InstallRealPrePushHook()

\t cmd := execx.NonInteractive(env.T.Context(), "git", "push", remote, refSpec)
\t cmd.Dir = env.RepoDir
\t cmd.Env = env.cliEnv()
\t if output, err := cmd.CombinedOutput(); err != nil {
\t\t env.T.Fatalf("git push (with hooks) %s %s failed: %v\n%s", remote, refSpec, err, output)
\t }
}

// RunPrePush runs the pre-push hook via the CLI binary, feeding realistic stdin
// refspec lines for the current branch (see defaultPrePushStdin). This is the
// direct-invocation stand-in for GitPushWithHooks used by tests that don't push
// the user branch. Consistent with other CLI invocations (RunCLI) it uses
// env.cliEnv().
func (env *TestEnv) RunPrePush(remote string) {
\t env.T.Helper()
\t if err := env.RunPrePushWithError(remote); err != nil {
4 unmodified lines

// RunPrePushWithError runs the pre-push hook and returns any error instead of failing.
func (env *TestEnv) RunPrePushWithError(remote string) error {
\t env.T.Helper()
\t return env.runPrePush(remote, env.defaultPrePushStdin())
}

// RunPrePushWithStdin runs the pre-push hook feeding the given stdin verbatim.
// Pass "" to exercise the real no-op case (a `git push` with nothing new to
// push feeds the hook empty stdin).
func (env *TestEnv) RunPrePushWithStdin(remote, stdin string) error {
\t env.T.Helper()
\t return env.runPrePush(remote, stdin)
}

func (env *TestEnv) runPrePush(remote, stdin string) error {
\t cmd := exec.CommandContext(env.T.Context(), getTestBinary(), "hooks", "git", "pre-push", remote)
\t cmd.Dir = env.RepoDir
\t cmd.Env = env.cliEnv()
\t cmd.Stdin = nil
\t if stdin != "" {
\t\t cmd.Stdin = strings.NewReader(stdin)
\t }

\t output, err := cmd.CombinedOutput()
\t env.T.Logf("pre-push output: %s", output)
\t return nil
}

// defaultPrePushStdin builds the stdin line git feeds a pre-push hook for the
// current branch: "<local-ref> <local-sha> <remote-ref> <remote-sha>". The
// remote sha is all-zeros (a new branch) since it doesn't change the checkpoint
// sync behavior. Returns "" when HEAD is detached or unresolvable, so callers
// exercise the empty-stdin (no-op) case.
func (env *TestEnv) defaultPrePushStdin() string {
\t branch := env.GetCurrentBranch()
\t if branch == "" {
\t\t return ""
\t }
\t repo, err := gitrepo.OpenPath(env.RepoDir)
\t if err != nil {
\t\t return ""
\t }
\t defer repo.Close()
\t head, err := repo.Head()
\t if err != nil {
\t\t return ""
\t }
\t ref := "refs/heads/" + branch
\t return fmt.Sprintf("%s %s %s %s\n", ref, head.Hash().String(), ref, plumbing.ZeroHash.String())
}

// FetchMetadataBranch fetches the entire/checkpoints/v1 branch from a remote URL.
// Fails the test on error. Use this for clone-and-resume tests that need metadata.
func (env *TestEnv) FetchMetadataBranch(remoteURL string) {