Skip to content

Commit a3fbcfd

Browse files
Enable other runners (DAN runner) (#155)
* Enable other runners (DAN runner) We had created a DAN runner previously, but there was no option in the label analysis command to actually select different runners. Another issue is that thwe return of `collect_tests` in a runner needs to be a list, and the DAN runner was not returning that. The changes here should make the DAN runner a viable alternative to the PythonStandardRunner, in case we need that. * Move default value for runner to the runner-name option
1 parent 1779f72 commit a3fbcfd

4 files changed

Lines changed: 67 additions & 10 deletions

File tree

codecov_cli/commands/labelanalysis.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@
3232
cls=CodecovOption,
3333
required=True,
3434
)
35+
@click.option(
36+
"--runner-name", "--runner", "runner_name", help="Runner to use", default="python"
37+
)
3538
@click.pass_context
3639
def label_analysis(
3740
ctx: click.Context,
3841
token: str,
3942
head_commit_sha: str,
4043
base_commit_sha: str,
41-
runner_name: str = "python",
44+
runner_name: str,
4245
):
4346
logger.debug(
4447
"Starting label analysis",

codecov_cli/runners/dan_runner.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import subprocess
23
from typing import List, Optional, Union
34

@@ -10,10 +11,19 @@
1011
class DoAnythingNowConfigParams(dict):
1112
@property
1213
def collect_tests_command(self) -> Union[List[str], str]:
14+
"""
15+
Command to run when collecting tests.
16+
The output of this command needs to be a list of test labels,
17+
one test label per line.
18+
"""
1319
return self.get("collect_tests_command", None)
1420

1521
@property
1622
def process_labelanalysis_result_command(self) -> Union[List[str], str]:
23+
"""
24+
Command to run that handles the label analysis result.
25+
The result will be passed as an argument to the command in JSON format.
26+
"""
1727
return self.get("process_labelanalysis_result_command", None)
1828

1929

@@ -30,12 +40,25 @@ def collect_tests(self) -> List[str]:
3040
raise Exception(
3141
"DAN runner missing 'collect_tests_command' configuration value"
3242
)
33-
return subprocess.run(command, check=True, capture_output=True).stdout.decode()
43+
return list(
44+
subprocess.run(command, check=True, capture_output=True)
45+
.stdout.decode()
46+
.splitlines()
47+
)
3448

3549
def process_labelanalysis_result(self, result: LabelAnalysisRequestResult):
50+
json_result = json.dumps(result)
3651
command = self.params.process_labelanalysis_result_command
3752
if command is None:
3853
raise Exception(
3954
"DAN runner missing 'process_labelanalysis_result_command' configuration value"
4055
)
41-
return subprocess.run(command, check=True, capture_output=True).stdout.decode()
56+
command_list = []
57+
if type(command) == list:
58+
command_list.extend(command)
59+
else:
60+
command_list.append(command)
61+
command_list.append(json_result)
62+
return subprocess.run(
63+
command_list, check=True, capture_output=True
64+
).stdout.decode()

tests/commands/test_invoke_labelanalysis.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ def test_labelanalysis_help(self, mocker, fake_ci_provider):
2121

2222
result = runner.invoke(cli, ["label-analysis", "--help"], obj={})
2323
assert result.exit_code == 0
24+
print(result.output)
2425
assert result.output.split("\n") == [
2526
"Usage: cli label-analysis [OPTIONS]",
2627
"",
2728
"Options:",
28-
" --token TEXT The static analysis token (NOT the same token as upload)",
29-
" [required]",
30-
" --head-sha TEXT Commit SHA (with 40 chars) [required]",
31-
" --base-sha TEXT Commit SHA (with 40 chars) [required]",
32-
" --help Show this message and exit.",
29+
" --token TEXT The static analysis token (NOT the same token as",
30+
" upload) [required]",
31+
" --head-sha TEXT Commit SHA (with 40 chars) [required]",
32+
" --base-sha TEXT Commit SHA (with 40 chars) [required]",
33+
" --runner-name, --runner TEXT Runner to use",
34+
" --help Show this message and exit.",
3335
"",
3436
]
3537

tests/runners/test_dan_runner.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_collect_tests(self, mock_run):
1919
runner = DoAnythingNowRunner(config_options)
2020
assert runner.params == config_options
2121
resp = runner.collect_tests()
22-
assert resp.split() == test_list
22+
assert resp == test_list
2323
mock_run.assert_called_with(
2424
["mycommand", "--option"],
2525
capture_output=True,
@@ -54,7 +54,36 @@ def test_process_labelanalysis_result(self, mock_run):
5454
runner.process_labelanalysis_result(label_analysis_result)
5555
assert runner.params == config_options
5656
mock_run.assert_called_with(
57-
["mycommand", "--option"],
57+
[
58+
"mycommand",
59+
"--option",
60+
'{"present_report_labels": ["test_present"], "absent_labels": ["test_absent"], "present_diff_labels": ["test_in_diff"], "global_level_labels": ["test_global"]}',
61+
],
62+
capture_output=True,
63+
check=True,
64+
)
65+
66+
@patch("codecov_cli.runners.dan_runner.subprocess.run")
67+
def test_process_labelanalysis_result_string_command(self, mock_run):
68+
label_analysis_result = {
69+
"present_report_labels": ["test_present"],
70+
"absent_labels": ["test_absent"],
71+
"present_diff_labels": ["test_in_diff"],
72+
"global_level_labels": ["test_global"],
73+
}
74+
cmd_output = "My command output"
75+
mock_stdout = MagicMock()
76+
mock_stdout.configure_mock(**{"stdout.decode.return_value": cmd_output})
77+
mock_run.return_value = mock_stdout
78+
config_options = {"process_labelanalysis_result_command": "mycommand --option"}
79+
runner = DoAnythingNowRunner(config_options)
80+
runner.process_labelanalysis_result(label_analysis_result)
81+
assert runner.params == config_options
82+
mock_run.assert_called_with(
83+
[
84+
"mycommand --option",
85+
'{"present_report_labels": ["test_present"], "absent_labels": ["test_absent"], "present_diff_labels": ["test_in_diff"], "global_level_labels": ["test_global"]}',
86+
],
5887
capture_output=True,
5988
check=True,
6089
)

0 commit comments

Comments
 (0)