1# 2# This file and its contents are supplied under the terms of the 3# Common Development and Distribution License ("CDDL"), version 1.0. 4# You may only use this file in accordance with the terms of version 5# 1.0 of the CDDL. 6# 7# A full copy of the text of the CDDL should have accompanied this 8# source. A copy of the CDDL is also available via the Internet at 9# http://www.illumos.org/license/CDDL. 10# 11 12# 13# Copyright 2021 OmniOS Community Edition (OmniOSce) Association. 14# 15 16# 17# Test whether the builtin head command properly handles files which do 18# not have a trailing newline. 19# 20 21# test setup 22function err_exit 23{ 24 print -u2 -n "\t" 25 print -u2 -r ${Command}[$1]: "${@:2}" 26 (( Errors++ )) 27} 28alias err_exit='err_exit $LINENO' 29 30set -o nounset 31Command=${0##*/} 32integer Errors=0 33 34builtin head tail 35 36t1=`mktemp` 37t2=`mktemp` 38t3=`mktemp` 39if [[ ! -f "$t1" || ! -f "$t2" || ! -f "$t3" ]]; then 40 # Don't use the global err _ exit function as the test harness uses 41 # calls to that to compute the number of tests present in this file. 42 echo "Could not create temporary files" 43 rm -f "$t1" "$t2" "$t3" 44 exit 1 45fi 46 47for f in test{0..4%02d}; do 48 echo $f 49done | tee $t1 > $t2 50printf "nonewline" >> $t2 51printf "nonewline" >> $t3 52 53# Standard file, five lines, with trailing newline 54 55[[ $(head -1 $t1) == 'test00' ]] || \ 56 err_exit "Shell head -1 standard file" 57 58[[ $(head -2 $t1) == $'test00\ntest01' ]] || \ 59 err_exit "Shell head -2 standard file" 60 61[[ $(head -s 2 -n2 $t1) == $'test02\ntest03' ]] || \ 62 err_exit "Shell head -s 2 -n 2 standard file" 63 64[[ $(head -5 $t1) == $'test00\ntest01\ntest02\ntest03\ntest04' ]] || \ 65 err_exit "Shell head -5 standard file" 66 67[[ $(head -10 $t1) == $'test00\ntest01\ntest02\ntest03\ntest04' ]] || \ 68 err_exit "Shell head -10 standard file" 69 70[[ $(tail -1 $t1) == 'test04' ]] || \ 71 err_exit "Shell tail -1 standard file" 72 73[[ $(tail -2 $t1) == $'test03\ntest04' ]] || \ 74 err_exit "Shell tail -2 standard file" 75 76[[ $(tail -10 $t1) == $'test00\ntest01\ntest02\ntest03\ntest04' ]] || \ 77 err_exit "Shell tail -10 standard file" 78 79# File with a single line, no trailing newline 80 81[[ $(head -1 $t3) == 'nonewline' ]] || \ 82 err_exit "Shell head -1 one-line file" 83 84[[ $(head -2 $t3) == 'nonewline' ]] || \ 85 err_exit "Shell head -2 one-line file" 86 87[[ $(tail -1 $t3) == 'nonewline' ]] || \ 88 err_exit "Shell tail -1 one-line file" 89 90[[ $(tail -2 $t3) == 'nonewline' ]] || \ 91 err_exit "Shell tail -2 one-line file" 92 93# File with six lines, no trailing newline 94 95[[ $(head -1 $t2) == "test00" ]] || \ 96 err_exit "Shell head -1 six-line file" 97 98[[ $(head -2 $t2) == $'test00\ntest01' ]] || \ 99 err_exit "Shell head -2 six-line file" 100 101[[ $(head -s 2 -n2 $t2) == $'test02\ntest03' ]] || \ 102 err_exit "Shell head -s 2 -n 2 six-line file" 103 104[[ $(head -5 $t2) == $'test00\ntest01\ntest02\ntest03\ntest04' ]] || \ 105 err_exit "Shell head -5 six-line file" 106 107[[ $(head -6 $t2) == $'test00\ntest01\ntest02\ntest03\ntest04\nnonewline' ]] \ 108 || err_exit "Shell head -6 six-line file" 109 110[[ $(head -10 $t2) == $'test00\ntest01\ntest02\ntest03\ntest04\nnonewline' ]] \ 111 || err_exit "Shell head -10 six-line file" 112 113[[ $(tail -1 $t2) == 'nonewline' ]] || \ 114 err_exit "Shell tail -1 six-line file" 115 116[[ $(tail -2 $t2) == $'test04\nnonewline' ]] || \ 117 err_exit "Shell tail -2 six-line file" 118 119[[ $(tail -10 $t2) == $'test00\ntest01\ntest02\ntest03\ntest04\nnonewline' ]] \ 120 || err_exit "Shell tail -10 six-line file" 121 122rm -f "$t1" "$t2" "$t3" 123 124# tests done 125exit $Errors 126