Skip to content

Commit

Permalink
feat: Add multiline support for log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
marverix committed Feb 3, 2025
1 parent 80bec06 commit 76acb65
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/log_helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,34 @@ function echo_err() {
}

function _log_x() {
local prefix=""
local lines=""

if [[ -n "$GITHUB_ACTIONS" ]]; then
prefix="::${1}"

if [[ -n "$3" ]]; then
echo "::${1} title=${2}::$3"
prefix="$prefix title=${2}::"
lines="$3"
else
echo "::${1}::$2"
prefix="$prefix::"
lines="$2"
fi
else
prefix="[${1^^}]"

if [[ -n "$3" ]]; then
echo_err "[${1^^}] ${2}: ${3}"
prefix="$prefix ${2}: "
lines="$3"
else
echo_err "[${1^^}] ${2}"
prefix="$prefix "
lines="$2"
fi
fi

for line in $(echo -e "$lines"); do
echo_err "${prefix}${line}"
done
}

function log_error() {
Expand Down
38 changes: 38 additions & 0 deletions test/log_helpers.bats
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ teardown() {
assert_output "[ERROR] Foo"
}

@test "log_error should print multiple errors when multiline message provided (using \n)" {
run log_error "Foo" "Bar\nBaz"

assert_success
assert_output "[ERROR] Foo: Bar
[ERROR] Foo: Baz"
}

@test "log_error should print multiple errors when multiline message provided (using multiple arguments)" {
run log_error "Foo" "Bar
Baz"

assert_success
assert_output "[ERROR] Foo: Bar
[ERROR] Foo: Baz"
}

@test "log_error should print GHA error command with title" {
GITHUB_ACTIONS="true"

Expand All @@ -43,6 +60,27 @@ teardown() {
assert_output "::error::Foo"
}

@test "log_error should print GHA error command with multiple errors when multiline message provided (using \n)" {
GITHUB_ACTIONS="true"

run log_error "Foo" "Bar\nBaz"

assert_success
assert_output "::error title=Foo::Bar
::error title=Foo::Baz"
}

@test "log_error should print GHA error command with multiple errors when multiline message provided (using multiple arguments)" {
GITHUB_ACTIONS="true"

run log_error "Foo" "Bar
Baz"

assert_success
assert_output "::error title=Foo::Bar
::error title=Foo::Baz"
}

@test "log_warning should print warning with title" {
run log_warning "Foo" "Bar"

Expand Down

0 comments on commit 76acb65

Please sign in to comment.