xref: /aosp_15_r20/external/toybox/tests/realpath.test (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1#!/bin/bash
2
3[ -f testing.sh ] && . testing.sh
4
5#testing "name" "command" "result" "infile" "stdin"
6
7TOP="$(readlink -f .)"
8export PWD
9
10touch file
11mkdir -p one/two/three
12ln -s ./one uno
13ln -s one/two dos
14
15testcmd '' '.' "$TOP\n" '' ''
16testcmd 'missing' 'missing' "$TOP/missing\n" '' ''
17testcmd 'missing2' 'missing/sub 2>/dev/null || echo err' 'err\n' '' ''
18testcmd '-z' '-z . | tr "\0" X' "${TOP}X" '' ''
19testcmd 'file' 'file' "$TOP/file\n" '' ''
20testcmd 'dir' 'one/two/three' "$TOP/one/two/three\n" '' ''
21testcmd '--relative-to' '. --relative-to=one/two/three' '../../..\n' '' ''
22testcmd '--relative-to2' \
23  '-m --relative-to=missing/that/ uno/../dos/linux/../../bingeley/bongeley/beep' \
24  '../../one/bingeley/bongeley/beep\n' '' ''
25testcmd '--relative-to3' '-m walrus --relative-to walrus' '.\n' '' ''
26testcmd '--relative-to4' '"$PWD" --relative-to one' '..\n' '' ''
27testcmd '--relative-to5' '--relative-to "$PWD" one' 'one\n' '' ''
28testcmd 'relative-to missing' \
29  '--relative-to nothing/potato . 2>/dev/null || echo fail' 'fail\n' '' ''
30testcmd 'relative-to missing -m' \
31  '-m --relative-to nothing/potato .' '../..\n' '' ''
32testcmd '--relative-base' 'one one/two one/two/three --relative-base=one/two' \
33  "$TOP/one\n.\nthree\n" '' ''
34testcmd '--relative-base stomps --relative-to' \
35  '--relative-to=.. --relative-base=one/two one' "$TOP/one\n" '' ''
36testcmd '-m with relative-base1' '-m --relative-base wurble wurble/poing' \
37  'poing\n' '' ''
38testcmd '-m with relative-base2' '-sm --relative-base wurble .' "$PWD\n" '' ''
39testcmd '-m with relative-base3' '-m --relative-base wurble wurble wurble/' \
40  '.\n.\n' '' ''
41testcmd 'missing defaults to -m' 'missing' "$TOP/missing\n" '' ''
42testcmd 'missing -e' '-e missing 2>/dev/null || echo ok' 'ok\n' '' ''
43testcmd '-L' '-L dos/../one' "$TOP/one\n" '' ''
44
45# -s vs -L
46ln -s .. parent
47testcmd "-s isn't L" '-s --relative-to=. parent' 'parent\n' '' ''
48testcmd "-L isn't s" '-L --relative-to=. parent' '..\n' '' ''
49
50# The -s tests use $PWD instead of $TOP because symlinks in path _to_ here
51# should not be resolved either. The shell exports $PWD: use it.
52testcmd '-s' '-s uno/two' "$PWD/uno/two\n" '' ''
53testcmd '-s link/..' '-es dos/three' "$PWD/dos/three\n" '' ''
54testcmd '-s .. eats symlink' '-s dos/..' "$PWD\n" '' ''
55# In toybox this test is consistent with the previous one
56toyonly testing '-s .. eats symlink in $PWD' \
57  'cd dos && realpath -s ..' "$PWD\n" '' ''
58# Logically -es means the _symlink_ should exist, but match behavior...
59ln -s missing dangling
60testcmd '-es dangling symlink' '-es dangling 2>/dev/null || echo ok' \
61  'ok\n' '' ''
62testcmd '-ms' '-ms dangling/../dos/../one/two' "$PWD/one/two\n" '' ''
63ln -s ../two/.. one/two/ichi
64testcmd '-es' '-es one/two/ichi/two/ichi/two' "$PWD/one/two/ichi/two/ichi/two\n" '' ''
65
66rm -rf file one uno dos
67