~sircmpwn/ctools

ref: fbf17d92f5ed1c38983f73df912f051ad0f9ef2d ctools/test/tee -rwxr-xr-x 1.6 KiB View raw
fbf17d92 — Gabor Koszegi tee: Fix missing output file truncation 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/sh
tool="tee"
. "$HARNESS"

should_handle_zero_file() (
	res="$(echo "tesT 1ine" | tee)"
	[ "$res" = "tesT 1ine" ]
)

should_handle_one_file() (
	stdout="$(echo "tesT 1ine" | tee "$TMPDIR"/one-file)"
	res="$(cat "$TMPDIR"/one-file)"
	[ "$res" = "tesT 1ine" ] && [ "$res" = "$stdout" ] || return 1

	echo "a really long line for test" >"$TMPDIR"/one-file
	stdout="$(echo "shorter line" | tee "$TMPDIR"/one-file)"
	res="$(cat "$TMPDIR"/one-file)"
	[ "$res" = "shorter line" ] && [ "$res" = "$stdout" ]
)

should_handle_two_files() (
	stdout="$(echo "tEst lINe" | tee "$TMPDIR"/two-files-1 \
		"$TMPDIR"/two-files-2)"
	res1="$(cat "$TMPDIR"/two-files-1)"
	res2="$(cat "$TMPDIR"/two-files-2)"
	[ "$res1" = "tEst lINe" ] && [ "$res1" = "$res2" ] && [ "$res1" = "$stdout" ]
)

should_handle_a_flag() (
	stdout1="$(echo "tesT 1ine" | tee "$TMPDIR"/a-flag)"
	stdout2="$(echo "tesT 2ine" | tee -a "$TMPDIR"/a-flag)"
	res="$(cat "$TMPDIR"/a-flag)"
	[ "$res" = "$(printf "tesT 1ine\ntesT 2ine")" ] \
		&& [ "$stdout1" = "tesT 1ine" ] && [ "$stdout2" = "tesT 2ine" ]
)

should_handle_sigint() (
	set -m
	tee </dev/urandom >/dev/null &
	sleep 1
	ps -o pid= 2>&1 | grep $!
	[ $? -eq 0 ] || return 1
	kill -s INT $!
	ps -o pid= 2>&1 | grep $!
	[ $? -ne 0 ]
)

should_handle_i_flag() (
	set -m
	tee -i </dev/urandom >/dev/null &
	sleep 1
	ps -o pid= 2>&1 | grep $!
	[ $? -eq 0 ] || return 1
	kill -s INT $!
	ps -o pid= 2>&1 | grep $!
	[ $? -eq 0 ] || return 1
	kill -s TERM $!
	ps -o pid= 2>&1 | grep $!
	[ $? -ne 0 ]
)

runtests \
	should_handle_zero_file \
	should_handle_one_file \
	should_handle_two_files \
	should_handle_a_flag \
	should_handle_sigint \
	should_handle_i_flag