2023-06-04 10:43:40 +02:00
|
|
|
use std *
|
2024-03-14 12:28:13 +01:00
|
|
|
use std log *
|
2023-06-04 10:43:40 +02:00
|
|
|
use commons.nu *
|
|
|
|
|
|
|
|
def run-command [
|
|
|
|
system_level: string,
|
|
|
|
message: string,
|
|
|
|
format: string,
|
|
|
|
log_level: int,
|
|
|
|
--level-prefix: string,
|
|
|
|
--ansi: string
|
|
|
|
] {
|
|
|
|
do {
|
|
|
|
if ($level_prefix | is-empty) {
|
|
|
|
if ($ansi | is-empty) {
|
2024-03-14 12:28:13 +01:00
|
|
|
^$nu.current-exe --commands $'use std; NU_log-level=($system_level) std log custom "($message)" "($format)" ($log_level)'
|
2023-06-04 10:43:40 +02:00
|
|
|
} else {
|
2024-03-14 12:28:13 +01:00
|
|
|
^$nu.current-exe --commands $'use std; NU_log-level=($system_level) std log custom "($message)" "($format)" ($log_level) --ansi "($ansi)"'
|
2023-06-04 10:43:40 +02:00
|
|
|
}
|
|
|
|
} else {
|
2024-03-14 12:28:13 +01:00
|
|
|
^$nu.current-exe --commands $'use std; NU_log-level=($system_level) std log custom "($message)" "($format)" ($log_level) --level-prefix "($level_prefix)" --ansi "($ansi)"'
|
2023-06-04 10:43:40 +02:00
|
|
|
}
|
|
|
|
} | complete | get --ignore-errors stderr
|
|
|
|
}
|
|
|
|
|
2023-07-02 10:41:33 +02:00
|
|
|
#[test]
|
|
|
|
def errors_during_deduction [] {
|
2023-06-04 10:43:40 +02:00
|
|
|
assert str contains (run-command "DEBUG" "msg" "%MSG%" 25) "Cannot deduce log level prefix for given log level"
|
|
|
|
assert str contains (run-command "DEBUG" "msg" "%MSG%" 25 --ansi (ansi red)) "Cannot deduce log level prefix for given log level"
|
|
|
|
assert str contains (run-command "DEBUG" "msg" "%MSG%" 25 --level-prefix "abc") "Cannot deduce ansi for given log level"
|
|
|
|
}
|
|
|
|
|
2023-07-02 10:41:33 +02:00
|
|
|
#[test]
|
|
|
|
def valid_calls [] {
|
2023-06-04 10:43:40 +02:00
|
|
|
assert equal (run-command "DEBUG" "msg" "%MSG%" 25 --level-prefix "abc" --ansi (ansi default) | str trim --right) "msg"
|
2024-03-14 12:28:13 +01:00
|
|
|
assert equal (run-command "DEBUG" "msg" "%LEVEL% %MSG%" 20 | str trim --right) $"((log-prefix).INFO) msg"
|
2023-06-04 10:43:40 +02:00
|
|
|
assert equal (run-command "DEBUG" "msg" "%LEVEL% %MSG%" --level-prefix "abc" 20 | str trim --right) "abc msg"
|
2024-03-14 12:28:13 +01:00
|
|
|
assert equal (run-command "INFO" "msg" "%ANSI_START%%LEVEL% %MSG%%ANSI_STOP%" ((log-level).CRITICAL) | str trim --right) $"((log-ansi).CRITICAL)CRT msg(ansi reset)"
|
2023-06-04 10:43:40 +02:00
|
|
|
}
|
|
|
|
|
2023-07-02 10:41:33 +02:00
|
|
|
#[test]
|
2024-03-14 12:28:13 +01:00
|
|
|
def log-level_handling [] {
|
|
|
|
assert equal (run-command "DEBUG" "msg" "%LEVEL% %MSG%" 20 | str trim --right) $"((log-prefix).INFO) msg"
|
2023-06-04 10:43:40 +02:00
|
|
|
assert equal (run-command "WARNING" "msg" "%LEVEL% %MSG%" 20 | str trim --right) ""
|
2023-06-10 20:16:17 +02:00
|
|
|
}
|