You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a pretty niche question.
I have some function in my shell script that takes a while to complete.
I show a little spinner, then clear the line it appears on. On my end, this works fine, but I wanted to write a unit test for it.
I haven't really been able to figure out how to accomplish this.
I clear the spinner char from stderr with printf "\r\033[K" >&2
When I try
The stderr should include "\r\033[K"
or
The stderr should include "\r"
My tests fail with a quite result that is very hard to interpret, due to the use of invisible characters:
Examples:
1) fetch_and_wait_for_ai_response returns the response from the aichat command with stderr spinner
When call fetch_and_wait_for_ai_response Sample prompt
1.1) The stderr should include \r
" to include "\r"
# spec/fetch_and_wait_for_ai_response_spec.sh:26
Finished in 0.92 seconds (user 0.61 seconds, sys 0.26 seconds)
16 examples, 1 failure
Right now, my workaround is just to mock the spinner and mock clear_spinner, which I guess is 'fine'. But I wanted to post here just in case someone actually knows how to do this!
The script:
# src/lib/fetch_and_wait_for_ai_response.sh
spinner() {
local pid=$1
local delay=0.1
local spin_chars=(⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏)
local i=0
while kill -0 "$pid" 2>/dev/null; do
printf "\r%s" "${spin_chars[$i]}" >&2
i=$(((i + 1) % ${#spin_chars[@]}))
sleep "$delay"
done
}
clear_spinner() {
printf "\r\033[K" >&2
}
fetch_and_wait_for_ai_response() {
local prompt="$1"
local response_file
response_file=$(mktemp)
# Run the actual AI fetch
fetch_ai_response "$prompt" >"$response_file" &
local fetch_pid=$!
# Show spinner on the user's terminal, not captured stdout
spinner "$fetch_pid" &
local spinner_pid=$!
wait "$fetch_pid"
kill "$spinner_pid" 2>/dev/null
wait "$spinner_pid" 2>/dev/null || true
# Clear spinner line from terminal
clear_spinner
# Print response to stdout (this is what gets captured)
cat "$response_file"
rm "$response_file"
}
My current test:
Describe 'fetch_and_wait_for_ai_response'
Include src/lib/fetch_and_wait_for_ai_response.sh
Include spec/helpers/load_default_env.sh
Include spec/helpers/unload_default_env.sh
setup() {
load_default_env
}
teardown() {
rm -rf "${CFME_CONFIG_DIR}"
unload_default_env
}
BeforeEach 'setup'
AfterEach 'teardown'
Mock spinner
echo "SPINNER_STARTED" >&2
End
Mock clear_spinner
echo "SPINNER_CLEARED" >&2
End
Mock fetch_ai_response
sleep 0.2
echo "SAMPLE_RESPONSE"
End
It 'returns the response from the aichat command with stderr spinner'
When call fetch_and_wait_for_ai_response "Sample prompt"
The output should equal "SAMPLE_RESPONSE"
The stderr should include "SPINNER_STARTED"
The stderr should include "SPINNER_CLEARED"
End
End
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
This is a pretty niche question.
I have some function in my shell script that takes a while to complete.
I show a little spinner, then clear the line it appears on. On my end, this works fine, but I wanted to write a unit test for it.
I haven't really been able to figure out how to accomplish this.
I clear the spinner char from
stderrwithprintf "\r\033[K" >&2When I try
or
My tests fail with a quite result that is very hard to interpret, due to the use of invisible characters:
Right now, my workaround is just to mock the
spinnerand mockclear_spinner, which I guess is 'fine'. But I wanted to post here just in case someone actually knows how to do this!The script:
My current test:
Beta Was this translation helpful? Give feedback.
All reactions