1#!/bin/bash
2
3# Copyright 2013 Robin Mittal <robinmittal.it@gmail.com>
4# Copyright 2013 Divya Kothari <divya.s.kothari@gmail.com>
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 lstest/dir1 lstest/dir2 || exit 1
13echo "test file1" > lstest/file1.txt
14echo "test file2" > lstest/file2.txt
15echo "hidden file1" > lstest/.hfile1
16
17IN="cd lstest"
18OUT="cd .. "
19
20testing "no argument" "$IN && ls; $OUT" "dir1\ndir2\nfile1.txt\nfile2.txt\n" "" ""
21testing "with -C: test column spacing equals 2" "$IN && ls -C; $OUT" "dir1  dir2  file1.txt  file2.txt\n" "" ""
22testing "with -x: test column spacing equals 2" "$IN && ls -x; $OUT" "dir1  dir2  file1.txt  file2.txt\n" "" ""
23testing "with wild char" "$IN && ls file*; $OUT" "file1.txt\nfile2.txt\n" "" ""
24testing "with wild char - long listing" "$IN && ls -1 file*; $OUT" "file1.txt\nfile2.txt\n" "" ""
25testing "with -p" "$IN && ls -p; $OUT" "dir1/\ndir2/\nfile1.txt\nfile2.txt\n" "" ""
26testing "with -a" "$IN && ls -a; $OUT" \
27        ".\n..\n.hfile1\ndir1\ndir2\nfile1.txt\nfile2.txt\n" "" ""
28testing "with -A" "$IN && ls -A; $OUT" \
29        ".hfile1\ndir1\ndir2\nfile1.txt\nfile2.txt\n" "" ""
30testing "with -d" "$IN && ls -d; $OUT" ".\n" "" ""
31testing "with wild char and -d *" "$IN && ls -d *; $OUT" "dir1\ndir2\nfile1.txt\nfile2.txt\n" "" ""
32testing "with -k" "$IN && ls -k; $OUT" "dir1\ndir2\nfile1.txt\nfile2.txt\n" "" ""
33testing "with -m" "$IN && ls -m; $OUT" "dir1, dir2, file1.txt, file2.txt\n" "" ""
34testing "with -F" "$IN && ls -F; $OUT" "dir1/\ndir2/\nfile1.txt\nfile2.txt\n" "" ""
35testing "with -dk *" "$IN && ls -dk *; $OUT" "dir1\ndir2\nfile1.txt\nfile2.txt\n" "" ""
36testing "with -Z" "$IN && ls -Z file1.txt | egrep -q '^[^ ]+ file1.txt' || echo fail; $OUT" "" "" ""
37testing "with -lZ" "$IN && ls --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; $OUT" "" "" ""
38
39ln -s file1.txt lstest/slink
40testing "-l symlink" \
41    "$IN && ls -l slink | grep -q -- ' slink -> file1.txt' && echo ok ; $OUT" \
42    "ok\n" "" ""
43rm -f lstest/slink
44
45ln -s /dev/null/nosuchfile lstest/nosuchfile
46testing "with -d - broken softlink" "$IN && ls -d nosuchfile; $OUT" "nosuchfile\n" "" ""
47rm -f lstest/nosuchfile
48
49rm -rf lstest/* && mkdir -p lstest/dir1 && touch lstest/file1.txt
50testing "nested recursively" "$IN && ls -R; $OUT" \
51          ".:\ndir1\nfile1.txt\n\n./dir1:\n" "" ""
52
53rm -rf lstest/* && touch lstest/file1.txt && INODE=`stat -c %i lstest/file1.txt`
54testing "with -i" "$IN && ls -i 2>/dev/null; $OUT" "$INODE file1.txt\n" "" ""
55unset INODE
56
57testing "missing" "$IN && ls does-not-exist 2>&1 >/dev/null | grep -o does-not-exist; $OUT" "does-not-exist\n" "" ""
58
59rm -f lstest/{file1.txt,err}
60touch lstest/{one,two,three,four,five,six,seven,eight,nine,ten}
61testing "-w test 1" "$IN && ls -Cw 20; $OUT" \
62  "eight  one    three\nfive   seven  two\nfour   six\nnine   ten\n" "" ""
63testing "-w test 2" "$IN && ls -Cw 19; $OUT" \
64  "eight  seven\nfive   six\nfour   ten\nnine   three\none    two\n" "" ""
65
66rm -rf lstest/*
67touch lstest/{a,b,c,d,e,f}
68testing "-w test 3" "$IN && ls -Cw 3; $OUT" "a\nb\nc\nd\ne\nf\n" "" ""
69testing "-w test 4" "$IN && ls -Cw 4; $OUT" "a  d\nb  e\nc  f\n" "" ""
70
71# Removing test dir for cleanup purpose
72rm -rf lstest
73