Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail release process when one from commands return non-zero code. #10

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

Loading