-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathff-test
More file actions
executable file
·308 lines (282 loc) · 10.7 KB
/
ff-test
File metadata and controls
executable file
·308 lines (282 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/sh
# ff-test: comprehensive test suite for the ff utility, which should be
# located in the same directory as this script. This test script creates
# temporary directories and files to verify ff's behaviors.
#
# Usage: ff-test
# Copyright (c) 2025, Malcolm Inglis <https://minglis.id.au>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
set -eu
FF_PATH="$(cd "$(dirname "$0")" && pwd)/ff"
if [ ! -x "$FF_PATH" ]; then
printf 'error: %s is not executable\n' "$FF_PATH"
exit 1
fi
ON_WINDOWS=false
if uname -s | grep -q '^MINGW\|^MSYS\|^CYGWIN'; then
ON_WINDOWS=true
fi
TESTS_RUN=0
TESTS_PASSED=0
TESTS_FAILED=0
sort_paths() {
printf '%s' "$1" | LC_COLLATE=C sort
}
run_test() {
# Parse arguments:
_test_name="${1:?error: test name is required}"
_test_command="${2:?error: test command is required}"
shift 2
_expected_stdout=''
_expected_stderr=''
_stdout_pattern=''
while [ $# -gt 0 ]; do
case "$1" in
--stdout)
_expected_stdout="$2"
shift
;;
--stdout-windows-extra)
if [ "$ON_WINDOWS" = "true" ]; then
_expected_stdout="$(printf '%s\n%s' "$_expected_stdout" "$2")"
fi
shift
;;
--stderr)
_expected_stderr="$2"
shift
;;
--stdout-pattern)
_stdout_pattern="$2"
shift
;;
*)
echo "run_test: unknown option '$1'" >&2
return 1
;;
esac
shift
done
# Normalize expected stdout sort ordering:
# Print test pre-amble:
TESTS_RUN=$((TESTS_RUN + 1))
# Run command and capture its stdout, stderr and exit code:
_stdout_file="$(mktemp)"
_stderr_file="$(mktemp)"
_exit_code=0
eval "$_test_command" >"$_stdout_file" 2>"$_stderr_file" || _exit_code=$?
# Check the command's output:
_test_stdout="$(cat "$_stdout_file")"
_test_stderr="$(cat "$_stderr_file")"
_test_passed=true
_failure_reason=""
# Check stdout - either exact match or pattern match:
if [ -n "$_expected_stdout" ]; then
if [ "$(sort_paths "$_test_stdout")" != "$(sort_paths "$_expected_stdout")" ]; then
_test_passed=false
_failure_reason="stdout mismatch"
fi
elif [ -n "$_stdout_pattern" ]; then
if ! echo "$_test_stdout" | grep -q "$_stdout_pattern"; then
_test_passed=false
_failure_reason="stdout pattern not found"
fi
fi
# Check stderr - either exact match or pattern match:
if [ -n "$_expected_stderr" ]; then
if [ "$_test_stderr" != "$_expected_stderr" ]; then
_test_passed=false
_failure_reason="${_failure_reason:+$_failure_reason; }stderr mismatch"
fi
fi
# Output success or failure per results:
if [ "$_test_passed" = true ]; then
TESTS_PASSED=$((TESTS_PASSED + 1))
printf 'ok %s - %s\n' "$TESTS_RUN" "$_test_name"
else
TESTS_FAILED=$((TESTS_FAILED + 1))
printf 'not ok %s - %s: %s\n' "$TESTS_RUN" "$_test_name" "$_failure_reason"
printf ' ---\n'
if [ -n "$_stdout_pattern" ]; then
echo " expected stdout pattern:"
printf '%s\n' "$_stdout_pattern" | sed 's/^/ > /'
elif [ -n "$_expected_stdout" ]; then
echo " expected stdout:"
printf '%s\n' "$_expected_stdout" | sed 's/^/ > /'
fi
echo " actual stdout:"
printf '%s\n' "$_test_stdout" | sed 's/^/ < /'
if [ -n "$_expected_stderr" ]; then
echo " expected stderr:"
printf '%s\n' "$_expected_stderr" | sed 's/^/ > /'
else
echo " expected no stderr"
fi
echo " actual stderr:"
printf '%s\n' "$_test_stderr" | sed 's/^/ < /'
printf ' ...\n'
fi
# Clean up temp files:
rm -f "$_stdout_file" "$_stderr_file"
}
setup_test_dir() {
TEST_DIR=$(mktemp -d)
cd "$TEST_DIR"
touch file1.txt
touch file2.py
touch file3.log
touch script.sh
chmod +x script.sh
touch README.md
touch config
touch .hidden_file
touch image.jpg
touch video.mp4
touch archive.tar.gz
touch 'file with spaces.txt'
touch file-with-dashes.txt
touch file_with_underscores.txt
mkdir -p dir1/subdir1
touch dir1/nested_file.txt
touch dir1/subdir1/deep_file.py
mkdir -p dir2/subdir2
touch dir2/another_file.log
mkdir -p .hidden_dir
touch .hidden_dir/secret.txt
mkdir -p deep/nested/structure/very/deep
touch deep/nested/structure/very/deep/buried.txt
ln -s file1.txt link_to_file1
ln -s dir1 link_to_dir1
}
cleanup_test_dir() {
if [ -d "$TEST_DIR" ]; then
rm -rf "$TEST_DIR"
fi
}
test_basic_functionality() {
run_test "List specific file" "$FF_PATH file1.txt" --stdout "./file1.txt"
run_test "Simple pattern - just script files" "$FF_PATH script" --stdout "./script.sh"
run_test "Pattern with extension" "$FF_PATH '*.py'" \
--stdout "./dir1/subdir1/deep_file.py
./file2.py" \
--stdout-windows-extra "./link_to_dir1/subdir1/deep_file.py"
run_test "Pattern with glob" "$FF_PATH 'config*'" --stdout "./config"
run_test "All files with 'file' in name" "$FF_PATH -d 2 file" \
--stdout "./dir1/nested_file.txt
./dir2/another_file.log
./file with spaces.txt
./file-with-dashes.txt
./file1.txt
./file2.py
./file3.log
./file_with_underscores.txt
./link_to_file1" \
--stdout-windows-extra "./link_to_dir1/nested_file.txt"
}
test_options() {
run_test "Hidden files included" "$FF_PATH -H hidden" --stdout "./.hidden_dir
./.hidden_file"
run_test "Hidden files excluded by default" "$FF_PATH hidden" --stdout ""
run_test "Type file - limited to specific files" "$FF_PATH -t f -e txt file1" --stdout "./file1.txt"
run_test "Type directory" "$FF_PATH -t d dir" \
--stdout "./dir1
./dir1/subdir1
./dir2
./dir2/subdir2" \
--stdout-windows-extra "./link_to_dir1
./link_to_dir1/subdir1"
run_test "Extension py" "$FF_PATH -e py" \
--stdout "./dir1/subdir1/deep_file.py
./file2.py" \
--stdout-windows-extra "./link_to_dir1/subdir1/deep_file.py"
run_test "Extension txt" "$FF_PATH -e txt file1" --stdout "./file1.txt"
run_test "Max depth 1" "$FF_PATH -d 1 -t f script" --stdout "./script.sh"
run_test "Max depth 2" "$FF_PATH -d 2 nested" \
--stdout "./deep/nested
./dir1/nested_file.txt" \
--stdout-windows-extra "./link_to_dir1/nested_file.txt"
run_test "Full path matching" "$FF_PATH -p dir1" \
--stdout "./dir1
./dir1/nested_file.txt
./dir1/subdir1
./dir1/subdir1/deep_file.py
./link_to_dir1" \
--stdout-windows-extra "./link_to_dir1/nested_file.txt
./link_to_dir1/subdir1
./link_to_dir1/subdir1/deep_file.py"
run_test "Exclude pattern - log files" "$FF_PATH -E '*log*' script" --stdout "./script.sh"
run_test "Follow links" "$FF_PATH -L -t f link" --stdout "./link_to_file1"
}
test_help_and_version() {
run_test "Help option" "$FF_PATH --help" --stdout-pattern "Usage: $(basename "$FF_PATH")"
run_test "Help short option" "$FF_PATH -h" --stdout-pattern "Usage: $(basename "$FF_PATH")"
run_test "Version option" "$FF_PATH --version" --stdout-pattern "ff v0.1.0"
run_test "Version short option" "$FF_PATH -V" --stdout-pattern "ff v0.1.0"
}
test_error_handling() {
run_test "Invalid option" "$FF_PATH --invalid-option 2>&1 || true" --stdout "error: unknown option '--invalid-option'"
run_test "Missing argument for --type" "$FF_PATH --type 2>&1 || true" --stdout "error: --type requires an argument"
run_test "Missing argument for --max-depth" "$FF_PATH -d 2>&1 || true" --stdout "error: -d requires an argument"
run_test "Invalid color option" "$FF_PATH --color invalid 2>&1 || true" --stdout "error: invalid --color argument: 'invalid'; must be always/never/auto"
}
test_color_options() {
run_test "Color auto" "$FF_PATH -c auto file1.txt" --stdout "./file1.txt"
run_test "Color never" "$FF_PATH -c never file1.txt" --stdout "./file1.txt"
# Note: Color always test removed because it's difficult to test exact ANSI codes
}
test_path_formats() {
run_test "Absolute paths" "$FF_PATH -a file1.txt" --stdout "$TEST_DIR/file1.txt"
run_test "Specific search path" "$FF_PATH file dir1" --stdout "dir1/nested_file.txt
dir1/subdir1/deep_file.py"
}
test_complex_scenarios() {
run_test "Multiple options" "$FF_PATH -t f -e txt -d 2 file1" --stdout "./file1.txt"
run_test "Hidden and type combined" "$FF_PATH -H -t f hidden" --stdout "./.hidden_file"
run_test "Extension and exclude combined" "$FF_PATH -e py -E subdir" --stdout "./file2.py"
run_test "Multiple search paths" "$FF_PATH txt dir1 dir2" --stdout "dir1/nested_file.txt"
}
test_special_characters() {
run_test "Files with spaces" "$FF_PATH 'with spaces'" --stdout "./file with spaces.txt"
run_test "Files with dashes" "$FF_PATH 'with-dashes'" --stdout "./file-with-dashes.txt"
run_test "Files with underscores" "$FF_PATH 'with_underscores'" --stdout "./file_with_underscores.txt"
}
main() {
setup_test_dir
echo 'TAP version 14'
test_help_and_version
test_basic_functionality
test_options
test_error_handling
test_color_options
test_path_formats
test_complex_scenarios
test_special_characters
cleanup_test_dir
printf '1..%s\n' "$TESTS_RUN"
if [ $TESTS_FAILED -gt 0 ]; then
exit 1
else
exit 0
fi
}
trap cleanup_test_dir EXIT
main "$@"