about summary refs log tree commit diff
path: root/Test/C03traps.ztst
blob: 81ef38a30c62eeba3eaf31c3aaf2da6d798ada91 (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Tests for both trap builtin and TRAP* functions.

%prep

  setopt localtraps
  mkdir traps.tmp && cd traps.tmp

%test

  fn1() {
    trap 'print EXIT1' EXIT
    fn2() { trap 'print EXIT2' EXIT; }
    fn2
  }
  fn1
0:Nested `trap ... EXIT'
>EXIT2
>EXIT1

  fn1() {
    TRAPEXIT() { print EXIT1; }
    fn2() { TRAPEXIT() { print EXIT2; }; }
    fn2
  }
  fn1
0: Nested TRAPEXIT
>EXIT2
>EXIT1

  fn1() {
    trap 'print EXIT1' EXIT
    fn2() { trap - EXIT; }
    fn2
  }
  fn1
0:Nested `trap - EXIT' on `trap ... EXIT'
>EXIT1

  fn1() {
    TRAPEXIT() { print EXIT1; }
    fn2() { trap - EXIT; }
    fn2
  }
  fn1
0:Nested `trap - EXIT' on `TRAPEXIT'
>EXIT1

# We can't test an EXIT trap for the shell as a whole, because
# we're inside a function scope which we don't leave when the
# subshell exits.  Not sure if that's the correct behaviour, but
# it's sort of consistent.
  ( fn1() { trap 'print Function 1 going' EXIT; exit; print Not reached; }
    fn2() { trap 'print Function 2 going' EXIT; fn1; print Not reached; }
    fn2
  )
0:EXIT traps on functions when exiting from function
>Function 1 going
>Function 2 going

  fn1() {
    trap
    trap 'print INT1' INT
    fn2() { trap 'print INT2' INT; trap; }
    trap
    fn2
    trap
  }
  fn1
0: Nested `trap ... INT', not triggered
>trap -- 'print INT1' INT
>trap -- 'print INT2' INT
>trap -- 'print INT1' INT

   fn1() {
    trap
    TRAPINT() { print INT1; }
    fn2() { TRAPINT() { print INT2; }; trap; }
    trap
    fn2
    trap
  }
  fn1
0: Nested `trap ... INT', not triggered
>TRAPINT () {
>	print INT1
>}
>TRAPINT () {
>	print INT2
>}
>TRAPINT () {
>	print INT1
>}

  fn1() {
    trap 'print INT1' INT
    fn2() { trap - INT; trap; }
    trap
    fn2
    trap
  }
  fn1
0: Nested `trap - INT' on untriggered `trap ... INT'
>trap -- 'print INT1' INT
>trap -- 'print INT1' INT

# Testing the triggering of traps here is very unpleasant.
# The delays are attempts to avoid race conditions, though there is
# no guarantee that they will work.  Note the subtlety that the
# `sleep' in the function which receives the trap does *not* get the
# signal, only the parent shell, which is waiting for a SIGCHILD.
# (At least, that's what I think is happening.) Thus we have to wait at
# least the full two seconds to make sure we have got the output from the
# execution of the trap.

  print 'This test takes at least three seconds...' >&8
  fn1() {
    trap 'print TERM1' TERM
    fn2() { trap 'print TERM2; return 1' TERM; sleep 2; }
    fn2 &
    sleep 1
    kill -TERM $!
    sleep 2
  }
  fn1
0: Nested `trap ... TERM', triggered on inner loop
>TERM2

  print 'This test, too, takes at least three seconds...' >&8
  fn1() {
    trap 'print TERM1; return 1' TERM
    fn2() { trap 'print TERM2; return 1' TERM; }
    fn2
    sleep 2
  }
  fn1 &
  sleep 1
  kill -TERM $!
  sleep 2
0: Nested `trap ... TERM', triggered on outer loop
>TERM1

  TRAPZERR() { print error activated; }
  fn() { print start of fn; false; print end of fn; }
  fn
  fn() {
    setopt localoptions localtraps
    unfunction TRAPZERR
    print start of fn
    false
    print end of fn
  }
  fn
  unfunction TRAPZERR
  print finish
0: basic localtraps handling
>start of fn
>error activated
>end of fn
>start of fn
>end of fn
>finish

  TRAPZERR() { print 'ERR-or!'; }
  f() { print f; false; }
  t() { print t; }
  f
  f && t
  t && f && true
  t && f
  testunset() {
    setopt localtraps
    unset -f TRAPZERR
    print testunset
    false
    true
  }
  testunset
  f
1: more sophisticated error trapping
>f
>ERR-or!
>f
>t
>t
>f
>ERR-or!
>testunset
>f
>ERR-or!

  f() {
    setopt localtraps
    TRAPWINCH() { print "Window changed.  That wrecked the test."; }
  }
  f
  f
  functions TRAPWINCH
1:Unsetting ordinary traps with localtraps.