MCPcopy
hub / github.com/cli/cli / Stub

Function Stub

internal/run/stub.go:22–51  ·  view source on GitHub ↗

Stub installs a catch-all for all external commands invoked from gh. It returns a restore func that, when invoked from tests, fails the current test if some stubs that were registered were never matched.

()

Source from the content-addressed store, hash-verified

20// Stub installs a catch-all for all external commands invoked from gh. It returns a restore func that, when
21// invoked from tests, fails the current test if some stubs that were registered were never matched.
22func Stub() (*CommandStubber, func(T)) {
23 cs := &CommandStubber{}
24 teardown := setPrepareCmd(func(cmd *exec.Cmd) Runnable {
25 s := cs.find(cmd.Args)
26 if s == nil {
27 panic(fmt.Sprintf("no exec stub for `%s`", strings.Join(cmd.Args, " ")))
28 }
29 for _, c := range s.callbacks {
30 c(cmd.Args)
31 }
32 s.matched = true
33 return s
34 })
35
36 return cs, func(t T) {
37 defer teardown()
38 var unmatched []string
39 for _, s := range cs.stubs {
40 if s.matched {
41 continue
42 }
43 unmatched = append(unmatched, s.pattern.String())
44 }
45 if len(unmatched) == 0 {
46 return
47 }
48 t.Helper()
49 t.Errorf("unmatched exec stubs (%d): %s", len(unmatched), strings.Join(unmatched, ", "))
50 }
51}
52
53func setPrepareCmd(fn func(*exec.Cmd) Runnable) func() {
54 origPrepare := PrepareCmd

Callers 15

Test_deleteRunFunction · 0.92
Test_createRunFunction · 0.92
Test_gitTagInfoFunction · 0.92
TestSetup_setOurs_nonGHFunction · 0.92
TestLoginFunction · 0.92
Test_loginRun_nonttyFunction · 0.92
Test_loginRun_SurveyFunction · 0.92
TestDevelopRunFunction · 0.92
TestIssueView_webFunction · 0.92

Calls 7

findMethod · 0.95
setPrepareCmdFunction · 0.85
cStruct · 0.85
JoinMethod · 0.80
HelperMethod · 0.65
ErrorfMethod · 0.65
StringMethod · 0.45

Tested by 15

Test_deleteRunFunction · 0.74
Test_createRunFunction · 0.74
Test_gitTagInfoFunction · 0.74
TestSetup_setOurs_nonGHFunction · 0.74
TestLoginFunction · 0.74
Test_loginRun_nonttyFunction · 0.74
Test_loginRun_SurveyFunction · 0.74
TestDevelopRunFunction · 0.74
TestIssueView_webFunction · 0.74