1#!/bin/sh
2# Ensure "ls --color" properly colors "normal" text and files.
3# I.e., that it uses NORMAL to style non file name output and
4# file names with no associated color (unless FILE is also set).
5
6# Copyright (C) 2010-2023 Free Software Foundation, Inc.
7
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
21. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
22print_ver_ ls
23
24# Don't let a different umask perturb the results.
25umask 22
26
27# Output time as something constant
28export TIME_STYLE="+norm"
29
30# helper to strip ls columns up to "norm" time
31qls() { sed 's/-r.*norm/norm/'; }
32
33touch exe || framework_failure_
34chmod u+x exe || framework_failure_
35touch nocolor || framework_failure_
36
37TCOLORS="no=7:ex=01;32"
38
39# Uncolored file names inherit NORMAL attributes.
40LS_COLORS=$TCOLORS      ls -gGU --color exe nocolor | qls >> out || fail=1
41LS_COLORS=$TCOLORS      ls -xU  --color exe nocolor       >> out || fail=1
42LS_COLORS=$TCOLORS      ls -gGU --color nocolor exe | qls >> out || fail=1
43LS_COLORS=$TCOLORS      ls -xU  --color nocolor exe       >> out || fail=1
44
45# NORMAL does not override FILE though
46LS_COLORS=$TCOLORS:fi=1 ls -gGU --color nocolor exe | qls >> out || fail=1
47
48# Support uncolored ordinary files that do _not_ inherit from NORMAL.
49# Note there is a redundant RESET output before a non colored
50# file in this case which may be removed in future.
51LS_COLORS=$TCOLORS:fi=  ls -gGU --color nocolor exe | qls >> out || fail=1
52LS_COLORS=$TCOLORS:fi=0 ls -gGU --color nocolor exe | qls >> out || fail=1
53
54# A caveat worth noting is that commas (-m), indicator chars (-F)
55# and the "total" line, do not currently use NORMAL attributes
56LS_COLORS=$TCOLORS      ls -mFU --color nocolor exe       >> out || fail=1
57
58# Ensure no coloring is done unless enabled
59LS_COLORS=$TCOLORS      ls -gGU         nocolor exe | qls >> out || fail=1
60
61cat -A out > out.display || framework_failure_
62mv out.display out || framework_failure_
63
64cat <<\EOF > exp || framework_failure_
65^[[0m^[[7mnorm ^[[m^[[01;32mexe^[[0m$
66^[[7mnorm nocolor^[[0m$
67^[[0m^[[7m^[[m^[[01;32mexe^[[0m  ^[[7mnocolor^[[0m$
68^[[0m^[[7mnorm nocolor^[[0m$
69^[[7mnorm ^[[m^[[01;32mexe^[[0m$
70^[[0m^[[7mnocolor^[[0m  ^[[7m^[[m^[[01;32mexe^[[0m$
71^[[0m^[[7mnorm ^[[m^[[1mnocolor^[[0m$
72^[[7mnorm ^[[m^[[01;32mexe^[[0m$
73^[[0m^[[7mnorm ^[[m^[[mnocolor^[[0m$
74^[[7mnorm ^[[m^[[01;32mexe^[[0m$
75^[[0m^[[7mnorm ^[[m^[[0mnocolor^[[0m$
76^[[7mnorm ^[[m^[[01;32mexe^[[0m$
77^[[0m^[[7mnocolor^[[0m, ^[[7m^[[m^[[01;32mexe^[[0m*$
78norm nocolor$
79norm exe$
80EOF
81
82compare exp out || fail=1
83
84Exit $fail
85