tests: improve ls --dired with symlink testing
[coreutils.git] / tests / ls / dired.sh
blob39a59d675ec5a43cbddc0c3fbcf397c859952dcb
1 #!/bin/sh
2 # make sure --dired option works
4 # Copyright (C) 2001-2024 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20 print_ver_ ls
23 # Check with constant positions
24 mkdir dir || framework_failure_
26 cat <<EOF > exp
27 dir:
28 total 0
29 //SUBDIRED// 2 5
30 //DIRED-OPTIONS// --quoting-style=literal
31 EOF
32 for opt in '-l' '' '--hyperlink' '-x'; do
33 LC_MESSAGES=C ls $opt -R --dired dir > out || fail=1
34 compare exp out || fail=1
35 done
38 # Check with varying positions (due to usernames etc.)
39 # Also use multibyte characters to show --dired counts bytes not characters
40 touch dir/1a dir/2á || framework_failure_
41 mkdir -p dir/3dir || framework_failure_
42 touch dir/aaa || framework_failure_
43 ln -s target dir/0aaa_link || framework_failure_
45 ls -l --dired dir > out || fail=1
47 dired_values=$(grep "//DIRED//" out| cut -d' ' -f2-)
48 expected_files="0aaa_link 1a 2á 3dir aaa"
50 dired_count=$(printf '%s\n' $dired_values | wc -l)
51 expected_count=$(printf '%s\n' $expected_files | wc -l)
53 if test "$expected_count" -ne $(($dired_count / 2)); then
54 echo "Mismatch in number of files!" \
55 "Expected: $expected_count, Found: $(($dired_count / 2))"
56 fail=1
59 # Split the values into pairs and extract the filenames
60 index=1
61 set -- $dired_values
62 while test "$#" -gt 0; do
63 extracted_filename=$(head -c "$2" out | tail -c+"$(($1 + 1))")
64 expected_file=$(echo $expected_files | cut -d' ' -f$index)
66 # For symlinks, compare only the symlink name, ignoring the target part.
67 if echo "$extracted_filename" | grep ' -> ' > /dev/null; then
68 extracted_filename=$(echo "$extracted_filename" | cut -d' ' -f1)
71 if test "$extracted_filename" != "$expected_file"; then
72 echo "Mismatch! Expected: $expected_file, Found: $extracted_filename"
73 fail=1
75 shift; shift
76 index=$(($index + 1))
77 done
80 Exit $fail