Skip to content

Commit 099e377

Browse files
committed
feat: Print functions no longer show fn name of callee
1 parent d924f7f commit 099e377

File tree

3 files changed

+82
-28
lines changed

3 files changed

+82
-28
lines changed

bake

+3-3
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,9 @@ __bake_main() {
346346

347347
set -ETeo pipefail
348348
shopt -s dotglob extglob globasciiranges globstar lastpipe shift_verbose
349-
export LANG='C' LC_CTYPE='C' LC_NUMERIC='C' LC_TIME='C' LC_COLLATE='C' \
350-
LC_MONETARY='C' LC_MESSAGES='C' LC_PAPER='C' LC_NAME='C' LC_ADDRESS='C' \
351-
LC_TELEPHONE='C' LC_MEASUREMENT='C' LC_IDENTIFICATION='C' LC_ALL='C'
349+
export LANG='C' LC_CTYPE='C' LC_NUMERIC='C' LC_TIME='C' LC_COLLATE='C' LC_MONETARY='C' \
350+
LC_MESSAGES='C' LC_PAPER='C' LC_NAME='C' LC_ADDRESS='C' LC_TELEPHONE='C' \
351+
LC_MEASUREMENT='C' LC_IDENTIFICATION='C' LC_ALL='C'
352352
trap '__bake_trap_err' 'ERR'
353353
bake.cfg pedantic-task-cd 'no'
354354

pkg/src/public/bash-core.sh

+43-19
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,26 @@ core.err_exists() {
241241
fi
242242
}
243243

244+
# @description Use when a serious fault occurs. It will print the current ERR (if it exists)
245+
core.panic() {
246+
local code='1'
247+
248+
if [[ $1 =~ [0-9]+ ]]; then
249+
code=$1
250+
elif [ -n "$1" ]; then
251+
if [ -n "$2" ]; then
252+
code=$2
253+
fi
254+
printf '%s\n' "Panic: $1" >&2
255+
fi
256+
257+
if core.err_exists; then
258+
core.util.err_print
259+
fi
260+
core.print_stacktrace
261+
exit "$code"
262+
}
263+
244264
# @description Prints stacktrace
245265
# @noargs
246266
# @example
@@ -285,46 +305,50 @@ core.print_stacktrace() {
285305

286306
# @description Print an error message to standard error
287307
# @arg $1 string message
288-
core.print_error() {
308+
core.print_error_fn() {
289309
local msg="$1"
290310

291311
printf '%s\n' "Error: ${FUNCNAME[1]}${msg:+": "}$msg" >&2
292312
}
293313

294314
# @description Print a warning message to standard error
295315
# @arg $1 string message
296-
core.print_warn() {
316+
core.print_warn_fn() {
297317
local msg="$1"
298318

299319
printf '%s\n' "Warn: ${FUNCNAME[1]}${msg:+": "}$msg" >&2
300320
}
301321

302322
# @description Print an informative message to standard output
303323
# @arg $1 string message
304-
core.print_info() {
324+
core.print_info_fn() {
305325
local msg="$1"
306326

307327
printf '%s\n' "Info: ${FUNCNAME[1]}${msg:+": "}$msg"
308328
}
309329

310-
# @description Use when a serious fault occurs. It will print the current ERR (if it exists)
311-
core.panic() {
312-
local code='1'
330+
# @description Print an error message to standard error
331+
# @arg $1 string message
332+
core.print_error() {
333+
local msg="$1"
313334

314-
if [[ $1 =~ [0-9]+ ]]; then
315-
code=$1
316-
elif [ -n "$1" ]; then
317-
if [ -n "$2" ]; then
318-
code=$2
319-
fi
320-
printf '%s\n' "Panic: $1" >&2
321-
fi
335+
printf '%s\n' "Error${msg:+": "}$msg" >&2
336+
}
322337

323-
if core.err_exists; then
324-
core.util.err_print
325-
fi
326-
core.print_stacktrace
327-
exit "$code"
338+
# @description Print a warning message to standard error
339+
# @arg $1 string message
340+
core.print_warn() {
341+
local msg="$1"
342+
343+
printf '%s\n' "Warn${msg:+": "}$msg" >&2
344+
}
345+
346+
# @description Print an informative message to standard output
347+
# @arg $1 string message
348+
core.print_info() {
349+
local msg="$1"
350+
351+
printf '%s\n' "Info${msg:+": "}$msg"
328352
}
329353

330354
# @description Determine if color should be printed. Note that this doesn't

tests/print.bats

+36-6
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,62 @@ errorfn() { "$@"; }
99
warnfn() { "$@"; }
1010
infofn() { "$@"; }
1111

12+
@test "core.print_error_fn works" {
13+
run errorfn core.print_error_fn
14+
assert_success
15+
assert_output 'Error: errorfn'
16+
17+
run errorfn core.print_error_fn 'Something'
18+
assert_success
19+
assert_output 'Error: errorfn: Something'
20+
}
21+
22+
@test "core.print_warn_fn works" {
23+
run warnfn core.print_warn_fn
24+
assert_success
25+
assert_output 'Warn: warnfn'
26+
27+
run warnfn core.print_warn_fn 'Something'
28+
assert_success
29+
assert_output 'Warn: warnfn: Something'
30+
}
31+
32+
@test "core.print_info_fn works" {
33+
run infofn core.print_info_fn
34+
assert_success
35+
assert_output 'Info: infofn'
36+
37+
run infofn core.print_info_fn 'Something'
38+
assert_success
39+
assert_output 'Info: infofn: Something'
40+
}
41+
1242
@test "core.print_error works" {
1343
run errorfn core.print_error
1444
assert_success
15-
assert_output 'Error: errorfn'
45+
assert_output 'Error'
1646

1747
run errorfn core.print_error 'Something'
1848
assert_success
19-
assert_output 'Error: errorfn: Something'
49+
assert_output 'Error: Something'
2050
}
2151

2252
@test "core.print_warn works" {
2353
run warnfn core.print_warn
2454
assert_success
25-
assert_output 'Warn: warnfn'
55+
assert_output 'Warn'
2656

2757
run warnfn core.print_warn 'Something'
2858
assert_success
29-
assert_output 'Warn: warnfn: Something'
59+
assert_output 'Warn: Something'
3060
}
3161

3262
@test "core.print_info works" {
3363
run infofn core.print_info
3464
assert_success
35-
assert_output 'Info: infofn'
65+
assert_output 'Info'
3666

3767
run infofn core.print_info 'Something'
3868
assert_success
39-
assert_output 'Info: infofn: Something'
69+
assert_output 'Info: Something'
4070
}

0 commit comments

Comments
 (0)