Skip to content

Commit

Permalink
feat: by default, for Xonsh shell, stop the target process if any com…
Browse files Browse the repository at this point in the history
…mand line fails (osl-incubator#19)

For xonsh shell, when there is any error in any of the command lines, by default it would be stopped, and a erro code 1 will be raised.
  • Loading branch information
xmnlab authored May 19, 2023
1 parent 1fc8f50 commit 8fa4c1d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
16 changes: 10 additions & 6 deletions makim/makim.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@


def escape_template_tag(v: str) -> str:
return str(v).replace('{{', r'\{\{').replace('}}', r'\}\}')
return v.replace('{{', r'\{\{').replace('}}', r'\}\}')


def unescape_template_tag(v: str) -> str:
return str(v).replace(r'\{\{', '{{').replace(r'\}\}', '}}')
return v.replace(r'\{\{', '{{').replace(r'\}\}', '}}')


class PrintPlugin:
Expand All @@ -50,6 +50,10 @@ class Makim(PrintPlugin):
target_name: str = ''
target_data: dict = {}

def __init__(self):
os.environ['RAISE_SUBPROC_ERROR'] = '1'
os.environ['XONSH_SHOW_TRACEBACK'] = '0'

def _call_shell_app(self, cmd):
fd, filepath = tempfile.mkstemp(suffix='.makim', text=True)

Expand Down Expand Up @@ -202,7 +206,7 @@ def _run_dependencies(self, args: dict):
# update the arguments
for arg_name, arg_value in dep_data.get('args', {}).items():
unescaped_value = (
unescape_template_tag(arg_value)
unescape_template_tag(str(arg_value))
if isinstance(arg_value, str)
else str(arg_value)
)
Expand All @@ -217,7 +221,7 @@ def _run_dependencies(self, args: dict):
# checking for the conditional statement
if_stmt = dep_data.get('if')
if if_stmt:
result = Template(unescape_template_tag(if_stmt)).render(
result = Template(unescape_template_tag(str(if_stmt))).render(
args=original_args_clean
)
if not yaml.safe_load(result):
Expand Down Expand Up @@ -297,13 +301,13 @@ def _run_command(self, args: dict):
]:
env.update(env_file)
for k, v in env_user.items():
env[k] = Template(unescape_template_tag(v)).render(
env[k] = Template(unescape_template_tag(str(v))).render(
args=args_input, env=env, vars=variables
)
for k, v in env.items():
os.environ[k] = v

cmd = unescape_template_tag(cmd)
cmd = unescape_template_tag(str(cmd))
cmd = Template(cmd).render(args=args_input, env=env, vars=variables)
if args.get('verbose'):
self._print_info('=' * 80)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ python = "^3.8.1"
sh = "^2.0.0"
pyyaml = "<6.0"
jinja2 = "<3.0.3"
xonsh = "^0.13.4"
xonsh = "^0.14.0"
python-dotenv = "^0.21.1"
colorama = "^0.4.6"
urllib3 = "<2"
Expand Down
22 changes: 22 additions & 0 deletions tests/.makim-simple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,25 @@ groups:
echo "build file x"
echo "build file y"
echo "build file z"
print:
targets:
force-error:
help: This target should print until item 3
run: |
echo 1
wrongecho
echo 2
wrongecho
echo 3
skip-error:
help: This target should print until item 3
env:
RAISE_SUBPROC_ERROR: 0
run: |
echo 1
wrongecho
echo 2
wrongecho
echo 3
13 changes: 7 additions & 6 deletions tests/test_failure.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
import pytest

import makim
from makim.error import MakimError


@pytest.mark.parametrize(
'target,args',
'target,args,error_code',
[
('tests.test-7', {}),
('tests.test-8', {}),
('tests.test-9', {}),
('tests.test-7', {}, MakimError.MAKIM_ARGUMENT_REQUIRED.value),
('tests.test-8', {}, MakimError.SH_ERROR_RETURN_CODE.value),
('tests.test-9', {}, MakimError.SH_ERROR_RETURN_CODE.value),
],
)
def test_failure(target, args):
def test_failure(target, args, error_code):
makim_file = Path(__file__).parent / '.makim-unittest.yaml'

m = makim.Makim()
Expand All @@ -33,4 +34,4 @@ def test_failure(target, args):
with pytest.raises(SystemExit) as pytest_wrapped_e:
m.run(args)
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == 1
assert pytest_wrapped_e.value.code == error_code

0 comments on commit 8fa4c1d

Please sign in to comment.