Skip to content

Commit

Permalink
fix: Run build CalledProcessError exception
Browse files Browse the repository at this point in the history
  • Loading branch information
apburnes committed Jun 7, 2024
1 parent 206740e commit ab19cfd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
10 changes: 1 addition & 9 deletions src/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@

from steps import (
build_hugo, build_jekyll, build_static, download_hugo,
fetch_repo, publish, run_build_script, fetch_commit_sha,
fetch_repo, publish, run_build_script, run_step, fetch_commit_sha,
setup_bundler, setup_node, setup_ruby, StepException, update_repo
)


TIMEOUT_SECONDS = 45 * 60 # 45 minutes

GENERATORS = ['hugo', 'jekyll', 'node.js', 'static']
Expand Down Expand Up @@ -97,13 +96,6 @@ def build(
# partially apply the callback url to post_metrics
post_metrics_p = partial(post_metrics, status_callback)

def run_step(step, msg, *args, **kwargs):
try:
step(*args, **kwargs)
except Exception as e:
logger.error(e)
raise StepException(msg)

logger.info(f'Running build for {owner}/{repository}/{branch}')

if generator not in GENERATORS:
Expand Down
4 changes: 3 additions & 1 deletion src/steps/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from .build import (
build_hugo, build_jekyll, build_static,
download_hugo, run_build_script, setup_bundler,
download_hugo, run_build_script, run_step, setup_bundler,
setup_node, setup_ruby,
)
from .exceptions import StepException
from .fetch import fetch_repo, update_repo, fetch_commit_sha
from .publish import publish


__all__ = [
'build_hugo',
'build_jekyll',
Expand All @@ -15,6 +16,7 @@
'fetch_repo',
'publish',
'run_build_script',
'run_step',
'setup_bundler',
'setup_node',
'setup_ruby',
Expand Down
8 changes: 8 additions & 0 deletions src/steps/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from log_utils import get_logger
from runner import run, setuser
from .cache import CacheFolder
from .exceptions import StepException

HUGO_BIN = 'hugo'
HUGO_VERSION = '.hugo-version'
Expand Down Expand Up @@ -222,6 +223,13 @@ def run_build_script(branch, owner, repository, site_prefix,
return


def run_step(step, msg, *args, **kwargs):
try:
step(*args, **kwargs)
except Exception:
raise StepException(msg)


def download_hugo(post_metrics):
logger = get_logger('download-hugo')

Expand Down
16 changes: 15 additions & 1 deletion test/test_build.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pytest import raises
import json
import os
from io import StringIO
Expand All @@ -12,7 +13,7 @@
import steps
from steps import (
build_hugo, build_jekyll, build_static, download_hugo,
run_build_script, setup_bundler, setup_node, setup_ruby
run_build_script, run_step, setup_bundler, setup_node, setup_ruby, StepException
)
from steps.build import (
build_env, check_supported_ruby_version, BUNDLER_VERSION, GEMFILE,
Expand Down Expand Up @@ -214,6 +215,19 @@ def test_it_does_not_run_otherwise(self, mock_get_logger, mock_run):
mock_run.assert_not_called()


class TestRunStep():
def test_it_should_raise_an_exception(self):
msg = 'testing-msg'
arg1 = 'arg1'
kwarg1 = 'kwarg1'
mock_step = Mock(side_effect=KeyError)

with raises(StepException):
run_step(mock_step, msg, arg1, kwarg1=kwarg1)

mock_step.assert_called_once_with(arg1, kwarg1=kwarg1)


@patch('steps.build.run')
@patch('steps.build.get_logger')
@patch('steps.build.check_supported_ruby_version')
Expand Down

0 comments on commit ab19cfd

Please sign in to comment.