fix(logging): keep diagnostics out of captured values (#12)
Keep diagnostics out of captured helper output
This commit is contained in:
+2
-2
@@ -32,7 +32,7 @@ log() {
|
|||||||
|
|
||||||
local ts
|
local ts
|
||||||
ts=$(date '+%H:%M:%S')
|
ts=$(date '+%H:%M:%S')
|
||||||
echo -e "${color}[${label}]${NC} ${msg}"
|
echo -e "${color}[${label}]${NC} ${msg}" >&2
|
||||||
|
|
||||||
if [ -n "$LOG_FILE" ]; then
|
if [ -n "$LOG_FILE" ]; then
|
||||||
echo "[${ts}] [${label}] ${msg}" >> "$LOG_FILE" 2>/dev/null || true
|
echo "[${ts}] [${label}] ${msg}" >> "$LOG_FILE" 2>/dev/null || true
|
||||||
@@ -40,7 +40,7 @@ log() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
error_exit() {
|
error_exit() {
|
||||||
log "ERROR" "$1" >&2
|
log "ERROR" "$1"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
# Tests for colors.sh - run with: bats tests/test_colors.bats
|
# Tests for colors.sh - run with: bats tests/test_colors.bats
|
||||||
|
|
||||||
setup() {
|
setup() {
|
||||||
|
bats_require_minimum_version 1.5.0
|
||||||
load '../lib/colors.sh'
|
load '../lib/colors.sh'
|
||||||
TEST_LOG=$(mktemp)
|
TEST_LOG=$(mktemp)
|
||||||
}
|
}
|
||||||
@@ -46,3 +47,33 @@ teardown() {
|
|||||||
[[ "$output" == *"╔═══════╗"* ]]
|
[[ "$output" == *"╔═══════╗"* ]]
|
||||||
[[ "$output" == *"╚═══════╝"* ]]
|
[[ "$output" == *"╚═══════╝"* ]]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "diagnostics use stderr without contaminating helper return values" {
|
||||||
|
local source_file
|
||||||
|
for source_file in lib/colors.sh unleash-standalone.sh; do
|
||||||
|
run --separate-stderr bash -u -c '
|
||||||
|
source <(sed "/^error_exit()/,\$d" "$1")
|
||||||
|
volume_path() {
|
||||||
|
log INFO "Locating Data volume"
|
||||||
|
log OK "Data volume found"
|
||||||
|
printf "%s\n" "/Volumes/Work Data"
|
||||||
|
}
|
||||||
|
result=$(volume_path)
|
||||||
|
printf "%s\n" "$result"
|
||||||
|
' bash "$BATS_TEST_DIRNAME/../$source_file"
|
||||||
|
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[ "$output" = "/Volumes/Work Data" ]
|
||||||
|
[[ "$stderr" == *"Locating Data volume"* ]]
|
||||||
|
[[ "$stderr" == *"Data volume found"* ]]
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "diagnostics still reach the log file when stdout is captured" {
|
||||||
|
LOG_FILE="$TEST_LOG"
|
||||||
|
local result
|
||||||
|
result=$(log WARN "Review the selected volume" 2>/dev/null)
|
||||||
|
|
||||||
|
[ -z "$result" ]
|
||||||
|
grep -q 'Review the selected volume' "$TEST_LOG"
|
||||||
|
}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ log() {
|
|||||||
|
|
||||||
local ts
|
local ts
|
||||||
ts=$(date '+%H:%M:%S')
|
ts=$(date '+%H:%M:%S')
|
||||||
echo -e "${color}[${label}]${NC} ${msg}"
|
echo -e "${color}[${label}]${NC} ${msg}" >&2
|
||||||
|
|
||||||
if [ -n "$LOG_FILE" ]; then
|
if [ -n "$LOG_FILE" ]; then
|
||||||
echo "[${ts}] [${label}] ${msg}" >> "$LOG_FILE" 2>/dev/null || true
|
echo "[${ts}] [${label}] ${msg}" >> "$LOG_FILE" 2>/dev/null || true
|
||||||
@@ -43,7 +43,7 @@ log() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
error_exit() {
|
error_exit() {
|
||||||
log "ERROR" "$1" >&2
|
log "ERROR" "$1"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user