aboutsummaryrefslogtreecommitdiff
path: root/contrib/bluegnu2.0.3/lib/testSessionApplication.itcl
blob: 3d577221a0a2d77b0acba540603b0eae2d55bd05 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#
# This file defines the Application Class
#

source $env(BLUEGNULIB)/testSessionFramework.itcl
source $env(BLUEGNULIB)/testSessionClasses.itcl

namespace eval ::BlueGnu {
    class Application {
	protected variable szName "Default"
	protected variable lTargets {}
	protected variable lTests
	protected variable szCurrentTarget
	protected variable objCurrentTarget
	protected variable objEnvironment
	protected variable szOutDir

	constructor {args} {
	    debug {======= Doing Application construction} 3
	    set szOutDir "..."
	    foreach varval $args {
		set varval [split $varval "="]
		if {[llength $varval] != 2} {
		    error "Missing <variable>=<value> pair"
		}
		set var [lindex $varval 0]
		set val [lindex $varval 1]
		set variables {}
		foreach v [lsort [info variable]] {
		    regexp {[^:]+$} $v v
		    lappend variables $v
		}
		if {[lsearch -exact $variables $var] >= 0} {
		    set $var $val
		} else {
		    error "$var does not exists in Class [info class]"
		}
	    }
	}

	destructor {
	}

	public method execute {} {
	    debug {======= Starting with Execution of the Application} 3
	    debug {   list of indexes for lTests is [array names lTests]} 4
	    set iTarget 0
	    set objEnvironment [uplevel #0 \
		    "::BlueGnu::Environment #auto \
		    szName=$szName"]
	    debug {   objEnvironment = >$objEnvironment<} 3
	    debug {   +++ [infoWhich $objEnvironment] +++} 4
	    debug {   === [::itcl::find objects] ===} 4
	    uplevel #0 set objCurrentEnvironment $objEnvironment
	    foreach target $lTargets {
		set szTargetName [lindex [split $target "="] 0]
		open_logs $szTargetName
		incr iTarget
		# set current Test Suite Namespace
		uplevel #0 set nspTestSuite "::TestSuite[format %.5d $iTarget]"
		debug {   Processing target: >$target< in Test Suite\
			[uplevel set nspTestSuite]} 3
		namespace eval [uplevel set nspTestSuite] {
		    debug {    Context is >[namespace current]<} 3
		    variable iTestNr 0
		    proc autoTest {} {
			variable iTestNr
			
			incr iTestNr
			debug {iTestNr = $iTestNr} 5
			debug {namespace current = >[namespace current]<} 5
			debug {format = >T[format %.5d $iTestNr]<} 5
			return [namespace current]::T[format %.5d $iTestNr]
		    }

		    set target [uplevel set target]
		    debug {   In namespace eval [namespace current]\
			    for target: >$target<} 3
		    if {! [catch {
			if {[string length $target] == 0} {
			    # Create a default Target Object
			    #
			    debug {   Create a default Target Object} 3
			    uplevel #0 set objCurrentTarget \
				    [infoWhich \
				    [::BlueGnu::Target #auto \
				    szID=default \
				    szName=default \
				    objQueue=[infoWhich [::BlueGnu::Queue #auto]] \
				    objEnvironment=[uplevel set objEnvironment]]]
			} else {
			    # Call the Target Procedure
			    # This procedure should return a Target Object.
			    # Arguments are passed to this procedure.
			    debug {   Create target: >$target<} 3
			    set list [split $target "="]
			    uplevel #0 set objCurrentTarget \
				    [infoWhich \
				    [eval [lindex $list 0] \
				    [join [lrange $list 1 end] "="] \
				    objEnvironment=[uplevel set objEnvironment]]]
			}
		    } szErrMsg]} {
			debug {   Current Target is\
				>[set target \
				[uplevel #0 set objCurrentTarget]]<} 3

			debug {   Working with target index\
				[uplevel set iTarget]} 4
			if {[uplevel {info exists lTests($iTarget)}]} {
			    foreach test [uplevel {set lTests($iTarget)}] {
				debug {   test: $test} 3
				$target queue append $test
			    }
			}
			$target start
			$target runTests
			$target exit

			# report results of the testing
			#
			debug {   #### All Objects: [::itcl::find objects]} 3
			foreach T [lsort [::itcl::find objects T*]] {
			    debug {   #### Deleting Object $T\
				    ([$T info class])} 0
			    delete object $T
			}
			# remove constructed objects
			#
			debug {    Removing Target Class Object $target} 3
			delete object $target
		    } else {
			global errorCode errorInfo
			perror "Couldn't create target >$target<!\
				\n    May be no procedure with name\
				>$target< defined!\
				\n    errorMsg : >$szErrMsg<\
				\n    errorInfo: >$errorInfo<\
				\n    errorCode: >$errorCode<"
			debug {        error info:\n$errorInfo} 3
		    }
		}
		namespace delete [uplevel set nspTestSuite]
		close_logs
	    }
	    debug {        objects: >[::itcl::find objects]<} 4
	    debug {####### deleting Object Environment >$objEnvironment<} 4
	    delete object $objEnvironment
	}

	public method processArguments {arguments} {
	    upvar $arguments argv
	    global szCurrentTestDirectory

	    set state NORMAL
	    set iTarget 0
	    foreach arg $argv {
		switch -regexp -- $arg {
		    {^-a(l(l)?)?$} {
			debug {   all_flag set to TRUE} 4
			set ::BlueGnu::all_flag 1
		    }
		    {^-o(u(t(d(i(r)?)?)?)?)?$} {
			debug {   Output Directory is next argument} 4
			set state OUTDIR
		    }
		    {^--o(u(t(d(i(r)?)?)?)?)?=.*} {
			set components [split $arg "="]
			debug {   Processing Output Directory >$arg<} 4
			set szOutDir [lindex $components 1]
			set state NORMAL
		    }
		    {^-[-]?t(a(r(g(e(t)?)?)?)?)?([=].*|$)} {
			set components [split $arg "="]
			if {[llength $components] == 1} {
			    debug {   Target is next argument} 4
			    set state TARGET
			} else {
			    debug {   Processing Target >$arg<} 4
			    setTarget iTarget \
				    [join [lrange $components 1 end] "="]
			    set state NORMAL
			}
		    }
		    default {
			debug {   Processing argument: >$arg<} 3
			switch $state {
			    OUTDIR {
				set szOutDir $arg
				set state NORMAL
			    }
			    TARGET {
				setTarget iTarget $arg
				set state NORMAL
			    }
			    NORMAL {
				set components [split $arg "="]
				regexp {([^[]*)(.*)} [lindex $components 0] \
					dummy szFileName szCaseArgs
				append szCaseArgs "=[join \
					[lrange $components 1 end] "="]"
				debug {          arg: >$arg<} 3
				debug {   components: >$components<} 3
				debug {    case+args: >$szCaseArgs<} 3
				debug {  Test Script: >$szFileName<} 3
				debug {             : >$szCurrentTestDirectory<} 3
				set szDname [file dirname $szFileName]
				set szFname [file tail $szFileName]
				
				if {[file exist [set test [file join \
					$szCurrentTestDirectory \
					$szFileName]]]} {
				    # file should be a test
				    debug {    is a test: >$test<!} 3
				    if {! [info exists szCurrentTarget]} {
					setTarget iTarget {}
				    }
				    lappend lTests($iTarget) \
					    [file join \
					    $szCurrentTestDirectory \
					    $arg]
				    debug {    Appended test:\
					    >[file join \
					    $szCurrentTestDirectory \
					    $arg]<!} 3
 				} elseif {[llength [set tests \
 					[locateFile $szFname $szDname]]] > 0} {
 				    foreach test $tests {
 					if {[file exists $test]} {
 					    # file should be a test
 					    debug {       is a test:\
 						    >$test<!!} 3
 					    if {! [info exists\
 						    szCurrentTarget]} {
 						setTarget iTarget {}
 					    }
 					    lappend lTests($iTarget) \
 						    $test$szCaseArgs
 					    debug {    Appended test:\
 						    >$test$szCaseArgs<!!} 2
 					} else {
 					    warning "Test >$test< can't\
 						    be found"
 					}
				    }
				} else {
				    perror "$szFileName is not a test!\
					    Does not exists!"
				}
			    }
			}
		    }
		}
	    }
	    debug {   ==== Found tests:} 3
	    foreach index [lsort [array names lTests]] {
		debug {        lTests($index) = $lTests($index)} 4
	    }
	    debug {   Targets are: $lTargets} 4
	}
	private method setTarget {index target} {
	    upvar $index iTarget

	    incr iTarget
	    if {[string length $target] == 0} {
		set szCurrentTarget "Default"
		lappend lTargets $szCurrentTarget
		debug {   Default Current Target} 3
	    } else {
		set szCurrentTarget  $target
		lappend lTargets $szCurrentTarget
		debug {   Current target: >$szCurrentTarget<} 3
	    }
	    debug {   Found target >$szCurrentTarget<} 3
	}

	private method open_logs {target} {
	    global env

	    set target [string trim $target]
	    if {[string compare $szOutDir "..."] == 0} {
		debug {   No Output directory defined, creating one} 3
		set szOutDir \
			"logs/$env(USER)_${target}_[exec date +%Y%m%d]_"
		set szI [format "%.4d" [set i 0]]
		while {[file isdirectory $szOutDir$szI]} {
		    set szI [format "%.4d" [incr i]]
		}
		set szOutDir $szOutDir$szI
	    }
	    if {! [file isdirectory $szOutDir]} {
		exec mkdir -p $szOutDir
	    }
	    if {[string length $target] == 0} {
		set szTool testrun
	    } else {
		set szTool $target
	    }
	    catch "exec rm -f $szOutDir/$szTool.sum"
	    namespace eval ::BlueGnu \
		    "set ::BlueGnu::sum_file [open "$szOutDir/$szTool.sum" w]"
	    puts $::BlueGnu::sum_file "# $szOutDir/$szTool.sum"
	    catch "exec rm -f $szOutDir/$szTool.log"
	    log_file -a "$szOutDir/$szTool.log"
	    send_log "# $szOutDir/$szTool.log\n"
	    debug {     Opening log and summary files in $szOutDir} 3
	}
	private method close_logs {} {
	}
	public method outDir {} {
	    return $szOutDir
	}
    }
}