|
1 | 1 | import json
|
| 2 | +from pathlib import Path |
2 | 3 |
|
3 | 4 | import click
|
4 | 5 | import pytest
|
@@ -122,10 +123,13 @@ def test_labelanalysis_help(self, mocker, fake_ci_provider):
|
122 | 123 | " --max-wait-time INTEGER Max time (in seconds) to wait for the label",
|
123 | 124 | " analysis result before falling back to running",
|
124 | 125 | " all tests. Default is to wait forever.",
|
125 |
| - " --dry-run Userful during setup. This will run the label", |
126 |
| - " analysis, but will print the result to stdout", |
127 |
| - " and terminate instead of calling the", |
128 |
| - " runner.process_labelanalysis_result", |
| 126 | + " --dry-run Print list of tests to run and options that need", |
| 127 | + " to be added to the test runner as a space-", |
| 128 | + " separated list to stdout. Format is", |
| 129 | + ' ATS_TESTS_TO_RUN="<options> <test_1> <test_2>', |
| 130 | + ' ... <test_n>"', |
| 131 | + " --dry-run-output-path PATH Prints the dry-run list into dry_run_output_path", |
| 132 | + " (in addition to stdout)", |
129 | 133 | " -h, --help Show this message and exit.",
|
130 | 134 | "",
|
131 | 135 | ]
|
@@ -258,21 +262,90 @@ def test_invoke_label_analysis_dry_run(self, get_labelanalysis_deps, mocker):
|
258 | 262 | json={"state": "finished", "result": label_analysis_result},
|
259 | 263 | )
|
260 | 264 | cli_runner = CliRunner()
|
261 |
| - result = cli_runner.invoke( |
262 |
| - cli, |
263 |
| - [ |
264 |
| - "label-analysis", |
265 |
| - "--token=STATIC_TOKEN", |
266 |
| - f"--base-sha={FAKE_BASE_SHA}", |
267 |
| - "--dry-run", |
| 265 | + with cli_runner.isolated_filesystem(): |
| 266 | + result = cli_runner.invoke( |
| 267 | + cli, |
| 268 | + [ |
| 269 | + "label-analysis", |
| 270 | + "--token=STATIC_TOKEN", |
| 271 | + f"--base-sha={FAKE_BASE_SHA}", |
| 272 | + "--dry-run", |
| 273 | + ], |
| 274 | + obj={}, |
| 275 | + ) |
| 276 | + mock_get_runner.assert_called() |
| 277 | + fake_runner.process_labelanalysis_result.assert_not_called() |
| 278 | + print(result.output) |
| 279 | + assert result.exit_code == 0 |
| 280 | + assert ( |
| 281 | + 'ATS_TESTS_TO_RUN="--labels test_absent test_global test_in_diff' |
| 282 | + in result.output |
| 283 | + ) |
| 284 | + |
| 285 | + def test_invoke_label_analysis_dry_run_with_output_path( |
| 286 | + self, get_labelanalysis_deps, mocker |
| 287 | + ): |
| 288 | + mock_get_runner = get_labelanalysis_deps["mock_get_runner"] |
| 289 | + fake_runner = get_labelanalysis_deps["fake_runner"] |
| 290 | + |
| 291 | + label_analysis_result = { |
| 292 | + "present_report_labels": ["test_present"], |
| 293 | + "absent_labels": ["test_absent"], |
| 294 | + "present_diff_labels": ["test_in_diff"], |
| 295 | + "global_level_labels": ["test_global"], |
| 296 | + } |
| 297 | + |
| 298 | + with responses.RequestsMock() as rsps: |
| 299 | + rsps.add( |
| 300 | + responses.POST, |
| 301 | + "https://api.codecov.io/labels/labels-analysis", |
| 302 | + json={"external_id": "label-analysis-request-id"}, |
| 303 | + status=201, |
| 304 | + match=[ |
| 305 | + matchers.header_matcher({"Authorization": "Repotoken STATIC_TOKEN"}) |
268 | 306 | ],
|
269 |
| - obj={}, |
270 | 307 | )
|
271 |
| - mock_get_runner.assert_called() |
272 |
| - fake_runner.process_labelanalysis_result.assert_not_called() |
| 308 | + rsps.add( |
| 309 | + responses.PATCH, |
| 310 | + "https://api.codecov.io/labels/labels-analysis/label-analysis-request-id", |
| 311 | + json={"external_id": "label-analysis-request-id"}, |
| 312 | + status=201, |
| 313 | + match=[ |
| 314 | + matchers.header_matcher({"Authorization": "Repotoken STATIC_TOKEN"}) |
| 315 | + ], |
| 316 | + ) |
| 317 | + rsps.add( |
| 318 | + responses.GET, |
| 319 | + "https://api.codecov.io/labels/labels-analysis/label-analysis-request-id", |
| 320 | + json={"state": "finished", "result": label_analysis_result}, |
| 321 | + ) |
| 322 | + cli_runner = CliRunner() |
| 323 | + with cli_runner.isolated_filesystem(): |
| 324 | + result = cli_runner.invoke( |
| 325 | + cli, |
| 326 | + [ |
| 327 | + "label-analysis", |
| 328 | + "--token=STATIC_TOKEN", |
| 329 | + f"--base-sha={FAKE_BASE_SHA}", |
| 330 | + "--dry-run", |
| 331 | + "--dry-run-output-path=ats_output_path", |
| 332 | + ], |
| 333 | + obj={}, |
| 334 | + ) |
| 335 | + labels_file = Path("ats_output_path") |
| 336 | + assert labels_file.exists() and labels_file.is_file() |
| 337 | + with open(labels_file, "r") as fd: |
| 338 | + assert fd.readlines() == [ |
| 339 | + "--labels test_absent test_global test_in_diff\n" |
| 340 | + ] |
| 341 | + mock_get_runner.assert_called() |
| 342 | + fake_runner.process_labelanalysis_result.assert_not_called() |
273 | 343 | print(result.output)
|
274 | 344 | assert result.exit_code == 0
|
275 |
| - assert json.dumps(label_analysis_result) in result.output |
| 345 | + assert ( |
| 346 | + 'ATS_TESTS_TO_RUN="--labels test_absent test_global test_in_diff' |
| 347 | + in result.output |
| 348 | + ) |
276 | 349 |
|
277 | 350 | def test_fallback_to_collected_labels(self, mocker):
|
278 | 351 | mock_runner = mocker.MagicMock()
|
@@ -367,7 +440,9 @@ def test_fallback_dry_run(self, get_labelanalysis_deps, mocker, use_verbose_opti
|
367 | 440 | "absent_labels": collected_labels,
|
368 | 441 | "present_diff_labels": [],
|
369 | 442 | "global_level_labels": [],
|
370 |
| - } |
| 443 | + }, |
| 444 | + fake_runner, |
| 445 | + None, |
371 | 446 | )
|
372 | 447 | assert result.exit_code == 0
|
373 | 448 |
|
|
0 commit comments