Skip to content

Commit

Permalink
Find sonar exectable jar in sonar home (#52)
Browse files Browse the repository at this point in the history
* Initial change and working on broken tests

* Added unit tests to cover new logic branches in code

* Cleaning up testing debug statements

Co-authored-by: Michael Dillard <[email protected]>
  • Loading branch information
mkdillard and Michael Dillard authored Aug 11, 2020
1 parent ef996ad commit 413a615
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
7 changes: 5 additions & 2 deletions flow/staticqualityanalysis/sonar/sonarmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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:
Expand Down
74 changes: 58 additions & 16 deletions tests/staticqualityanalysis/sonar/test_sonar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '
Expand All @@ -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()

Expand Down Expand Up @@ -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')

0 comments on commit 413a615

Please sign in to comment.