Skip to content

Commit

Permalink
return a proper result from CommandBuilder.run()
Browse files Browse the repository at this point in the history
  • Loading branch information
mriehl committed Feb 25, 2015
1 parent 71c8c03 commit 9d7931b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
9 changes: 8 additions & 1 deletion src/main/python/pybuilder/pluginhelper/external_command.py
Expand Up @@ -64,7 +64,14 @@ def as_string(self):
return ' '.join(self.parts)

def run(self, outfile_name):
return execute_command(self.parts, outfile_name)
error_file_name = "{0}.err".format(outfile_name)
return_code = execute_command(self.parts, outfile_name)
error_file_lines = read_file(error_file_name)
outfile_lines = read_file(outfile_name)

return ExternalCommandResult(return_code,
outfile_name, outfile_lines,
error_file_name, error_file_lines)

def run_on_production_source_files(self, logger, include_test_sources=False, include_scripts=False):
execution_result = execute_tool_on_source_files(project=self.project,
Expand Down
16 changes: 11 additions & 5 deletions src/main/python/pybuilder/plugins/python/sonarqube_plugin.py
Expand Up @@ -41,14 +41,20 @@ def run_sonar_analysis(project, logger):

sonar_runner = build_sonar_runner(project)

return_code = sonar_runner.run(project.expand_path("$dir_reports/sonar-runner"))
result = sonar_runner.run(project.expand_path("$dir_reports/sonar-runner"))

if return_code != 0:
raise BuildFailedException(
"sonar-runner exited with code {exit_code}. See reports at {reports_dir} for more information.".format(
exit_code=return_code,
if result.exit_code != 0:
logger.error(
"sonar-runner exited with code {exit_code}. See {reports_dir} for more information.".format(
exit_code=result.exit_code,
reports_dir=project.expand_path("$dir_reports")))

if project.get_property("verbose"):
logger.error("Contents of {0}:".format(result.error_report_file))
logger.error("\n".join(result.error_report_lines))

raise BuildFailedException("Sonar analysis failed.")


def build_sonar_runner(project):
return (SonarCommandBuilder("sonar-runner", project)
Expand Down
3 changes: 2 additions & 1 deletion src/unittest/python/external_command_tests.py
Expand Up @@ -107,7 +107,8 @@ def setUp(self):
self.command.use_argument('--foo').use_argument('--bar')

@patch("pybuilder.pluginhelper.external_command.execute_command")
def test_should_execute_external_command(self, execute_command):
@patch("pybuilder.pluginhelper.external_command.read_file")
def test_should_execute_external_command(self, _, execute_command):
self.command.run("any-outfile-name")

execute_command.assert_called_with(['command-name', '--foo', '--bar'], 'any-outfile-name')
Expand Down
4 changes: 2 additions & 2 deletions src/unittest/python/plugins/python/sonarqube_plugin_tests.py
Expand Up @@ -48,13 +48,13 @@ def test_should_build_sonar_runner_for_project(self):

@patch("pybuilder.plugins.python.sonarqube_plugin.SonarCommandBuilder.run")
def test_should_break_build_when_sonar_runner_fails(self, run_sonar_command):
run_sonar_command.return_value = 1
run_sonar_command.return_value = Mock(exit_code=1)

self.assertRaises(BuildFailedException, run_sonar_analysis, self.project, Mock())

@patch("pybuilder.plugins.python.sonarqube_plugin.SonarCommandBuilder.run")
def test_should_not_break_build_when_sonar_runner_succeeds(self, run_sonar_command):
run_sonar_command.return_value = 0
run_sonar_command.return_value = Mock(exit_code=0)

run_sonar_analysis(self.project, Mock())

Expand Down

0 comments on commit 9d7931b

Please sign in to comment.