xref: /freebsd/bin/pwd/tests/pwd_test.sh (revision 5b398611607b0dab2f2550ef73f62d41dab6fac5)
1#
2# Copyright (c) 2026 Dag-Erling Smørgrav
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6
7atf_test_case logical
8logical_head()
9{
10	atf_set "descr" "Cases where pwd prints the " \
11		"logical working directory"
12}
13logical_body()
14{
15	root=$(realpath $PWD)
16	atf_check mkdir -p phy/baz
17	atf_check ln -s phy log
18	cd log/baz
19
20	# explicitly request logical
21	export PWD="$root/log/baz"
22	atf_check -o inline:"$root/log/baz\n" pwd -L
23	atf_check -o inline:"$root/log/baz\n" pwd -P -L
24
25	# logical is also the default
26	export PWD="$root/log/baz"
27	atf_check -o inline:"$root/log/baz\n" pwd
28}
29
30atf_test_case physical
31physical_head()
32{
33	atf_set "descr" "Cases where pwd prints the " \
34		"physical working directory"
35}
36physical_body()
37{
38	root=$(realpath $PWD)
39	atf_check mkdir -p phy/baz
40	atf_check ln -s phy log
41	cd log/baz
42
43	# explicitly request physical
44	export PWD="$root/log/baz"
45	atf_check -o inline:"$root/phy/baz\n" pwd -P
46	atf_check -o inline:"$root/phy/baz\n" pwd -L -P
47
48	# request logical but $PWD is relative
49	export PWD="log/baz"
50	atf_check -o inline:"$root/phy/baz\n" pwd -L
51
52	# request logical but $PWD contains dot
53	export PWD="$root/log/./baz"
54	atf_check -o inline:"$root/phy/baz\n" pwd -L
55
56	# request logical but $PWD contains dot-dot
57	export PWD="$root/log/../log/baz"
58	atf_check -o inline:"$root/phy/baz\n" pwd -L
59
60	# request logical but $PWD does not exist
61	export PWD="$root/baz"
62	atf_check -o inline:"$root/phy/baz\n" pwd -L
63
64	# request logical but $PWD does not match
65	export PWD="$root/log"
66	atf_check -o inline:"$root/phy/baz\n" pwd -L
67}
68
69atf_test_case stdout
70stdout_head()
71{
72	atf_set descr "error writing to stdout"
73}
74stdout_body()
75{
76	pwd=$(which pwd)
77	[ -f "$pwd" ] || atf_skip "unable to distinguish binary from builtin"
78	(
79		trap "" PIPE
80		# Give true(1) some time to exit.
81		sleep 1
82		$pwd 2>stderr
83		echo $? >result
84	) | true
85	atf_check -o inline:"1\n" cat result
86	atf_check -o match:"stdout" cat stderr
87}
88
89atf_init_test_cases()
90{
91	atf_add_test_case logical
92	atf_add_test_case physical
93	atf_add_test_case stdout
94}
95