Skip to content

Commit 2042771

Browse files
PYSCAN-9 Add search for config files (#20)
1 parent d360078 commit 2042771

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

src/py_sonar_scanner/configuration.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ def __init__(self):
3737

3838
def setup(self) -> None:
3939
"""This is executed when run from the command line"""
40-
scan_arguments = sys.argv[1:]
41-
scan_arguments.extend(self._read_toml_args())
42-
43-
self.scan_arguments = scan_arguments
40+
self.scan_arguments = sys.argv[1:]
41+
self.scan_arguments.extend(self._read_toml_args())
4442

4543
def _read_toml_args(self) -> list[str]:
4644
scan_arguments: list[str] = []
@@ -64,9 +62,39 @@ def _add_parameter_to_scanner_args(self, scan_arguments: list[str], key: str, va
6462
self._add_parameter_to_scanner_args(scan_arguments, f"{key}.{k}", v)
6563

6664
def _read_toml_file(self) -> dict:
67-
if not os.path.isfile("pyproject.toml"):
65+
toml_file_path = self._get_toml_file_path()
66+
if not os.path.isfile(toml_file_path):
6867
return {}
69-
with open("pyproject.toml", "r") as file:
68+
with open(toml_file_path, "r") as file:
7069
# TODO: actually search for pyproject.toml
7170
toml_data = file.read()
7271
return toml.loads(toml_data)
72+
73+
def _get_toml_file_path(self) -> str:
74+
args = self._get_arguments_dict()
75+
if "-Dtoml.path" in args:
76+
return args["-Dtoml.path"]
77+
elif "-Dproject.home" in args:
78+
return os.path.join(args["-Dproject.home"], "pyproject.toml")
79+
else:
80+
return os.path.join(os.curdir, "pyproject.toml")
81+
82+
def _get_arguments_dict(self):
83+
args = self.scan_arguments
84+
arguments_dict = {}
85+
i = 0
86+
while i < len(args):
87+
current_arg = args[i]
88+
89+
if "=" in current_arg:
90+
arg_parts = current_arg.split("=", 1)
91+
arguments_dict[arg_parts[0]] = arg_parts[1]
92+
else:
93+
if i + 1 < len(args) and "=" not in args[i + 1]:
94+
arguments_dict[current_arg] = args[i + 1]
95+
i += 1
96+
else:
97+
arguments_dict[current_arg] = None
98+
99+
i += 1
100+
return arguments_dict

tests/resources/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[tool.sonar]
2+
a="b"
3+
c="d"

tests/test_configuration.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
#
2020
import unittest
21-
from unittest.mock import patch, Mock
21+
from unittest.mock import patch
2222
from py_sonar_scanner.configuration import Configuration
2323

2424

2525
class TestConfiguration(unittest.TestCase):
2626
@patch("py_sonar_scanner.configuration.sys")
27-
def test_argument_parsing_empty_toml(self, mock_sys):
27+
def test_argument_parsing(self, mock_sys):
2828
configuration = Configuration()
29-
configuration._read_toml_args = Mock(return_value=[])
3029

3130
mock_sys.argv = ["path/to/scanner/py-sonar-scanner"]
3231
configuration.setup()
@@ -47,3 +46,23 @@ def test_argument_parsing_empty_toml(self, mock_sys):
4746
self.assertListEqual(
4847
configuration.scan_arguments, ["-DSomeJVMArg", "-DAnotherJVMArg", "-dNotAJVMArg", "-SomeNonsense"]
4948
)
49+
50+
mock_sys.argv = ["path/to/scanner/py-sonar-scanner", "-Dtoml.path=tests/resources/pyproject.toml"]
51+
configuration.setup()
52+
self.assertListEqual(
53+
configuration.scan_arguments, ["-Dtoml.path=tests/resources/pyproject.toml", "-Dsonar.a=b", "-Dsonar.c=d"]
54+
)
55+
56+
mock_sys.argv = ["path/to/scanner/py-sonar-scanner", "-Dproject.home=tests/resources/"]
57+
configuration.setup()
58+
self.assertListEqual(
59+
configuration.scan_arguments, ["-Dproject.home=tests/resources/", "-Dsonar.a=b", "-Dsonar.c=d"]
60+
)
61+
62+
mock_sys.argv = ["path/to/scanner/py-sonar-scanner", "-Dproject.home=tests2"]
63+
configuration.setup()
64+
self.assertListEqual(configuration.scan_arguments, ["-Dproject.home=tests2"])
65+
66+
mock_sys.argv = ["path/to/scanner/py-sonar-scanner", "-Dproject.home=tests=2"]
67+
configuration.setup()
68+
self.assertListEqual(configuration.scan_arguments, ["-Dproject.home=tests=2"])

0 commit comments

Comments
 (0)