Skip to content

Commit

Permalink
Merge pull request #10 from logchange/8-fix-fail-when-cmd-fails
Browse files Browse the repository at this point in the history
Fail release process when one from commands return non-zero code.
  • Loading branch information
marwin1991 authored Apr 3, 2024
2 parents f8e94aa + 1333c7f commit d8f7132
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
Empty file added test/commit/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions test/commit/before_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import unittest
from unittest.mock import patch, Mock, call
from valhalla.commit.before import execute # Adjust the import according to your module structure


class TestExecuteFunction(unittest.TestCase):

@patch('valhalla.commit.before.exit')
@patch('valhalla.commit.before.resolve')
@patch('valhalla.commit.before.info')
@patch('valhalla.commit.before.error')
def test_execute_success(self, mock_error: Mock, mock_info: Mock, mock_resolve: Mock, mock_exit: Mock):
mock_resolve.side_effect = lambda x: x # Simply return the command itself

execute(["echo 'Hello World'"])

mock_info.assert_has_calls([call("Output for command 'echo 'Hello World'':\nHello World\n"),
call("Successfully executed command: 'echo 'Hello World''")])
mock_error.assert_not_called()
mock_exit.assert_not_called()

@patch('valhalla.commit.before.exit')
@patch('valhalla.commit.before.resolve')
@patch('valhalla.commit.before.info')
@patch('valhalla.commit.before.error')
def test_execute_error(self, mock_error: Mock, mock_info: Mock, mock_resolve: Mock, mock_exit: Mock):
mock_resolve.side_effect = lambda x: x # Simply return the command itself

execute(["mvn clean"])

mock_exit.assert_called_with(1)
File renamed without changes.
22 changes: 19 additions & 3 deletions valhalla/commit/before.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,30 @@ def execute(commands: List[str]):
try:
for command in commands:
command = resolve(command)
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
result = subprocess.run(command, shell=True, check=False, capture_output=True, text=True)
stdout = result.stdout
stderr = result.stderr
if stdout:
info(f"Output for command '{command}':\n{stdout}")
if stderr:
error(f"Error output for command '{command}':\n{stderr}")
except subprocess.CalledProcessError as e:

if result.returncode != 0:
error(f"\n\n\n-----------------------------------------------------------\n"
f"Executing command {command} finished with code: {result.returncode} , valhalla cannot \n" +
f"continue releasing process! Fix it and retry!\n" +
f"Delete this branch (and tag if created), fix your main branch \n"
f"and create release-* branch again, this simplifies fixes and reduce mistakes \n"
f"-----------------------------------------------------------\n\n")
exit(result.returncode)
else:
info(f"Successfully executed command: '{command}'")
except subprocess.CalledProcessError as e: # run is called with check=False, so it should not be called
error(f"Error executing command '{e.cmd}': {e.stderr}")
for line in e.output.splitlines():
error("------" + line)
exit(1)
except Exception as e:
error(f"Error occurred: {str(e)}")
error(f"Exception occurred: {str(e)} during executing command")
exit(1)

0 comments on commit d8f7132

Please sign in to comment.