Skip to content

Commit 6f25157

Browse files
authored
SCANPY-161 add Python report related properties to cli (#178)
1 parent 887ee96 commit 6f25157

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

src/pysonar_scanner/configuration/cli.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,12 +413,14 @@ def __parse_cli_args(cls) -> tuple[argparse.Namespace, list[str]]:
413413
)
414414
parser.add_argument(
415415
"--sonar-python-pylint-report-path",
416+
"--pylint-report-path",
416417
"-Dsonar.python.pylint.reportPath",
417418
type=str,
418419
help="Path to third-parties issues report file for pylint",
419420
)
420421
parser.add_argument(
421422
"--sonar-python-coverage-report-paths",
423+
"--coverage-report-paths",
422424
"-Dsonar.python.coverage.reportPaths",
423425
type=str,
424426
help="Comma-delimited list of paths to coverage reports in the Cobertura XML format.",
@@ -436,12 +438,14 @@ def __parse_cli_args(cls) -> tuple[argparse.Namespace, list[str]]:
436438
)
437439
parser.add_argument(
438440
"--sonar-python-xunit-report-path",
441+
"--xunit-report-path",
439442
"-Dsonar.python.xunit.reportPath",
440443
type=str,
441444
help="Path to the report of test execution, relative to project's root",
442445
)
443446
parser.add_argument(
444447
"--sonar-python-xunit-skip-details",
448+
"--xunit-skip-details",
445449
type=bool,
446450
action=argparse.BooleanOptionalAction,
447451
help="When enabled, the test execution statistics is provided only on project level",
@@ -451,6 +455,34 @@ def __parse_cli_args(cls) -> tuple[argparse.Namespace, list[str]]:
451455
type=bool,
452456
help="Equivalent to -Dsonar.python.xunit.skipDetails",
453457
)
458+
parser.add_argument(
459+
"--sonar-python-mypy-report-paths",
460+
"--mypy-report-paths",
461+
"-Dsonar.python.mypy.reportPaths",
462+
type=str,
463+
help="Comma-separated mypy report paths, relative to project's root",
464+
)
465+
parser.add_argument(
466+
"--sonar-python-bandit-report-paths",
467+
"--bandit-report-paths",
468+
"-Dsonar.python.bandit.reportPaths",
469+
type=str,
470+
help="Comma-separated bandit report paths, relative to project's root",
471+
)
472+
parser.add_argument(
473+
"--sonar-python-flake8-report-paths",
474+
"--flake8-report-paths",
475+
"-Dsonar.python.flake8.reportPaths",
476+
type=str,
477+
help="Comma-separated flake8 report paths, relative to project's root",
478+
)
479+
parser.add_argument(
480+
"--sonar-python-ruff-report-paths",
481+
"--ruff-report-paths",
482+
"-Dsonar.python.ruff.reportPaths",
483+
type=str,
484+
help="Comma-separated ruff report paths, relative to project's root",
485+
)
454486
parser.add_argument(
455487
"--sonar-modules", "-Dsonar.modules", type=str, help="Comma-delimited list of modules to analyze"
456488
)

src/pysonar_scanner/configuration/properties.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@
9393
SONAR_MODULES: Key = "sonar.modules"
9494
SONAR_PYTHON_XUNIT_REPORT_PATH = "sonar.python.xunit.reportPath"
9595
SONAR_PYTHON_XUNIT_SKIP_DETAILS = "sonar.python.xunit.skipDetails"
96+
SONAR_PYTHON_MYPY_REPORT_PATHS = "sonar.python.mypy.reportPaths"
97+
SONAR_PYTHON_BANDIT_REPORT_PATHS = "sonar.python.bandit.reportPaths"
98+
SONAR_PYTHON_FLAKE8_REPORT_PATHS = "sonar.python.flake8.reportPaths"
99+
SONAR_PYTHON_RUFF_REPORT_PATHS = "sonar.python.ruff.reportPaths"
96100
TOML_PATH: Key = "toml-path"
97101

98102

@@ -484,6 +488,26 @@ def env_variable_name(self) -> str:
484488
default_value=None,
485489
cli_getter=lambda args: args.sonar_python_xunit_skip_details or getattr(args, "Dsonar.python.xunit.skipDetails")
486490
),
491+
Property(
492+
name=SONAR_PYTHON_MYPY_REPORT_PATHS,
493+
default_value=None,
494+
cli_getter=lambda args: args.sonar_python_mypy_report_paths
495+
),
496+
Property(
497+
name=SONAR_PYTHON_BANDIT_REPORT_PATHS,
498+
default_value=None,
499+
cli_getter=lambda args: args.sonar_python_bandit_report_paths
500+
),
501+
Property(
502+
name=SONAR_PYTHON_FLAKE8_REPORT_PATHS,
503+
default_value=None,
504+
cli_getter=lambda args: args.sonar_python_flake8_report_paths
505+
),
506+
Property(
507+
name=SONAR_PYTHON_RUFF_REPORT_PATHS,
508+
default_value=None,
509+
cli_getter=lambda args: args.sonar_python_ruff_report_paths
510+
),
487511
Property(
488512
name=SONAR_MODULES,
489513
default_value=None,

tests/unit/test_configuration_cli.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
from pysonar_scanner.configuration.configuration_loader import CliConfigurationLoader
2626
from pysonar_scanner.configuration.properties import (
2727
SONAR_HOST_URL,
28+
SONAR_PYTHON_BANDIT_REPORT_PATHS,
29+
SONAR_PYTHON_FLAKE8_REPORT_PATHS,
30+
SONAR_PYTHON_MYPY_REPORT_PATHS,
31+
SONAR_PYTHON_RUFF_REPORT_PATHS,
2832
SONAR_REGION,
2933
SONAR_SCANNER_API_BASE_URL,
3034
SONAR_SCANNER_ARCH,
@@ -151,6 +155,10 @@
151155
SONAR_PYTHON_SKIP_UNCHANGED: True,
152156
SONAR_PYTHON_XUNIT_REPORT_PATH: "path/to/xunit/report",
153157
SONAR_PYTHON_XUNIT_SKIP_DETAILS: True,
158+
SONAR_PYTHON_MYPY_REPORT_PATHS: "path/to/mypy/reports",
159+
SONAR_PYTHON_BANDIT_REPORT_PATHS: "path/to/bandit/reports",
160+
SONAR_PYTHON_FLAKE8_REPORT_PATHS: "path/to/flake8/reports",
161+
SONAR_PYTHON_RUFF_REPORT_PATHS: "path/to/ruff/reports",
154162
SONAR_MODULES: "module1,module2",
155163
}
156164

@@ -184,6 +192,43 @@ def test_alternative_cli_args(self):
184192
}
185193
self.assertDictEqual(configuration, expected_configuration)
186194

195+
def test_alternative_report_cli_args(self):
196+
base_args = ["myscript.py", "-t", "myToken", "--sonar-project-key", "myProjectKey"]
197+
report_args = [
198+
"--bandit-report-paths",
199+
"path/to/bandit/reports",
200+
"--flake8-report-paths",
201+
"path/to/flake8/reports",
202+
"--mypy-report-paths",
203+
"path/to/mypy/reports",
204+
"--pylint-report-path",
205+
"path/to/pylint/report",
206+
"--coverage-report-paths",
207+
"path/to/coverage/reports",
208+
"--xunit-report-path",
209+
"path/to/xunit/report",
210+
"--ruff-report-paths",
211+
"path/to/ruff/reports",
212+
"--xunit-skip-details",
213+
]
214+
215+
expected_configuration = {
216+
SONAR_TOKEN: "myToken",
217+
SONAR_PROJECT_KEY: "myProjectKey",
218+
SONAR_PYTHON_BANDIT_REPORT_PATHS: "path/to/bandit/reports",
219+
SONAR_PYTHON_FLAKE8_REPORT_PATHS: "path/to/flake8/reports",
220+
SONAR_PYTHON_MYPY_REPORT_PATHS: "path/to/mypy/reports",
221+
SONAR_PYTHON_PYLINT_REPORT_PATH: "path/to/pylint/report",
222+
SONAR_PYTHON_COVERAGE_REPORT_PATHS: "path/to/coverage/reports",
223+
SONAR_PYTHON_XUNIT_REPORT_PATH: "path/to/xunit/report",
224+
SONAR_PYTHON_RUFF_REPORT_PATHS: "path/to/ruff/reports",
225+
SONAR_PYTHON_XUNIT_SKIP_DETAILS: True,
226+
}
227+
228+
with patch("sys.argv", base_args + report_args), patch("sys.stderr", new=StringIO()):
229+
configuration = CliConfigurationLoader.load()
230+
self.assertDictEqual(configuration, expected_configuration)
231+
187232
def test_multiple_alias_cli_args(self):
188233
alternatives = [
189234
["-t", "overwrittenToken", "--sonar-token", "sonarToken"],
@@ -328,6 +373,14 @@ def test_impossible_os_choice(self):
328373
"--sonar-python-xunit-report-path",
329374
"path/to/xunit/report",
330375
"--sonar-python-xunit-skip-details",
376+
"--sonar-python-mypy-report-paths",
377+
"path/to/mypy/reports",
378+
"--sonar-python-bandit-report-paths",
379+
"path/to/bandit/reports",
380+
"--sonar-python-flake8-report-paths",
381+
"path/to/flake8/reports",
382+
"--sonar-python-ruff-report-paths",
383+
"path/to/ruff/reports",
331384
"--sonar-modules",
332385
"module1,module2",
333386
],
@@ -401,6 +454,10 @@ def test_all_cli_args(self):
401454
"-Dsonar.python.skipUnchanged=true",
402455
"-Dsonar.python.xunit.reportPath=path/to/xunit/report",
403456
"-Dsonar.python.xunit.skipDetails=true",
457+
"-Dsonar.python.mypy.reportPaths=path/to/mypy/reports",
458+
"-Dsonar.python.bandit.reportPaths=path/to/bandit/reports",
459+
"-Dsonar.python.flake8.reportPaths=path/to/flake8/reports",
460+
"-Dsonar.python.ruff.reportPaths=path/to/ruff/reports",
404461
"-Dsonar.modules=module1,module2",
405462
],
406463
)

0 commit comments

Comments
 (0)