Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/ksh93/tests/namespace.sh
1810 views
1
########################################################################
2
# #
3
# This software is part of the ast package #
4
# Copyright (c) 1982-2012 AT&T Intellectual Property #
5
# and is licensed under the #
6
# Eclipse Public License, Version 1.0 #
7
# by AT&T Intellectual Property #
8
# #
9
# A copy of the License is available at #
10
# http://www.eclipse.org/org/documents/epl-v10.html #
11
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
12
# #
13
# Information and Software Systems Research #
14
# AT&T Research #
15
# Florham Park NJ #
16
# #
17
# David Korn <[email protected]> #
18
# #
19
########################################################################
20
function err_exit
21
{
22
print -u2 -n "\t"
23
print -u2 -r ${Command}[$1]: "${@:2}"
24
let Errors+=1
25
}
26
alias err_exit='err_exit $LINENO'
27
28
Command=${0##*/}
29
integer Errors=0
30
31
tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32
trap "cd /; rm -rf $tmp" EXIT
33
34
foo=abc
35
typeset -C bar=(x=3 y=4 t=7)
36
typeset -A z=([abc]=qqq)
37
integer r=9
38
function fn
39
{
40
print global fn $foo
41
}
42
function fun
43
{
44
print global fun $foo
45
}
46
mkdir -p $tmp/global/bin $tmp/local/bin
47
cat > $tmp/global/xfun <<- \EOF
48
function xfun
49
{
50
print xfun global $foo
51
}
52
EOF
53
cat > $tmp/local/xfun <<- \EOF
54
function xfun
55
{
56
print xfun local $foo
57
}
58
EOF
59
chmod +x "$tmp/global/xfun" "$tmp/local/xfun"
60
print 'print local prog $1' > $tmp/local/bin/run
61
print 'print global prog $1' > $tmp/global/bin/run
62
chmod +x "$tmp/local/bin/run" "$tmp/global/bin/run"
63
PATH=$tmp/global/bin:$PATH
64
FPATH=$tmp/global
65
66
namespace x
67
{
68
foo=bar
69
typeset -C bar=(x=1 y=2 z=3)
70
typeset -A z=([qqq]=abc)
71
function fn
72
{
73
print local fn $foo
74
}
75
[[ $(fn) == 'local fn bar' ]] || err_exit 'fn inside namespace should run local function'
76
[[ $(fun) == 'global fun abc' ]] || err_exit 'global fun run from namespace not working'
77
(( r == 9 )) || err_exit 'global variable r not set in namespace'
78
false
79
[[ ${z[qqq]} == abc ]] || err_exit 'local array element not correct'
80
[[ ${z[abc]} == '' ]] || err_exit 'global array element should not be visible when local element exists'
81
[[ ${bar.y} == 2 ]] || err_exit 'local variable bar.y not found'
82
[[ ${bar.t} == '' ]] || err_exit 'global bar.t should not be visible'
83
function runxrun
84
{
85
xfun
86
}
87
function runrun
88
{
89
run $1
90
}
91
PATH=$tmp/local/bin:/bin
92
FPATH=$tmp/local
93
[[ $(runxrun) == 'xfun local bar' ]] || err_exit 'local function on FPATH failed'
94
[[ $(runrun $foo) == 'local prog bar' ]] || err_exit 'local binary on PATH failed'
95
}
96
[[ $(fn) == 'global fn abc' ]] || err_exit 'fn outside namespace should run global function'
97
[[ $(.x.fn) == 'local fn bar' ]] || err_exit 'namespace function called from global failed'
98
[[ ${z[abc]} == qqq ]] || err_exit 'global associative array should not be affected by definiton in namespace'
99
[[ ${bar.y} == 4 ]] || err_exit 'global compound variable should not be affected by definiton in namespace'
100
[[ ${bar.z} == '' ]] || err_exit 'global compound variable should not see elements in namespace'
101
[[ $(xfun) == 'xfun global abc' ]] || err_exit 'global function on FPATH failed'
102
[[ $(run $foo) == 'global prog abc' ]] || err_exit 'global binary on PATH failed'
103
false
104
[[ $(.x.runxrun) == 'xfun local bar' ]] || err_exit 'namespace function on FPATH failed'
105
106
exit $((Errors<125?Errors:125))
107
108