• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3[ -f testing.sh ] && . testing.sh
4
5#testing "name" "command" "result" "infile" "stdin"
6
7APWD="$(pwd -P)"
8
9testing "missing" "readlink notfound || echo yes" "yes\n" "" ""
10
11# simple tests on a file
12
13touch file
14testing "file" "readlink file || echo yes" "yes\n" "" ""
15testing "-f dir" "readlink -f ." "$APWD\n" "" ""
16testing "-f missing" "readlink -f notfound" "$APWD/notfound\n" "" ""
17
18ln -sf notfound link
19testing "link" "readlink link" "notfound\n" "" ""
20testing "links" "readlink link link" "notfound\nnotfound\n" "" ""
21testing "link->missing" "readlink -f link" "$APWD/notfound\n" "" ""
22ln -sf ../../ link
23testing "stays relative" "readlink link" "../../\n" "" ""
24rm link
25ln -sf file link
26testing "-f link->file" "readlink -f link" "$APWD/file\n" "" ""
27ln -sf . link
28testing "-f link->dir" "readlink -f link" "$APWD\n" "" ""
29ln -snf link link
30testing "link->link (recursive)" "readlink link" "link\n" "" ""
31testing "-f link->link (recursive)" \
32  "readlink -f link 2>/dev/null || echo yes" "yes\n" "" ""
33
34testing "-q notlink" "readlink -q file 2>&1 || echo yes" "yes\n" "" ""
35testing "-q link" "readlink -q link && echo yes" "link\nyes\n" "" ""
36testing "-q notfound" "readlink -q notfound || echo yes" "yes\n" "" ""
37testing "-e found" "readlink -e file" "$APWD/file\n" "" ""
38testing "-e notfound" \
39  "readlink -e notfound 2>/dev/null || echo yes" "yes\n" "" ""
40testing "-nf ." "readlink -nf ." "$APWD" "" ""
41# -n means no newline at _end_. I.E. on last argument.
42toyonly testcmd '-nf multiple args' '-n link link' "link\nlink" '' ''
43testcmd '-nz' '-nz link' 'link' '' ''
44testcmd '-z' '-z link' 'link\0' '' ''
45
46mkdir sub &&
47ln -s . here &&
48ln -s ./sub dir &&
49touch sub/bang || exit 1
50testing "-f multi" "readlink -f dir/../here/dir/bang" \
51  "$APWD/sub/bang\n" "" ""
52testing "-f link/missing" "readlink -f dir/boing" \
53  "$APWD/sub/boing\n" "" ""
54testing "-f /dev/null/file" \
55  "readlink -f /dev/null/file 2>/dev/null || echo yes" "yes\n" "" ""
56testing "-m missing/dir" "readlink -m sub/two/three" "$APWD/sub/two/three\n" "" ""
57testing "-m missing/../elsewhere" "readlink -m sub/two/../../three" "$APWD/three\n" "" ""
58# TODO: host bug? That's not missing, that's "cannot exist".
59toyonly testing "-m file/dir" "readlink -m sub/bang/two 2>/dev/null || echo err" "err\n" "" ""
60rm link
61ln -sf / link || exit 1
62testing "-f link->/" "readlink -e link/dev" "/dev\n" "" ""
63testing "-f /dev/null/.." \
64  "readlink -f link/null/.. 2>/dev/null || echo yes" "yes\n" "" ""
65rm -f link && ln -sf link link || exit 1
66testing "recurse" "readlink link" "link\n" "" ""
67
68rm file link sub/bang dir here
69rmdir sub
70
71# Make sure circular links don't run away.
72
73ln -s link1 link2
74ln -s link2 link1
75testing "follow recursive2" "readlink -f link1 || echo yes" \
76	"yes\n" "" ""
77rm link1 link2
78