1#!/bin/bash 2 3# Copyright 2013 Robin Mittal <[email protected]> 4# Copyright 2013 Divya Kothari <[email protected]> 5 6[ -f testing.sh ] && . testing.sh 7 8#testing "name" "command" "result" "infile" "stdin" 9#set -x 10 11# Creating test-file/dir for testing ls 12mkdir -p dir1 dir2 || exit 1 13echo "test file1" > file1.txt 14echo "test file2" > file2.txt 15echo "hidden file1" > .hfile1 16 17testcmd "no argument" "" "dir1\ndir2\nfile1.txt\nfile2.txt\n" "" "" 18testcmd "-C column spacing equals 2" "-C" "dir1 dir2 file1.txt file2.txt\n" \ 19 "" "" 20testcmd "-x column spacing equals 2" "-x" "dir1 dir2 file1.txt file2.txt\n" \ 21 "" "" 22testcmd "explicit files" "file*" "file1.txt\nfile2.txt\n" "" "" 23testcmd "explicit -1" "-1 file*" "file1.txt\nfile2.txt\n" "" "" 24testcmd "" "-p" "dir1/\ndir2/\nfile1.txt\nfile2.txt\n" "" "" 25testcmd "" "-a" ".\n..\n.hfile1\ndir1\ndir2\nfile1.txt\nfile2.txt\n" "" "" 26testcmd "" "-A" ".hfile1\ndir1\ndir2\nfile1.txt\nfile2.txt\n" "" "" 27testcmd "" "-d" ".\n" "" "" 28testcmd "" "-d *" "dir1\ndir2\nfile1.txt\nfile2.txt\n" "" "" 29testcmd "" "-k" "dir1\ndir2\nfile1.txt\nfile2.txt\n" "" "" 30testcmd "-m" "-m" "dir1, dir2, file1.txt, file2.txt\n" "" "" 31testcmd "-F" "-F" "dir1/\ndir2/\nfile1.txt\nfile2.txt\n" "" "" 32testcmd "-dk *" "-dk *" "dir1\ndir2\nfile1.txt\nfile2.txt\n" "" "" 33testcmd "-Z" "-Z file1.txt | egrep -q '^[^ ]+ file1.txt' || echo fail" "" "" "" 34testcmd "-lZ" "--full-time -lZ file1.txt | egrep -q '^-[rwx-]+ +[0-9]+ +[^ ]+ +[^ ]+ +[^ ]+ +[0-9]+ [0-9][0-9][0-9][0-9]-[0-9][0-9]-.* file1.txt' || echo fail" "" "" "" 35 36ln -s file1.txt slink 37testcmd "-l symlink" "-l slink | grep -q -- ' slink -> file1.txt' && echo ok" \ 38 "ok\n" "" "" 39rm -f slink 40 41ln -s /dev/null/nosuchfile nosuchfile 42testcmd "-d broken symlink" "-d nosuchfile" "nosuchfile\n" "" "" 43rm -f nosuchfile 44 45rm -rf file{1,2}.txt .hfile1 dir1 dir2 46 47mkdir -p dir1 && touch file1.txt dir1/file2 48testcmd "" "-R" ".:\ndir1\nfile1.txt\n\n./dir1:\nfile2\n" "" "" 49rm -rf dir1 file1.txt 50 51touch file1.txt 52testcmd "-i" "-i" "$(stat -c %i file1.txt) file1.txt\n" "" "" 53testcmd "missing" "does-not-exist 2>&1 >/dev/null | grep -o does-not-exist" \ 54 "does-not-exist\n" "" "" 55rm -f file1.txt 56 57# sort tests 58TIME=1234567890 59for i in one two three four five six seven eight nine ten 60 do touch -d @$((TIME++)) $i 61done 62testcmd "" "-r" "two\nthree\nten\nsix\nseven\none\nnine\nfour\nfive\neight\n" \ 63 "" "" 64testcmd "-w test 1" "-Cw 20" \ 65 "eight one three\nfive seven two\nfour six\nnine ten\n" "" "" 66testcmd "-w test 2" "-Cw 19" \ 67 "eight seven\nfive six\nfour ten\nnine three\none two\n" "" "" 68rm -f one two three four five six seven eight nine ten 69 70touch a b c d e f 71testcmd "-w test 3" "-Cw 3" "a\nb\nc\nd\ne\nf\n" "" "" 72testcmd "-w test 4" "-Cw 4" "a d\nb e\nc f\n" "" "" 73rm -f a b c d e f 74 75touch 'hello 76world' 77testcmd "default escaping" "" "hello 78world\n" "" "" 79testcmd "" "-b" 'hello\\ \\rworld\n' "" "" 80testcmd "" "-q" 'hello ?world\n' "" "" 81testcmd "" "-N" 'hello 82world\n' "" "" 83# testcmd "" "-qN" 'hello ?world\n' "" "" behaves differently, and ok with it. 84rm hello* 85 86mkdir zzz dir && echo hello > xxx && sleep .05 && echo longer > abc 87# pause because filesystem granularity may be "jiffies". 88X=999; for i in abc x abc.jkl zzz xxx Abc cde.def dir bcd.ghi 89do sleep .05; touch -d @$((X--)) $i; done 90 91# MacOS filesystems are case insensitive, so Abc and abc can't coexist. 92[ "$(uname)" == Darwin ] && SKIP=999 93testcmd '' '--group-directories-first' \ 94 'dir\nzzz\nAbc\nabc\nabc.jkl\nbcd.ghi\ncde.def\nx\nxxx\n' '' '' 95testcmd '' '-cr' 'abc\nx\nabc.jkl\nzzz\nxxx\nAbc\ncde.def\ndir\nbcd.ghi\n' '' '' 96testcmd '' '-tr' 'bcd.ghi\ndir\ncde.def\nAbc\nxxx\nzzz\nabc.jkl\nx\nabc\n' '' '' 97testcmd '' '-S --group-directories-first' 'dir\nzzz\nabc\nxxx\nAbc\nabc.jkl\nbcd.ghi\ncde.def\nx\n' '' '' 98testcmd '' '-X' 'Abc\nabc\ndir\nx\nxxx\nzzz\ncde.def\nbcd.ghi\nabc.jkl\n' '' '' 99testcmd '-U matches -f' '-aU1' "$("$C" -f1)\n" '' '' 100# -ltu sorts by atime, -lu shows atime but sorts by name (same for -oc/-otc) 101TZ=utc testcmd '-ou' '-ou --full-time . | sed -n "s/.*:\([0-9]*\).*/\1/p"' \ 102 '34\n39\n37\n31\n33\n32\n38\n35\n36\n' '' '' 103TZ=utc testcmd '-otu' '-otu --full-time . | sed -n "s/.*:\([0-9]*\).*/\1/p"' \ 104 '39\n38\n37\n36\n35\n34\n33\n32\n31\n' '' '' 105toyonly testcmd '' '--sort=reverse' 'zzz\nxxx\nx\ndir\ncde.def\nbcd.ghi\nabc.jkl\nabc\nAbc\n' '' '' 106toyonly testcmd '' '--sort=nocase,time' 'abc\nAbc\nabc.jkl\nbcd.ghi\ncde.def\ndir\nx\nxxx\nzzz\n' '' '' 107SKIP=0 108