| 1 |
# Commands covered: none (tests environment variable implementation) |
| 2 |
# |
| 3 |
# This file contains a collection of tests for one or more of the Tcl built-in |
| 4 |
# commands. Sourcing this file into Tcl runs the tests and generates output |
| 5 |
# for errors. No output means no errors were found. |
| 6 |
# |
| 7 |
# Copyright (c) 1991-1993 The Regents of the University of California. |
| 8 |
# Copyright (c) 1994 Sun Microsystems, Inc. |
| 9 |
# Copyright (c) 1998-1999 by Scriptics Corporation. |
| 10 |
# |
| 11 |
# See the file "license.terms" for information on usage and redistribution of |
| 12 |
# this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 13 |
|
| 14 |
if {"::tcltest" ni [namespace children]} { |
| 15 |
package require tcltest 2.5 |
| 16 |
namespace import -force ::tcltest::* |
| 17 |
} |
| 18 |
|
| 19 |
source [file join [file dirname [info script]] tcltests.tcl] |
| 20 |
|
| 21 |
# [exec] is required here to see the actual environment received by child |
| 22 |
# processes. |
| 23 |
proc getenv {} { |
| 24 |
global printenvScript |
| 25 |
catch {exec [interpreter] $printenvScript} out |
| 26 |
if {$out eq "child process exited abnormally"} { |
| 27 |
set out {} |
| 28 |
} |
| 29 |
return $out |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
proc envrestore {} { |
| 34 |
# Restore the environment variables at the end of the test. |
| 35 |
global env |
| 36 |
variable env2 |
| 37 |
|
| 38 |
foreach name [array names env] { |
| 39 |
unset env($name) |
| 40 |
} |
| 41 |
array set env $env2 |
| 42 |
return |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
proc envprep {} { |
| 47 |
# Save the current environment variables at the start of the test. |
| 48 |
global env |
| 49 |
variable keep |
| 50 |
variable env2 |
| 51 |
|
| 52 |
set env2 [array get env] |
| 53 |
foreach name [array names env] { |
| 54 |
# Keep some environment variables that support operation of the tcltest |
| 55 |
# package. |
| 56 |
if {[string toupper $name] ni [string toupper $keep]} { |
| 57 |
unset env($name) |
| 58 |
} |
| 59 |
} |
| 60 |
return |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
proc encodingrestore {} { |
| 65 |
variable sysenc |
| 66 |
encoding system $sysenc |
| 67 |
return |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
proc encodingswitch encoding { |
| 72 |
variable sysenc |
| 73 |
# Need to run [getenv] in known encoding, so save the current one here... |
| 74 |
set sysenc [encoding system] |
| 75 |
encoding system $encoding |
| 76 |
return |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
proc setup1 {} { |
| 81 |
global env |
| 82 |
envprep |
| 83 |
encodingswitch iso8859-1 |
| 84 |
} |
| 85 |
|
| 86 |
proc setup2 {} { |
| 87 |
global env |
| 88 |
setup1 |
| 89 |
set env(NAME1) {test string} |
| 90 |
set env(NAME2) {new value} |
| 91 |
set env(XYZZY) {garbage} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
proc cleanup1 {} { |
| 96 |
encodingrestore |
| 97 |
envrestore |
| 98 |
} |
| 99 |
|
| 100 |
variable keep { |
| 101 |
TCL_LIBRARY PATH LD_LIBRARY_PATH LIBPATH PURE_PROG_NAME DISPLAY |
| 102 |
SHLIB_PATH SYSTEMDRIVE SYSTEMROOT DYLD_LIBRARY_PATH DYLD_FRAMEWORK_PATH |
| 103 |
DYLD_NEW_LOCAL_SHARED_REGIONS DYLD_NO_FIX_PREBINDING MSYSTEM |
| 104 |
__CF_USER_TEXT_ENCODING SECURITYSESSIONID LANG WINDIR TERM |
| 105 |
CommonProgramFiles CommonProgramFiles(x86) ProgramFiles |
| 106 |
ProgramFiles(x86) CommonProgramW6432 ProgramW6432 |
| 107 |
PROCESSOR_ARCHITECTURE PROCESSOR_ARCHITEW6432 USERPROFILE |
| 108 |
WINECONFIGDIR WINEDATADIR WINEDLLDIR0 WINEHOMEDIR |
| 109 |
} |
| 110 |
|
| 111 |
variable printenvScript [makeFile [string map [list @keep@ [list $keep]] { |
| 112 |
encoding system iso8859-1 |
| 113 |
proc lrem {listname name} { |
| 114 |
upvar $listname list |
| 115 |
set i [lsearch -nocase $list $name] |
| 116 |
if {$i >= 0} { |
| 117 |
set list [lreplace $list $i $i] |
| 118 |
} |
| 119 |
return $list |
| 120 |
} |
| 121 |
proc mangle s { |
| 122 |
regsub -all {\[|\\|\]} $s {\\&} s |
| 123 |
regsub -all "\[\u0000-\u001f\u007f-\uffff\]" $s {[manglechar {&}]} s |
| 124 |
return [subst -novariables $s] |
| 125 |
} |
| 126 |
proc manglechar c { |
| 127 |
return [format {\u%04x} [scan $c %c]] |
| 128 |
} |
| 129 |
|
| 130 |
set names [lsort [array names env]] |
| 131 |
if {$tcl_platform(platform) eq "windows"} { |
| 132 |
lrem names HOME |
| 133 |
lrem names COMSPEC |
| 134 |
lrem names ComSpec |
| 135 |
lrem names "" |
| 136 |
} |
| 137 |
foreach name @keep@ { |
| 138 |
lrem names $name |
| 139 |
} |
| 140 |
foreach p $names { |
| 141 |
puts [mangle $p]=[mangle $env($p)] |
| 142 |
} |
| 143 |
exit |
| 144 |
}] printenv] |
| 145 |
|
| 146 |
|
| 147 |
test env-1.1 {propagation of env values to child interpreters} -setup { |
| 148 |
catch {interp delete child} |
| 149 |
catch {unset env(test)} |
| 150 |
} -body { |
| 151 |
interp create child |
| 152 |
set env(test) garbage |
| 153 |
child eval {set env(test)} |
| 154 |
} -cleanup { |
| 155 |
interp delete child |
| 156 |
unset env(test) |
| 157 |
} -result {garbage} |
| 158 |
|
| 159 |
|
| 160 |
# This one crashed on Solaris under Tcl8.0, so we only want to make sure it |
| 161 |
# runs. |
| 162 |
test env-1.2 {lappend to env value} -setup { |
| 163 |
catch {unset env(test)} |
| 164 |
} -body { |
| 165 |
set env(test) aaaaaaaaaaaaaaaa |
| 166 |
append env(test) bbbbbbbbbbbbbb |
| 167 |
unset env(test) |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
test env-1.3 {reflection of env by "array names"} -setup { |
| 172 |
catch {interp delete child} |
| 173 |
catch {unset env(test)} |
| 174 |
} -body { |
| 175 |
interp create child |
| 176 |
child eval {set env(test) garbage} |
| 177 |
expr {"test" in [array names env]} |
| 178 |
} -cleanup { |
| 179 |
interp delete child |
| 180 |
catch {unset env(test)} |
| 181 |
} -result 1 |
| 182 |
|
| 183 |
|
| 184 |
test env-2.1 { |
| 185 |
adding environment variables |
| 186 |
} -constraints exec -setup setup1 -body { |
| 187 |
getenv |
| 188 |
} -cleanup cleanup1 -result {} |
| 189 |
|
| 190 |
|
| 191 |
test env-2.2 { |
| 192 |
adding environment variables |
| 193 |
} -constraints exec -setup setup1 -body { |
| 194 |
set env(NAME1) "test string" |
| 195 |
getenv |
| 196 |
} -cleanup cleanup1 -result {NAME1=test string} |
| 197 |
|
| 198 |
|
| 199 |
test env-2.3 {adding environment variables} -constraints exec -setup { |
| 200 |
setup1 |
| 201 |
set env(NAME1) "test string" |
| 202 |
} -body { |
| 203 |
set env(NAME2) "more" |
| 204 |
getenv |
| 205 |
} -cleanup cleanup1 -result {NAME1=test string |
| 206 |
NAME2=more} |
| 207 |
|
| 208 |
|
| 209 |
test env-2.4 { |
| 210 |
adding environment variables |
| 211 |
} -constraints exec -setup { |
| 212 |
setup1 |
| 213 |
set env(NAME1) "test string" |
| 214 |
set env(NAME2) "more" |
| 215 |
} -body { |
| 216 |
set env(XYZZY) "garbage" |
| 217 |
getenv |
| 218 |
} -cleanup { cleanup1 |
| 219 |
} -result {NAME1=test string |
| 220 |
NAME2=more |
| 221 |
XYZZY=garbage} |
| 222 |
|
| 223 |
test env-2.5 {different encoding (wide chars)} -constraints {win exec} -setup { |
| 224 |
# be sure set of (Unicode) environment occurs if single-byte encoding is used: |
| 225 |
encodingswitch cp1252 |
| 226 |
# German (cp1252) and Russian (cp1251) characters together encoded as utf-8: |
| 227 |
set val 2d2dc3a4c3b6c3bcc39f2dd182d0b5d181d1822d2d |
| 228 |
set env(XYZZY) [encoding convertfrom utf-8 [binary decode hex $val]] |
| 229 |
# now switch to utf-8 (to see correct values from test): |
| 230 |
encoding system utf-8 |
| 231 |
} -body { |
| 232 |
exec [interpreter] << [string map [list \$val $val] { |
| 233 |
encoding system utf-8; fconfigure stdout -encoding utf-8 |
| 234 |
set test [encoding convertfrom utf-8 [binary decode hex $val]] |
| 235 |
puts "[expr {$env(XYZZY) eq $test}] \ngot:\t\ |
| 236 |
$env(XYZZY) ([binary encode hex [encoding convertto $env(XYZZY)]]) \nexp:\t\ |
| 237 |
$test ([binary encode hex [encoding convertto $test]])" |
| 238 |
}] |
| 239 |
} -cleanup { |
| 240 |
encodingrestore |
| 241 |
unset -nocomplain val f env(XYZZY) |
| 242 |
} -match glob -result {1 *} |
| 243 |
|
| 244 |
test env-3.1 { |
| 245 |
changing environment variables |
| 246 |
} -constraints exec -setup setup2 -body { |
| 247 |
set result [getenv] |
| 248 |
unset env(NAME2) |
| 249 |
set result |
| 250 |
} -cleanup { |
| 251 |
cleanup1 |
| 252 |
} -result {NAME1=test string |
| 253 |
NAME2=new value |
| 254 |
XYZZY=garbage} |
| 255 |
|
| 256 |
|
| 257 |
test env-4.1 { |
| 258 |
unsetting environment variables |
| 259 |
} -constraints exec -setup setup2 -body { |
| 260 |
unset -nocomplain env(NAME2) |
| 261 |
getenv |
| 262 |
} -cleanup cleanup1 -result {NAME1=test string |
| 263 |
XYZZY=garbage} |
| 264 |
|
| 265 |
# env-4.2 is deleted |
| 266 |
|
| 267 |
test env-4.3 { |
| 268 |
setting international environment variables |
| 269 |
} -constraints exec -setup setup1 -body { |
| 270 |
set env(\ua7) \ub6 |
| 271 |
getenv |
| 272 |
} -cleanup cleanup1 -result {\u00a7=\u00b6} |
| 273 |
|
| 274 |
|
| 275 |
test env-4.4 { |
| 276 |
changing international environment variables |
| 277 |
} -constraints exec -setup setup1 -body { |
| 278 |
set env(\ua7) \ua7 |
| 279 |
getenv |
| 280 |
} -cleanup cleanup1 -result {\u00a7=\u00a7} |
| 281 |
|
| 282 |
|
| 283 |
test env-4.5 { |
| 284 |
unsetting international environment variables |
| 285 |
} -constraints exec -setup { |
| 286 |
setup1 |
| 287 |
set env(\ua7) \ua7 |
| 288 |
} -body { |
| 289 |
set env(\ub6) \ua7 |
| 290 |
unset env(\ua7) |
| 291 |
getenv |
| 292 |
} -cleanup cleanup1 -result {\u00b6=\u00a7} |
| 293 |
|
| 294 |
test env-5.0 { |
| 295 |
corner cases - set a value, it should exist |
| 296 |
} -setup setup1 -body { |
| 297 |
set env(temp) a |
| 298 |
set env(temp) |
| 299 |
} -cleanup cleanup1 -result a |
| 300 |
|
| 301 |
|
| 302 |
test env-5.1 { |
| 303 |
corner cases - remove one elem at a time |
| 304 |
} -setup setup1 -body { |
| 305 |
# When no environment variables exist, the env var will contain no |
| 306 |
# entries. The "array names" call syncs up the C-level environ array with |
| 307 |
# the Tcl level env array. Make sure an empty Tcl array is created. |
| 308 |
foreach e [array names env] { |
| 309 |
unset env($e) |
| 310 |
} |
| 311 |
array size env |
| 312 |
} -cleanup cleanup1 -result 0 |
| 313 |
|
| 314 |
|
| 315 |
test env-5.2 {corner cases - unset the env array} -setup { |
| 316 |
interp create i |
| 317 |
} -body { |
| 318 |
# Unsetting a variable in an interp detaches the C-level traces from the |
| 319 |
# Tcl "env" variable. |
| 320 |
i eval { |
| 321 |
unset env |
| 322 |
set env(THIS_SHOULDNT_EXIST) a |
| 323 |
} |
| 324 |
info exists env(THIS_SHOULDNT_EXIST) |
| 325 |
} -cleanup { |
| 326 |
interp delete i |
| 327 |
} -result {0} |
| 328 |
|
| 329 |
|
| 330 |
test env-5.3 {corner cases: unset the env in parent should unset child} -setup { |
| 331 |
setup1 |
| 332 |
interp create i |
| 333 |
} -body { |
| 334 |
# Variables deleted in a parent interp should be deleted in child interp |
| 335 |
# too. |
| 336 |
i eval {set env(THIS_SHOULD_EXIST) a} |
| 337 |
set result [set env(THIS_SHOULD_EXIST)] |
| 338 |
unset env(THIS_SHOULD_EXIST) |
| 339 |
lappend result [i eval {catch {set env(THIS_SHOULD_EXIST)}}] |
| 340 |
} -cleanup { |
| 341 |
cleanup1 |
| 342 |
interp delete i |
| 343 |
} -result {a 1} |
| 344 |
|
| 345 |
|
| 346 |
test env-5.4 {corner cases - unset the env array} -setup { |
| 347 |
setup1 |
| 348 |
interp create i |
| 349 |
} -body { |
| 350 |
# The info exists command should be in sync with the env array. |
| 351 |
# Know Bug: 1737 |
| 352 |
i eval {set env(THIS_SHOULD_EXIST) a} |
| 353 |
set result [info exists env(THIS_SHOULD_EXIST)] |
| 354 |
lappend result [set env(THIS_SHOULD_EXIST)] |
| 355 |
lappend result [info exists env(THIS_SHOULD_EXIST)] |
| 356 |
} -cleanup { |
| 357 |
cleanup1 |
| 358 |
interp delete i |
| 359 |
} -result {1 a 1} |
| 360 |
|
| 361 |
|
| 362 |
test env-5.5 { |
| 363 |
corner cases - cannot have null entries on Windows |
| 364 |
} -constraints win -body { |
| 365 |
set env() a |
| 366 |
catch {set env()} |
| 367 |
} -cleanup cleanup1 -result 1 |
| 368 |
|
| 369 |
test env-6.1 {corner cases - add lots of env variables} -setup setup1 -body { |
| 370 |
set size [array size env] |
| 371 |
for {set i 0} {$i < 100} {incr i} { |
| 372 |
set env(BOGUS$i) $i |
| 373 |
} |
| 374 |
expr {[array size env] - $size} |
| 375 |
} -cleanup cleanup1 -result 100 |
| 376 |
|
| 377 |
test env-7.1 {[219226]: whole env array should not be unset by read} -body { |
| 378 |
set n [array size env] |
| 379 |
set s [array startsearch env] |
| 380 |
while {[array anymore env $s]} { |
| 381 |
array nextelement env $s |
| 382 |
incr n -1 |
| 383 |
} |
| 384 |
array donesearch env $s |
| 385 |
return $n |
| 386 |
} -result 0 |
| 387 |
|
| 388 |
test env-7.2 { |
| 389 |
[219226]: links to env elements should not be removed by read |
| 390 |
} -setup setup1 -body { |
| 391 |
apply {{} { |
| 392 |
set ::env(test7_2) ok |
| 393 |
upvar env(test7_2) elem |
| 394 |
set ::env(PATH) |
| 395 |
return $elem |
| 396 |
}} |
| 397 |
} -cleanup cleanup1 -result ok |
| 398 |
|
| 399 |
test env-7.3 { |
| 400 |
[9b4702]: testing existence of env(some_thing) should not destroy trace |
| 401 |
} -setup setup1 -body { |
| 402 |
apply {{} { |
| 403 |
catch {unset ::env(test7_3)} |
| 404 |
proc foo args { |
| 405 |
set ::env(test7_3) ok |
| 406 |
} |
| 407 |
trace add variable ::env(not_yet_existent) write foo |
| 408 |
info exists ::env(not_yet_existent) |
| 409 |
set ::env(not_yet_existent) "Now I'm here"; |
| 410 |
return [info exists ::env(test7_3)] |
| 411 |
}} |
| 412 |
} -cleanup cleanup1 -result 1 |
| 413 |
|
| 414 |
test env-8.0 { |
| 415 |
memory usage - valgrind does not report reachable memory |
| 416 |
} -body { |
| 417 |
set res [set env(__DUMMY__) {i'm with dummy}] |
| 418 |
unset env(__DUMMY__) |
| 419 |
return $res |
| 420 |
} -result {i'm with dummy} |
| 421 |
|
| 422 |
|
| 423 |
test env-9.0 { |
| 424 |
Initialization of HOME from HOMEDRIVE and HOMEPATH |
| 425 |
} -constraints win -setup { |
| 426 |
setup1 |
| 427 |
unset -nocomplain ::env(HOME) |
| 428 |
set ::env(HOMEDRIVE) X: |
| 429 |
set ::env(HOMEPATH) \\home\\path |
| 430 |
} -cleanup { |
| 431 |
cleanup1 |
| 432 |
} -body { |
| 433 |
set pipe [open |[list [interpreter]] r+] |
| 434 |
puts $pipe {puts $::env(HOME); flush stdout; exit} |
| 435 |
flush $pipe |
| 436 |
set result [gets $pipe] |
| 437 |
close $pipe |
| 438 |
set result |
| 439 |
} -result {X:\home\path} |
| 440 |
|
| 441 |
test env-9.1 { |
| 442 |
Initialization of HOME from USERPROFILE |
| 443 |
} -constraints win -setup { |
| 444 |
setup1 |
| 445 |
unset -nocomplain ::env(HOME) |
| 446 |
unset -nocomplain ::env(HOMEDRIVE) |
| 447 |
unset -nocomplain ::env(HOMEPATH) |
| 448 |
} -cleanup { |
| 449 |
cleanup1 |
| 450 |
} -body { |
| 451 |
set pipe [open |[list [interpreter]] r+] |
| 452 |
puts $pipe {puts $::env(HOME); flush stdout; exit} |
| 453 |
flush $pipe |
| 454 |
set result [gets $pipe] |
| 455 |
close $pipe |
| 456 |
if {$result ne $::env(USERPROFILE)} { |
| 457 |
list ERROR $result ne $::env(USERPROFILE) |
| 458 |
} |
| 459 |
} -result {} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
# cleanup |
| 464 |
rename getenv {} |
| 465 |
rename envrestore {} |
| 466 |
rename envprep {} |
| 467 |
rename encodingrestore {} |
| 468 |
rename encodingswitch {} |
| 469 |
|
| 470 |
removeFile $printenvScript |
| 471 |
::tcltest::cleanupTests |
| 472 |
return |
| 473 |
|
| 474 |
# Local Variables: |
| 475 |
# mode: tcl |
| 476 |
# End: |