Commit 265be51d authored by Ed Morley's avatar Ed Morley Committed by Jeremy Morrell

Adjust output() so it preserves leading whitespace (#572)

* Adjust output() so it preserves leading whitespace

Unless `IFS` is adjusted, `read` strips leading and trailing
whitespace from each line. In addition, without the `-r` argument
to `read`, unescaped backslashes are discarded.

See:
http://mywiki.wooledge.org/BashFAQ/001

Fixes #571.
parent 6b12e59c
# TODO: Merge these with the output helpers in buildpack-stdlib:
# https://github.com/heroku/buildpack-stdlib
info() { info() {
echo " $*" || true echo " $*" || true
} }
...@@ -6,7 +9,7 @@ info() { ...@@ -6,7 +9,7 @@ info() {
output() { output() {
local logfile="$1" local logfile="$1"
while read LINE; while IFS= read -r LINE;
do do
# do not indent headers that are being piped through the output # do not indent headers that are being piped through the output
if [[ "$LINE" =~ ^-----\>.* ]]; then if [[ "$LINE" =~ ^-----\>.* ]]; then
......
...@@ -32,12 +32,23 @@ testMonitorMemory() { ...@@ -32,12 +32,23 @@ testMonitorMemory() {
monitor_memory_usage $mem_output print_args --foo --bar="baz lol hi" > $stdout_capture monitor_memory_usage $mem_output print_args --foo --bar="baz lol hi" > $stdout_capture
assertTrue "should use less than 2mb" "[[ $(cat $mem_output) -lt 2 ]]" assertTrue "should use less than 2mb" "[[ $(cat $mem_output) -lt 2 ]]"
assertTrue "should output 2 lines" "[[ $(wc -l < $stdout_capture) -eq 2 ]]" assertTrue "should output 2 lines" "[[ $(wc -l < $stdout_capture) -eq 2 ]]"
assertEquals "first line" "$(head -n 1 $stdout_capture)" "--foo" assertEquals "first line" "--foo" "$(head -n 1 $stdout_capture)"
assertEquals "second line" "$(tail -n 1 $stdout_capture)" "--bar=baz lol hi" assertEquals "second line" "--bar=baz lol hi" "$(tail -n 1 $stdout_capture)"
}
testOutput() {
local stdout
stdout=$(echo ' Indented line' | output /dev/null)
assertEquals 'should preserve leading whitespace' ' Indented line' "${stdout}"
stdout=$(echo 'Foo \ bar' | output /dev/null)
assertEquals 'should preserve unescaped backslashes' ' Foo \ bar' "${stdout}"
} }
# the modules to be tested # the modules to be tested
source "$(pwd)"/lib/monitor.sh source "$(pwd)"/lib/monitor.sh
source "$(pwd)"/lib/output.sh
# import the testing framework # import the testing framework
source "$(pwd)"/test/shunit2 source "$(pwd)"/test/shunit2
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment