summaryrefslogtreecommitdiffstats
path: root/tcllib/support/devel/sak/validate/testsuites.tcl
blob: 71ea694aa080f6fad1ea69d1d3bbc185c92c3407 (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# -*- tcl -*-
# (C) 2008 Andreas Kupries <andreas_kupries@users.sourceforge.net>
##
# ###

package require  sak::animate
package require  sak::feedback
package require  sak::color

getpackage textutil::repeat textutil/repeat.tcl
getpackage interp interp/interp.tcl

namespace eval ::sak::validate::testsuites {
    namespace import ::textutil::repeat::blank
    namespace import ::sak::color::*
    namespace import ::sak::feedback::!
    namespace import ::sak::feedback::>>
    namespace import ::sak::feedback::+=
    namespace import ::sak::feedback::=
    namespace import ::sak::feedback::=|
    namespace import ::sak::feedback::log
    namespace import ::sak::feedback::summary
    rename summary sum
}

# ###

proc ::sak::validate::testsuites {modules mode stem tclv} {
    testsuites::run $modules $mode $stem $tclv
    testsuites::summary
    return
}

proc ::sak::validate::testsuites::run {modules mode stem tclv} {
    sak::feedback::init $mode $stem
    sak::feedback::first log  "\[ Testsuites \] =================================================="
    sak::feedback::first unc  "\[ Testsuites \] =================================================="
    sak::feedback::first fail "\[ Testsuites \] =================================================="
    sak::feedback::first miss "\[ Testsuites \] =================================================="
    sak::feedback::first none "\[ Testsuites \] =================================================="

    # Preprocessing of module names to allow better formatting of the
    # progress output, i.e. vertically aligned columns

    # Per module we can distinguish the following levels of
    # testsuite completeness:
    # - No package has a testsuite
    # - Some, but not all packages have a testsuite
    # - All packages have a testsuite.
    #
    # Validity of the testsuites is not done here. It requires
    # execution, see 'sak test run ...'.

    # Progress report per module: Packages it is working on.
    # Summary at module level:
    # - Number of packages, number of packages with testsuites,

    # Full log:
    # - Lists packages without testsuites.

    # Global preparation: Pull information about all packages and the
    # modules they belong to.

    Setup
    Count $modules
    MapPackages

    InitCounters
    foreach m $modules {
	# Skip tcllibc shared library, not a module.
	if {[string equal $m tcllibc]} continue

	InitModuleCounters
	!
	log "@@ Module $m"
	Head $m

	# Per module: Find all testsuites in the module and process
	# them. We determine the package(s) they may belong to.

	# Per package: Have they .test files claiming them? After
	# that, are .test files left over (i.e. without a package)?

	ProcessTestsuites $m
	ProcessPackages   $m
	ProcessUnclaimed
	ModuleSummary
    }

    Shutdown
    return
}

proc ::sak::validate::testsuites::summary {} {
    Summary
    return
}

# ###

proc ::sak::validate::testsuites::ProcessTestsuites {m} {
    !claims
    foreach f [glob -nocomplain [file join [At $m] *.test]] {
	ProcessTestsuite $f
    }
    return
}

proc ::sak::validate::testsuites::ProcessTestsuite {f} {
    variable testing
    =file $f

    if {[catch {
	Scan [get_input $f]
    } msg]} {
	+e $msg
    } else {
	foreach p $testing { +claim $p }
    }


    return
}

proc ::sak::validate::testsuites::Setup {} {
    variable ip [interp create]

    # Make it mostly empty (We keep the 'set' command).

    foreach n [interp eval $ip [list ::namespace children ::]] {
	if {[string equal $n ::tcl]} continue
	interp eval $ip [list namespace delete $n]
    }
    foreach c [interp eval $ip [list ::info commands]] {
	if {[string equal $c set]}       continue
	if {[string equal $c if]}        continue
	if {[string equal $c rename]}    continue
	if {[string equal $c namespace]} continue
	interp eval $ip [list ::rename $c {}]
    }

    if {![package vsatisfies [package present Tcl] 8.6]} {
	interp eval $ip [list ::namespace delete ::tcl]
    }
    interp eval $ip [list ::rename namespace {}]
    interp eval $ip [list ::rename rename    {}]

    foreach m {
	testing unknown useLocal useLocalKeep useAccel
    } {
	interp alias $ip $m {} ::sak::validate::testsuites::Process/$m $ip
    }
    return
}

proc ::sak::validate::testsuites::Shutdown {} {
    variable ip
    interp delete $ip
    return
}

proc ::sak::validate::testsuites::Scan {data} {
    variable ip
    while {1} {
	if {[catch {
	    $ip eval $data
	} msg]} {
	    if {[string match {can't read "*": no such variable} $msg]} {
		regexp  {can't read "(.*)": no such variable} $msg -> var
		log "@@ + variable \"$var\""
		$ip eval [list set $var {}]
		continue
	    }
	    return -code error $msg
	}
	break
    }
    return
}

proc ::sak::validate::testsuites::Process/useTcllibC {ip args} {
    return 0
}

proc ::sak::validate::testsuites::Process/unknown {ip args} {
    return 0
}

proc ::sak::validate::testsuites::Process/testing {ip script} {
    variable testing {}
    $ip eval $script
    return -code return
}

proc ::sak::validate::testsuites::Process/useLocal {ip f p args} {
    variable testing
    lappend  testing $p
    return
}

proc ::sak::validate::testsuites::Process/useLocalKeep {ip f p args} {
    variable testing
    lappend  testing $p
    return
}

proc ::sak::validate::testsuites::Process/useAccel {ip _ f p} {
    variable testing
    lappend  testing $p
    return
}

proc ::sak::validate::testsuites::ProcessPackages {m} {
    !used
    if {![HasPackages $m]} return

    foreach p [ThePackages $m] {
	+pkg $p
	if {[claimants $p]} {
	    +tests $p
	} else {
	    notests $p
	}
    }
    return
}

proc ::sak::validate::testsuites::ProcessUnclaimed {} {
    variable claims
    if {![array size claims]} return
    foreach p [lsort -dict [array names claims]] {
	foreach fx $claims($p) { +u $fx }
    }
    return
}

###

proc ::sak::validate::testsuites::=file {f} {
    variable current [file tail $f]
    = "$current ..."
    return
}

###

proc ::sak::validate::testsuites::!claims {} {
    variable    claims
    array unset claims *
    return
}

proc ::sak::validate::testsuites::+claim {pkg} {
    variable current
    variable claims
    lappend  claims($pkg) $current
    return
}

proc ::sak::validate::testsuites::claimants {pkg} {
    variable claims
    expr { [info exists claims($pkg)] && [llength $claims($pkg)] }
}


###

proc ::sak::validate::testsuites::!used {} {
    variable    used
    array unset used *
    return
}

proc ::sak::validate::testsuites::+use {pkg} {
    variable used
    variable claims
    foreach fx $claims($pkg) { set used($fx) . }
    unset claims($pkg)
    return
}

###

proc ::sak::validate::testsuites::MapPackages {} {
    variable    pkg
    array unset pkg *

    !
    += Package
    foreach {pname pdata} [ipackages] {
	= "$pname ..."
	foreach {pver pmodule} $pdata break
	lappend pkg($pmodule) $pname
    }
    !
    =| {Packages mapped ...}
    return
}

proc ::sak::validate::testsuites::HasPackages {m} {
    variable pkg
    expr { [info exists pkg($m)] && [llength $pkg($m)] }
}

proc ::sak::validate::testsuites::ThePackages {m} {
    variable pkg
    return [lsort -dict $pkg($m)]
}

###

proc ::sak::validate::testsuites::+pkg {pkg} {
    variable mtotal ; incr mtotal
    variable total  ; incr total
    return
}

proc ::sak::validate::testsuites::+tests {pkg} {
    variable mhavetests ; incr mhavetests
    variable havetests  ; incr havetests
    = "$pkg Ok"
    +use $pkg
    return
}

proc ::sak::validate::testsuites::notests {pkg} {
    = "$pkg Bad"
    log "@@ WARN  No testsuite: $pkg"
    return
}

###

proc ::sak::validate::testsuites::+e {msg} {
    variable merrors ; incr merrors
    variable errors  ; incr errors
    variable current
    log "@@ ERROR $current $msg"
    return
}

proc ::sak::validate::testsuites::+u {f} {
    variable used
    if {[info exists used($f)]} return
    variable munclaimed ; incr munclaimed
    variable unclaimed  ; incr unclaimed
    set used($f) .
    log "@@ NOTE  Unclaimed testsuite $f"
    return
}

###

proc ::sak::validate::testsuites::Count {modules} {
    variable maxml 0
    !
    foreach m [linsert $modules 0 Module] {
	= "M $m"
	set l [string length $m]
	if {$l > $maxml} {set maxml $l}
    }
    =| "Validate testsuites (existence) ..."
    return
}

proc ::sak::validate::testsuites::Head {m} {
    variable maxml
    += ${m}[blank [expr {$maxml - [string length $m]}]]
    return
}

###

proc ::sak::validate::testsuites::InitModuleCounters {} {
    variable mtotal     0
    variable mhavetests 0
    variable munclaimed 0
    variable merrors    0
    return
}

proc ::sak::validate::testsuites::ModuleSummary {} {
    variable mtotal
    variable mhavetests
    variable munclaimed
    variable merrors

    set complete [F $mhavetests]/[F $mtotal]
    set not      "! [F [expr {$mtotal - $mhavetests}]]"
    set err      "E [F $merrors]"
    set unc      "U [F $munclaimed]"

    if {$munclaimed} {
	set unc [=cya $unc]
	>> unc
    }
    if {!$mhavetests && $mtotal} {
	set complete [=red $complete]
	set not      [=red $not]
	>> none
    } elseif {$mhavetests < $mtotal} {
	set complete [=yel $complete]
	set not      [=yel $not]
	>> miss
    }
    if {$merrors} {
	set err [red]$err[rst]
	>> fail
    }

    =| "~~ $complete $not $unc $err"
    return
}

###

proc ::sak::validate::testsuites::InitCounters {} {
    variable total     0
    variable havetests 0
    variable unclaimed 0
    variable errors    0
    return
}

proc ::sak::validate::testsuites::Summary {} {
    variable total
    variable havetests
    variable unclaimed
    variable errors

    set tot   [F $total]
    set tst   [F $havetests]
    set uts   [F [expr {$total - $havetests}]]
    set unc   [F $unclaimed]
    set per   [format %6.2f [expr {$havetests*100./$total}]]
    set uper  [format %6.2f [expr {($total - $havetests)*100./$total}]]
    set err   [F $errors]

    if {$errors}    { set err [=red $err] }
    if {$unclaimed} { set unc [=cya $unc] }

    if {!$havetests && $total} {
	set tst [=red $tst]
	set uts [=red $uts]
    } elseif {$havetests < $total} {
	set tst [=yel $tst]
	set uts [=yel $uts]
    }

    sum ""
    sum "Testsuite statistics"
    sum "#Packages:     $tot"
    sum "#Tested:       $tst (${per}%)"
    sum "#Untested:     $uts (${uper}%)"
    sum "#Unclaimed:    $unc"
    sum "#Errors:       $err"
    return
}

###

proc ::sak::validate::testsuites::F {n} { format %6d $n }

###

proc ::sak::validate::testsuites::At {m} {
    global distribution
    return [file join $distribution modules $m]
}

# ###

namespace eval ::sak::validate::testsuites {
    # Max length of module names and patchlevel information.
    variable maxml 0

    # Counters across all modules
    variable total     0 ; # Number of packages overall.
    variable havetests 0 ; # Number of packages with testsuites.
    variable unclaimed 0 ; # Number of testsuites not claimed by a specific package.
    variable errors    0 ; # Number of errors found with all testsuites.

    # Same counters, per module.
    variable mtotal     0
    variable mhavetests 0
    variable munclaimed 0
    variable merrors    0

    # Name of currently processed testsuite
    variable current ""

    # Map from packages to files claiming to test them.
    variable  claims
    array set claims {}

    # Set of files taken by packages, as array
    variable  used
    array set used {}

    # Map from modules to packages contained in them
    variable  pkg
    array set pkg {}

    # Transient storage used while collecting packages per testsuite.
    variable testing {}
    variable ip      {}
}

##
# ###

package provide sak::validate::testsuites 1.0