fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Nahorerror rewrite: use new `Error` to report errors30e6aa85e

master
30.3 KiB1149 linesraw
1# RUN: env FISH=%fish %fish %s
2# Environment variable tests
3
4# Test if variables can be properly set
5
6set smurf blue
7
8if test $smurf = blue
9    echo Test 1 pass
10else
11    echo Test 1 fail
12end
13# CHECK: Test 1 pass
14
15# Test if variables can be erased
16
17set -e smurf
18if set -q smurf
19    echo Test 2 fail
20else
21    echo Test 2 pass
22end
23# CHECK: Test 2 pass
24
25# Test if local variables go out of scope
26
27set -e t3
28if true
29    set -l t3 bar
30end
31
32if set -q t3
33    echo Test 3 fail
34else
35    echo Test 3 pass
36end
37# CHECK: Test 3 pass
38
39# Test if globals can be set in block scope
40
41if true
42    set -g baz qux
43end
44
45if test $baz = qux
46    echo Test 4 pass
47else
48    echo Test 4 fail
49end
50# CHECK: Test 4 pass
51
52#Test that scope is preserved when setting a new value
53
54set t5 a
55
56if true
57    set t5 b
58end
59
60if test $t5 = b
61    echo Test 5 pass
62else
63    echo Test 5 fail
64end
65# CHECK: Test 5 pass
66
67# Test that scope is preserved in double blocks
68
69for i in 1
70    set t6 $i
71    for j in a
72        if test $t6$j = 1a
73            echo Test 6 pass
74        else
75            echo Test 6 fail
76        end
77    end
78end
79# CHECK: Test 6 pass
80
81# Test if variables in for loop blocks do not go out of scope on new laps
82
83set res fail
84
85set -e t7
86for i in 1 2
87    if test $i = 1
88        set t7 lala
89    else
90        if test $t7
91            set res pass
92        end
93    end
94end
95
96echo Test 7 $res
97# CHECK: Test 7 pass
98
99# Test if variables are properly exported
100
101set -e t8
102if true
103    set -lx t8 foo
104    if test ($FISH -c "echo $t8") = foo
105        echo Test 8 pass
106    else
107        echo Test 8 fail
108    end
109end
110# CHECK: Test 8 pass
111
112# Test if exported variables go out of scope
113
114if $FISH -c "set -q t8; and exit 0; or exit 1"
115    echo Test 9 fail
116else
117    echo Test 9 pass
118end
119# CHECK: Test 9 pass
120
121# Test erasing variables in specific scope
122
123set -eU __fish_test_universal_variables_variable_foo
124set -g __fish_test_universal_variables_variable_foo bar
125begin
126    set -l __fish_test_universal_variables_variable_foo baz
127    set -eg __fish_test_universal_variables_variable_foo
128end
129
130if set -q __fish_test_universal_variables_variable_foo
131    echo Test 10 fail
132else
133    echo Test 10 pass
134end
135# CHECK: Test 10 pass
136
137set __fish_test_universal_variables_variable_foo abc def
138set -e __fish_test_universal_variables_variable_foo[1]
139if test $__fish_test_universal_variables_variable_foo '=' def
140    echo Test 11 pass
141else
142    echo Test 11 fail
143end
144# CHECK: Test 11 pass
145
146# Test combinations of export and scope
147
148set -ge __fish_test_universal_variables_variable_foo
149set -Ue __fish_test_universal_variables_variable_foo
150set -Ux __fish_test_universal_variables_variable_foo bar
151set __fish_test_universal_variables_variable_foo baz
152if test (/bin/sh -c 'echo $__fish_test_universal_variables_variable_foo') = baz -a ($FISH -c 'echo $__fish_test_universal_variables_variable_foo') = baz
153    echo Test 12 pass
154else
155    echo Test 12 fail
156end
157# CHECK: Test 12 pass
158
159set -Ue __fish_test_universal_variables_variable_foo
160
161# Should no longer be in environment (#2046)
162env | string match '__fish_test_universal_variables_variable_foo=*'
163
164set -Ux __fish_test_universal_variables_variable_foo bar
165set -U __fish_test_universal_variables_variable_foo baz
166if test (/bin/sh -c 'echo $__fish_test_universal_variables_variable_foo') = baz -a ($FISH -c 'echo $__fish_test_universal_variables_variable_foo') = baz
167    echo Test 13 pass
168else
169    echo Test 13 fail
170end
171# CHECK: Test 13 pass
172
173set -Ux __fish_test_universal_variables_variable_foo bar
174set -u __fish_test_universal_variables_variable_foo bar
175if test (/bin/sh -c 'echo $__fish_test_universal_variables_variable_foo') = '' -a ($FISH -c 'echo $__fish_test_universal_variables_variable_foo') = bar
176    echo Test 14 pass
177else
178    echo Test 14 fail
179end
180# CHECK: Test 14 pass
181
182set -Ux __fish_test_universal_variables_variable_foo bar
183set -Uu __fish_test_universal_variables_variable_foo baz
184if test (/bin/sh -c 'echo $__fish_test_universal_variables_variable_foo') = '' -a ($FISH -c 'echo $__fish_test_universal_variables_variable_foo') = baz
185    echo Test 15 pass
186else
187    echo Test 15 fail
188end
189# CHECK: Test 15 pass
190
191set -eU __fish_test_universal_variables_variable_foo
192function watch_foo --on-variable __fish_test_universal_variables_variable_foo
193    echo Foo change detected
194end
195
196set -U __fish_test_universal_variables_variable_foo 1234
197# CHECK: Foo change detected
198set -eU __fish_test_universal_variables_variable_foo
199# CHECK: Foo change detected
200# WTF set -eg __fish_test_universal_variables_variable_foo
201
202functions -e watch_foo
203
204# test erasing variables without a specified scope
205
206set -g test16res
207set -U __fish_test_universal_variables_variable_foo universal
208set -g __fish_test_universal_variables_variable_foo global
209
210begin
211    set -l __fish_test_universal_variables_variable_foo blocklocal
212
213    function test16
214        set -l __fish_test_universal_variables_variable_foo function
215        begin
216            set -l __fish_test_universal_variables_variable_foo functionblock
217            set test16res $test16res $__fish_test_universal_variables_variable_foo
218
219            # This sequence seems pointless but it's really verifying that we
220            # successfully expose higher scopes as we erase the closest scope.
221            set -e __fish_test_universal_variables_variable_foo
222            set test16res $test16res $__fish_test_universal_variables_variable_foo
223
224            set -e __fish_test_universal_variables_variable_foo
225            set test16res $test16res $__fish_test_universal_variables_variable_foo
226
227            set -e __fish_test_universal_variables_variable_foo
228            set test16res $test16res $__fish_test_universal_variables_variable_foo
229
230            set -e __fish_test_universal_variables_variable_foo
231            set -q __fish_test_universal_variables_variable_foo
232            and set test16res $test16res $__fish_test_universal_variables_variable_foo
233        end
234
235        set -q __fish_test_universal_variables_variable_foo
236        and echo __fish_test_universal_variables_variable_foo should set after test16 inner begin..end
237    end
238
239    test16
240    set test16res $test16res $__fish_test_universal_variables_variable_foo
241end
242# CHECK: count:5 content:[functionblock function global universal blocklocal]
243
244set -q __fish_test_universal_variables_variable_foo
245and echo __fish_test_universal_variables_variable_foo should set after test16 outer begin..end
246
247echo count:(count $test16res) "content:[$test16res]"
248if test (count $test16res) = 5 -a "$test16res" = "functionblock function global universal blocklocal"
249    echo Test 16 pass
250else
251    echo Test 16 fail
252end
253# CHECK: Test 16 pass
254
255# Test that shadowing with a non-exported variable works
256set -gx __fish_test_env17 UNSHADOWED
257env | string match '__fish_test_env17=*'
258# CHECK: __fish_test_env17=UNSHADOWED
259
260function __fish_test_shadow
261    set -l __fish_test_env17
262    env | string match -q '__fish_test_env17=*'; or echo SHADOWED
263end
264__fish_test_shadow
265# CHECK: SHADOWED
266
267# Test that the variable is still exported (#2611)
268env | string match '__fish_test_env17=*'
269# CHECK: __fish_test_env17=UNSHADOWED
270
271# Test that set var (command substitution) works with if/while.
272
273if set fish_test_18 (false)
274    echo Test 18 fail
275else
276    echo Test 18 pass
277end
278# CHECK: Test 18 pass
279
280if not set fish_test_18 (true)
281    echo Test 18 fail
282else
283    echo Test 18 pass
284end
285# CHECK: Test 18 pass
286
287set __fish_test_18_status pass
288while set fish_test_18 (false); or not set fish_test_18 (true)
289    set __fish_test_18_status fail
290    break
291end
292echo Test 18 $__fish_test_18_status
293# CHECK: Test 18 pass
294
295# Test that local exported variables are copied to functions (#1091)
296function __fish_test_local_export
297    echo $var
298    set var boo
299    echo $var
300end
301set -lx var wuwuwu
302__fish_test_local_export
303# CHECK: wuwuwu
304# CHECK: boo
305echo $var
306# CHECK: wuwuwu
307
308# Test that we don't copy local-exports to blocks.
309set -lx var foo
310begin
311    echo $var
312    # CHECK: foo
313    set var bar
314    echo $var
315    # CHECK: bar
316end
317echo $var # should be "bar"
318# CHECK: bar
319
320# clear for other shells
321set -eU __fish_test_universal_variables_variable_foo
322
323# Test behavior of universals on startup (#1526)
324echo Testing Universal Startup
325# CHECK: Testing Universal Startup
326set -U testu 0
327$FISH -c 'set -U testu 1'
328echo $testu
329# CHECK: 1
330$FISH -c 'set -q testu; and echo $testu'
331# CHECK: 1
332
333$FISH -c 'set -U testu 2'
334echo $testu
335# CHECK: 2
336$FISH -c 'set -q testu; and echo $testu'
337# CHECK: 2
338
339$FISH -c 'set -e testu'
340set -q testu
341or echo testu undef in top level shell
342# CHECK: testu undef in top level shell
343$FISH -c 'set -q testu; or echo testu undef in sub shell'
344# CHECK: testu undef in sub shell
345
346begin
347    # test SHLVL
348    # use a subshell to ensure a clean slate
349    env SHLVL= $FISH -ic 'echo SHLVL: $SHLVL; $FISH -ic \'echo SHLVL: $SHLVL\''
350    # CHECK: SHLVL: 1
351    # CHECK: SHLVL: 2
352
353    # exec should decrement SHLVL - outer fish increments by 1, decrements for exec,
354    # inner fish increments again so the value stays the same.
355    env SHLVL=1 $FISH -ic 'echo SHLVL: $SHLVL; exec $FISH -ic \'echo SHLVL: $SHLVL\''
356    # CHECK: SHLVL: 2
357    # CHECK: SHLVL: 2
358
359    # garbage SHLVLs should be treated as garbage
360    env SHLVL=3foo $FISH -ic 'echo SHLVL: $SHLVL'
361    # CHECK: SHLVL: 1
362
363    # whitespace is allowed though (for bash compatibility)
364    env SHLVL="3  " $FISH -ic 'echo SHLVL: $SHLVL'
365    env SHLVL="  3" $FISH -ic 'echo SHLVL: $SHLVL'
366    # CHECK: SHLVL: 4
367    # CHECK: SHLVL: 4
368end
369
370# Non-interactive fish doesn't touch $SHLVL
371env SHLVL=2 $FISH -c 'echo SHLVL: $SHLVL'
372# CHECK: SHLVL: 2
373env SHLVL=banana $FISH -c 'echo SHLVL: $SHLVL'
374# CHECK: SHLVL: banana
375
376# Test transformation of inherited variables
377env DISPLAY="localhost:0.0" $FISH -c 'echo Elements in DISPLAY: (count $DISPLAY)'
378# CHECK: Elements in DISPLAY: 1
379
380# We can't use PATH for this because the global configuration will modify PATH
381# based on /etc/paths and /etc/paths.d.
382# Exported arrays are colon delimited; they are automatically split on colons if they end in PATH.
383set -gx FOO one two three four
384set -gx FOOPATH one two three four
385$FISH -c 'echo Elements in FOO and FOOPATH: (count $FOO) (count $FOOPATH)'
386# CHECK: Elements in FOO and FOOPATH: 1 4
387
388# some must use colon separators!
389set -lx MANPATH man1 man2 man3
390env | grep '^MANPATH='
391# CHECK: MANPATH=man1:man2:man3
392
393# ensure we don't escape space and colon values
394set -x DONT_ESCAPE_COLONS 1: 2: :3:
395env | grep '^DONT_ESCAPE_COLONS='
396# CHECK: DONT_ESCAPE_COLONS=1: 2: :3:
397
398set -x DONT_ESCAPE_SPACES '1 ' '2 ' ' 3 ' 4
399env | grep '^DONT_ESCAPE_SPACES='
400# CHECK: DONT_ESCAPE_SPACES=1  2   3  4
401
402set -x DONT_ESCAPE_COLONS_PATH 1: 2: :3:
403env | grep '^DONT_ESCAPE_COLONS_PATH='
404# CHECK: DONT_ESCAPE_COLONS_PATH=1::2:::3:
405
406# Path universal variables
407set -U __fish_test_path_not a b c
408set -U __fish_test_PATH 1 2 3
409echo "$__fish_test_path_not $__fish_test_PATH" $__fish_test_path_not $__fish_test_PATH
410# CHECK: a b c 1:2:3 a b c 1 2 3
411
412set --unpath __fish_test_PATH $__fish_test_PATH
413echo "$__fish_test_path_not $__fish_test_PATH" $__fish_test_path_not $__fish_test_PATH
414# CHECK: a b c 1 2 3 a b c 1 2 3
415
416set -q --path __fish_test_PATH
417and echo __fish_test_PATH is a pathvar
418or echo __fish_test_PATH is not a pathvar
419# CHECK: __fish_test_PATH is not a pathvar
420
421set -q --unpath __fish_test_PATH
422and echo __fish_test_PATH is not a pathvar
423or echo __fish_test_PATH is not not a pathvar
424# CHECK: __fish_test_PATH is not a pathvar
425
426set --path __fish_test_path_not $__fish_test_path_not
427echo "$__fish_test_path_not $__fish_test_PATH" $__fish_test_path_not $__fish_test_PATH
428# CHECK: a:b:c 1 2 3 a b c 1 2 3
429
430set -q --path __fish_test_path_not
431and echo __fish_test_path_not is a pathvar
432or echo __fish_test_path_not is not a pathvar
433# CHECK: __fish_test_path_not is a pathvar
434
435set -q --unpath __fish_test_path_not
436and echo __fish_test_path_not is not a pathvar
437or echo __fish_test_path_not is not not a pathvar
438# CHECK: __fish_test_path_not is not not a pathvar
439
440set --path __fish_test_PATH $__fish_test_PATH
441echo "$__fish_test_path_not $__fish_test_PATH" $__fish_test_path_not $__fish_test_PATH
442# CHECK: a:b:c 1:2:3 a b c 1 2 3
443
444set -U __fish_test_PATH 1:2:3
445echo "$__fish_test_PATH" $__fish_test_PATH
446# CHECK: 1:2:3 1 2 3
447
448set -e __fish_test_PATH
449set -e __fish_test_path_not
450
451set -U --path __fish_test_path2 a:b
452echo "$__fish_test_path2" $__fish_test_path2
453# CHECK: a:b a b
454
455set -e __fish_test_path2
456
457# Test empty uvars (#5992)
458set -Ux __fish_empty_uvar
459set -Uq __fish_empty_uvar
460echo $status
461# CHECK: 0
462$FISH -c 'set -Uq __fish_empty_uvar; echo $status'
463# CHECK: 0
464env | grep __fish_empty_uvar
465# CHECK: __fish_empty_uvar=
466
467# Variable names in other commands
468# Test invalid variable names in loops (#5800)
469for a,b in y 1 z 3
470    echo $a,$b
471end
472# CHECKERR: {{.*}} for: a,b: invalid variable name. See `help language#shell-variable-and-function-names`
473# CHECKERR: for a,b in y 1 z 3
474# CHECKERR:     ^~^
475
476# Global vs Universal Unspecified Scopes
477set -U __fish_test_global_vs_universal universal
478echo "global-vs-universal 1: $__fish_test_global_vs_universal"
479# CHECK: global-vs-universal 1: universal
480
481set -g __fish_test_global_vs_universal global
482echo "global-vs-universal 2: $__fish_test_global_vs_universal"
483# CHECK: global-vs-universal 2: global
484
485set __fish_test_global_vs_universal global2
486echo "global-vs-universal 3: $__fish_test_global_vs_universal"
487# CHECK: global-vs-universal 3: global2
488
489set -e -g __fish_test_global_vs_universal
490echo "global-vs-universal 4: $__fish_test_global_vs_universal"
491# CHECK: global-vs-universal 4: universal
492
493set -e -U __fish_test_global_vs_universal
494echo "global-vs-universal 5: $__fish_test_global_vs_universal"
495# CHECK: global-vs-universal 5:
496
497# Export local variables from all parent scopes (issue #6153).
498function func
499    echo $local
500end
501set -lx local outer
502func
503# CHECK: outer
504begin
505    func
506    # CHECK: outer
507
508    set -lx local inner
509    begin
510        func
511    end
512    # CHECK: inner
513end
514
515# Skip importing universal variables (#5258)
516while set -q EDITOR
517    set -e EDITOR
518end
519set -Ux EDITOR emacs -nw
520# CHECK: $EDITOR: set in universal scope, exported, with 2 elements
521$FISH -c 'set -S EDITOR' | string match -r -e 'global|universal'
522
523# When the variable has been changed outside of fish we accept it.
524# CHECK: $EDITOR: set in global scope, exported, with 1 elements
525# CHECK: $EDITOR: set in universal scope, exported, with 2 elements
526sh -c 'EDITOR=vim "$@" -c "set -S EDITOR"' uselessargument $FISH | string match -r -e 'global|universal'
527
528# Verify behavior of `set --show` given an invalid var name
529set --show 'argle bargle'
530#CHECKERR: set: argle bargle: invalid variable name. See `help language#shell-variable-and-function-names`
531#CHECKERR: {{.*}}set.fish (line {{\d+}}):
532#CHECKERR: set --show 'argle bargle'
533#CHECKERR: ^
534#CHECKERR: (Type 'help set' for related documentation)
535
536set --show array[1]
537# CHECKERR: set: `set --show` does not allow slices with the var names
538# CHECKERR: {{.*}}set.fish (line {{\d+}}):
539# CHECKERR: set --show array[1]
540# CHECKERR: ^
541# CHECKERR: (Type 'help set' for related documentation)
542
543# Verify behavior of `set --show`
544set semiempty ''
545set --show semiempty
546#CHECK: $semiempty: set in global scope, unexported, with 1 elements
547#CHECK: $semiempty[1]: ||
548
549set -U var1 hello
550set --show var1
551#CHECK: $var1: set in universal scope, unexported, with 1 elements
552#CHECK: $var1[1]: |hello|
553
554set -l var1
555set -g var1 goodbye "and don't come back"
556set --show var1
557#CHECK: $var1: set in local scope, unexported, with 0 elements
558#CHECK: $var1: set in global scope, unexported, with 2 elements
559#CHECK: $var1[1]: |goodbye|
560#CHECK: $var1[2]: |and don't come back|
561#CHECK: $var1: set in universal scope, unexported, with 1 elements
562#CHECK: $var1[1]: |hello|
563
564set -g var2
565set --show _unset_var var2
566#CHECK: $var2: set in global scope, unexported, with 0 elements
567
568# Appending works
569set -g var3a a b c
570set -a var3a
571set -a var3a d
572set -a var3a e f
573set --show var3a
574#CHECK: $var3a: set in global scope, unexported, with 6 elements
575#CHECK: $var3a[1]: |a|
576#CHECK: $var3a[2]: |b|
577#CHECK: $var3a[3]: |c|
578#CHECK: $var3a[4]: |d|
579#CHECK: $var3a[5]: |e|
580#CHECK: $var3a[6]: |f|
581set -g var3b
582set -a var3b
583set --show var3b
584#CHECK: $var3b: set in global scope, unexported, with 0 elements
585set -g var3c
586set -a var3c 'one string'
587set --show var3c
588#CHECK: $var3c: set in global scope, unexported, with 1 elements
589#CHECK: $var3c[1]: |one string|
590
591# Prepending works
592set -g var4a a b c
593set -p var4a
594set -p var4a d
595set -p var4a e f
596set --show var4a
597#CHECK: $var4a: set in global scope, unexported, with 6 elements
598#CHECK: $var4a[1]: |e|
599#CHECK: $var4a[2]: |f|
600#CHECK: $var4a[3]: |d|
601#CHECK: $var4a[4]: |a|
602#CHECK: $var4a[5]: |b|
603#CHECK: $var4a[6]: |c|
604set -g var4b
605set -p var4b
606set --show var4b
607#CHECK: $var4b: set in global scope, unexported, with 0 elements
608set -g var4c
609set -p var4c 'one string'
610set --show var4c
611#CHECK: $var4c: set in global scope, unexported, with 1 elements
612#CHECK: $var4c[1]: |one string|
613
614# Appending and prepending at same time works
615set -g var5 abc def
616set -a -p var5 0 x 0
617set --show var5
618#CHECK: $var5: set in global scope, unexported, with 8 elements
619#CHECK: $var5[1]: |0|
620#CHECK: $var5[2]: |x|
621#CHECK: $var5[3]: |0|
622#CHECK: $var5[4]: |abc|
623#CHECK: $var5[5]: |def|
624#CHECK: $var5[6]: |0|
625#CHECK: $var5[7]: |x|
626#CHECK: $var5[8]: |0|
627
628set -a
629# CHECKERR: set: expected >= 1 arguments; got 0
630# CHECKERR: {{.*}}checks/set.fish (line {{\d+}}):
631# CHECKERR: set -a
632# CHECKERR: ^
633# CHECKERR: (Type 'help set' for related documentation)
634set -p
635# CHECKERR: set: expected >= 1 arguments; got 0
636# CHECKERR: {{.*}}checks/set.fish (line {{\d+}}):
637# CHECKERR: set -p
638# CHECKERR: ^
639# CHECKERR: (Type 'help set' for related documentation)
640
641set -a foo[1]
642# CHECKERR: set: Cannot use --append or --prepend when assigning to a slice
643# CHECKERR: {{.*}}checks/set.fish (line {{\d+}}):
644# CHECKERR: set -a foo[1]
645# CHECKERR: ^
646# CHECKERR: (Type 'help set' for related documentation)
647set -p foo[1]
648# CHECKERR: set: Cannot use --append or --prepend when assigning to a slice
649# CHECKERR: {{.*}}checks/set.fish (line {{\d+}}):
650# CHECKERR: set -p foo[1]
651# CHECKERR: ^
652# CHECKERR: (Type 'help set' for related documentation)
653
654# Setting local scope when no local scope of the var uses the closest scope
655set -g var6 ghi jkl
656begin
657    set -l -a var6 mno
658    set --show var6
659end
660#CHECK: $var6: set in local scope, unexported, with 3 elements
661#CHECK: $var6[1]: |ghi|
662#CHECK: $var6[2]: |jkl|
663#CHECK: $var6[3]: |mno|
664#CHECK: $var6: set in global scope, unexported, with 2 elements
665#CHECK: $var6[1]: |ghi|
666#CHECK: $var6[2]: |jkl|
667
668# `and` creates no new scope on its own
669true; and set -l var7a 89 179
670set -q var7a
671echo $status
672#CHECK: 0
673
674# `begin` of an `and` creates a new scope
675true; and begin
676    set -l var7b 359 719
677end
678set -q var7b
679echo $status
680#CHECK: 1
681
682# `or` creates no new scope on its own
683false; or set -l var8a 1439 2879
684set -q var8a
685echo $status
686#CHECK: 0
687
688# `begin` of an `or` creates a new scope
689false; or begin
690    set -l var8b 9091 9901
691end
692set -q var8b
693echo $status
694#CHECK: 1
695
696# Exporting works
697set -x TESTVAR0
698set -x TESTVAR1 a
699set -x TESTVAR2 a b
700env | grep TESTVAR | sort | cat -v
701#CHECK: TESTVAR0=
702#CHECK: TESTVAR1=a
703#CHECK: TESTVAR2=a b
704
705set -x | grep TESTVAR | sort | cat -v
706#CHECK: TESTVAR0
707#CHECK: TESTVAR1 a
708#CHECK: TESTVAR2 'a' 'b'
709
710set -u TESTVAR0
711set -u TESTVAR2 a b
712set -u | grep TESTVAR | sort | cat -v
713#CHECK: TESTVAR0
714#CHECK: TESTVAR2 'a' 'b'
715set -x | grep TESTVAR | sort | cat -v
716#CHECK: TESTVAR1 a
717
718# if/for/while scope
719function test_ifforwhile_scope
720    if set -l ifvar1 (true && echo val1)
721    end
722    if set -l ifvar2 (echo val2 && false)
723    end
724    if false
725    else if set -l ifvar3 (echo val3 && false)
726    end
727    while set -l whilevar1 (echo val3 ; false)
728    end
729    set --show ifvar1 ifvar2 ifvar3 whilevar1
730end
731test_ifforwhile_scope
732#CHECK: $ifvar1: set in local scope, unexported, with 1 elements
733#CHECK: $ifvar1[1]: |val1|
734#CHECK: $ifvar2: set in local scope, unexported, with 1 elements
735#CHECK: $ifvar2[1]: |val2|
736#CHECK: $ifvar3: set in local scope, unexported, with 1 elements
737#CHECK: $ifvar3[1]: |val3|
738#CHECK: $whilevar1: set in local scope, unexported, with 1 elements
739#CHECK: $whilevar1[1]: |val3|
740
741# $status should always be read-only, setting it makes no sense because it's immediately overwritten.
742set -g status 5
743#CHECKERR: set: Tried to change the read-only variable 'status'
744
745while set -e __fish_test_universal_exported_var
746end
747set -xU __fish_test_universal_exported_var 1
748$FISH -c 'set __fish_test_universal_exported_var 2'
749env | string match -e __fish_test_universal_exported_var
750#CHECK: __fish_test_universal_exported_var=2
751
752# Test that computed variables are global.
753# If they can be set they can only be set in global scope,
754# so they should only be shown in global scope.
755set -S status
756#CHECK: $status: set in global scope, unexported, with 1 elements (read-only)
757#CHECK: $status[1]: |0|
758
759# PWD is also read-only.
760set -S PWD
761#CHECK: $PWD: set in global scope, exported, with 1 elements (read-only)
762#CHECK: $PWD[1]: |{{.*}}|
763#CHECK: $PWD: originally inherited as |{{.*}}|
764
765set -ql history
766echo $status
767#CHECK: 1
768
769set --path newvariable foo
770set -S newvariable
771#CHECK: $newvariable: set in global scope, unexported, a path variable with 1 elements
772#CHECK: $newvariable[1]: |foo|
773
774set foo foo
775set bar bar
776set -e baz
777
778set -e foo baz bar
779echo $status
780#CHECK: 4
781set -S foo baz bar
782
783set foo 1 2 3
784set bar 1 2 3
785
786set -e foo[1] bar[2]
787echo $foo
788#CHECK: 2 3
789echo $bar
790#CHECK: 1 3
791
792# Test that `set -q` does not return 0 if there are 256 missing variables
793
794set -lq a(seq 1 256)
795echo $status
796#CHECK: 255
797
798true
799
800set "" foo
801#CHECKERR: set: : invalid variable name. See `help language#shell-variable-and-function-names`
802#CHECKERR: {{.*}}set.fish (line {{\d+}}):
803#CHECKERR: set "" foo
804#CHECKERR: ^
805#CHECKERR: (Type 'help set' for related documentation)
806
807set --show ""
808#CHECKERR: set: : invalid variable name. See `help language#shell-variable-and-function-names`
809#CHECKERR: {{.*}}set.fish (line {{\d+}}):
810#CHECKERR: set --show ""
811#CHECKERR: ^
812#CHECKERR: (Type 'help set' for related documentation)
813
814set foo="ba nana"
815#CHECKERR: set: foo=ba nana: invalid variable name. See `help language#shell-variable-and-function-names`
816#CHECKERR: set: Did you mean `set foo 'ba nana'`?
817#CHECKERR: {{.*}}set.fish (line {{\d+}}):
818#CHECKERR: set foo="ba nana"
819#CHECKERR: ^
820#CHECKERR: (Type 'help set' for related documentation)
821# Test path splitting
822begin
823    set -l PATH /usr/local/bin:/usr/bin
824    echo $PATH
825    # CHECK: /usr/local/bin /usr/bin
826    set -l CDPATH .:/usr
827    echo $CDPATH
828    # CHECK: . /usr
829end
830
831# Function scope:
832set -f actuallystilllocal "this one is still local"
833set -ql actuallystilllocal
834and echo "Yep, it's local"
835# CHECK: Yep, it's local
836set -S actuallystilllocal
837#CHECK: $actuallystilllocal: set in local scope, unexported, with 1 elements
838#CHECK: $actuallystilllocal[1]: |this one is still local|
839
840# Blocks aren't functions, "function" scope is still top-level local:
841begin
842    set -f stilllocal "as local as the moon is wet"
843    echo $stilllocal
844    # CHECK: as local as the moon is wet
845end
846set -S stilllocal
847#CHECK: $stilllocal: set in local scope, unexported, with 1 elements
848#CHECK: $stilllocal[1]: |as local as the moon is wet|
849
850set -g globalvar global
851
852function test-function-scope
853    set -f funcvar function
854    echo $funcvar
855    # CHECK: function
856    set -S funcvar
857    #CHECK: $funcvar: set in local scope, unexported, with 1 elements
858    #CHECK: $funcvar[1]: |function|
859    begin
860        set -l funcvar block
861        echo $funcvar
862        # CHECK: block
863        set -S funcvar
864        #CHECK: $funcvar: set in local scope, unexported, with 1 elements
865        #CHECK: $funcvar[1]: |block|
866    end
867    echo $funcvar
868    # CHECK: function
869
870    begin
871        set -f funcvar2 "function from block"
872        echo $funcvar2
873        # CHECK: function from block
874        set -S funcvar2
875        #CHECK: $funcvar2: set in local scope, unexported, with 1 elements
876        #CHECK: $funcvar2[1]: |function from block|
877    end
878    echo $funcvar2
879    # CHECK: function from block
880    set -S funcvar2
881    #CHECK: $funcvar2: set in local scope, unexported, with 1 elements
882    #CHECK: $funcvar2[1]: |function from block|
883
884    set -l fruit banana
885    if true
886        set -f fruit orange
887    end
888    echo $fruit #orange
889    # function scope *is* the outermost local scope,
890    # so that `set -f` altered the same funcvariable as that `set -l` outside!
891    # CHECK: orange
892
893    set -f globalvar function
894    set -S globalvar
895    #CHECK: $globalvar: set in local scope, unexported, with 1 elements
896    #CHECK: $globalvar[1]: |function|
897    #CHECK: $globalvar: set in global scope, unexported, with 1 elements
898    #CHECK: $globalvar[1]: |global|
899end
900
901test-function-scope
902echo $funcvar $funcvar2
903# CHECK:
904
905function erase-funcvar
906    set -l banana 1
907    begin
908        set -l banana 2
909        set -ef banana
910        echo $banana
911        # CHECK: 2
912    end
913    echo $banana
914    # CHECK:
915end
916
917erase-funcvar
918
919set -f foo
920set -l banana
921set -g global
922begin
923    set -qf foo
924    and echo foo is function scoped
925    # CHECK: foo is function scoped
926
927    set -l localvar414
928    set -qf localvar414
929    or echo localvar414 is not function scoped
930    # CHECK: localvar414 is not function scoped
931
932    set -qf banana
933    and echo banana is function scoped
934    # CHECK: banana is function scoped
935
936    set -l global
937    set -qf global
938    or echo global is not function scoped
939    # CHECK: global is not function scoped
940end
941
942set --query $this_is_not_set
943echo $status
944# CHECK: 255
945
946set --query
947echo $status
948# CHECK: 255
949
950set -U status
951# CHECKERR: set: Tried to modify the special variable 'status' with the wrong scope
952set -S status
953# CHECK: $status: set in global scope, unexported, with 1 elements (read-only)
954# CHECK: $status[1]: |2|
955
956# See that we show inherited variables correctly:
957foo=bar $FISH -c 'set foo 1 2 3; set --show foo'
958# CHECK: $foo: set in global scope, exported, with 3 elements
959# CHECK: $foo[1]: |1|
960# CHECK: $foo[2]: |2|
961# CHECK: $foo[3]: |3|
962# CHECK: $foo: originally inherited as |bar|
963
964# Verify behavior of erasing in multiple scopes simultaneously
965set -U marbles global
966set -g marbles global
967set -l marbles local
968
969set -eUg marbles
970set -ql marbles || echo "erased in more scopes than it should!"
971set -qg marbles && echo "didn't erase from global scope!"
972set -qU marbles && echo "didn't erase from universal scope!"
973
974begin
975    set -l secret local 4 8 15 16 23 42
976    set -g secret global 4 8 15 16 23 42
977    set -egl secret[3..5]
978    echo $secret
979    # CHECK: local 4 23 42
980end
981echo $secret
982# CHECK: global 4 23 42
983
984set -e
985# CHECKERR: set: --erase: option requires an argument
986# CHECKERR: {{.*}}set.fish (line {{\d+}}):
987# CHECKERR: set -e
988# CHECKERR: ^
989# CHECKERR: (Type 'help set' for related documentation)
990
991while set -e undefined
992end
993
994set -e undefined[x..]
995# CHECKERR: set: Invalid index starting at 'x..]'
996# CHECKERR: {{.*}}checks/set.fish (line {{\d+}}):
997# CHECKERR: set -e undefined[x..]
998# CHECKERR: ^
999# CHECKERR: (Type 'help set' for related documentation)
1000
1001set -e undefined[..y]
1002# CHECKERR: set: Invalid index starting at 'y]'
1003# CHECKERR: {{.*}}checks/set.fish (line {{\d+}}):
1004# CHECKERR: set -e undefined[..y]
1005# CHECKERR: ^
1006# CHECKERR: (Type 'help set' for related documentation)
1007
1008set -e undefined[1..]
1009set -e undefined[..]
1010set -e undefined[..1]
1011
1012set -l negative_oob 1 2 3
1013set -q negative_oob[-10..1]
1014
1015set foo[1 2 3] a b
1016# CHECKERR: set: given 3 indexes but 2 values
1017set foo[1 2 3] a b c d
1018# CHECKERR: set: given 3 indexes but 4 values
1019
1020# --no-event
1021
1022function onevent --on-variable nonevent
1023    echo ONEVENT $argv $nonevent
1024end
1025
1026set -g nonevent bar
1027set -e nonevent
1028
1029# CHECK: ONEVENT VARIABLE SET nonevent bar
1030# CHECK: ONEVENT VARIABLE ERASE nonevent
1031
1032set -g --no-event nonevent 2
1033set -e --no-event nonevent
1034set -S nonevent
1035
1036set -g --no-event nonevent 3
1037set -e nonevent
1038# CHECK: ONEVENT VARIABLE ERASE nonevent
1039
1040set -g nonevent 4
1041# CHECK: ONEVENT VARIABLE SET nonevent 4
1042set -e --no-event nonevent
1043
1044set -l nonevent 4
1045set -e nonevent
1046# CHECK: ONEVENT VARIABLE SET nonevent
1047# CHECK: ONEVENT VARIABLE ERASE nonevent
1048
1049mkdir -p empty
1050env XDG_CONFIG_HOME= HOME=$PWD/empty LC_ALL=en_US.UTF-8 $FISH -c 'set -Ux LC_ALL en_US.UTF-8'
1051env XDG_CONFIG_HOME= HOME=$PWD/empty LC_ALL=en_US.UTF-8 $FISH -c 'set -S LC_ALL'
1052# CHECK: $LC_ALL: set in universal scope, exported, with 1 elements
1053# CHECK: $LC_ALL[1]: |en_US.UTF-8|
1054# CHECK: $LC_ALL: originally inherited as |en_US.UTF-8|
1055
1056# This used to crash
1057set line[0] ""
1058# CHECKERR: set: array index out of bounds
1059# CHECKERR: {{.*}}set.fish (line {{\d+}}):
1060# CHECKERR: set line[0] ""
1061# CHECKERR: ^
1062# CHECKERR: (Type 'help set' for related documentation)
1063
1064echo Still here
1065# CHECK: Still here
1066
1067set -o xtrace
1068# CHECKERR: fish does not have shell options. See `help fish_for_bash_users`.
1069# CHECKERR: set: -o: unknown option
1070
1071set -o vi
1072# CHECKERR: fish does not have shell options. See `help fish_for_bash_users`.
1073# CHECKERR: To enable vi-mode, run `fish_vi_key_bindings`.
1074# CHECKERR: set: -o: unknown option
1075
1076set -o ed
1077# CHECKERR: fish does not have shell options. See `help fish_for_bash_users`.
1078# CHECKERR: ?
1079# CHECKERR: ?
1080# CHECKERR: ?
1081# CHECKERR: set: -o: unknown option
1082
1083set -q -e foo
1084# CHECKERR: set: invalid option combination
1085# CHECKERR: {{.*}}set.fish (line {{\d+}}):
1086# CHECKERR: set -q -e foo
1087# CHECKERR: ^
1088# CHECKERR: (Type 'help set' for related documentation)
1089
1090set -q -n
1091# CHECKERR: set: invalid option combination
1092# CHECKERR: {{.*}}set.fish (line {{\d+}}):
1093# CHECKERR: set -q -n
1094# CHECKERR: ^
1095# CHECKERR: (Type 'help set' for related documentation)
1096
1097set -e -n
1098# CHECKERR: set: invalid option combination
1099# CHECKERR: {{.*}}set.fish (line {{\d+}}):
1100# CHECKERR: set -e -n
1101# CHECKERR: ^
1102# CHECKERR: (Type 'help set' for related documentation)
1103
1104set -l -g foo bar
1105# CHECKERR: set: scope can be only one of: universal function global local
1106# CHECKERR: {{.*}}set.fish (line {{\d+}}):
1107# CHECKERR: set -l -g foo bar
1108# CHECKERR: ^
1109# CHECKERR: (Type 'help set' for related documentation)
1110
1111set -u -x foo
1112# CHECKERR: set: cannot both export and unexport
1113# CHECKERR: {{.*}}set.fish (line {{\d+}}):
1114# CHECKERR: set -u -x foo
1115# CHECKERR: ^
1116# CHECKERR: (Type 'help set' for related documentation)
1117
1118set -e -x foo
1119# CHECKERR: set: invalid option combination
1120# CHECKERR: {{.*}}set.fish (line {{\d+}}):
1121# CHECKERR: set -e -x foo
1122# CHECKERR: ^
1123# CHECKERR: (Type 'help set' for related documentation)
1124
1125set -e -u foo
1126# CHECKERR: set: invalid option combination
1127# CHECKERR: {{.*}}set.fish (line {{\d+}}):
1128# CHECKERR: set -e -u foo
1129# CHECKERR: ^
1130# CHECKERR: (Type 'help set' for related documentation)
1131
1132set --path --unpath foo
1133# CHECKERR: set: cannot both path and unpath
1134# CHECKERR: {{.*}}set.fish (line {{\d+}}):
1135# CHECKERR: set --path --unpath foo
1136# CHECKERR: ^
1137# CHECKERR: (Type 'help set' for related documentation)
1138
1139set -s -l
1140# CHECKERR: set: invalid option combination
1141# CHECKERR: {{.*}}set.fish (line {{\d+}}):
1142# CHECKERR: set -s -l
1143# CHECKERR: ^
1144# CHECKERR: (Type 'help set' for related documentation)
1145
1146set umask abc
1147# CHECKERR: set: Tried to modify the special variable 'umask' to an invalid value
1148
1149exit 0