| [25] | 1 | # Commands covered:  none (tests environment variable implementation) | 
|---|
|  | 2 | # | 
|---|
|  | 3 | # This file contains a collection of tests for one or more of the Tcl | 
|---|
|  | 4 | # built-in commands.  Sourcing this file into Tcl runs the tests and | 
|---|
|  | 5 | # generates output 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 | 
|---|
|  | 12 | # of this file, and for a DISCLAIMER OF ALL WARRANTIES. | 
|---|
|  | 13 | # | 
|---|
|  | 14 | # RCS: @(#) $Id: env.test,v 1.28 2007/01/19 01:04:00 das Exp $ | 
|---|
|  | 15 |  | 
|---|
|  | 16 | if {[lsearch [namespace children] ::tcltest] == -1} { | 
|---|
|  | 17 | package require tcltest 2 | 
|---|
|  | 18 | namespace import -force ::tcltest::* | 
|---|
|  | 19 | } | 
|---|
|  | 20 |  | 
|---|
|  | 21 | # Some tests require the "exec" command. | 
|---|
|  | 22 | # Skip them if exec is not defined. | 
|---|
|  | 23 | testConstraint exec [llength [info commands exec]] | 
|---|
|  | 24 |  | 
|---|
|  | 25 | # | 
|---|
|  | 26 | # These tests will run on any platform (and indeed crashed | 
|---|
|  | 27 | # on the Mac).  So put them before you test for the existance | 
|---|
|  | 28 | # of exec. | 
|---|
|  | 29 | # | 
|---|
|  | 30 | test env-1.1 {propagation of env values to child interpreters} { | 
|---|
|  | 31 | catch {interp delete child} | 
|---|
|  | 32 | catch {unset env(test)} | 
|---|
|  | 33 | interp create child | 
|---|
|  | 34 | set env(test) garbage | 
|---|
|  | 35 | set return [child eval {set env(test)}] | 
|---|
|  | 36 | interp delete child | 
|---|
|  | 37 | unset env(test) | 
|---|
|  | 38 | set return | 
|---|
|  | 39 | } {garbage} | 
|---|
|  | 40 | # | 
|---|
|  | 41 | # This one crashed on Solaris under Tcl8.0, so we only | 
|---|
|  | 42 | # want to make sure it runs. | 
|---|
|  | 43 | # | 
|---|
|  | 44 | test env-1.2 {lappend to env value} { | 
|---|
|  | 45 | catch {unset env(test)} | 
|---|
|  | 46 | set env(test) aaaaaaaaaaaaaaaa | 
|---|
|  | 47 | append env(test) bbbbbbbbbbbbbb | 
|---|
|  | 48 | unset env(test) | 
|---|
|  | 49 | } {} | 
|---|
|  | 50 | test env-1.3 {reflection of env by "array names"} { | 
|---|
|  | 51 | catch {interp delete child} | 
|---|
|  | 52 | catch {unset env(test)} | 
|---|
|  | 53 | interp create child | 
|---|
|  | 54 | child eval {set env(test) garbage} | 
|---|
|  | 55 | set names [array names env] | 
|---|
|  | 56 | interp delete child | 
|---|
|  | 57 | set ix [lsearch $names test] | 
|---|
|  | 58 | catch {unset env(test)} | 
|---|
|  | 59 | expr {$ix >= 0} | 
|---|
|  | 60 | } {1} | 
|---|
|  | 61 |  | 
|---|
|  | 62 | set printenvScript [makeFile { | 
|---|
|  | 63 | proc lrem {listname name} { | 
|---|
|  | 64 | upvar $listname list | 
|---|
|  | 65 | set i [lsearch -nocase $list $name] | 
|---|
|  | 66 | if {$i >= 0} { | 
|---|
|  | 67 | set list [lreplace $list $i $i] | 
|---|
|  | 68 | } | 
|---|
|  | 69 | return $list | 
|---|
|  | 70 | } | 
|---|
|  | 71 |  | 
|---|
|  | 72 | set names [lsort [array names env]] | 
|---|
|  | 73 | if {$tcl_platform(platform) == "windows"} { | 
|---|
|  | 74 | lrem names HOME | 
|---|
|  | 75 | lrem names COMSPEC | 
|---|
|  | 76 | lrem names ComSpec | 
|---|
|  | 77 | lrem names "" | 
|---|
|  | 78 | } | 
|---|
|  | 79 | foreach name { | 
|---|
|  | 80 | TCL_LIBRARY PATH LD_LIBRARY_PATH LIBPATH PURE_PROG_NAME DISPLAY | 
|---|
|  | 81 | SHLIB_PATH SYSTEMDRIVE SYSTEMROOT DYLD_LIBRARY_PATH DYLD_FRAMEWORK_PATH | 
|---|
|  | 82 | DYLD_NEW_LOCAL_SHARED_REGIONS DYLD_NO_FIX_PREBINDING | 
|---|
|  | 83 | __CF_USER_TEXT_ENCODING SECURITYSESSIONID | 
|---|
|  | 84 | } { | 
|---|
|  | 85 | lrem names $name | 
|---|
|  | 86 | } | 
|---|
|  | 87 | foreach p $names { | 
|---|
|  | 88 | puts "$p=$env($p)" | 
|---|
|  | 89 | } | 
|---|
|  | 90 | exit | 
|---|
|  | 91 | } printenv] | 
|---|
|  | 92 |  | 
|---|
|  | 93 | # [exec] is required here to see the actual environment received | 
|---|
|  | 94 | # by child processes. | 
|---|
|  | 95 | proc getenv {} { | 
|---|
|  | 96 | global printenvScript tcltest | 
|---|
|  | 97 | catch {exec [interpreter] $printenvScript} out | 
|---|
|  | 98 | if {$out == "child process exited abnormally"} { | 
|---|
|  | 99 | set out {} | 
|---|
|  | 100 | } | 
|---|
|  | 101 | return $out | 
|---|
|  | 102 | } | 
|---|
|  | 103 |  | 
|---|
|  | 104 | # Save the current environment variables at the start of the test. | 
|---|
|  | 105 |  | 
|---|
|  | 106 | foreach name [array names env] { | 
|---|
|  | 107 | set env2($name) $env($name) | 
|---|
|  | 108 |  | 
|---|
|  | 109 | # Keep some environment variables that support operation of the | 
|---|
|  | 110 | # tcltest package. | 
|---|
|  | 111 | if {[string toupper $name] ni { | 
|---|
|  | 112 | TCL_LIBRARY PATH LD_LIBRARY_PATH LIBPATH DISPLAY SHLIB_PATH | 
|---|
|  | 113 | SYSTEMDRIVE SYSTEMROOT DYLD_LIBRARY_PATH DYLD_FRAMEWORK_PATH | 
|---|
|  | 114 | DYLD_NEW_LOCAL_SHARED_REGIONS DYLD_NO_FIX_PREBINDING | 
|---|
|  | 115 | SECURITYSESSIONID | 
|---|
|  | 116 | }} { | 
|---|
|  | 117 | unset env($name) | 
|---|
|  | 118 | } | 
|---|
|  | 119 | } | 
|---|
|  | 120 |  | 
|---|
|  | 121 | test env-2.1 {adding environment variables} {exec} { | 
|---|
|  | 122 | getenv | 
|---|
|  | 123 | } {} | 
|---|
|  | 124 |  | 
|---|
|  | 125 | set env(NAME1) "test string" | 
|---|
|  | 126 | test env-2.2 {adding environment variables} {exec} { | 
|---|
|  | 127 | getenv | 
|---|
|  | 128 | } {NAME1=test string} | 
|---|
|  | 129 |  | 
|---|
|  | 130 | set env(NAME2) "more" | 
|---|
|  | 131 | test env-2.3 {adding environment variables} {exec} { | 
|---|
|  | 132 | getenv | 
|---|
|  | 133 | } {NAME1=test string | 
|---|
|  | 134 | NAME2=more} | 
|---|
|  | 135 |  | 
|---|
|  | 136 | set env(XYZZY) "garbage" | 
|---|
|  | 137 | test env-2.4 {adding environment variables} {exec} { | 
|---|
|  | 138 | getenv | 
|---|
|  | 139 | } {NAME1=test string | 
|---|
|  | 140 | NAME2=more | 
|---|
|  | 141 | XYZZY=garbage} | 
|---|
|  | 142 |  | 
|---|
|  | 143 | set env(NAME2) "new value" | 
|---|
|  | 144 | test env-3.1 {changing environment variables} {exec} { | 
|---|
|  | 145 | set result [getenv] | 
|---|
|  | 146 | unset env(NAME2) | 
|---|
|  | 147 | set result | 
|---|
|  | 148 | } {NAME1=test string | 
|---|
|  | 149 | NAME2=new value | 
|---|
|  | 150 | XYZZY=garbage} | 
|---|
|  | 151 |  | 
|---|
|  | 152 | test env-4.1 {unsetting environment variables} {exec} { | 
|---|
|  | 153 | set result [getenv] | 
|---|
|  | 154 | unset env(NAME1) | 
|---|
|  | 155 | set result | 
|---|
|  | 156 | } {NAME1=test string | 
|---|
|  | 157 | XYZZY=garbage} | 
|---|
|  | 158 |  | 
|---|
|  | 159 | test env-4.2 {unsetting environment variables} {exec} { | 
|---|
|  | 160 | set result [getenv] | 
|---|
|  | 161 | unset env(XYZZY) | 
|---|
|  | 162 | set result | 
|---|
|  | 163 | } {XYZZY=garbage} | 
|---|
|  | 164 |  | 
|---|
|  | 165 | test env-4.3 {setting international environment variables} {exec} { | 
|---|
|  | 166 | set env(\ua7) \ub6 | 
|---|
|  | 167 | getenv | 
|---|
|  | 168 | } "\ua7=\ub6" | 
|---|
|  | 169 | test env-4.4 {changing international environment variables} {exec} { | 
|---|
|  | 170 | set env(\ua7) \ua7 | 
|---|
|  | 171 | getenv | 
|---|
|  | 172 | } "\ua7=\ua7" | 
|---|
|  | 173 | test env-4.5 {unsetting international environment variables} {exec} { | 
|---|
|  | 174 | set env(\ub6) \ua7 | 
|---|
|  | 175 | unset env(\ua7) | 
|---|
|  | 176 | set result [getenv] | 
|---|
|  | 177 | unset env(\ub6) | 
|---|
|  | 178 | set result | 
|---|
|  | 179 | } "\ub6=\ua7" | 
|---|
|  | 180 |  | 
|---|
|  | 181 | test env-5.0 {corner cases - set a value, it should exist} {} { | 
|---|
|  | 182 | set env(temp) a | 
|---|
|  | 183 | set result [set env(temp)] | 
|---|
|  | 184 | unset env(temp) | 
|---|
|  | 185 | set result | 
|---|
|  | 186 | } {a} | 
|---|
|  | 187 | test env-5.1 {corner cases - remove one elem at a time} {} { | 
|---|
|  | 188 | # When no environment variables exist, the env var will | 
|---|
|  | 189 | # contain no entries.  The "array names" call synchs up | 
|---|
|  | 190 | # the C-level environ array with the Tcl level env array. | 
|---|
|  | 191 | # Make sure an empty Tcl array is created. | 
|---|
|  | 192 |  | 
|---|
|  | 193 | set x [array get env] | 
|---|
|  | 194 | foreach e [array names env] { | 
|---|
|  | 195 | unset env($e) | 
|---|
|  | 196 | } | 
|---|
|  | 197 | set result [catch {array names env}] | 
|---|
|  | 198 | array set env $x | 
|---|
|  | 199 | set result | 
|---|
|  | 200 | } {0} | 
|---|
|  | 201 | test env-5.2 {corner cases - unset the env array} {} { | 
|---|
|  | 202 | # Unsetting a variable in an interp detaches the C-level | 
|---|
|  | 203 | # traces from the Tcl "env" variable. | 
|---|
|  | 204 |  | 
|---|
|  | 205 | interp create i | 
|---|
|  | 206 | i eval { unset env } | 
|---|
|  | 207 | i eval { set env(THIS_SHOULDNT_EXIST) a} | 
|---|
|  | 208 | set result [info exists env(THIS_SHOULDNT_EXIST)] | 
|---|
|  | 209 | interp delete i | 
|---|
|  | 210 | set result | 
|---|
|  | 211 | } {0} | 
|---|
|  | 212 | test env-5.3 {corner cases - unset the env in master should unset child} {} { | 
|---|
|  | 213 | # Variables deleted in a master interp should be deleted in | 
|---|
|  | 214 | # child interp too. | 
|---|
|  | 215 |  | 
|---|
|  | 216 | interp create i | 
|---|
|  | 217 | i eval { set env(THIS_SHOULD_EXIST) a} | 
|---|
|  | 218 | set result [set env(THIS_SHOULD_EXIST)] | 
|---|
|  | 219 | unset env(THIS_SHOULD_EXIST) | 
|---|
|  | 220 | lappend result [i eval {catch {set env(THIS_SHOULD_EXIST)}}] | 
|---|
|  | 221 | interp delete i | 
|---|
|  | 222 | set result | 
|---|
|  | 223 | } {a 1} | 
|---|
|  | 224 | test env-5.4 {corner cases - unset the env array} {} { | 
|---|
|  | 225 | # The info exists command should be in synch with the env array. | 
|---|
|  | 226 | # Know Bug: 1737 | 
|---|
|  | 227 |  | 
|---|
|  | 228 | interp create i | 
|---|
|  | 229 | i eval { set env(THIS_SHOULD_EXIST) a} | 
|---|
|  | 230 | set     result [info exists env(THIS_SHOULD_EXIST)] | 
|---|
|  | 231 | lappend result [set env(THIS_SHOULD_EXIST)] | 
|---|
|  | 232 | lappend result [info exists env(THIS_SHOULD_EXIST)] | 
|---|
|  | 233 | interp delete i | 
|---|
|  | 234 | set result | 
|---|
|  | 235 | } {1 a 1} | 
|---|
|  | 236 | test env-5.5 {corner cases - cannot have null entries on Windows} {win} { | 
|---|
|  | 237 | set env() a | 
|---|
|  | 238 | catch {set env()} | 
|---|
|  | 239 | } {1} | 
|---|
|  | 240 |  | 
|---|
|  | 241 | test env-6.1 {corner cases - add lots of env variables} {} { | 
|---|
|  | 242 | set size [array size env] | 
|---|
|  | 243 | for {set i 0} {$i < 100} {incr i} { | 
|---|
|  | 244 | set env(BOGUS$i) $i | 
|---|
|  | 245 | } | 
|---|
|  | 246 | expr {[array size env] - $size} | 
|---|
|  | 247 | } 100 | 
|---|
|  | 248 |  | 
|---|
|  | 249 | # Restore the environment variables at the end of the test. | 
|---|
|  | 250 |  | 
|---|
|  | 251 | foreach name [array names env] { | 
|---|
|  | 252 | unset env($name) | 
|---|
|  | 253 | } | 
|---|
|  | 254 | foreach name [array names env2] { | 
|---|
|  | 255 | set env($name) $env2($name) | 
|---|
|  | 256 | } | 
|---|
|  | 257 |  | 
|---|
|  | 258 | # cleanup | 
|---|
|  | 259 | removeFile $printenvScript | 
|---|
|  | 260 | ::tcltest::cleanupTests | 
|---|
|  | 261 | return | 
|---|