Skip to content

Commit

Permalink
feat: Commands should fail the build if their exit code is not zero (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Fran-Rg authored Jan 22, 2024
1 parent 52a230b commit eebfc36
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ source_path = [
- If you specify a source path as a string that references a folder and the runtime begins with `python` or `nodejs`, the build process will automatically build python and nodejs dependencies if `requirements.txt` or `package.json` file will be found in the source folder. If you want to customize this behavior, please use the object notation as explained below.
- All arguments except `path` are optional.
- `patterns` - List of Python regex filenames should satisfy. Default value is "include everything" which is equal to `patterns = [".*"]`. This can also be specified as multiline heredoc string (no comments allowed). Some examples of valid patterns:
- If you use the `commands` option and chain multiple commands, only the exit code of last command will be checked for success. If you prefer to fail fast, start the commands with the bash option `set -e` or powershell option `$ErrorActionPreference="Stop"`

```txt
!.*/.*\.txt # Filter all txt files recursively
Expand Down
26 changes: 18 additions & 8 deletions package.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,17 +914,27 @@ def execute(self, build_plan, zip_stream, query):
# XXX: timestamp=0 - what actually do with it?
zs.write_dirs(rd, prefix=prefix, timestamp=0)
elif cmd == "sh":
r, w = os.pipe()
side_ch = os.fdopen(r)
path, script = action[1:]
script = "{}\npwd >&{}".format(script, w)
p = subprocess.Popen(
script,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=path,
)

p = subprocess.Popen(script, shell=True, cwd=path, pass_fds=(w,))
os.close(w)
sh_work_dir = side_ch.read().strip()
p.wait()
log.info("WD: %s", sh_work_dir)
side_ch.close()
call_stdout, call_stderr = p.communicate()
exit_code = p.returncode
log.info("exit_code: %s", exit_code)
if exit_code != 0:
raise RuntimeError(
"Script did not run successfully, exit code {}: {} - {}".format(
exit_code,
call_stdout.decode("utf-8").strip(),
call_stderr.decode("utf-8").strip(),
)
)
elif cmd == "set:filter":
patterns = action[1]
pf = ZipContentFilter(args=self._args)
Expand Down
20 changes: 19 additions & 1 deletion tests/test_package_toml.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from package import get_build_system_from_pyproject_toml
from package import get_build_system_from_pyproject_toml, BuildPlanManager
from pytest import raises
from unittest.mock import Mock


def test_get_build_system_from_pyproject_toml_inexistent():
Expand All @@ -14,6 +16,22 @@ def test_get_build_system_from_pyproject_toml_unknown():
)


def test_build_manager_sucess_command():
bpm = BuildPlanManager(args=Mock())
# Should not have exception raised
bpm.execute(build_plan=[["sh", "/tmp", "pwd"]], zip_stream=None, query=None)


def test_build_manager_failing_command():
bpm = BuildPlanManager(args=Mock())
with raises(Exception):
bpm.execute(
build_plan=[["sh", "/tmp", "NOTACOMMAND"]],
zip_stream=None,
query=None,
)


def test_get_build_system_from_pyproject_toml_poetry():
assert (
get_build_system_from_pyproject_toml(
Expand Down

0 comments on commit eebfc36

Please sign in to comment.