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.
()
| 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. |
| 22 | func 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 | |
| 53 | func setPrepareCmd(fn func(*exec.Cmd) Runnable) func() { |
| 54 | origPrepare := PrepareCmd |