tests: add test for locale decimal processing
[coreutils.git] / tests / misc / ls-misc.pl
blobf33562b6db917d6ad440c32274d0643ff8c0acf0
1 #!/usr/bin/perl
3 # Copyright (C) 1998-2019 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18 use strict;
20 (my $ME = $0) =~ s|.*/||;
21 my $prog = 'ls';
23 # Turn off localization of executable's output.
24 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
26 my $saved_ls_colors;
28 sub push_ls_colors($)
30 $saved_ls_colors = $ENV{LS_COLORS} || '';
31 $ENV{LS_COLORS} = $_[0];
34 sub restore_ls_colors()
36 $ENV{LS_COLORS} = $saved_ls_colors;
39 # If the string $S is a well-behaved file name, simply return it.
40 # If it contains white space, quotes, etc., quote it, and return the new string.
41 sub shell_quote($)
43 my ($s) = @_;
44 if ($s =~ m![^\w+/.,-]!)
46 # Convert each single quote to '\''
47 $s =~ s/\'/\'\\\'\'/g;
48 # Then single quote the string.
49 $s = "'$s'";
51 return $s;
54 # Set up files used by the setuid-etc tests; skip this entire test if
55 # that cannot be done.
56 sub setuid_setup()
58 my $test = 'env test';
59 system (qq(touch setuid && chmod u+s setuid && $test -u setuid &&
60 touch setgid && chmod g+s setgid && $test -g setgid &&
61 mkdir sticky && chmod +t sticky && $test -k sticky &&
62 mkdir owt && chmod +t,o+w owt && $test -k owt &&
63 mkdir owr && chmod o+w owr)) == 0
64 or CuSkip::skip "$ME: cannot create setuid/setgid/sticky files,"
65 . "so can't run this test\n";
68 sub mk_file(@)
70 foreach my $f (@_)
72 open (F, '>', $f) && close F
73 or die "creating $f: $!\n";
77 sub mkdir_d {mkdir 'd',0755 or die "d: $!\n"}
78 sub rmdir_d {rmdir 'd' or die "d: $!\n"}
79 my $mkdir = {PRE => sub {mkdir_d}};
80 my $rmdir = {POST => sub {rmdir_d}};
81 my $mkdir_reg = {PRE => sub {mkdir_d; mk_file 'd/f' }};
82 my $rmdir_reg = {POST => sub {unlink 'd/f' or die "d/f: $!\n";
83 rmdir 'd' or die "d: $!\n"}};
85 my $mkdir2 = {PRE => sub {mkdir 'd',0755 or die "d: $!\n";
86 mkdir 'd/e',0755 or die "d/e: $!\n" }};
87 my $rmdir2 = {POST => sub {rmdir 'd/e' or die "d/e: $!\n";
88 rmdir 'd' or die "d: $!\n" }};
90 my $target = {PRE => sub {
91 mkdir 'd',0755 or die "d: $!\n";
92 symlink '.', 'd/X' or die "d/X: $!\n";
93 push_ls_colors('ln=target')
94 }};
95 my $target2 = {POST => sub {unlink 'd/X' or die "d/X: $!\n";
96 rmdir 'd' or die "d: $!\n";
97 restore_ls_colors
98 }};
99 my $slink_d = {PRE => sub {symlink '/', 'd' or die "d: $!\n";
100 push_ls_colors('ln=01;36:di=01;34:or=40;31;01')
102 my $unlink_d = {POST => sub {unlink 'd' or die "d: $!\n"; restore_ls_colors}};
104 my $mkdir_d_slink = {PRE => sub {mkdir 'd',0755 or die "d: $!\n";
105 symlink '/', 'd/s' or die "d/s: $!\n" }};
106 my $rmdir_d_slink = {POST => sub {unlink 'd/s' or die "d/s: $!\n";
107 rmdir 'd' or die "d: $!\n" }};
109 sub make_j_d ()
111 mkdir 'j', 0700 or die "creating j: $!\n";
112 mk_file 'j/d';
113 chmod 0555, 'j/d' or die "making j/d executable: $!\n";
116 my @v1 = (qw(0 9 A Z a z), 'zz~', 'zz', 'zz.~1~', 'zz.0');
117 my @v_files = ((map { ".$_" } @v1), @v1);
118 my $exe_in_subdir = {PRE => sub { make_j_d (); push_ls_colors('ex=01;32') }};
119 my $remove_j = {POST => sub {unlink 'j/d' or die "j/d: $!\n";
120 rmdir 'j' or die "j: $!\n";
121 restore_ls_colors }};
123 my $e = "\e[0m";
124 my $q_bell = {IN => {"q\a" => ''}};
125 my @Tests =
127 # test-name options input expected-output
129 # quoting tests............................................
130 ['q-', $q_bell, {OUT => "q\a\n"}, {EXIT => 0}],
131 ['q-N', '-N', $q_bell, {OUT => "q\a\n"}, {ERR => ''}],
132 ['q-q', '-q', $q_bell, {OUT => "q?\n"}],
133 ['q-Q', '-Q', $q_bell, {OUT => "\"q\\a\"\n"}],
135 ['q-qs-lit', '--quoting=literal', $q_bell, {OUT => "q\a\n"}],
136 ['q-qs-sh', '--quoting=shell', $q_bell, {OUT => "q\a\n"}],
137 ['q-qs-sh-a', '--quoting=shell-always',$q_bell, {OUT => "'q\a'\n"}],
138 ['q-qs-sh-e', '--quoting=shell-escape',$q_bell, {OUT => "'q'\$'\\a'\n"}],
139 ['q-qs-c', '--quoting=c', $q_bell, {OUT => "\"q\\a\"\n"}],
140 ['q-qs-esc', '--quoting=escape', $q_bell, {OUT => "q\\a\n"}],
141 ['q-qs-loc', '--quoting=locale', $q_bell, {OUT => "'q\\a'\n"}],
142 ['q-qs-cloc', '--quoting=clocale', $q_bell, {OUT => "\"q\\a\"\n"}],
144 ['q-qs-lit-q', '--quoting=literal -q', $q_bell, {OUT => "q?\n"}],
145 ['q-qs-sh-q', '--quoting=shell -q', $q_bell, {OUT => "q?\n"}],
146 ['q-qs-sh-a-q', '--quoting=shell-al -q', $q_bell, {OUT => "'q?'\n"}],
147 ['q-qs-sh-e-q', '--quoting=shell-escape -q',
148 $q_bell, {OUT => "'q'\$'\\a'\n"}],
149 ['q-qs-c-q', '--quoting=c -q', $q_bell, {OUT => "\"q\\a\"\n"}],
150 ['q-qs-esc-q', '--quoting=escape -q', $q_bell, {OUT => "q\\a\n"}],
151 ['q-qs-loc-q', '--quoting=locale -q', $q_bell, {OUT => "'q\\a'\n"}],
152 ['q-qs-cloc-q', '--quoting=clocale -q', $q_bell, {OUT => "\"q\\a\"\n"}],
154 ['q-qs-c-1', '--quoting=c',
155 {IN => {"t\004" => ''}}, {OUT => "\"t\\004\"\n"}],
157 ['emptydir', 'd', {OUT => ''}, $mkdir, $rmdir],
158 ['emptydir-x2', 'd d', {OUT => "d:\n\nd:\n"}, $mkdir, $rmdir],
159 ['emptydir-R', '-R d', {OUT => "d:\n"}, $mkdir, $rmdir],
161 # test 'ls -R .' ............................................
162 ['R-dot', '--ignore="[a-ce-zA-Z]*" -R .', {OUT => ".:\nd\n\n\./d:\n"},
163 $mkdir, $rmdir],
165 ['slink-dir-F', '-F d', {OUT => "d@\n"}, $slink_d, $unlink_d],
166 ['slink-dir-dF', '-dF d', {OUT => "d@\n"}, $slink_d, $unlink_d],
167 ['slinkdir-dFH', '-dFH d', {OUT => "d/\n"}, $slink_d, $unlink_d],
168 ['slinkdir-dFL', '-dFL d', {OUT => "d/\n"}, $slink_d, $unlink_d],
170 # Test for a bug that was fixed in coreutils-4.5.4.
171 ['sl-F-color', '-F --color=always d',
172 {OUT => "$e\e[01;36md$e\@\n"},
173 $slink_d, $unlink_d],
174 ['sl-dF-color', '-dF --color=always d',
175 {OUT => "$e\e[01;36md$e\@\n"},
176 $slink_d, $unlink_d],
178 # A listing with no output should have no color sequences at all.
179 ['no-c-empty', '--color=always d', {OUT => ""}, $mkdir, $rmdir],
180 # A listing with only regular files should have no color sequences at all.
181 ['no-c-reg', '--color=always d', {OUT => "f\n"}, $mkdir_reg, $rmdir_reg],
183 # Test for a bug fixed after coreutils-6.9.
184 ['sl-target', '--color=always d',
185 {OUT => "$e\e[01;34mX$e\n"}, $target, $target2],
187 # Test for another bug fixed after coreutils-6.9.
188 # This one bites only for a system/file system with d_type support.
189 ['sl-dangle', '--color=always d',
190 {OUT => "$e\e[40;31;01mX$e\n"},
191 {PRE => sub {
192 mkdir 'd',0755 or die "d: $!\n";
193 symlink 'non-existent', 'd/X' or die "d/X: $!\n";
194 push_ls_colors('or=40;31;01')
196 {POST => sub {unlink 'd/X' or die "d/X: $!\n";
197 rmdir 'd' or die "d: $!\n";
198 restore_ls_colors; }},
201 # Test for a bug fixed after coreutils-8.2.
202 ['sl-dangle2', '-o --time-style=+:TIME: --color=always l',
203 {OUT_SUBST => 's/.*:TIME: //'},
204 {OUT => "l -> nowhere\n"},
205 {PRE => sub {symlink 'nowhere', 'l' or die "l: $!\n";
206 push_ls_colors('ln=target')
208 {POST => sub {unlink 'l' or die "l: $!\n";
209 restore_ls_colors; }},
211 ['sl-dangle3', '-o --time-style=+:TIME: --color=always l',
212 {OUT_SUBST => 's/.*:TIME: //'},
213 {OUT => "$e\e[40ml$e -> \e[34mnowhere$e\n"},
214 {PRE => sub {symlink 'nowhere', 'l' or die "l: $!\n";
215 push_ls_colors('ln=target:or=40:mi=34:')
217 {POST => sub {unlink 'l' or die "l: $!\n";
218 restore_ls_colors; }},
220 ['sl-dangle4', '-o --time-style=+:TIME: --color=always l',
221 {OUT_SUBST => 's/.*:TIME: //'},
222 {OUT => "$e\e[36ml$e -> \e[35mnowhere$e\n"},
223 {PRE => sub {symlink 'nowhere', 'l' or die "l: $!\n";
224 push_ls_colors('ln=34:mi=35:or=36:')
226 {POST => sub {unlink 'l' or die "l: $!\n";
227 restore_ls_colors; }},
229 ['sl-dangle5', '-o --time-style=+:TIME: --color=always l',
230 {OUT_SUBST => 's/.*:TIME: //'},
231 {OUT => "$e\e[34ml$e -> \e[35mnowhere$e\n"},
232 {PRE => sub {symlink 'nowhere', 'l' or die "l: $!\n";
233 push_ls_colors('ln=34:mi=35:')
235 {POST => sub {unlink 'l' or die "l: $!\n";
236 restore_ls_colors; }},
239 # Test for a bug fixed after coreutils-8.13
240 # where 'argetm' was errenously printed for dangling links
241 # when ln=target was used in LS_COLORS
242 ['sl-dangle6', '-L --color=always d',
243 {OUT => "s\n"},
244 {PRE => sub {mkdir 'd',0755 or die "d: $!\n";
245 symlink 'dangle', 'd/s' or die "d/s: $!\n";
246 push_ls_colors('ln=target')
248 {POST => sub {unlink 'd/s' or die "d/s: $!\n";
249 rmdir 'd' or die "d: $!\n";
250 restore_ls_colors; }},
251 {ERR => "ls: cannot access 'd/s': No such file or directory\n"},
252 {EXIT => 1}
254 # Related to the above fix, is this case where
255 # the code simulates "linkok". In this case "linkmode"
256 # should always be zero, and hence not trigger any
257 # issues with type being set to C_LINK
258 ['sl-dangle7', '--color=always d',
259 {OUT => "$e\e[ms$e\n"},
260 {PRE => sub {mkdir 'd',0755 or die "d: $!\n";
261 symlink 'dangle', 'd/s' or die "d/s: $!\n";
262 push_ls_colors('ln=target:or=:ex=:')
264 {POST => sub {unlink 'd/s' or die "d/s: $!\n";
265 rmdir 'd' or die "d: $!\n";
266 restore_ls_colors; }},
268 # Another case with simulated "linkok", that does
269 # actually use the value of 'ln' from $LS_COLORS.
270 # This path is not taken though when 'ln=target'.
271 ['sl-dangle8', '--color=always s',
272 {OUT => "$e\e[1;36ms$e\n"},
273 {PRE => sub {symlink 'dangle', 's' or die "s: $!\n";
274 push_ls_colors('ln=1;36:or=:')
276 {POST => sub {unlink 's' or die "s: $!\n";
277 restore_ls_colors; }},
279 # The patch associated with sl-dangle[678] introduced a regression
280 # that was fixed after coreutils-8.19. This edge case triggers when
281 # listing a dir containing dangling symlinks, but with orphans uncolored.
282 # I.e., the same as the previous test, but listing the directory
283 # rather than the symlink directly.
284 ['sl-dangle9', '--color=always d',
285 {OUT => "$e\e[1;36ms$e\n"},
286 {PRE => sub {mkdir 'd',0755 or die "d: $!\n";
287 symlink 'dangle', 'd/s' or die "d/s: $!\n";
288 push_ls_colors('ln=1;36:or=:')
290 {POST => sub {unlink 'd/s' or die "d/s: $!\n";
291 rmdir 'd' or die "d: $!\n";
292 restore_ls_colors; }},
295 # Test for a bug that was introduced in coreutils-4.5.4; fixed in 4.5.5.
296 # To demonstrate it, the file in question (with executable bit set)
297 # must not be a command line argument.
298 ['color-exe1', '--color=always j',
299 {OUT => "$e\e[01;32md$e\n"},
300 $exe_in_subdir, $remove_j],
302 # From Stéphane Chazelas.
303 ['no-a-isdir-b', 'no-dir d',
304 {OUT => "d:\n"},
305 {ERR => "ls: cannot access 'no-dir': No such file or directory\n"},
306 $mkdir, $rmdir, {EXIT => 2}],
308 ['recursive-2', '-R d', {OUT => "d:\ne\n\nd/e:\n"}, $mkdir2, $rmdir2],
310 ['setuid-etc', '-1 -d --color=always owr owt setgid setuid sticky',
311 {OUT =>
312 "$e\e[34;42mowr$e\n"
313 . "\e[30;42mowt$e\n"
314 . "\e[30;43msetgid$e\n"
315 . "\e[37;41msetuid$e\n"
316 . "\e[37;44msticky$e\n"
319 {PRE => sub {
320 push_ls_colors('ow=34;42:tw=30;42:sg=30;43:su=37;41:st=37;44'); }},
321 {POST => sub {
322 unlink qw(setuid setgid);
323 foreach my $dir (qw(owr owt sticky)) {rmdir $dir}
324 restore_ls_colors; }},
327 # For 5.97 and earlier, --file-type acted like --indicator-style=slash.
328 ['file-type', '--file-type d', {OUT => "s@\n"},
329 $mkdir_d_slink, $rmdir_d_slink],
331 # 7.1 had a regression in how -v -a ordered some files
332 ['version-sort', '-v -A ' . join (' ', @v_files),
333 {OUT => join ("\n", @v_files) . "\n"},
334 {PRE => sub { mk_file @v_files }},
335 {POST => sub { unlink @v_files }},
338 # Test for the ls -1U bug fixed in coreutils-7.5.
339 # It is triggered only with -1U and with two or more arguments,
340 # at least one of which is a nonempty directory.
341 ['multi-arg-U1', '-U1 d no-such',
342 {OUT => "d:\nf\n"},
343 {ERR_SUBST=>"s/ch':.*/ch':/"},
344 {ERR => "$prog: cannot access 'no-such':\n"},
345 $mkdir_reg,
346 $rmdir_reg,
347 {EXIT => 2},
351 umask 022;
353 # Start with an unset LS_COLORS environment variable.
354 delete $ENV{LS_COLORS};
356 my $save_temps = $ENV{SAVE_TEMPS};
357 my $verbose = $ENV{VERBOSE};
359 setuid_setup;
360 my $fail = run_tests ($ME, $prog, \@Tests, $save_temps, $verbose);
361 $fail
362 and exit 1;
364 # Be careful to use the just-build dircolors.
365 my $env = qx/dircolors -b/;
366 $env =~ s/^LS_COLORS=\'//;
367 $env =~ s/\';.*//sm;
368 $ENV{LS_COLORS} = $env;
370 setuid_setup;
371 $fail = run_tests ($ME, $prog, \@Tests, $save_temps, $verbose);
372 exit $fail;