summaryrefslogtreecommitdiffstats
path: root/contrib/tcl/tests/env.test
blob: e76ad7d5872074c3305a618ae1843441f78b1960 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Commands covered:  none (tests environment variable implementation)
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1991-1993 The Regents of the University of California.
# Copyright (c) 1994 Sun Microsystems, Inc.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# SCCS: @(#) env.test 1.13 97/08/05 11:40:30

if {[string compare test [info procs test]] == 1} then {source defs}

if {[info commands exec] == ""} {
    puts "exec not implemented for this machine"
    return
}

if {$tcl_platform(os) == "Win32s"} {
    puts "Cannot run multiple copies of tcl at the same time under Win32s"
    return
}
   
set f [open printenv w]
puts $f {
    proc lrem {listname name} {
	upvar $listname list
	set i [lsearch $list $name]
	if {$i >= 0} {
	    set list [lreplace $list $i $i]
	}
	return $list
    }
	
    set names [lsort [array names env]]
    if {$tcl_platform(platform) == "windows"} {
	lrem names HOME
        lrem names COMSPEC
	lrem names ComSpec
	lrem names ""
    }	
    foreach name {TCL_LIBRARY PATH LD_LIBRARY_PATH} {
	lrem names $name
    }
    foreach p $names {
	puts "$p=$env($p)"
    }
}
close $f
	
proc getenv {} {
    global printenv tcltest
    catch {exec $tcltest printenv} out
    if {$out == "child process exited abnormally"} {
	set out {}
    }
    return $out
}

# Save the current environment variables at the start of the test.

foreach name [array names env] {
    set env2($name) $env($name)
    unset env($name)
}

# Added the following lines so that child tcltest can actually find its
# library if the initial tcltest is run from a non-standard place.
# ('saved' env vars)
foreach name {TCL_LIBRARY PATH LD_LIBRARY_PATH} {
  if {[info exists env2($name)]} {
     set env($name) $env2($name);
  }
}

test env-1.1 {adding environment variables} {
    getenv
} {}

set env(NAME1) "test string"
test env-1.2 {adding environment variables} {
    getenv
} {NAME1=test string}

set env(NAME2) "more"
test env-1.3 {adding environment variables} {
    getenv
} {NAME1=test string
NAME2=more}

set env(XYZZY) "garbage"
test env-1.4 {adding environment variables} {
    getenv
} {NAME1=test string
NAME2=more
XYZZY=garbage}

set env(NAME2) "new value"
test env-2.1 {changing environment variables} {
    getenv
} {NAME1=test string
NAME2=new value
XYZZY=garbage}

unset env(NAME2)
test env-3.1 {unsetting environment variables} {
    getenv
} {NAME1=test string
XYZZY=garbage}
unset env(NAME1)
test env-3.2 {unsetting environment variables} {
    getenv
} {XYZZY=garbage}

# Restore the environment variables at the end of the test.

foreach name [array names env] {
    unset env($name)
}
foreach name [array names env2] {
    set env($name) $env2($name)
}

file delete printenv
OpenPOWER on IntegriCloud