| 116 | } |
| 117 | |
| 118 | func TestCreateRun(t *testing.T) { |
| 119 | tests := []struct { |
| 120 | name string |
| 121 | opts *createOptions |
| 122 | stubCreator stubAutolinkCreator |
| 123 | expectedErr error |
| 124 | errMsg string |
| 125 | wantStdout string |
| 126 | wantStderr string |
| 127 | }{ |
| 128 | { |
| 129 | name: "success, alphanumeric", |
| 130 | opts: &createOptions{ |
| 131 | KeyPrefix: "TICKET-", |
| 132 | URLTemplate: "https://example.com/TICKET?query=<num>", |
| 133 | }, |
| 134 | stubCreator: stubAutolinkCreator{}, |
| 135 | wantStdout: "✓ Created repository autolink 1 on OWNER/REPO\n", |
| 136 | }, |
| 137 | { |
| 138 | name: "success, numeric", |
| 139 | opts: &createOptions{ |
| 140 | KeyPrefix: "TICKET-", |
| 141 | URLTemplate: "https://example.com/TICKET?query=<num>", |
| 142 | Numeric: true, |
| 143 | }, |
| 144 | stubCreator: stubAutolinkCreator{}, |
| 145 | wantStdout: "✓ Created repository autolink 1 on OWNER/REPO\n", |
| 146 | }, |
| 147 | { |
| 148 | name: "client error", |
| 149 | opts: &createOptions{ |
| 150 | KeyPrefix: "TICKET-", |
| 151 | URLTemplate: "https://example.com/TICKET?query=<num>", |
| 152 | }, |
| 153 | stubCreator: stubAutolinkCreator{err: testAutolinkClientCreateError{}}, |
| 154 | expectedErr: testAutolinkClientCreateError{}, |
| 155 | errMsg: fmt.Sprint("error creating autolink: ", testAutolinkClientCreateError{}.Error()), |
| 156 | }, |
| 157 | } |
| 158 | |
| 159 | for _, tt := range tests { |
| 160 | t.Run(tt.name, func(t *testing.T) { |
| 161 | ios, _, stdout, stderr := iostreams.Test() |
| 162 | |
| 163 | opts := tt.opts |
| 164 | opts.IO = ios |
| 165 | opts.Browser = &browser.Stub{} |
| 166 | |
| 167 | opts.IO = ios |
| 168 | opts.BaseRepo = func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil } |
| 169 | |
| 170 | opts.AutolinkClient = &tt.stubCreator |
| 171 | err := createRun(opts) |
| 172 | |
| 173 | if tt.expectedErr != nil { |
| 174 | require.Error(t, err) |
| 175 | assert.ErrorIs(t, err, tt.expectedErr) |