MCPcopy Create free account
hub / github.com/cli/cli / TestCmdGit

Function TestCmdGit

script/ghmock/gitcmd_test.go:202–365  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

200}
201
202func TestCmdGit(t *testing.T) {
203 tests := []struct {
204 name string
205 run func(*testing.T, *state)
206 }{
207 {name: "shared clones remain usable after the fixture is copied", run: func(t *testing.T, st *state) {
208 prepareGitRepo(t, "https://github.com/octo/hello.git")
209 gitForTest(t, "--", "clone", "--quiet", "--shared", "main", "shared")
210 copied := filepath.Join(t.TempDir(), "state")
211 destination := (&state{dir: copied}).gitFixtureDir()
212 require.NoError(t, copyGitTree(st.gitFixtureDir(), destination))
213 require.NoError(t, os.Rename(st.gitFixtureDir(), st.gitFixtureDir()+"-moved"))
214 t.Setenv("GHMOCK_DIR", copied)
215 assert.Equal(t, "one\n", gitForTest(t, "--in", "shared", "--", "show", "HEAD:example.txt"))
216 data, err := os.ReadFile(filepath.Join(destination, "work", "shared", ".git", "objects", "info", "alternates"))
217 require.NoError(t, err)
218 assert.False(t, filepath.IsAbs(strings.TrimSpace(string(data))))
219 }},
220 {name: "SSH inherits an extended command deadline", run: func(t *testing.T, _ *state) {
221 if runtime.GOOS == "windows" {
222 t.Skip("uses a Unix test wrapper")
223 }
224 prepareGitRepo(t, "git@github.com:octo/hello.git")
225 gitPath, err := exec.LookPath("git")
226 require.NoError(t, err)
227 bin := t.TempDir()
228 captured := filepath.Join(bin, "deadline")
229 wrapper := "#!/bin/sh\nif [ \"$1\" = upload-pack ]; then printf '%s' \"$GHMOCK_GIT_DEADLINE\" > " + shellQuote(captured) + "; fi\nexec " + shellQuote(gitPath) + " \"$@\"\n"
230 require.NoError(t, os.WriteFile(filepath.Join(bin, "git"), []byte(wrapper), 0o755))
231 t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
232 t.Setenv("GIT_EXEC_PATH", bin)
233 start := time.Now()
234 gitForTest(t, "--timeout", "2m", "--", "ls-remote", "git@github.com:octo/hello.git")
235 data, err := os.ReadFile(captured)
236 require.NoError(t, err)
237 deadline, err := time.Parse(time.RFC3339Nano, string(data))
238 require.NoError(t, err)
239 assert.WithinDuration(t, start.Add(2*time.Minute), deadline, 5*time.Second)
240 }},
241 {name: "SSH requests remain recorded when their helper is cancelled", run: func(t *testing.T, st *state) {
242 if runtime.GOOS == "windows" {
243 t.Skip("uses a Unix test wrapper")
244 }
245 prepareGitRepo(t, "git@github.com:octo/hello.git")
246 gitPath, err := exec.LookPath("git")
247 require.NoError(t, err)
248 bin := t.TempDir()
249 wrapper := "#!/bin/sh\nif [ \"$1\" = upload-pack ]; then sleep 15; fi\nexec " + shellQuote(gitPath) + " \"$@\"\n"
250 require.NoError(t, os.WriteFile(filepath.Join(bin, "git"), []byte(wrapper), 0o755))
251 t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
252 t.Setenv("GIT_EXEC_PATH", bin)
253 err = cmdGit([]string{"--timeout", "500ms", "--", "ls-remote", "git@github.com:octo/hello.git"}, io.Discard, io.Discard)
254 require.ErrorContains(t, err, "git stopped after")
255 records, err := readJSONL[gitCommandRecord](filepath.Join(st.gitFixtureDir(), "commands.jsonl"))
256 require.NoError(t, err)
257 requests := records[len(records)-1].Requests
258 require.Len(t, requests, 1)
259 assert.Equal(t, "SSH", requests[0].Method)

Callers

nothing calls this directly

Calls 15

prepareGitRepoFunction · 0.85
gitForTestFunction · 0.85
copyGitTreeFunction · 0.85
cmdGitFunction · 0.85
cmdRepoFunction · 0.85
cmdWriteFunction · 0.85
openStateFunction · 0.85
JoinMethod · 0.80
gitFixtureDirMethod · 0.80
EqualMethod · 0.80
SkipMethod · 0.80
WriteFileMethod · 0.80

Tested by

no test coverage detected