Skip to content

Commit f36016f

Browse files
Avoid keeping business logic in __init__.py
1 parent 5a4e448 commit f36016f

File tree

6 files changed

+71
-52
lines changed

6 files changed

+71
-52
lines changed

src/pysonar_scanner/__main__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
#
2020

21-
from pysonar_scanner import app_logging, cache
22-
from pysonar_scanner import configuration
21+
from pysonar_scanner import app_logging
22+
from pysonar_scanner import cache
2323
from pysonar_scanner.api import get_base_urls, SonarQubeApi
24-
from pysonar_scanner.configuration import ConfigurationLoader
24+
from pysonar_scanner.configuration import configuration_loader
25+
from pysonar_scanner.configuration.configuration_loader import ConfigurationLoader
2526
from pysonar_scanner.configuration.properties import SONAR_VERBOSE
2627
from pysonar_scanner.scannerengine import ScannerEngine
2728

@@ -43,6 +44,6 @@ def set_logging_options(config):
4344

4445

4546
def __build_api(config: dict[str, any]) -> SonarQubeApi:
46-
token = configuration.get_token(config)
47+
token = configuration_loader.get_token(config)
4748
base_urls = get_base_urls(config)
4849
return SonarQubeApi(base_urls, token)

src/pysonar_scanner/configuration/__init__.py

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,3 @@
1717
# along with this program; if not, write to the Free Software Foundation,
1818
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
#
20-
from pathlib import Path
21-
22-
from pysonar_scanner.configuration import properties
23-
from pysonar_scanner.configuration.cli import CliConfigurationLoader
24-
from pysonar_scanner.configuration.properties import SONAR_TOKEN, SONAR_PROJECT_BASE_DIR, Key
25-
from pysonar_scanner.configuration import properties, sonar_project_properties, toml_file
26-
27-
from pysonar_scanner.exceptions import MissingKeyException
28-
29-
30-
def get_static_default_properties() -> dict[Key, any]:
31-
return {prop.name: prop.default_value for prop in properties.PROPERTIES if prop.default_value is not None}
32-
33-
34-
class ConfigurationLoader:
35-
@staticmethod
36-
def load() -> dict[Key, any]:
37-
# each property loader is required to return NO default values.
38-
# E.g. if no property has been set, an empty dict must be returned.
39-
# Default values should be set through the get_static_default_properties() method
40-
resolved_properties = get_static_default_properties()
41-
cli_properties = CliConfigurationLoader.load()
42-
# CLI properties have a higher priority than properties file,
43-
# but we need to resolve them first to load the properties file
44-
base_dir = Path(cli_properties.get(SONAR_PROJECT_BASE_DIR, "."))
45-
resolved_properties.update(sonar_project_properties.load(base_dir))
46-
47-
toml_path_property = cli_properties.get("toml-path", ".")
48-
toml_dir = Path(toml_path_property) if "toml-path" in cli_properties else base_dir
49-
resolved_properties.update(toml_file.load(toml_dir))
50-
51-
resolved_properties.update(cli_properties)
52-
return resolved_properties
53-
54-
55-
def get_token(config: dict[Key, any]) -> str:
56-
if SONAR_TOKEN not in config:
57-
raise MissingKeyException(f'Missing property "{SONAR_TOKEN}"')
58-
return config[SONAR_TOKEN]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#
2+
# Sonar Scanner Python
3+
# Copyright (C) 2011-2024 SonarSource SA.
4+
# mailto:info AT sonarsource DOT com
5+
#
6+
# This program is free software; you can redistribute it and/or
7+
# modify it under the terms of the GNU Lesser General Public
8+
# License as published by the Free Software Foundation; either
9+
# version 3 of the License, or (at your option) any later version.
10+
# This program is distributed in the hope that it will be useful,
11+
#
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
# Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with this program; if not, write to the Free Software Foundation,
18+
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
#
20+
from pathlib import Path
21+
22+
from pysonar_scanner.configuration.cli import CliConfigurationLoader
23+
from pysonar_scanner.configuration.properties import SONAR_TOKEN, SONAR_PROJECT_BASE_DIR, Key
24+
from pysonar_scanner.configuration.properties import PROPERTIES
25+
from pysonar_scanner.configuration import sonar_project_properties, pyproject_toml
26+
27+
from pysonar_scanner.exceptions import MissingKeyException
28+
29+
30+
def get_static_default_properties() -> dict[Key, any]:
31+
return {prop.name: prop.default_value for prop in PROPERTIES if prop.default_value is not None}
32+
33+
34+
class ConfigurationLoader:
35+
@staticmethod
36+
def load() -> dict[Key, any]:
37+
# each property loader is required to return NO default values.
38+
# E.g. if no property has been set, an empty dict must be returned.
39+
# Default values should be set through the get_static_default_properties() method
40+
resolved_properties = get_static_default_properties()
41+
cli_properties = CliConfigurationLoader.load()
42+
# CLI properties have a higher priority than properties file,
43+
# but we need to resolve them first to load the properties file
44+
base_dir = Path(cli_properties.get(SONAR_PROJECT_BASE_DIR, "."))
45+
resolved_properties.update(sonar_project_properties.load(base_dir))
46+
47+
toml_path_property = cli_properties.get("toml-path", ".")
48+
toml_dir = Path(toml_path_property) if "toml-path" in cli_properties else base_dir
49+
resolved_properties.update(pyproject_toml.load(toml_dir))
50+
51+
resolved_properties.update(cli_properties)
52+
return resolved_properties
53+
54+
55+
def get_token(config: dict[Key, any]) -> str:
56+
if SONAR_TOKEN not in config:
57+
raise MissingKeyException(f'Missing property "{SONAR_TOKEN}"')
58+
return config[SONAR_TOKEN]

tests/test_configuration_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from unittest.mock import patch
2323
from io import StringIO
2424

25-
from pysonar_scanner.configuration import CliConfigurationLoader
25+
from pysonar_scanner.configuration.configuration_loader import CliConfigurationLoader
2626
from pysonar_scanner.configuration.properties import (
2727
SONAR_HOST_URL,
2828
SONAR_REGION,

tests/test_configuration.py renamed to tests/test_configuration_loader.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import pyfakefs.fake_filesystem_unittest as pyfakefs
2424

25-
from pysonar_scanner import configuration
25+
from pysonar_scanner.configuration import configuration_loader
2626
from pysonar_scanner.configuration.properties import (
2727
SONAR_PROJECT_KEY,
2828
SONAR_PROJECT_NAME,
@@ -43,7 +43,7 @@
4343
SONAR_VERBOSE,
4444
TOML_PATH,
4545
)
46-
from pysonar_scanner.configuration import ConfigurationLoader, SONAR_PROJECT_BASE_DIR
46+
from pysonar_scanner.configuration.configuration_loader import ConfigurationLoader, SONAR_PROJECT_BASE_DIR
4747
from pysonar_scanner.exceptions import MissingKeyException
4848

4949

@@ -72,18 +72,18 @@ def test_defaults(self):
7272
}
7373
self.assertDictEqual(configuration, expected_configuration)
7474

75-
@patch("pysonar_scanner.configuration.get_static_default_properties", return_value={})
75+
@patch("pysonar_scanner.configuration.configuration_loader.get_static_default_properties", return_value={})
7676
@patch("sys.argv", ["myscript.py"])
7777
def test_no_defaults_in_configuration_loaders(self, get_static_default_properties_mock):
7878
config = ConfigurationLoader.load()
7979
self.assertDictEqual(config, {})
8080

8181
def test_get_token(self):
8282
with self.subTest("Token is present"):
83-
self.assertEqual(configuration.get_token({SONAR_TOKEN: "myToken"}), "myToken")
83+
self.assertEqual(configuration_loader.get_token({SONAR_TOKEN: "myToken"}), "myToken")
8484

8585
with self.subTest("Token is absent"), self.assertRaises(MissingKeyException):
86-
configuration.get_token({})
86+
configuration_loader.get_token({})
8787

8888
@patch("sys.argv", ["myscript.py", "--token", "myToken", "--sonar-project-key", "myProjectKey"])
8989
def test_load_sonar_project_properties(self):

tests/test_main.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717
# along with this program; if not, write to the Free Software Foundation,
1818
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
#
20-
from unittest import mock
2120
from unittest.mock import patch
2221

2322
from pyfakefs import fake_filesystem_unittest as pyfakefs
2423

25-
from pysonar_scanner.configuration import ConfigurationLoader
26-
from pysonar_scanner.configuration.properties import SONAR_PROJECT_KEY, SONAR_TOKEN
2724
from pysonar_scanner.__main__ import scan
25+
from pysonar_scanner.configuration.configuration_loader import ConfigurationLoader
26+
from pysonar_scanner.configuration.properties import SONAR_PROJECT_KEY, SONAR_TOKEN
2827
from pysonar_scanner.scannerengine import ScannerEngine
2928

3029

0 commit comments

Comments
 (0)