StubFinderForRunCommandStyleTests is the NewMockFinder substitute to be used ONLY in runCommand-style tests.
(t *testing.T, selector string, pr *api.PullRequest, repo ghrepo.Interface)
| 76 | |
| 77 | // StubFinderForRunCommandStyleTests is the NewMockFinder substitute to be used ONLY in runCommand-style tests. |
| 78 | func StubFinderForRunCommandStyleTests(t *testing.T, selector string, pr *api.PullRequest, repo ghrepo.Interface) *mockFinder { |
| 79 | // Create a new mock finder and override the "runCommandFinder" variable so that calls to |
| 80 | // NewFinder() will return this mock. This is a bad pattern, and a result of old style runCommand |
| 81 | // tests that would ideally be replaced. The reason we need to do this is that the runCommand style tests |
| 82 | // construct the cobra command via NewCmd* functions, and then Execute them directly, providing no opportunity |
| 83 | // to inject a test double unless it's on the factory, which finder never is, because only PR commands need it. |
| 84 | finder := NewMockFinder(selector, pr, repo) |
| 85 | finderForRunCommandStyleTests = finder |
| 86 | |
| 87 | // Ensure that at the end of the test, we reset the "runCommandFinder" variable so that tests are isolated, |
| 88 | // at least if they are run sequentially. |
| 89 | t.Cleanup(func() { |
| 90 | finderForRunCommandStyleTests = nil |
| 91 | }) |
| 92 | return finder |
| 93 | } |
| 94 | |
| 95 | type FindOptions struct { |
| 96 | // Selector can be a number with optional `#` prefix, a branch name with optional `<owner>:` prefix, or |