|
| 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 | +import unittest |
| 21 | + |
| 22 | +from unittest.mock import patch |
| 23 | +from io import StringIO |
| 24 | + |
| 25 | +from pysonar_scanner.configuration import ConfigurationLoader, Configuration, Scanner, Internal, Sonar |
| 26 | + |
| 27 | + |
| 28 | +class TestMain(unittest.TestCase): |
| 29 | + |
| 30 | + @patch("sys.argv", ["myscript.py"]) |
| 31 | + def test_missing_cli_args(self): |
| 32 | + with patch("sys.stderr", new=StringIO()) as mock_stderr, self.assertRaises(SystemExit): |
| 33 | + ConfigurationLoader.initialize_configuration() |
| 34 | + |
| 35 | + error_output = mock_stderr.getvalue() |
| 36 | + self.assertIn("the following arguments are required: -t/--token", error_output) |
| 37 | + |
| 38 | + @patch("sys.argv", ["myscript.py", "--token", "myToken"]) |
| 39 | + def test_minimal_cli_args(self): |
| 40 | + configuration = ConfigurationLoader.initialize_configuration() |
| 41 | + expected_internal = Internal() |
| 42 | + expected_scanner = Scanner(internal=expected_internal) |
| 43 | + expected_sonar = Sonar(scanner=expected_scanner, token="myToken") |
| 44 | + expected_configuration = Configuration(sonar=expected_sonar) |
| 45 | + self.assertEqual(configuration, expected_configuration) |
| 46 | + |
| 47 | + @patch("sys.argv", ["myscript.py", "-t", "myToken", "-v"]) |
| 48 | + def test_alternative_cli_args(self): |
| 49 | + alternatives = [["-t", "myToken", "-v"], ["--sonar-token", "myToken", "--sonar-verbose"]] |
| 50 | + for alternative in alternatives: |
| 51 | + with patch("sys.argv", ["myscript.py", *alternative]), patch("sys.stderr", new=StringIO()): |
| 52 | + configuration = ConfigurationLoader.initialize_configuration() |
| 53 | + expected_internal = Internal() |
| 54 | + expected_scanner = Scanner(internal=expected_internal) |
| 55 | + expected_sonar = Sonar(scanner=expected_scanner, token="myToken", verbose=True) |
| 56 | + expected_configuration = Configuration(sonar=expected_sonar) |
| 57 | + self.assertEqual(configuration, expected_configuration) |
| 58 | + |
| 59 | + @patch("sys.argv", ["myscript.py", "-t", "myToken", "--sonar-scanner-os", "windows2"]) |
| 60 | + def test_impossible_os_choice(self): |
| 61 | + with patch("sys.stderr", new=StringIO()) as mock_stderr: |
| 62 | + with self.assertRaises(SystemExit): |
| 63 | + ConfigurationLoader.initialize_configuration() |
| 64 | + |
| 65 | + error_output = mock_stderr.getvalue() |
| 66 | + self.assertIn("""argument --sonar-scanner-os: invalid choice: 'windows2'""", error_output) |
| 67 | + |
| 68 | + @patch( |
| 69 | + "sys.argv", |
| 70 | + [ |
| 71 | + "myscript.py", |
| 72 | + "-t", |
| 73 | + "myToken", |
| 74 | + "-v", |
| 75 | + "--sonar-host-url", |
| 76 | + "mySonarHostUrl", |
| 77 | + "--sonar-region", |
| 78 | + "us", |
| 79 | + "--sonar-user-home", |
| 80 | + "mySonarUserHome", |
| 81 | + "--sonar-scanner-cloud-url", |
| 82 | + "mySonarScannerCloudUrl", |
| 83 | + "--sonar-scanner-api-url", |
| 84 | + "mySonarScannerApiUrl", |
| 85 | + "--sonar-scanner-os", |
| 86 | + "windows", |
| 87 | + "--sonar-scanner-arch", |
| 88 | + "x64", |
| 89 | + "--skip-jre-provisioning", |
| 90 | + "--sonar-scanner-java-exe-path", |
| 91 | + "mySonarScannerJavaExePath", |
| 92 | + "--sonar-scanner-java-opts", |
| 93 | + "mySonarScannerJavaOpts", |
| 94 | + "--sonar-scanner-internal-dump-to-file", |
| 95 | + "mySonarScannerInternalDumpToFile", |
| 96 | + "--sonar-scanner-internal-sq-version", |
| 97 | + "mySonarScannerInternalSqVersion", |
| 98 | + "--sonar-scanner-connect-timeout", |
| 99 | + "42", |
| 100 | + "--sonar-scanner-socket-timeout", |
| 101 | + "43", |
| 102 | + "--sonar-scanner-response-timeout", |
| 103 | + "44", |
| 104 | + "--sonar-scanner-truststore-path", |
| 105 | + "mySonarScannerTruststorePath", |
| 106 | + "--sonar-scanner-truststore-password", |
| 107 | + "mySonarScannerTruststorePassword", |
| 108 | + "--sonar-scanner-keystore-path", |
| 109 | + "mySonarScannerKeystorePath", |
| 110 | + "--sonar-scanner-keystore-password", |
| 111 | + "mySonarScannerKeystorePassword", |
| 112 | + "--sonar-scanner-proxy-host", |
| 113 | + "mySonarScannerProxyHost", |
| 114 | + "--sonar-scanner-proxy-port", |
| 115 | + "45", |
| 116 | + "--sonar-scanner-proxy-user", |
| 117 | + "mySonarScannerProxyUser", |
| 118 | + "--sonar-scanner-proxy-password", |
| 119 | + "mySonarScannerProxyPassword", |
| 120 | + "--sonar-project-base-dir", |
| 121 | + "mySonarProjectBaseDir", |
| 122 | + ], |
| 123 | + ) |
| 124 | + def test_all_cli_args(self): |
| 125 | + configuration = ConfigurationLoader.initialize_configuration() |
| 126 | + expected_internal = Internal( |
| 127 | + dump_to_file="mySonarScannerInternalDumpToFile", sq_version="mySonarScannerInternalSqVersion" |
| 128 | + ) |
| 129 | + |
| 130 | + expected_scanner = Scanner( |
| 131 | + internal=expected_internal, |
| 132 | + os="windows", |
| 133 | + arch="x64", |
| 134 | + connect_timeout=42, |
| 135 | + socket_timeout=43, |
| 136 | + response_timeout=44, |
| 137 | + truststore_path="mySonarScannerTruststorePath", |
| 138 | + truststore_password="mySonarScannerTruststorePassword", |
| 139 | + keystore_path="mySonarScannerKeystorePath", |
| 140 | + keystore_password="mySonarScannerKeystorePassword", |
| 141 | + proxy_host="mySonarScannerProxyHost", |
| 142 | + proxy_port=45, |
| 143 | + proxy_user="mySonarScannerProxyUser", |
| 144 | + proxy_password="mySonarScannerProxyPassword", |
| 145 | + skip_jre_provisioning=True, |
| 146 | + java_exe_path="mySonarScannerJavaExePath", |
| 147 | + java_opts="mySonarScannerJavaOpts", |
| 148 | + sonarcloud_url="mySonarScannerCloudUrl", |
| 149 | + api_base_url="mySonarScannerApiUrl", |
| 150 | + ) |
| 151 | + |
| 152 | + expected_sonar = Sonar( |
| 153 | + scanner=expected_scanner, |
| 154 | + token="myToken", |
| 155 | + verbose=True, |
| 156 | + host_url="mySonarHostUrl", |
| 157 | + region="us", |
| 158 | + user_home="mySonarUserHome", |
| 159 | + project_base_dir="mySonarProjectBaseDir", |
| 160 | + ) |
| 161 | + |
| 162 | + expected_configuration = Configuration(sonar=expected_sonar) |
| 163 | + self.assertEqual(configuration, expected_configuration) |
0 commit comments