Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions aws_lambda_builders/workflows/ruby_bundler/bundler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import logging
from os import linesep

from aws_lambda_builders.utils import decode

LOG = logging.getLogger(__name__)

"""
Expand Down Expand Up @@ -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}")
Expand All @@ -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)
53 changes: 53 additions & 0 deletions tests/functional/workflows/ruby_bundler/test_bundler_output.py
Original file line number Diff line number Diff line change
@@ -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"