shellspec
shellspec copied to clipboard
Pipping echo command prints to output
Hello!
I wrote this example, I think it's self explanatory
% LOG_FILE: ./test
remove_logs() {
rm $LOG_FILE
}
Context 'log'
AfterEach 'remove_logs'
File log_file="$LOG_FILE"
It 'should log a message to file'
When run command echo 'test message' >> "$LOG_FILE"
The file log_file should be a file
The file log_file should not be a empty file
The file log_file should be readable
The length of output should equal 0
End
End
The result I got:
Randomized with seed 93493
Running: /bin/bash [bash 5.0.3(1)-release] {--random examples}
F
Examples:
1) log should log a message to file
When run command echo test message
1.1) The file log_file should not be a empty file
The specified path is empty file
path: ./test
# spec/test_spec.sh:259
1.2) The length of output should equal 0
expected: 0
got: 12
# spec/test_spec.sh:261
Finished in 0.12 seconds (user 0.12 seconds, sys 0.03 seconds)
1 example, 1 failure
Failure examples / Errors: (Listed here affect your suite's status)
shellspec spec/test_spec.sh:255 # 1) log should log a message to file FAILED
Using Dump I found out that the echo command is printed to output. I expected a success, is this a bug or I'm doing it wrong?
I don't think evaluation line "When" can resolve redirection. You can otherwise encapsuled this in a function and then call it.
LOG_FILE="$SHELLSPEC_TMPDIR/test"
remove_logs() {
rm "$LOG_FILE"
}
Context 'log'
AfterEach 'remove_logs'
File log_file="$LOG_FILE"
log_to_file(){
echo 'test message' >> "$LOG_FILE"
}
It 'should log a message to file'
When call echo 'toto' >> "$LOG_FILE"
The file log_file should be a file
The file log_file should not be a empty file
The file log_file should be readable
The length of output should equal 0
End
End
Hello JConan! Thanks for your reply. After some study I came to a passing answer of the same type of yours. Pasting the full test here:
# Constants accessed from functions
% DATE_STR: dom 26 mar 2023 22:38:45
% LOG_DIR: .
% LOG_PATH: "$LOG_DIR/$DATE_STR test_env_builder.log"
#######################################
Mock date
if [ ! -e "$date_lock" ]; then
echo $DATE_STR
touch "$date_lock"
else
echo dom 26 mar 2023 22:38:46
fi
End
export "DATE_STR"
Context 'log'
Include $script_file
Context 'usage: caller'
It 'should redirect the stdout to the file setted by log'
usage() {
log
echo 'test message' &>> "$LOG_FILE"
}
When call usage
The file "$LOG_PATH" should be a file
The file "$LOG_PATH" should not be a empty file
The file "$LOG_PATH" should be readable
The output should equal ""
End
End
It 'should set the log file with the right format'
When call log
The variable "LOG_FILE" should equal "$LOG_PATH"
End
It 'should set the log file path one time only'
log_twice() {
log
log
}
When call log_twice
The variable "LOG_FILE" should equal "$LOG_PATH"
End
End