xref: /freebsd/usr.bin/yes/tests/yes_test.sh (revision c5961b6fcfe0f3587be11187a9c666a7fa200f4b)
1#
2# Copyright (c) 2026 Klara, Inc.
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6
7atf_test_case none
8none_head()
9{
10	atf_set "descr" "No arguments"
11}
12none_body()
13{
14	atf_check \
15	    -o inline:"y\ny\ny\ny\ny\n" \
16	    -x "yes | head -5"
17}
18
19atf_test_case one
20one_head()
21{
22	atf_set "descr" "One argument"
23}
24one_body()
25{
26	local y="Hello, world!"
27	atf_check \
28	    -o inline:"${y}\n${y}\n${y}\n${y}\n${y}\n" \
29	    -x "yes '${y}' | head -5"
30}
31
32atf_test_case multi
33multi_head()
34{
35	atf_set "descr" "Multiple arguments"
36}
37multi_body()
38{
39	set -- The Magic Words are Squeamish Ossifrage
40	local y="$*"
41	atf_check \
42	    -o inline:"${y}\n${y}\n${y}\n${y}\n${y}\n" \
43	    -x "yes $* | head -5"
44}
45
46atf_test_case argv
47argv_head()
48{
49	atf_set "descr" "Verify that argv is unmolested"
50}
51argv_body()
52{
53	yes y >/dev/null &
54	local pid=$!
55	# Wait for yes(1) to exec before checking args
56	sleep 0.1
57	atf_check -o inline:"yes y\n" ps -o args= $pid
58	kill $pid
59	wait
60}
61
62atf_test_case stdout
63stdout_head()
64{
65	atf_set descr "Error writing to stdout"
66}
67stdout_body()
68{
69	(
70		trap "" PIPE
71		# Give true(1) some time to exit.
72		sleep 1
73		yes 2>stderr
74		echo $? >result
75	) | true
76	atf_check -o inline:"1\n" cat result
77	atf_check -o match:"stdout" cat stderr
78}
79
80atf_init_test_cases()
81{
82	atf_add_test_case none
83	atf_add_test_case one
84	atf_add_test_case multi
85	atf_add_test_case argv
86	atf_add_test_case stdout
87}
88