-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_main.py
More file actions
139 lines (120 loc) · 5.47 KB
/
test_main.py
File metadata and controls
139 lines (120 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#
# Sonar Scanner Python
# Copyright (C) 2011-2026 SonarSource Sàrl
# mailto:info AT sonarsource DOT com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
#
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
import pathlib
from unittest.mock import patch, Mock, call
from pyfakefs import fake_filesystem_unittest as pyfakefs
from pysonar_scanner.__main__ import scan, main, check_version, create_jre
from pysonar_scanner.api import SQVersion, SonarQubeApi
from pysonar_scanner.cache import Cache
from pysonar_scanner.configuration.configuration_loader import ConfigurationLoader
from pysonar_scanner.configuration.properties import (
SONAR_PROJECT_KEY,
SONAR_TOKEN,
SONAR_HOST_URL,
SONAR_SCANNER_API_BASE_URL,
SONAR_SCANNER_SONARCLOUD_URL,
SONAR_SCANNER_PROXY_PORT,
SONAR_SCANNER_OS,
SONAR_SCANNER_ARCH,
SONAR_SCANNER_JAVA_EXE_PATH,
)
from pysonar_scanner.exceptions import SQTooOldException
from pysonar_scanner.jre import JREResolvedPath, JREResolver
from pysonar_scanner.scannerengine import ScannerEngine, ScannerEngineProvisioner
from tests.unit import sq_api_utils
class TestMain(pyfakefs.TestCase):
@patch("pysonar_scanner.__main__.logging")
@patch.object(pathlib.Path, "home", return_value=pathlib.Path("home/user"))
@patch.object(
ConfigurationLoader,
"load",
return_value={
SONAR_TOKEN: "myToken",
SONAR_PROJECT_KEY: "myProjectKey",
SONAR_SCANNER_OS: "linux",
SONAR_SCANNER_ARCH: "x64",
},
)
@patch.object(
ScannerEngineProvisioner, "provision", return_value=JREResolvedPath(pathlib.Path("scanner_engine_path"))
)
@patch("pysonar_scanner.__main__.create_jre", return_value=JREResolvedPath(pathlib.Path("jre_path")))
@patch.object(ScannerEngine, "run", return_value=0)
def test_minimal_success_run(
self, run_mock, create_jre_mock, provision_mock, load_mock, path_home_mock, mock_logging
):
exitcode = scan()
self.assertEqual(exitcode, 0)
# Verify that run was called with the expected configuration
run_mock.assert_called_once()
config = run_mock.call_args[0][0] # Extract the configuration arg
# Check expected configuration with a single assertion
expected_config = {
SONAR_TOKEN: "myToken",
SONAR_PROJECT_KEY: "myProjectKey",
SONAR_SCANNER_OS: "linux",
SONAR_SCANNER_ARCH: "x64",
SONAR_HOST_URL: "https://sonarcloud.io",
SONAR_SCANNER_API_BASE_URL: "https://api.sonarcloud.io",
SONAR_SCANNER_SONARCLOUD_URL: "https://sonarcloud.io",
SONAR_SCANNER_PROXY_PORT: "443",
SONAR_SCANNER_JAVA_EXE_PATH: "jre_path",
}
self.assertEqual(expected_config, config)
info_logs = [
call(
"Enhance your workflow: Pair pysonar with SonarQube Server per your license or SonarQube Cloud for deeper analysis, and try SonarQube-IDE in your favourite IDE."
),
call("Starting Pysonar, the Sonar scanner CLI for Python"),
call("Starting the analysis..."),
]
mock_logging.info.assert_has_calls(info_logs)
@patch.object(ConfigurationLoader, "load")
def test_scan_with_exception(self, load_mock):
load_mock.side_effect = Exception("Test exception")
exitcode = scan()
self.assertEqual(1, exitcode)
def test_main_with_exitcode_not_zero(self):
with patch("pysonar_scanner.__main__.scan", return_value=42):
with self.assertRaises(SystemExit) as main_exit:
main()
self.assertEqual(main_exit.exception.code, 42)
def test_version_check_outdated_sonarqube(self):
sq_cloud_api = sq_api_utils.get_sq_server()
sq_cloud_api.get_analysis_version = Mock(return_value=SQVersion.from_str("9.9.9"))
with self.assertRaises(SQTooOldException):
check_version(sq_cloud_api)
def test_version_check_recent_sonarqube(self):
sq_cloud_api = sq_api_utils.get_sq_server()
sq_cloud_api.get_analysis_version = Mock(return_value=SQVersion.from_str("10.7"))
check_version(sq_cloud_api)
sq_cloud_api.get_analysis_version.assert_called_once()
def test_version_check_sonarqube_cloud(self):
sq_cloud_api = sq_api_utils.get_sq_cloud()
sq_cloud_api.get_analysis_version = Mock()
check_version(sq_cloud_api)
sq_cloud_api.get_analysis_version.assert_not_called()
@patch("pysonar_scanner.scannerengine.CmdExecutor")
@patch.object(JREResolver, "resolve_jre")
def test_get_jre(self, resolve_jre_mock, cmd_executor_mock):
resolve_jre_mock.return_value = JREResolvedPath(pathlib.Path("jre/bin/java"))
api = SonarQubeApi(Mock(), Mock())
cache = Cache(Mock())
create_jre(api, cache, {SONAR_SCANNER_OS: "linux", SONAR_SCANNER_ARCH: "x64"})