Skip to content

Commit 98bb670

Browse files
author
Jeremi Do Dinh
authored
PYSCAN-31 Correct Configuration to look for the table instead of the table in pyproject.toml (#17)
1 parent 547822a commit 98bb670

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/py_sonar_scanner/configuration.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from __future__ import annotations
2121
import os
2222
import sys
23-
from typing import Union
23+
from typing import Union, AnyStr
2424
import toml
2525

2626

@@ -47,16 +47,12 @@ def setup(self) -> None:
4747
def _read_toml_args(self) -> list[str]:
4848
scan_arguments: list[str] = []
4949
try:
50-
if os.path.isfile("pyproject.toml"):
51-
with open("pyproject.toml", "r") as file:
52-
# TODO: actually search for pyproject.toml
53-
toml_data = file.read()
54-
parsed_data = toml.loads(toml_data)
55-
print(parsed_data)
56-
if "sonar" in parsed_data:
57-
sonar_properties = parsed_data["sonar"]
58-
for key, value in sonar_properties.items():
59-
self._add_parameter_to_scanner_args(scan_arguments, key, value)
50+
parsed_data = self._read_toml_file()
51+
if ("tool" not in parsed_data) or ("sonar" not in parsed_data["tool"]):
52+
return scan_arguments
53+
sonar_properties = parsed_data["tool"]["sonar"]
54+
for key, value in sonar_properties.items():
55+
self._add_parameter_to_scanner_args(scan_arguments, key, value)
6056
except BaseException as e:
6157
print(e)
6258
return scan_arguments
@@ -67,3 +63,11 @@ def _add_parameter_to_scanner_args(self, scan_arguments: list[str], key: str, va
6763
if isinstance(value, dict):
6864
for k, v in value.items():
6965
self._add_parameter_to_scanner_args(scan_arguments, f"{key}.{k}", v)
66+
67+
def _read_toml_file(self) -> dict:
68+
if not os.path.isfile("pyproject.toml"):
69+
return {}
70+
with open("pyproject.toml", "r") as file:
71+
# TODO: actually search for pyproject.toml
72+
toml_data = file.read()
73+
return toml.loads(toml_data)

0 commit comments

Comments
 (0)