diff --git a/test/commit/__init__.py b/test/commit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/commit/before_test.py b/test/commit/before_test.py new file mode 100644 index 0000000..f0f4c82 --- /dev/null +++ b/test/commit/before_test.py @@ -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) diff --git a/test/extends/resolver_test.py b/test/common/resolver_test.py similarity index 100% rename from test/extends/resolver_test.py rename to test/common/resolver_test.py diff --git a/valhalla/commit/before.py b/valhalla/commit/before.py index b80365c..e4f6a68 100644 --- a/valhalla/commit/before.py +++ b/valhalla/commit/before.py @@ -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) +