diff --git a/flow/staticqualityanalysis/sonar/sonarmodule.py b/flow/staticqualityanalysis/sonar/sonarmodule.py index 3d41f85..626fd76 100644 --- a/flow/staticqualityanalysis/sonar/sonarmodule.py +++ b/flow/staticqualityanalysis/sonar/sonarmodule.py @@ -76,7 +76,11 @@ def _submit_scan(self): commons.print_msg(SonarQube.clazz, method, '\'SONAR_HOME\' environment variable must be defined', 'ERROR') exit(1) - if not self.config.settings.has_section('sonar') or not self.config.settings.has_option('sonar', + sonar_jar_files = commons.get_files_of_type_from_directory('jar', os.environ.get('SONAR_HOME')) + if len(sonar_jar_files) > 0: + sonar_jar_files.sort(reverse=True) + sonar_runner_executable = sonar_jar_files[0] + elif not self.config.settings.has_section('sonar') or not self.config.settings.has_option('sonar', 'sonar_runner'): commons.print_msg(SonarQube.clazz, method, 'Sonar runner undefined. Please define path to sonar ' 'runner in settings.ini.', 'ERROR') @@ -95,7 +99,6 @@ def _submit_scan(self): sonar_cmd = 'java -Dsonar.projectKey="' + self.config.project_name + '" -Dsonar.projectName="' + self.config.project_name + '" -Dsonar.projectVersion="' + self.config.version_number + '" -Dsonar.login=$SONAR_USER -Dsonar.password=$SONAR_PWD -Dproject.settings="' + custom_sonar_file + '" -Dproject.home="$PWD" -jar $SONAR_HOME/' + sonar_runner_executable + ' -e -X' else: sonar_cmd = 'java -Dsonar.projectKey="' + self.config.project_name + '" -Dsonar.projectName="' + self.config.project_name + '" -Dsonar.projectVersion="' + self.config.version_number + '" -Dproject.settings="' + custom_sonar_file + '" -Dproject.home="$PWD" -jar $SONAR_HOME/' + sonar_runner_executable + ' -e -X' - commons.print_msg(SonarQube.clazz, method, sonar_cmd) else: if sonar_user is not None and sonar_pwd is not None: diff --git a/tests/staticqualityanalysis/sonar/test_sonar.py b/tests/staticqualityanalysis/sonar/test_sonar.py index 9c7b771..d4ef6c4 100644 --- a/tests/staticqualityanalysis/sonar/test_sonar.py +++ b/tests/staticqualityanalysis/sonar/test_sonar.py @@ -9,18 +9,57 @@ from flow.buildconfig import BuildConfig +def test_scan_code_single_jar_executable_path(monkeypatch): + monkeypatch.setenv('SONAR_HOME','FAKEHOME') + + with patch('flow.utils.commons.print_msg') as mock_printmsg_fn: + with patch('flow.utils.commons.get_files_of_type_from_directory') as mock_getfiletypefromdir_fn: + with pytest.raises(SystemExit): + mock_getfiletypefromdir_fn.return_value = ['sonar-scanner.jar'] + _b = MagicMock(BuildConfig) + parser = configparser.ConfigParser() + parser.add_section('sonar') + _b.settings = parser + + _sonar = SonarQube(config_override=_b) + _sonar.scan_code() + + mock_getfiletypefromdir_fn.assert_called_with('jar', 'FAKEHOME') + + +def test_scan_code_settings_executable_path(monkeypatch): + monkeypatch.setenv('SONAR_HOME','FAKEHOME') + + with patch('flow.utils.commons.print_msg') as mock_printmsg_fn: + with patch('flow.utils.commons.get_files_of_type_from_directory') as mock_getfiletypefromdir_fn: + with pytest.raises(SystemExit): + mock_getfiletypefromdir_fn.return_value = [] + _b = MagicMock(BuildConfig) + parser = configparser.ConfigParser() + parser.add_section('sonar') + parser.set('sonar', 'sonar_runner', 'sonar-runner-dist-2.4.jar') + _b.settings = parser + + _sonar = SonarQube(config_override=_b) + _sonar.scan_code() + + mock_getfiletypefromdir_fn.assert_called_with('jar', 'FAKEHOME') + + def test_scan_code_missing_executable_path(monkeypatch): monkeypatch.setenv('SONAR_HOME','FAKEHOME') with patch('flow.utils.commons.print_msg') as mock_printmsg_fn: - with pytest.raises(SystemExit): - _b = MagicMock(BuildConfig) - parser = configparser.ConfigParser() - parser.add_section('sonar') - _b.settings = parser + with patch('flow.utils.commons.get_files_of_type_from_directory') as mock_getfiletypefromdir_fn: + with pytest.raises(SystemExit): + mock_getfiletypefromdir_fn.return_value = [] + _b = MagicMock(BuildConfig) + parser = configparser.ConfigParser() + parser.add_section('sonar') + _b.settings = parser - _sonar = SonarQube(config_override=_b) - _sonar.scan_code() + _sonar = SonarQube(config_override=_b) + _sonar.scan_code() mock_printmsg_fn.assert_called_with('SonarQube', '_submit_scan', 'Sonar runner undefined. Please define path to ' 'sonar ' @@ -44,6 +83,7 @@ def _submit_scan_failure(): parser.add_section('project') parser.set('project', 'retry_sleep_interval', '0') _b.settings = parser + _sonar = SonarQube(config_override=_b) _sonar.scan_code() @@ -73,15 +113,17 @@ def test_scan_code_missing_sonar_project_properties(monkeypatch): monkeypatch.setenv('SONAR_HOME','FAKEHOME') with patch('flow.utils.commons.print_msg') as mock_printmsg_fn: - with patch('os.path.isfile', return_value=False): - with pytest.raises(SystemExit): - _b = MagicMock(BuildConfig) - parser = configparser.ConfigParser() - parser.add_section('sonar') - parser.set('sonar', 'sonar_runner', 'sonar-runner-dist-2.4.jar') - _b.settings = parser + with patch('flow.utils.commons.get_files_of_type_from_directory') as mock_getfiletypefromdir_fn: + with patch('os.path.isfile', return_value=False): + with pytest.raises(SystemExit): + mock_getfiletypefromdir_fn.return_value = ['sonar-scanner.jar'] + _b = MagicMock(BuildConfig) + parser = configparser.ConfigParser() + parser.add_section('sonar') + parser.set('sonar', 'sonar_runner', 'sonar-runner-dist-2.4.jar') + _b.settings = parser - _sonar = SonarQube(config_override=_b) - _sonar.scan_code() + _sonar = SonarQube(config_override=_b) + _sonar.scan_code() mock_printmsg_fn.assert_called_with('SonarQube', '_submit_scan', 'No sonar-project.properties file was found. Please include in the root of your project with a valid value for \'sonar.host.url\'', 'ERROR')