|
5 | 5 | import responses # type: ignore |
6 | 6 |
|
7 | 7 | from launchable.utils.http_client import get_base_url |
| 8 | +from launchable.utils.link import LinkKind |
8 | 9 | from tests.cli_test_case import CliTestCase |
9 | 10 |
|
10 | 11 |
|
@@ -188,3 +189,86 @@ def test_run_session_with_timestamp(self): |
188 | 189 | "testSuite": None, |
189 | 190 | "timestamp": "2023-10-01T12:00:00+00:00", |
190 | 191 | }, payload) |
| 192 | + |
| 193 | + @responses.activate |
| 194 | + @mock.patch.dict(os.environ, { |
| 195 | + "LAUNCHABLE_TOKEN": CliTestCase.launchable_token, |
| 196 | + 'LANG': 'C.UTF-8', |
| 197 | + "GITHUB_PULL_REQUEST_URL": "https://github.com/launchableinc/cli/pull/1", |
| 198 | + }, clear=True) |
| 199 | + def test_run_session_with_links(self): |
| 200 | + # Endpoint to assert |
| 201 | + endpoint = "{}/intake/organizations/{}/workspaces/{}/builds/{}/test_sessions".format( |
| 202 | + get_base_url(), |
| 203 | + self.organization, |
| 204 | + self.workspace, |
| 205 | + self.build_name) |
| 206 | + |
| 207 | + # Capture from environment |
| 208 | + result = self.cli("record", "session", "--build", self.build_name) |
| 209 | + self.assert_success(result) |
| 210 | + payload = json.loads(self.find_request(endpoint, 0).request.body.decode()) |
| 211 | + self.assertEqual([{ |
| 212 | + "kind": LinkKind.GITHUB_PULL_REQUEST.name, |
| 213 | + "title": "", |
| 214 | + "url": "https://github.com/launchableinc/cli/pull/1", |
| 215 | + }], payload["links"]) |
| 216 | + |
| 217 | + # Priority check |
| 218 | + result = self.cli("record", "session", "--build", self.build_name, "--link", |
| 219 | + "GITHUB_PULL_REQUEST|PR=https://github.com/launchableinc/cli/pull/2") |
| 220 | + self.assert_success(result) |
| 221 | + payload = json.loads(self.find_request(endpoint, 1).request.body.decode()) |
| 222 | + self.assertEqual([{ |
| 223 | + "kind": LinkKind.GITHUB_PULL_REQUEST.name, |
| 224 | + "title": "PR", |
| 225 | + "url": "https://github.com/launchableinc/cli/pull/2", |
| 226 | + }], payload["links"]) |
| 227 | + |
| 228 | + # Infer kind |
| 229 | + result = self.cli("record", "session", "--build", self.build_name, "--link", |
| 230 | + "PR=https://github.com/launchableinc/cli/pull/2") |
| 231 | + self.assert_success(result) |
| 232 | + payload = json.loads(self.find_request(endpoint, 2).request.body.decode()) |
| 233 | + self.assertEqual([{ |
| 234 | + "kind": LinkKind.GITHUB_PULL_REQUEST.name, |
| 235 | + "title": "PR", |
| 236 | + "url": "https://github.com/launchableinc/cli/pull/2", |
| 237 | + }], payload["links"]) |
| 238 | + |
| 239 | + # Explicit kind |
| 240 | + result = self.cli("record", "session", "--build", self.build_name, "--link", |
| 241 | + "GITHUB_PULL_REQUEST|PR=https://github.com/launchableinc/cli/pull/2") |
| 242 | + self.assert_success(result) |
| 243 | + payload = json.loads(self.find_request(endpoint, 3).request.body.decode()) |
| 244 | + self.assertEqual([{ |
| 245 | + "kind": LinkKind.GITHUB_PULL_REQUEST.name, |
| 246 | + "title": "PR", |
| 247 | + "url": "https://github.com/launchableinc/cli/pull/2", |
| 248 | + }], payload["links"]) |
| 249 | + |
| 250 | + # Multiple kinds |
| 251 | + result = self.cli("record", "session", "--build", self.build_name, "--link", |
| 252 | + "GITHUB_ACTIONS|=https://github.com/launchableinc/mothership/actions/runs/3747451612") |
| 253 | + self.assert_success(result) |
| 254 | + payload = json.loads(self.find_request(endpoint, 4).request.body.decode()) |
| 255 | + self.assertEqual([{ |
| 256 | + "kind": LinkKind.GITHUB_ACTIONS.name, |
| 257 | + "title": "", |
| 258 | + "url": "https://github.com/launchableinc/mothership/actions/runs/3747451612", |
| 259 | + }, |
| 260 | + { |
| 261 | + "kind": LinkKind.GITHUB_PULL_REQUEST.name, |
| 262 | + "title": "", |
| 263 | + "url": "https://github.com/launchableinc/cli/pull/1", |
| 264 | + }], payload["links"]) |
| 265 | + |
| 266 | + # Invalid kind |
| 267 | + result = self.cli("record", "session", "--build", self.build_name, "--link", |
| 268 | + "UNKNOWN_KIND|PR=https://github.com/launchableinc/cli/pull/2") |
| 269 | + self.assertIn("Invalid kind 'UNKNOWN_KIND' passed to --link option", result.output) |
| 270 | + |
| 271 | + # Invalid URL |
| 272 | + result = self.cli("record", "session", "--build", self.build_name, "--link", |
| 273 | + "GITHUB_PULL_REQUEST|PR=https://github.com/launchableinc/cli/pull/2/files") |
| 274 | + self.assertIn("Invalid url 'https://github.com/launchableinc/cli/pull/2/files' passed to --link option", result.output) |
0 commit comments