Skip to content

Commit aa91766

Browse files
SCANPY-130 Accept options of the form -Dsonar.*** (#156)
1 parent 096e925 commit aa91766

File tree

2 files changed

+122
-11
lines changed

2 files changed

+122
-11
lines changed

src/pysonar_scanner/configuration/cli.py

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,116 +44,159 @@ def __parse_cli_args(cls) -> argparse.Namespace:
4444
"-t",
4545
"--token",
4646
"--sonar-token",
47+
"-Dsonar.token",
4748
type=str,
4849
help="Token used to authenticate against the SonarQube Server or SonarQube Cloud",
4950
)
5051
parser.add_argument(
5152
"--sonar-project-key",
53+
"-Dsonar.projectKey",
5254
type=str,
5355
help="Key of the project that usually corresponds to the project name in SonarQube",
5456
)
5557

5658
parser.add_argument(
57-
"-v", "--verbose", "--sonar-verbose", action="store_true", default=None, help="Increase output verbosity"
59+
"-v",
60+
"--verbose",
61+
"--sonar-verbose",
62+
"-Dsonar.verbose",
63+
action="store_true",
64+
default=None,
65+
help="Increase output verbosity",
5866
)
5967

6068
parser.add_argument(
6169
"--sonar-host-url",
70+
"-Dsonar.host.url",
6271
type=str,
6372
help="SonarQube Server base URL. For example, http://localhost:9000 for a local instance of SonarQube Server",
6473
)
6574
parser.add_argument(
6675
"--sonar-region",
76+
"-Dsonar.region",
6777
type=str,
6878
choices=["us"],
6979
help="The region to contact, only for SonarQube Cloud",
7080
)
71-
parser.add_argument("--sonar-user-home", type=str, help="Base sonar directory, ~/.sonar by default")
81+
parser.add_argument(
82+
"--sonar-user-home", "-Dsonar.userHome", type=str, help="Base sonar directory, ~/.sonar by default"
83+
)
7284

7385
parser.add_argument(
7486
"--sonar-scanner-cloud-url",
87+
"-Dsonar.scanner.cloudUrl",
7588
type=str,
7689
help="SonarQube Cloud base URL, https://sonarcloud.io for example",
7790
)
7891
parser.add_argument(
7992
"--sonar-scanner-api-url",
93+
"-Dsonar.scanner.apiUrl",
8094
type=str,
8195
help="Base URL for all REST-compliant API calls, https://api.sonarcloud.io for example",
8296
)
8397
parser.add_argument(
8498
"--sonar-scanner-os",
99+
"-Dsonar.scanner.os",
85100
type=str,
86101
choices=["windows", "linux", "macos", "alpine"],
87102
help="OS running the scanner",
88103
)
89104
parser.add_argument(
90105
"--sonar-scanner-arch",
106+
"-Dsonar.scanner.arch",
91107
type=str,
92108
choices=["x64", "aarch64"],
93109
help="Architecture on which the scanner will be running",
94110
)
95111

96112
parser.add_argument(
97113
"--skip-jre-provisioning",
114+
"-Dsonar.scanner.skipJreProvisioning",
98115
action="store_true",
99116
default=None,
100117
help="If provided, the provisioning of the JRE will be skipped",
101118
)
102119
parser.add_argument(
103-
"--sonar-scanner-java-exe-path", type=str, help="If defined, the scanner engine will be run with this JRE"
120+
"--sonar-scanner-java-exe-path",
121+
"-Dsonar.scanner.javaExePath",
122+
type=str,
123+
help="If defined, the scanner engine will be run with this JRE",
104124
)
105125
parser.add_argument(
106-
"--sonar-scanner-java-opts", type=str, help="Arguments provided to the JVM when running the scanner"
126+
"--sonar-scanner-java-opts",
127+
"-Dsonar.scanner.javaOpts",
128+
type=str,
129+
help="Arguments provided to the JVM when running the scanner",
107130
)
108131

109132
parser.add_argument(
110133
"--sonar-scanner-internal-dump-to-file",
134+
"-Dsonar.scanner.internal.dumpToFile",
111135
type=str,
112136
help="Filename where the input to the scanner engine will be dumped. Useful for debugging",
113137
)
114138
parser.add_argument(
115139
"--sonar-scanner-internal-sq-version",
140+
"-Dsonar.scanner.internal.sqVersion",
116141
type=str,
117142
help="Emulate the result of the call to get SQ server version. Useful for debugging with --sonar-scanner-internal-dump-to-file",
118143
)
119144

120145
parser.add_argument(
121146
"--sonar-scanner-connect-timeout",
147+
"-Dsonar.scanner.connectTimeout",
122148
type=int,
123149
help="Time period to establish connections with the server (in seconds)",
124150
)
125151
parser.add_argument(
126152
"--sonar-scanner-socket-timeout",
153+
"-Dsonar.scanner.socketTimeout",
127154
type=int,
128155
help="Maximum time of inactivity between two data packets when exchanging data with the server (in seconds)",
129156
)
130157
parser.add_argument(
131158
"--sonar-scanner-response-timeout",
159+
"-Dsonar.scanner.responseTimeout",
132160
type=int,
133161
help="Time period required to process an HTTP call: from sending a request to receiving a response (in seconds)",
134162
)
135163

136164
parser.add_argument(
137165
"--sonar-scanner-truststore-path",
166+
"-Dsonar.scanner.truststorePath",
138167
type=str,
139168
help="Path to the keystore containing trusted server certificates, used by the Scanner in addition to OS and the built-in certificates",
140169
)
141-
parser.add_argument("--sonar-scanner-truststore-password", type=str, help="Password to access the truststore")
170+
parser.add_argument(
171+
"--sonar-scanner-truststore-password",
172+
"-Dsonar.scanner.truststorePassword",
173+
type=str,
174+
help="Password to access the truststore",
175+
)
142176

143177
parser.add_argument(
144178
"--sonar-scanner-keystore-path",
179+
"-Dsonar.scanner.keystorePath",
145180
type=str,
146181
help="Path to the keystore containing the client certificates used by the scanner. By default, <sonar.userHome>/ssl/keystore.p12",
147182
)
148-
parser.add_argument("--sonar-scanner-keystore-password", type=str, help="Password to access the keystore")
183+
parser.add_argument(
184+
"--sonar-scanner-keystore-password",
185+
"-Dsonar.scanner.keystorePassword",
186+
type=str,
187+
help="Password to access the keystore",
188+
)
149189

150-
parser.add_argument("--sonar-scanner-proxy-host", type=str, help="Proxy host")
151-
parser.add_argument("--sonar-scanner-proxy-port", type=int, help="Proxy port")
152-
parser.add_argument("--sonar-scanner-proxy-user", type=str, help="Proxy user")
153-
parser.add_argument("--sonar-scanner-proxy-password", type=str, help="Proxy password")
190+
parser.add_argument("--sonar-scanner-proxy-host", "-Dsonar.scanner.proxyHost", type=str, help="Proxy host")
191+
parser.add_argument("--sonar-scanner-proxy-port", "-Dsonar.scanner.proxyPort", type=int, help="Proxy port")
192+
parser.add_argument("--sonar-scanner-proxy-user", "-Dsonar.scanner.proxyUser", type=str, help="Proxy user")
193+
parser.add_argument(
194+
"--sonar-scanner-proxy-password", "-Dsonar.scanner.proxyPassword", type=str, help="Proxy password"
195+
)
154196

155197
parser.add_argument(
156198
"--sonar-project-base-dir",
199+
"-Dsonar.projectBaseDir",
157200
type=str,
158201
help="Directory containing the project to be analyzed. Default is the current directory",
159202
)

tests/test_configuration_cli.py

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def test_impossible_os_choice(self):
106106
CliConfigurationLoader.load()
107107

108108
error_output = mock_stderr.getvalue()
109-
self.assertIn("""argument --sonar-scanner-os: invalid choice: 'windows2'""", error_output)
109+
self.assertIn("""invalid choice: 'windows2'""", error_output)
110110

111111
@patch(
112112
"sys.argv",
@@ -199,3 +199,71 @@ def test_all_cli_args(self):
199199
}
200200

201201
self.assertEqual(configuration, expected_configuration)
202+
203+
@patch(
204+
"sys.argv",
205+
[
206+
"myscript.py",
207+
"-Dsonar.token=myToken",
208+
"-Dsonar.projectKey=myProjectKey",
209+
"-Dsonar.verbose",
210+
"-Dsonar.host.url=mySonarHostUrl",
211+
"-Dsonar.scanner.cloudUrl=mySonarScannerCloudUrl",
212+
"-Dsonar.scanner.apiUrl=mySonarScannerApiUrl",
213+
"-Dsonar.scanner.os=windows",
214+
"-Dsonar.scanner.arch=x64",
215+
"-Dsonar.scanner.connectTimeout=42",
216+
"-Dsonar.scanner.internal.dumpToFile=mySonarScannerInternalDumpToFile",
217+
"-Dsonar.scanner.internal.sqVersion=mySonarScannerInternalSqVersion",
218+
"-Dsonar.scanner.socketTimeout=43",
219+
"-Dsonar.scanner.responseTimeout=44",
220+
"-Dsonar.scanner.truststorePath=mySonarScannerTruststorePath",
221+
"-Dsonar.scanner.truststorePassword=mySonarScannerTruststorePassword",
222+
"-Dsonar.scanner.keystorePath=mySonarScannerKeystorePath",
223+
"-Dsonar.scanner.keystorePassword=mySonarScannerKeystorePassword",
224+
"-Dsonar.scanner.proxyHost=mySonarScannerProxyHost",
225+
"-Dsonar.scanner.proxyPort=45",
226+
"-Dsonar.scanner.proxyUser=mySonarScannerProxyUser",
227+
"-Dsonar.scanner.proxyPassword=mySonarScannerProxyPassword",
228+
"-Dsonar.scanner.skipJreProvisioning",
229+
"-Dsonar.scanner.javaExePath=mySonarScannerJavaExePath",
230+
"-Dsonar.scanner.javaOpts=mySonarScannerJavaOpts",
231+
"-Dsonar.region=us",
232+
"-Dsonar.userHome=mySonarUserHome",
233+
"-Dsonar.projectBaseDir=mySonarProjectBaseDir",
234+
],
235+
)
236+
def test_jvm_style_cli_args(self):
237+
configuration = CliConfigurationLoader.load()
238+
expected_configuration = {
239+
"sonar.token": "myToken",
240+
"sonar.projectKey": "myProjectKey",
241+
"sonar.verbose": True,
242+
"sonar.host.url": "mySonarHostUrl",
243+
"sonar.scanner.sonarcloudUrl": "mySonarScannerCloudUrl",
244+
"sonar.scanner.apiBaseUrl": "mySonarScannerApiUrl",
245+
"sonar.scanner.os": "windows",
246+
"sonar.scanner.arch": "x64",
247+
"sonar.scanner.connectTimeout": 42,
248+
"sonar.scanner.internal.dumpToFile": "mySonarScannerInternalDumpToFile",
249+
"sonar.scanner.internal.sqVersion": "mySonarScannerInternalSqVersion",
250+
"sonar.scanner.socketTimeout": 43,
251+
"sonar.scanner.responseTimeout": 44,
252+
"sonar.scanner.truststorePath": "mySonarScannerTruststorePath",
253+
"sonar.scanner.truststorePassword": "mySonarScannerTruststorePassword",
254+
"sonar.scanner.keystorePath": "mySonarScannerKeystorePath",
255+
"sonar.scanner.keystorePassword": "mySonarScannerKeystorePassword",
256+
"sonar.scanner.proxyHost": "mySonarScannerProxyHost",
257+
"sonar.scanner.proxyPort": 45,
258+
"sonar.scanner.proxyUser": "mySonarScannerProxyUser",
259+
"sonar.scanner.proxyPassword": "mySonarScannerProxyPassword",
260+
"sonar.scanner.skipJreProvisioning": True,
261+
"sonar.scanner.javaExePath": "mySonarScannerJavaExePath",
262+
"sonar.scanner.javaOpts": "mySonarScannerJavaOpts",
263+
"sonar.region": "us",
264+
"sonar.userHome": "mySonarUserHome",
265+
"sonar.projectBaseDir": "mySonarProjectBaseDir",
266+
}
267+
for key in expected_configuration:
268+
self.assertEqual(configuration[key], expected_configuration[key])
269+
self.assertEqual(configuration, expected_configuration)

0 commit comments

Comments
 (0)