OILS / spec / ysh-place.test.sh View on Github | oilshell.org

136 lines, 43 significant
1## our_shell: ysh
2## oils_failures_allowed: 1
3
4#### Local place
5
6# Work around stdin buffering issue with read --line
7#
8# The framework test/sh_spec.py uses echo "$code_string" | $SH
9#
10# But then we have TWO different values of file descriptor 0 (stdin)
11#
12# - the pipe with the code
13# - the pipe created in th shell for echo zzz | read --line
14#
15# Shells read one line at a time, but read --line explicitly avoids this.
16#
17# TODO: I wonder if we should consider outlawing read --line when stdin has code
18# Only allow it for:
19#
20# $SH -c 'echo hi'
21# $SH myscript.sh
22#
23# There could be a warning like read --line --no-fighting or something.
24
25cat >tmp.sh <<'EOF'
26func f(place) {
27 var x = 'f'
28 echo zzz | read --all (place)
29 echo "f x=$x"
30}
31
32func fillPlace(place) {
33 var x = 'fillPlace'
34 call f(place)
35 echo "fillPlace x=$x"
36}
37
38proc p {
39 var x = 'hi'
40 call fillPlace(&x)
41 echo "p x=$x"
42}
43
44x=global
45
46p
47
48echo "global x=$x"
49EOF
50
51$SH tmp.sh
52
53## STDOUT:
54f x=f
55fillPlace x=fillPlace
56p x=zzz
57
58global x=global
59## END
60
61#### place->setValue()
62
63func f(place) {
64 var x = 'f'
65 call place->setValue('zzz')
66 echo "f x=$x"
67}
68
69func fillPlace(place) {
70 var x = 'fillPlace'
71 call f(place)
72 echo "fillPlace x=$x"
73}
74
75proc p {
76 var x = 'hi'
77 call fillPlace(&x)
78 echo "p x=$x"
79}
80
81x=global
82
83p
84echo "global x=$x"
85
86## STDOUT:
87f x=f
88fillPlace x=fillPlace
89p x=zzz
90global x=global
91## END
92
93#### Places can't dangle; they should be passed UP the stakc only
94
95func f() {
96 var f_local = null
97 return (&f_local)
98}
99
100func g() {
101 # This place is now INVALID!
102 var place = f()
103
104 # Should fail when we try to use the place
105 echo zzz | read --all (place)
106
107 # This should also fail
108 # call place->setValue('zzz')
109
110}
111
112call g()
113
114echo done
115
116## status: 1
117## STDOUT:
118## END
119
120
121#### Container Place (Dict)
122
123var d = {key: 'hi'}
124
125echo zzz | read --all (&d.key)
126
127# I guess this works
128echo newkey | read --all (&d.newkey)
129
130echo key=$[d.key]
131echo key=$[d.newkey]
132
133## STDOUT:
134## END
135
136