Repository navigation
Open
Description
Problem
Reading the documentation it is said that file is a subject, and later it is said that modifiers can be applied to subjects (no reported limit here):

Example
Given these info I would have expected the following to work:
# shellcheck shell=bash
setup () {
local temp_dir="$1"
%logger "INFO - Creating temporary directory: $temp_dir"
mkdir -p "$temp_dir"
}
cleanup () {
local temp_dir="$1"
%logger "INFO - Removing temporary directory: $temp_dir"
rm -rf "$temp_dir"
}
dummy_process () {
printf "id,column\n" > "$1/output.csv"
printf "%s,data\n" {1..20} >> "$1/output.csv"
}
ExampleGroup "[E2E] Script 'interface_report'" needs:server-on
temp_dir="$(mktemp --directory)"
BeforeEach "setup '$temp_dir'"
AfterEach "cleanup '$temp_dir'"
It 'outputs the csv'
When run dummy_process "$temp_dir"
# ------ OK ------
The status should be success
The file "${temp_dir}/output.csv" should be exist
The file "${temp_dir}/output.csv" should be readable
The contents of file "${temp_dir}/output.csv" should include "1,data"
# ------ FAIL ------
# However, the logger correctly shows `Lines of output file: 21`
%logger "Lines of output file: $(wc -l < "${temp_dir}/output.csv")"
The lines of file "${temp_dir}/output.csv" should equal 21
The line 1 of file "${temp_dir}/output.csv" should equal "id,column"
End
EndYou can try for yourself by saving it to a spec file.
Output:
Running: /usr/bin/bash [bash 5.1.4(1)-release]
INFO - Creating temporary directory: /tmp/tmp.vblrw8ETZh
Lines of output file: 21
INFO - Removing temporary directory: /tmp/tmp.vblrw8ETZh
[E2E] Script 'interface_report'
outputs the csv (FAILED - 1)
Examples:
1) [E2E] Script 'interface_report' outputs the csv
When run test_data /tmp/tmp.vblrw8ETZh
1.1) The line 1 of file /tmp/tmp.vblrw8ETZh/output.csv should equal id,column
expected: "id,column"
got: "/tmp/tmp.vblrw8ETZh/output.csv"
# spec/e2e/interface_spec.sh:37
1.2) The lines of file /tmp/tmp.vblrw8ETZh/output.csv should equal 21
expected: 21
got: 1
# spec/e2e/interface_spec.sh:38
Finished in 0.12 seconds (user 0.09 seconds, sys 0.02 seconds)
1 example, 1 failure
Is this something that can be implemented? I think it would be very useful while testing complex scripts that write logs or output files
Metadata
Metadata
Assignees
Labels
No labels
Activity
LukeSavefrogs commentedon Aug 21, 2024
Workaround
It seems like
filewhile used together withlines,lineor other unsupported modifiers is treated as ifvaluewas used:This lead to the following workaround, which uses
valuetogether with command substitution to perform the right check:Output:
LukeSavefrogs commentedon Aug 21, 2024
I think however that there should be an "official" way to do these kind of checks