Skip to content

Commit

Permalink
fix(plugins/python): output error messages to stderr
Browse files Browse the repository at this point in the history
Properly redirect error messages instead of using the combined stream
output.

Signed-off-by: Claudio Matsuoka <[email protected]>
  • Loading branch information
cmatsuoka committed Feb 11, 2025
1 parent b4f5ca6 commit 522e316
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
4 changes: 2 additions & 2 deletions craft_parts/executor/step_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def _builtin_build(self) -> StepContents:
raise errors.PluginBuildError(
part_name=self._part.name,
plugin_name=self._part.plugin_name,
stderr=process_error.result.combined,
stderr=process_error.result.stderr,
) from process_error

return StepContents()
Expand Down Expand Up @@ -296,7 +296,7 @@ def run_scriptlet(
part_name=self._part.name,
scriptlet_name=scriptlet_name,
exit_code=process_error.result.returncode,
stderr=process_error.result.combined,
stderr=process_error.result.stderr,
) from process_error
finally:
ctl_socket.close()
Expand Down
4 changes: 2 additions & 2 deletions craft_parts/plugins/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ def _get_find_python_interpreter_commands(self) -> list[str]:
fi
else
# Otherwise use what _get_system_python_interpreter() told us.
echo "Python interpreter not found in payload."
echo "Python interpreter not found in payload." >&2
symlink_target="{python_interpreter}"
fi
if [ -z "$symlink_target" ]; then
echo "No suitable Python interpreter found, giving up."
echo "No suitable Python interpreter found, giving up." >&2
exit 1
fi
Expand Down
12 changes: 9 additions & 3 deletions tests/integration/plugins/test_poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,21 @@ def _get_system_python_interpreter(self) -> str | None:
actions = lf.plan(Step.PRIME)

out = pathlib.Path("out.txt")
with out.open(mode="w") as outfile, pytest.raises(errors.PluginBuildError):
with out.open(mode="w") as outfile, err.open(mode="w") as errfile, pytest.raises(errors.PluginBuildError):
with lf.action_executor() as ctx:
ctx.execute(actions, stdout=outfile)
ctx.execute(actions, stdout=outfile, stderr=errfile)

output = out.read_text()
expected_text = textwrap.dedent(
f"""\
Looking for a Python interpreter called "{real_basename}" in the payload...
Python interpreter not found in payload.
"""
)
assert expected_text in output

output = err.read_text()
expected_text = textwrap.dedent(
f"""\
No suitable Python interpreter found, giving up.
"""
)
Expand Down
13 changes: 10 additions & 3 deletions tests/integration/plugins/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,22 @@ def _get_system_python_interpreter(self) -> str | None:
actions = lf.plan(Step.PRIME)

out = Path("out.txt")
with out.open(mode="w") as outfile, pytest.raises(errors.PluginBuildError):
err = Path("err.txt")
with out.open(mode="w") as outfile, err.open(mode="w") as errfile, pytest.raises(errors.PluginBuildError):
with lf.action_executor() as ctx:
ctx.execute(actions, stdout=outfile)
ctx.execute(actions, stdout=outfile, stderr=errfile)

output = out.read_text()
expected_text = textwrap.dedent(
f"""\
Looking for a Python interpreter called "{real_basename}" in the payload...
Python interpreter not found in payload.
"""
)
assert expected_text in output

output = err.read_text()
expected_text = textwrap.dedent(
f"""\
No suitable Python interpreter found, giving up.
"""
)
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/plugins/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ def test_python_get_find_python_interpreter_commands(
fi
else
# Otherwise use what _get_system_python_interpreter() told us.
echo "Python interpreter not found in payload."
echo "Python interpreter not found in payload." >&2
symlink_target="$(readlink -f "$(which "${{PARTS_PYTHON_INTERPRETER}}")")"
fi
if [ -z "$symlink_target" ]; then
echo "No suitable Python interpreter found, giving up."
echo "No suitable Python interpreter found, giving up." >&2
exit 1
fi
Expand Down Expand Up @@ -253,12 +253,12 @@ def test_python_get_build_commands(new_dir, python_plugin: FooPythonPlugin):
fi
else
# Otherwise use what _get_system_python_interpreter() told us.
echo "Python interpreter not found in payload."
echo "Python interpreter not found in payload." >&2
symlink_target="$(readlink -f "$(which "${{PARTS_PYTHON_INTERPRETER}}")")"
fi
if [ -z "$symlink_target" ]; then
echo "No suitable Python interpreter found, giving up."
echo "No suitable Python interpreter found, giving up." >&2
exit 1
fi
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/plugins/test_base_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ def get_python_build_commands(
fi
else
# Otherwise use what _get_system_python_interpreter() told us.
echo "Python interpreter not found in payload."
echo "Python interpreter not found in payload." >&2
symlink_target="$(readlink -f "$(which "${{PARTS_PYTHON_INTERPRETER}}")")"
fi
if [ -z "$symlink_target" ]; then
echo "No suitable Python interpreter found, giving up."
echo "No suitable Python interpreter found, giving up." >&2
exit 1
fi
Expand Down

0 comments on commit 522e316

Please sign in to comment.