about summary refs log tree commit diff
path: root/Test/D10nofork.ztst
blob: 5bb10266fa4b721932ca2f915726cba5224b7bec (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# Tests for "nofork" command substitution.

%prep
  mkdir nofork.tmp
  touch nofork.tmp/file{1,2}.txt

  purr() { print -r -- "$@" }
  purl() { print -rl -- "$@" }

%test

  REPLY=OUTER
  purr ${| REPLY=INNER } $REPLY
0:Basic substitution and REPLY scoping
>INNER OUTER

  reply=(x OUTER x)
  purl ${{reply} reply=(\{ INNER \})} $reply
0:Basic substitution, brace quoting, and array result
>{
>INNER
>}
>{
>INNER
>}

  () {
    setopt localoptions ignorebraces
    purl ${{reply} reply=({ INNER })} $reply
  }
0:Basic substitution, ignorebraces, and array result
>{
>INNER
>}
>{
>INNER
>}

  purr ${| REPLY=first}:${| REPLY=second}:$REPLY
0:re-scoping of REPLY in one statement
>first:second:OUTER

  purr BEGIN${| printf -v REPLY '%s\n' one two three ; }END
0:Adjacent words
>BEGINone
>two
>three
>END

  purr "BEGIN${| printf -v REPLY '%s\n' one two three }END"
0:Adjacent words and quoting, part 1
>BEGINone
>two
>three
>END

  purr BEGIN"${| printf -v REPLY '%s\n' one two three }"END
0:Adjacent words and quoting, part 2
>BEGINone
>two
>three
>END

  purr BEGIN"${|
   printf -v REPLY '%s\n'\
    one two three
  }"END
0:Embedded newlines
>BEGINone
>two
>three
>END

  purr BEGIN"${|
   printf -v REPLY $'%s\n' one two three
  }"END
0:Embedded newlines and $'...'
>BEGINone
>two
>three
>END

  purl ${| print -v REPLY one word here; setopt shwordsplit }
  purl ${| print -v REPLY three words here }
  purl "and ${| print -v REPLY one word here }"
  unsetopt shwordsplit
0:test word splitting on result
F:setting option inside is too late for that substitution
>one word here
>three
>words
>here
>and one word here

 (
  cd nofork.tmp
  setopt globsubst
  purr ${| REPLY=f* }
  purr ${| REPLY=f? }*
  unsetopt globsubst
  purr ${| REPLY=f* }
  purr ${| REPLY=f? }*
 )
1:globsubst on result
>file1.txt file2.txt
>file1.txt file2.txt
>f*
?(eval):8: no matches found: f?*

  purr ${| REPLY=$'trailing newlines remain\n\n' }
0:newline removal should not occur, part 1
>trailing newlines remain
>
>

  purr ${ echo $'one trailing newline\nremoved\n\n\n' }
0:newline removal in ${ ... }, zsh mode
>one trailing newline
>removed
>
>
>

  () {
    emulate -L ksh
    purl ${ echo $'all trailing newlines\nremoved\n\n\n' }
    purr "${ echo $'all trailing newlines\nremoved\n\n\n' }"
  }
0:newline removal in ${ ... }, emulation mode, shwordsplit
>all
>trailing
>newlines
>removed
>all trailing newlines
>removed

  purr "${ echo $'no trailing newlines\nremoved\n\n\n' }"
0:newline removal should not occur, part 2
>no trailing newlines
>removed
>
>
>
>

  () {
   purr ${| REPLY=$* ; shift 2 }
   purr $*
  } these are arguments
0:access to context $argv
>these are arguments
>arguments

  purr ${:-${| REPLY=${REPLY:-buried}}}
  purr ${:-"${| REPLY=${REPLY:-more buried}}"}
0:nofork inside parameter scope
>buried
>more buried

  : ${(e):-'${| REPLY=oops'}
1:unclosed braces are sometimes a bad substitution
F:This seems silly, but see A01grammar ${(e):-'${'} test
?(eval):1: bad substitution

  purr ${| REPLY=oops
1:other times lack of closing brace is merely unexpected
F:Why not use this error in the previous case as well?
?(eval):1: closing brace expected

# Next tests check that the PS2 stack is properly managed on error

  purr ${| REPLY=${REPLY:-buried}}}
1:unbalanced braces, part 0
?(eval):1: parse error near `}'

  purr ${:-${| REPLY=${REPLY:-buried}}
1:unbalanced braces, part 1
?(eval):1: closing brace expected

  purr ${:-"${| REPLY=${REPLY:-more buried}"}
1:unbalanced braces, part 2
?(eval):1: unmatched "

  purr ${:-"${| REPLY=${REPLY:-more buried"}}}
1:unbalanced braces, part 3
?(eval):1: unmatched "

  purr ${:-"${| REPLY=${REPLY:-more buried}}}"
1:unbalanced braces, part 4
?(eval):1: closing brace expected

# Same tests with leading space (future-proofing)

  purr ${ purr ${REPLY:-buried}}}
1:unbalanced braces, part 0+
?(eval):1: parse error near `}'

  purr ${:-${ purr ${REPLY:-buried}}
1:unbalanced braces, part 1+
?(eval):1: closing brace expected

  purr ${:-"${ purr ${REPLY:-more buried}"}
1:unbalanced braces, part 2+
?(eval):1: unmatched "

  purr ${:-"${ purr ${REPLY:-more buried"}}}
1:unbalanced braces, part 3+
?(eval):1: unmatched "

  purr ${:-"${ purr ${REPLY:-more buried}}}"
1:unbalanced braces, part 4+
?(eval):1: closing brace expected

  purr "${ purr STDOUT }"
0:capture stdout
>STDOUT
>

# end PS2 stack tests 

  purr $(purr outside ${| REPLY=inside })
  purr BEGIN$(purr outside ${| REPLY=inside })END
  purr "BEGIN$(purr outside ${| REPLY=inside })END"
  purr outside ${| REPLY=$(purr inside)}
  purr "outside ${| REPLY=$(purr inside)}"
0:mixing with forking cmdsubst
>outside inside
>BEGINoutside insideEND
>BEGINoutside insideEND
>outside inside
>outside inside

  purr `purr outside ${| REPLY=inside }`
  purr "outside `purr ${| REPLY=inside }`"
  purr outside ${| REPLY=`purr inside`}
  purr "outside ${| REPLY=`purr inside`}"
  purr outside "`purr ${| REPLY="${:-inside}"}`"
  purr "outside ${| REPLY=`purr ${:-inside}`}"
0:mixing with backticks
>outside inside
>outside inside
>outside inside
>outside inside
>outside inside
>outside inside

  purr ${| REPLY=$(( 9 + 17 )) }
  purr $(( 9 + ${| REPLY=17 } ))
0:mixing with arithemetic
>26
>26

  unset reply
  purl ${{reply} reply=(1 2 ${| REPLY=3 } 4) }
  typeset -p reply
0:array behavior with global assignment
>1
>2
>3
>4
>typeset -g -a reply=( 1 2 3 4 )

  unset outer
  purr "${|
   outer=OUTER
   REPLY=INNER
   return 7
   OUTER=NOTREACHED
  } $outer $?"
0:return statement inside, part 1
F:status of "print" should hide return
>INNER OUTER 7

  unset outer
  outer=${| REPLY=${| return 7}}
7:return status propages in assignment like $(...)

  unset outer
  purr "${|
   outer=OUTER
   REPLY=INNER
   return 7
   OUTER=NOTREACHED
  } $outer $?"
  print REACHED $OUTER
0:return statement inside, part 2
>INNER OUTER 7
>REACHED

  unset outer
  purr "${|
   # Localoptions needed to avoid breaking test harness?
   # The setopt command affects surrounding context
   setopt localoptions errreturn
   outer=OUTER
   REPLY=INNER
   false
   OUTER=NOTREACHED
  } $outer $?"
  print REACHED $OUTER ${options[errreturn]}
0:errreturn works inside and remains outside
>INNER OUTER 1
>REACHED on

 (
  unset outer
  purr "${|
   outer=OUTER
   REPLY=INNER
   exit 7
   OUTER=NOTREACHED
  } $outer $OUTER $?"
  print NOT REACHED
 )
7:exit statement inside

 (
  unset outer
  purr "${|
   setopt errexit
   outer=OUTER
   REPLY=INNER
   false
   OUTER=NOTREACHED
  } $outer $?"
  print NOT REACHED
 )
1:errexit inside

  outer=GLOBAL
  purr "${|
   local outer=LOCAL
   REPLY=INNER
  } $outer $?"
0:local declaration inside
>INNER GLOBAL 0

  unset zz
  outer=GLOBAL
  purr "${{zz}
   local outer=LOCAL
   zz=NONLOCAL
  } $outer $?"
  print $zz
0:local declaration, global assignment, part 1
>NONLOCAL GLOBAL 0
>NONLOCAL

  unset zz
  outer=GLOBAL
  purr "${${|
   local outer=LOCAL
   zz=NONLOCAL
  }:-$zz} $outer $?"
0:local declaration, global assignment, part 2 (evaluation order)
>NONLOCAL GLOBAL 0

  : ${| fn1() { () { print -v REPLY $'Defined Function' ;} ;} }
  print "IN${| fn2() { () { print "${:-Second }${|fn1}" ;} ;} }OUT"
  fn2
0:function definition, brace nesting, quote nesting
>INOUT
>Second Defined Function

  <<-EOF
	${| REPLY=$'in a here document\n' }
	EOF
0:here-document behavior
F:Fiddly here to get EOF past the test syntax
>in a here document
>

  <<<${| REPLY="in a here string" }
0:here-string behavior
>in a here string

  <<<"${ purr $'stdout as a here string' }"
0:another capture stdout
>stdout as a here string
>

  wrap=${| REPLY="REPLY in environment assignment" } typeset -p wrap
  wrap=${ purr "capture in environment assignment" } typeset -p wrap
0:assignment context
>typeset -g wrap='REPLY in environment assignment'
>typeset -g wrap='capture in environment assignment'

# Repeat return and exit tests with stdout capture

  purr "${
   print INNER
   return 7
  } $?"
0:return statement inside, part 1+
F:status of "print" should hide return
>INNER
> 7

  unset outer
  outer=${ return 7 }
7:return status propages in stdout capture

  unset outer
  purr "${
   outer=OUTER
   print INNER
   return 7
   OUTER=NOTREACHED
  } $outer $?"
  print REACHED $OUTER
0:return statement inside, part 2+
>INNER
> OUTER 7
>REACHED

  unset outer
  purr "${
   # Localoptions needed to avoid breaking test harness?
   # The setopt command affects surrounding context
   setopt localoptions errreturn
   outer=OUTER
   print INNER
   false
   OUTER=NOTREACHED
  } $outer $?"
  print REACHED $OUTER ${options[errreturn]}
0:errreturn works inside stdout capture
>INNER
> OUTER 1
>REACHED on

 (
  unset outer
  purr "${
   outer=OUTER
   print INNER
   exit 7
   OUTER=NOTREACHED
  } $outer $OUTER $?"
  print NOT REACHED
 )
7:exit statement inside stdout capture

 (
  unset outer
  purr "${
   setopt errexit
   outer=OUTER
   print INNER
   false
   OUTER=NOTREACHED
  } $outer $?"
  print NOT REACHED
 )
1:errexit inside stdout capture

  setopt ignorebraces
0:dummy test to set option soon enough
F:must do this before evaluating the next test block

  purr ${| REPLY=${REPLY:-buried}}}
0:ignored braces, part 1
>buried}

  # Global $REPLY still set from earlier test
  purr "${ purr ${REPLY:+buried}}}"
0:ignored braces, part 2
>buried
>}

  purr ${ { echo nested ;} }
0:ignored braces, part 3
>nested

  purr ${ { echo nested } } DONE
1:ignored braces, part 4
?(eval):3: parse error near `}'

  unsetopt ignorebraces
  # "break" blocks function calls in outer loop
  # Could use print, but that might get fixed
  repeat 3 do purr ${
   for x in 1 2 3 4
    do (( x == 3 )) && break 2
     # use error output to confirm loop count
     print -u 2 $x
    done
   } XX
  done
0:break N propagates
?1
?2

 # Cannot "purr": break skips pending function calls
 # Use "repeat" to avoid infinite loop on failure 
 repeat 3 do; echo ${|REPLY=x; break }; done
 repeat 3 do; echo ${{x} x=y; break }; done
 repeat 3 do; echo ${ echo z; break }; done
0:break after assignment completes the assignment
>x
>y
>z

 # Subshell because error exits
 ( purr ${ echo ${unset?oops} } )
1:error handling (without crashing)
*?*unset: oops

 purr ${ .zsh.cmdsubst=error }
1:reserved parameter name (without crashing)
*?*.zsh.cmdsubst: can't modify read-only parameter

%clean

  unfunction purr purl