From a3e00b02835b1b894e1e46bb22a6911d656bc4b1 Mon Sep 17 00:00:00 2001 From: narayana006 <69020110+narayana006@users.noreply.github.com> Date: Mon, 30 Nov 2026 16:13:20 -0500 Subject: [PATCH] fix(ruby): decode Bundler output without hiding build errors --- .../workflows/ruby_bundler/bundler.py | 8 +-- .../ruby_bundler/test_bundler_output.py | 53 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 tests/functional/workflows/ruby_bundler/test_bundler_output.py diff --git a/aws_lambda_builders/workflows/ruby_bundler/bundler.py b/aws_lambda_builders/workflows/ruby_bundler/bundler.py index fe3f46859..fa1bfa6ed 100644 --- a/aws_lambda_builders/workflows/ruby_bundler/bundler.py +++ b/aws_lambda_builders/workflows/ruby_bundler/bundler.py @@ -5,6 +5,8 @@ import logging from os import linesep +from aws_lambda_builders.utils import decode + LOG = logging.getLogger(__name__) """ @@ -67,8 +69,8 @@ def run(self, args, cwd=None): self.osutils.remove_directory(check_dir) else: # Bundler can contain information in both stdout and stderr so we check and log both - err_str = err.decode("utf8").strip() - out_str = out.decode("utf8").strip() + err_str = decode(err) + out_str = decode(out) if out_str and err_str: message_out = f"{out_str}{linesep}{err_str}" LOG.debug(f"Bundler output: {out_str}") @@ -81,4 +83,4 @@ def run(self, args, cwd=None): LOG.debug(f"Bundler error: {err_str}") raise BundlerExecutionError(message=message_out) - return out.decode("utf8").strip() + return decode(out) diff --git a/tests/functional/workflows/ruby_bundler/test_bundler_output.py b/tests/functional/workflows/ruby_bundler/test_bundler_output.py new file mode 100644 index 000000000..ecfc62959 --- /dev/null +++ b/tests/functional/workflows/ruby_bundler/test_bundler_output.py @@ -0,0 +1,53 @@ +import os +import sys +from unittest.mock import patch + +import pytest + +from aws_lambda_builders.workflows.ruby_bundler.bundler import BundlerExecutionError, SubprocessBundler +from aws_lambda_builders.workflows.ruby_bundler.utils import OSUtils + + +def run_with_output(stdout, stderr, returncode, cwd): + # Exercise real process pipes without requiring Ruby or contacting a gem index. + program = ( + f"import sys; sys.stdout.buffer.write({stdout!r}); " + f"sys.stderr.buffer.write({stderr!r}); sys.exit({returncode})" + ) + return SubprocessBundler(OSUtils(), bundler_exe=sys.executable).run(["-c", program], cwd=str(cwd)) + + +@pytest.mark.parametrize( + "encoding,output,expected", + [("cp1252", b"install\xe9\n", "installé"), ("utf-8", b"done\xff\n", "done�")], +) +def test_successful_output_uses_safe_locale_decoding(tmp_path, encoding, output, expected): + with patch("aws_lambda_builders.utils._locale_encoding", return_value=encoding): + assert run_with_output(output, b"", 0, tmp_path) == expected + + +@pytest.mark.parametrize( + "encoding,stdout,stderr,expected", + [ + ("cp1252", b"", b"failed\xe9\n", "failedé"), + ("utf-8", b"context\xff\n", b"failed\xff\n", f"context�{os.linesep}failed�"), + ], +) +def test_failed_output_preserves_the_bundler_error(tmp_path, encoding, stdout, stderr, expected): + with patch("aws_lambda_builders.utils._locale_encoding", return_value=encoding): + with pytest.raises(BundlerExecutionError) as caught: + run_with_output(stdout, stderr, 1, tmp_path) + assert str(caught.value) == f"Bundler Failed: {expected}" + + +def test_missing_gemfile_still_cleans_up_and_continues(tmp_path, caplog): + bundle_dir = tmp_path / ".bundle" + bundle_dir.mkdir() + with patch("aws_lambda_builders.utils._locale_encoding", return_value="utf-8"): + assert run_with_output(b"no manifest\xff\n", b"", 10, tmp_path) == "no manifest�" + assert not bundle_dir.exists() + assert "Continuing the build without dependencies" in caplog.text + + +def test_plain_output_keeps_whitespace_handling(tmp_path): + assert run_with_output(b" installed\n\n", b"ignored", 0, tmp_path) == "installed"