tcltest-2.5.5.tm 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533
  1. # tcltest.tcl --
  2. #
  3. # This file contains support code for the Tcl test suite. It
  4. # defines the tcltest namespace and finds and defines the output
  5. # directory, constraints available, output and error channels,
  6. # etc. used by Tcl tests. See the tcltest man page for more
  7. # details.
  8. #
  9. # This design was based on the Tcl testing approach designed and
  10. # initially implemented by Mary Ann May-Pumphrey of Sun
  11. # Microsystems.
  12. #
  13. # Copyright © 1994-1997 Sun Microsystems, Inc.
  14. # Copyright © 1998-1999 Scriptics Corporation.
  15. # Copyright © 2000 Ajuba Solutions
  16. # Contributions from Don Porter, NIST, 2002. (not subject to US copyright)
  17. # All rights reserved.
  18. package require Tcl 8.5- ;# -verbose line uses [info frame]
  19. namespace eval tcltest {
  20. # When the version number changes, be sure to update the pkgIndex.tcl file,
  21. # and the install directory in the Makefiles. When the minor version
  22. # changes (new feature) be sure to update the man page as well.
  23. variable Version 2.5.5
  24. # Compatibility support for dumb variables defined in tcltest 1
  25. # Do not use these. Call [package provide Tcl] and [info patchlevel]
  26. # yourself. You don't need tcltest to wrap it for you.
  27. variable version [package provide Tcl]
  28. variable patchLevel [info patchlevel]
  29. ##### Export the public tcltest procs; several categories
  30. #
  31. # Export the main functional commands that do useful things
  32. namespace export cleanupTests loadTestedCommands makeDirectory \
  33. makeFile removeDirectory removeFile runAllTests test
  34. # Export configuration commands that control the functional commands
  35. namespace export configure customMatch errorChannel interpreter \
  36. outputChannel testConstraint
  37. # Export commands that are duplication (candidates for deprecation)
  38. if {![package vsatisfies [package provide Tcl] 8.7-]} {
  39. namespace export bytestring ;# dups [encoding convertfrom identity]
  40. }
  41. namespace export debug ;# [configure -debug]
  42. namespace export errorFile ;# [configure -errfile]
  43. namespace export limitConstraints ;# [configure -limitconstraints]
  44. namespace export loadFile ;# [configure -loadfile]
  45. namespace export loadScript ;# [configure -load]
  46. namespace export match ;# [configure -match]
  47. namespace export matchFiles ;# [configure -file]
  48. namespace export matchDirectories ;# [configure -relateddir]
  49. namespace export normalizeMsg ;# application of [customMatch]
  50. namespace export normalizePath ;# [file normalize] (8.4)
  51. namespace export outputFile ;# [configure -outfile]
  52. namespace export preserveCore ;# [configure -preservecore]
  53. namespace export singleProcess ;# [configure -singleproc]
  54. namespace export skip ;# [configure -skip]
  55. namespace export skipFiles ;# [configure -notfile]
  56. namespace export skipDirectories ;# [configure -asidefromdir]
  57. namespace export temporaryDirectory ;# [configure -tmpdir]
  58. namespace export testsDirectory ;# [configure -testdir]
  59. namespace export verbose ;# [configure -verbose]
  60. namespace export viewFile ;# binary encoding [read]
  61. namespace export workingDirectory ;# [cd] [pwd]
  62. # Export deprecated commands for tcltest 1 compatibility
  63. namespace export getMatchingFiles mainThread restoreState saveState \
  64. threadReap
  65. # tcltest::normalizePath --
  66. #
  67. # This procedure resolves any symlinks in the path thus creating
  68. # a path without internal redirection. It assumes that the
  69. # incoming path is absolute.
  70. #
  71. # Arguments
  72. # pathVar - name of variable containing path to modify.
  73. #
  74. # Results
  75. # The path is modified in place.
  76. #
  77. # Side Effects:
  78. # None.
  79. #
  80. proc normalizePath {pathVar} {
  81. upvar 1 $pathVar path
  82. set oldpwd [pwd]
  83. catch {cd $path}
  84. set path [pwd]
  85. cd $oldpwd
  86. return $path
  87. }
  88. ##### Verification commands used to test values of variables and options
  89. #
  90. # Verification command that accepts everything
  91. proc AcceptAll {value} {
  92. return $value
  93. }
  94. # Verification command that accepts valid Tcl lists
  95. proc AcceptList { list } {
  96. return [lrange $list 0 end]
  97. }
  98. # Verification command that accepts a glob pattern
  99. proc AcceptPattern { pattern } {
  100. return [AcceptAll $pattern]
  101. }
  102. # Verification command that accepts integers
  103. proc AcceptInteger { level } {
  104. return [incr level 0]
  105. }
  106. # Verification command that accepts boolean values
  107. proc AcceptBoolean { boolean } {
  108. return [expr {$boolean && $boolean}]
  109. }
  110. # Verification command that accepts (syntactically) valid Tcl scripts
  111. proc AcceptScript { script } {
  112. if {![info complete $script]} {
  113. return -code error "invalid Tcl script: $script"
  114. }
  115. return $script
  116. }
  117. # Verification command that accepts (converts to) absolute pathnames
  118. proc AcceptAbsolutePath { path } {
  119. return [file join [pwd] $path]
  120. }
  121. # Verification command that accepts existing readable directories
  122. proc AcceptReadable { path } {
  123. if {![file readable $path]} {
  124. return -code error "\"$path\" is not readable"
  125. }
  126. return $path
  127. }
  128. proc AcceptDirectory { directory } {
  129. set directory [AcceptAbsolutePath $directory]
  130. if {![file exists $directory]} {
  131. return -code error "\"$directory\" does not exist"
  132. }
  133. if {![file isdir $directory]} {
  134. return -code error "\"$directory\" is not a directory"
  135. }
  136. return [AcceptReadable $directory]
  137. }
  138. ##### Initialize internal arrays of tcltest, but only if the caller
  139. # has not already pre-initialized them. This is done to support
  140. # compatibility with older tests that directly access internals
  141. # rather than go through command interfaces.
  142. #
  143. proc ArrayDefault {varName value} {
  144. variable $varName
  145. if {[array exists $varName]} {
  146. return
  147. }
  148. if {[info exists $varName]} {
  149. # Pre-initialized value is a scalar: destroy it!
  150. unset $varName
  151. }
  152. array set $varName $value
  153. }
  154. # save the original environment so that it can be restored later
  155. ArrayDefault originalEnv [array get ::env]
  156. # initialize numTests array to keep track of the number of tests
  157. # that pass, fail, and are skipped.
  158. ArrayDefault numTests [list Total 0 Passed 0 Skipped 0 Failed 0]
  159. # createdNewFiles will store test files as indices and the list of
  160. # files (that should not have been) left behind by the test files
  161. # as values.
  162. ArrayDefault createdNewFiles {}
  163. # initialize skippedBecause array to keep track of constraints that
  164. # kept tests from running; a constraint name of "userSpecifiedSkip"
  165. # means that the test appeared on the list of tests that matched the
  166. # -skip value given to the flag; "userSpecifiedNonMatch" means that
  167. # the test didn't match the argument given to the -match flag; both
  168. # of these constraints are counted only if tcltest::debug is set to
  169. # true.
  170. ArrayDefault skippedBecause {}
  171. # initialize the testConstraints array to keep track of valid
  172. # predefined constraints (see the explanation for the
  173. # InitConstraints proc for more details).
  174. ArrayDefault testConstraints {}
  175. ##### Initialize internal variables of tcltest, but only if the caller
  176. # has not already pre-initialized them. This is done to support
  177. # compatibility with older tests that directly access internals
  178. # rather than go through command interfaces.
  179. #
  180. proc Default {varName value {verify AcceptAll}} {
  181. variable $varName
  182. if {![info exists $varName]} {
  183. variable $varName [$verify $value]
  184. } else {
  185. variable $varName [$verify [set $varName]]
  186. }
  187. }
  188. # Save any arguments that we might want to pass through to other
  189. # programs. This is used by the -args flag.
  190. # FINDUSER
  191. Default parameters {}
  192. # Count the number of files tested (0 if runAllTests wasn't called).
  193. # runAllTests will set testSingleFile to false, so stats will
  194. # not be printed until runAllTests calls the cleanupTests proc.
  195. # The currentFailure var stores the boolean value of whether the
  196. # current test file has had any failures. The failFiles list
  197. # stores the names of test files that had failures.
  198. Default numTestFiles 0 AcceptInteger
  199. Default testSingleFile true AcceptBoolean
  200. Default currentFailure false AcceptBoolean
  201. Default failFiles {} AcceptList
  202. # Tests should remove all files they create. The test suite will
  203. # check the current working dir for files created by the tests.
  204. # filesMade keeps track of such files created using the makeFile and
  205. # makeDirectory procedures. filesExisted stores the names of
  206. # pre-existing files.
  207. #
  208. # Note that $filesExisted lists only those files that exist in
  209. # the original [temporaryDirectory].
  210. Default filesMade {} AcceptList
  211. Default filesExisted {} AcceptList
  212. proc FillFilesExisted {} {
  213. variable filesExisted
  214. # Save the names of files that already exist in the scratch directory.
  215. foreach file [glob -nocomplain -directory [temporaryDirectory] *] {
  216. lappend filesExisted [file tail $file]
  217. }
  218. # After successful filling, turn this into a no-op.
  219. proc FillFilesExisted args {}
  220. }
  221. # Kept only for compatibility
  222. Default constraintsSpecified {} AcceptList
  223. trace add variable constraintsSpecified read [namespace code {
  224. set constraintsSpecified [array names testConstraints] ;#}]
  225. # tests that use threads need to know which is the main thread
  226. Default mainThread 1
  227. variable mainThread
  228. if {[info commands thread::id] ne {}} {
  229. set mainThread [thread::id]
  230. } elseif {[info commands testthread] ne {}} {
  231. set mainThread [testthread id]
  232. }
  233. # Set workingDirectory to [pwd]. The default output directory for
  234. # Tcl tests is the working directory. Whenever this value changes
  235. # change to that directory.
  236. variable workingDirectory
  237. trace add variable workingDirectory write \
  238. [namespace code {cd $workingDirectory ;#}]
  239. Default workingDirectory [pwd] AcceptAbsolutePath
  240. proc workingDirectory { {dir ""} } {
  241. variable workingDirectory
  242. if {[llength [info level 0]] == 1} {
  243. return $workingDirectory
  244. }
  245. set workingDirectory [AcceptAbsolutePath $dir]
  246. }
  247. # Set the location of the execuatble
  248. Default tcltest [info nameofexecutable]
  249. trace add variable tcltest write [namespace code {testConstraint stdio \
  250. [eval [ConstraintInitializer stdio]] ;#}]
  251. # save the platform information so it can be restored later
  252. Default originalTclPlatform [array get ::tcl_platform]
  253. # If a core file exists, save its modification time.
  254. if {[file exists [file join [workingDirectory] core]]} {
  255. Default coreModTime \
  256. [file mtime [file join [workingDirectory] core]]
  257. }
  258. # stdout and stderr buffers for use when we want to store them
  259. Default outData {}
  260. Default errData {}
  261. # keep track of test level for nested test commands
  262. variable testLevel 0
  263. # the variables and procs that existed when saveState was called are
  264. # stored in a variable of the same name
  265. Default saveState {}
  266. # Internationalization support -- used in [SetIso8859_1_Locale] and
  267. # [RestoreLocale]. Those commands are used in cmdIL.test.
  268. if {![info exists [namespace current]::isoLocale]} {
  269. variable isoLocale fr
  270. switch -- $::tcl_platform(platform) {
  271. "unix" {
  272. # Try some 'known' values for some platforms:
  273. switch -exact -- $::tcl_platform(os) {
  274. "FreeBSD" {
  275. set isoLocale fr_FR.ISO_8859-1
  276. }
  277. HP-UX {
  278. set isoLocale fr_FR.iso88591
  279. }
  280. Linux -
  281. IRIX {
  282. set isoLocale fr
  283. }
  284. default {
  285. # Works on SunOS 4 and Solaris, and maybe
  286. # others... Define it to something else on your
  287. # system if you want to test those.
  288. set isoLocale iso_8859_1
  289. }
  290. }
  291. }
  292. "windows" {
  293. set isoLocale French
  294. }
  295. }
  296. }
  297. variable ChannelsWeOpened; array set ChannelsWeOpened {}
  298. # output goes to stdout by default
  299. Default outputChannel stdout
  300. proc outputChannel { {filename ""} } {
  301. variable outputChannel
  302. variable ChannelsWeOpened
  303. # This is very subtle and tricky, so let me try to explain.
  304. # (Hopefully this longer comment will be clear when I come
  305. # back in a few months, unlike its predecessor :) )
  306. #
  307. # The [outputChannel] command (and underlying variable) have to
  308. # be kept in sync with the [configure -outfile] configuration
  309. # option ( and underlying variable Option(-outfile) ). This is
  310. # accomplished with a write trace on Option(-outfile) that will
  311. # update [outputChannel] whenver a new value is written. That
  312. # much is easy.
  313. #
  314. # The trick is that in order to maintain compatibility with
  315. # version 1 of tcltest, we must allow every configuration option
  316. # to get its inital value from command line arguments. This is
  317. # accomplished by setting initial read traces on all the
  318. # configuration options to parse the command line option the first
  319. # time they are read. These traces are cancelled whenever the
  320. # program itself calls [configure].
  321. #
  322. # OK, then so to support tcltest 1 compatibility, it seems we want
  323. # to get the return from [outputFile] to trigger the read traces,
  324. # just in case.
  325. #
  326. # BUT! A little known feature of Tcl variable traces is that
  327. # traces are disabled during the handling of other traces. So,
  328. # if we trigger read traces on Option(-outfile) and that triggers
  329. # command line parsing which turns around and sets an initial
  330. # value for Option(-outfile) -- <whew!> -- the write trace that
  331. # would keep [outputChannel] in sync with that new initial value
  332. # would not fire!
  333. #
  334. # SO, finally, as a workaround, instead of triggering read traces
  335. # by invoking [outputFile], we instead trigger the same set of
  336. # read traces by invoking [debug]. Any command that reads a
  337. # configuration option would do. [debug] is just a handy one.
  338. # The end result is that we support tcltest 1 compatibility and
  339. # keep outputChannel and -outfile in sync in all cases.
  340. debug
  341. if {[llength [info level 0]] == 1} {
  342. return $outputChannel
  343. }
  344. if {[info exists ChannelsWeOpened($outputChannel)]} {
  345. close $outputChannel
  346. unset ChannelsWeOpened($outputChannel)
  347. }
  348. switch -exact -- $filename {
  349. stderr -
  350. stdout {
  351. set outputChannel $filename
  352. }
  353. default {
  354. set outputChannel [open $filename a]
  355. if {[package vsatisfies [package provide Tcl] 8.7-]} {
  356. fconfigure $outputChannel -encoding utf-8
  357. }
  358. set ChannelsWeOpened($outputChannel) 1
  359. # If we created the file in [temporaryDirectory], then
  360. # [cleanupTests] will delete it, unless we claim it was
  361. # already there.
  362. set outdir [normalizePath [file dirname \
  363. [file join [pwd] $filename]]]
  364. if {$outdir eq [temporaryDirectory]} {
  365. variable filesExisted
  366. FillFilesExisted
  367. set filename [file tail $filename]
  368. if {$filename ni $filesExisted} {
  369. lappend filesExisted $filename
  370. }
  371. }
  372. }
  373. }
  374. return $outputChannel
  375. }
  376. # errors go to stderr by default
  377. Default errorChannel stderr
  378. proc errorChannel { {filename ""} } {
  379. variable errorChannel
  380. variable ChannelsWeOpened
  381. # This is subtle and tricky. See the comment above in
  382. # [outputChannel] for a detailed explanation.
  383. debug
  384. if {[llength [info level 0]] == 1} {
  385. return $errorChannel
  386. }
  387. if {[info exists ChannelsWeOpened($errorChannel)]} {
  388. close $errorChannel
  389. unset ChannelsWeOpened($errorChannel)
  390. }
  391. switch -exact -- $filename {
  392. stderr -
  393. stdout {
  394. set errorChannel $filename
  395. }
  396. default {
  397. set errorChannel [open $filename a]
  398. if {[package vsatisfies [package provide Tcl] 8.7-]} {
  399. fconfigure $errorChannel -encoding utf-8
  400. }
  401. set ChannelsWeOpened($errorChannel) 1
  402. # If we created the file in [temporaryDirectory], then
  403. # [cleanupTests] will delete it, unless we claim it was
  404. # already there.
  405. set outdir [normalizePath [file dirname \
  406. [file join [pwd] $filename]]]
  407. if {$outdir eq [temporaryDirectory]} {
  408. variable filesExisted
  409. FillFilesExisted
  410. set filename [file tail $filename]
  411. if {$filename ni $filesExisted} {
  412. lappend filesExisted $filename
  413. }
  414. }
  415. }
  416. }
  417. return $errorChannel
  418. }
  419. ##### Set up the configurable options
  420. #
  421. # The configurable options of the package
  422. variable Option; array set Option {}
  423. # Usage strings for those options
  424. variable Usage; array set Usage {}
  425. # Verification commands for those options
  426. variable Verify; array set Verify {}
  427. # Initialize the default values of the configurable options that are
  428. # historically associated with an exported variable. If that variable
  429. # is already set, support compatibility by accepting its pre-set value.
  430. # Use [trace] to establish ongoing connection between the deprecated
  431. # exported variable and the modern option kept as a true internal var.
  432. # Also set up usage string and value testing for the option.
  433. proc Option {option value usage {verify AcceptAll} {varName {}}} {
  434. variable Option
  435. variable Verify
  436. variable Usage
  437. variable OptionControlledVariables
  438. variable DefaultValue
  439. set Usage($option) $usage
  440. set Verify($option) $verify
  441. set DefaultValue($option) $value
  442. if {[catch {$verify $value} msg]} {
  443. return -code error $msg
  444. } else {
  445. set Option($option) $msg
  446. }
  447. if {[string length $varName]} {
  448. variable $varName
  449. if {[info exists $varName]} {
  450. if {[catch {$verify [set $varName]} msg]} {
  451. return -code error $msg
  452. } else {
  453. set Option($option) $msg
  454. }
  455. unset $varName
  456. }
  457. namespace eval [namespace current] \
  458. [list upvar 0 Option($option) $varName]
  459. # Workaround for Bug (now Feature Request) 572889. Grrrr....
  460. # Track all the variables tied to options
  461. lappend OptionControlledVariables $varName
  462. # Later, set auto-configure read traces on all
  463. # of them, since a single trace on Option does not work.
  464. proc $varName {{value {}}} [subst -nocommands {
  465. if {[llength [info level 0]] == 2} {
  466. Configure $option [set value]
  467. }
  468. return [Configure $option]
  469. }]
  470. }
  471. }
  472. proc MatchingOption {option} {
  473. variable Option
  474. set match [array names Option $option*]
  475. switch -- [llength $match] {
  476. 0 {
  477. set sorted [lsort [array names Option]]
  478. set values [join [lrange $sorted 0 end-1] ", "]
  479. append values ", or [lindex $sorted end]"
  480. return -code error "unknown option $option: should be\
  481. one of $values"
  482. }
  483. 1 {
  484. return [lindex $match 0]
  485. }
  486. default {
  487. # Exact match trumps ambiguity
  488. if {$option in $match} {
  489. return $option
  490. }
  491. set values [join [lrange $match 0 end-1] ", "]
  492. append values ", or [lindex $match end]"
  493. return -code error "ambiguous option $option:\
  494. could match $values"
  495. }
  496. }
  497. }
  498. proc EstablishAutoConfigureTraces {} {
  499. variable OptionControlledVariables
  500. foreach varName [concat $OptionControlledVariables Option] {
  501. variable $varName
  502. trace add variable $varName read [namespace code {
  503. ProcessCmdLineArgs ;#}]
  504. }
  505. }
  506. proc RemoveAutoConfigureTraces {} {
  507. variable OptionControlledVariables
  508. foreach varName [concat $OptionControlledVariables Option] {
  509. variable $varName
  510. foreach pair [trace info variable $varName] {
  511. lassign $pair op cmd
  512. if {($op eq "read") &&
  513. [string match *ProcessCmdLineArgs* $cmd]} {
  514. trace remove variable $varName $op $cmd
  515. }
  516. }
  517. }
  518. # Once the traces are removed, this can become a no-op
  519. proc RemoveAutoConfigureTraces {} {}
  520. }
  521. proc Configure args {
  522. variable Option
  523. variable Verify
  524. set n [llength $args]
  525. if {$n == 0} {
  526. return [lsort [array names Option]]
  527. }
  528. if {$n == 1} {
  529. if {[catch {MatchingOption [lindex $args 0]} option]} {
  530. return -code error $option
  531. }
  532. return $Option($option)
  533. }
  534. while {[llength $args] > 1} {
  535. if {[catch {MatchingOption [lindex $args 0]} option]} {
  536. return -code error $option
  537. }
  538. if {[catch {$Verify($option) [lindex $args 1]} value]} {
  539. return -code error "invalid $option\
  540. value \"[lindex $args 1]\": $value"
  541. }
  542. set Option($option) $value
  543. set args [lrange $args 2 end]
  544. }
  545. if {[llength $args]} {
  546. if {[catch {MatchingOption [lindex $args 0]} option]} {
  547. return -code error $option
  548. }
  549. return -code error "missing value for option $option"
  550. }
  551. }
  552. proc configure args {
  553. if {[llength $args] > 1} {
  554. RemoveAutoConfigureTraces
  555. }
  556. set code [catch {Configure {*}$args} msg]
  557. return -code $code $msg
  558. }
  559. proc AcceptVerbose { level } {
  560. set level [AcceptList $level]
  561. set levelMap {
  562. l list
  563. p pass
  564. b body
  565. s skip
  566. t start
  567. e error
  568. l line
  569. m msec
  570. u usec
  571. }
  572. set levelRegexp "^([join [dict values $levelMap] |])\$"
  573. if {[llength $level] == 1} {
  574. if {![regexp $levelRegexp $level]} {
  575. # translate single characters abbreviations to expanded list
  576. set level [string map $levelMap [split $level {}]]
  577. }
  578. }
  579. set valid [list]
  580. foreach v $level {
  581. if {[regexp $levelRegexp $v]} {
  582. lappend valid $v
  583. }
  584. }
  585. return $valid
  586. }
  587. proc IsVerbose {level} {
  588. variable Option
  589. return [expr {$level in $Option(-verbose)}]
  590. }
  591. # Default verbosity is to show bodies of failed tests
  592. Option -verbose {body error} {
  593. Takes any combination of the values 'p', 's', 'b', 't', 'e' and 'l'.
  594. Test suite will display all passed tests if 'p' is specified, all
  595. skipped tests if 's' is specified, the bodies of failed tests if
  596. 'b' is specified, and when tests start if 't' is specified.
  597. ErrorInfo is displayed if 'e' is specified. Source file line
  598. information of failed tests is displayed if 'l' is specified.
  599. } AcceptVerbose verbose
  600. # Match and skip patterns default to the empty list, except for
  601. # matchFiles, which defaults to all .test files in the
  602. # testsDirectory and matchDirectories, which defaults to all
  603. # directories.
  604. Option -match * {
  605. Run all tests within the specified files that match one of the
  606. list of glob patterns given.
  607. } AcceptList match
  608. Option -skip {} {
  609. Skip all tests within the specified tests (via -match) and files
  610. that match one of the list of glob patterns given.
  611. } AcceptList skip
  612. Option -file *.test {
  613. Run tests in all test files that match the glob pattern given.
  614. } AcceptPattern matchFiles
  615. # By default, skip files that appear to be SCCS lock files.
  616. Option -notfile l.*.test {
  617. Skip all test files that match the glob pattern given.
  618. } AcceptPattern skipFiles
  619. Option -relateddir * {
  620. Run tests in directories that match the glob pattern given.
  621. } AcceptPattern matchDirectories
  622. Option -asidefromdir {} {
  623. Skip tests in directories that match the glob pattern given.
  624. } AcceptPattern skipDirectories
  625. # By default, don't save core files
  626. Option -preservecore 0 {
  627. If 2, save any core files produced during testing in the directory
  628. specified by -tmpdir. If 1, notify the user if core files are
  629. created.
  630. } AcceptInteger preserveCore
  631. # debug output doesn't get printed by default; debug level 1 spits
  632. # up only the tests that were skipped because they didn't match or
  633. # were specifically skipped. A debug level of 2 would spit up the
  634. # tcltest variables and flags provided; a debug level of 3 causes
  635. # some additional output regarding operations of the test harness.
  636. # The tcltest package currently implements only up to debug level 3.
  637. Option -debug 0 {
  638. Internal debug level
  639. } AcceptInteger debug
  640. proc SetSelectedConstraints args {
  641. variable Option
  642. foreach c $Option(-constraints) {
  643. testConstraint $c 1
  644. }
  645. }
  646. Option -constraints {} {
  647. Do not skip the listed constraints listed in -constraints.
  648. } AcceptList
  649. trace add variable Option(-constraints) write \
  650. [namespace code {SetSelectedConstraints ;#}]
  651. # Don't run only the "-constraint" specified tests by default
  652. proc ClearUnselectedConstraints args {
  653. variable Option
  654. variable testConstraints
  655. if {!$Option(-limitconstraints)} {return}
  656. foreach c [array names testConstraints] {
  657. if {$c ni $Option(-constraints)} {
  658. testConstraint $c 0
  659. }
  660. }
  661. }
  662. Option -limitconstraints 0 {
  663. whether to run only tests with the constraints
  664. } AcceptBoolean limitConstraints
  665. trace add variable Option(-limitconstraints) write \
  666. [namespace code {ClearUnselectedConstraints ;#}]
  667. # A test application has to know how to load the tested commands
  668. # into the interpreter.
  669. Option -load {} {
  670. Specifies the script to load the tested commands.
  671. } AcceptScript loadScript
  672. # Default is to run each test file in a separate process
  673. Option -singleproc 0 {
  674. whether to run all tests in one process
  675. } AcceptBoolean singleProcess
  676. proc AcceptTemporaryDirectory { directory } {
  677. set directory [AcceptAbsolutePath $directory]
  678. if {![file exists $directory]} {
  679. file mkdir $directory
  680. }
  681. set directory [AcceptDirectory $directory]
  682. if {![file writable $directory]} {
  683. if {[workingDirectory] eq $directory} {
  684. # Special exception: accept the default value
  685. # even if the directory is not writable
  686. return $directory
  687. }
  688. return -code error "\"$directory\" is not writeable"
  689. }
  690. return $directory
  691. }
  692. # Directory where files should be created
  693. Option -tmpdir [workingDirectory] {
  694. Save temporary files in the specified directory.
  695. } AcceptTemporaryDirectory temporaryDirectory
  696. trace add variable Option(-tmpdir) write \
  697. [namespace code {normalizePath Option(-tmpdir) ;#}]
  698. # Tests should not rely on the current working directory.
  699. # Files that are part of the test suite should be accessed relative
  700. # to [testsDirectory]
  701. Option -testdir [workingDirectory] {
  702. Search tests in the specified directory.
  703. } AcceptDirectory testsDirectory
  704. trace add variable Option(-testdir) write \
  705. [namespace code {normalizePath Option(-testdir) ;#}]
  706. proc AcceptLoadFile { file } {
  707. if {$file eq {}} {return $file}
  708. set file [file join [temporaryDirectory] $file]
  709. return [AcceptReadable $file]
  710. }
  711. proc ReadLoadScript {args} {
  712. variable Option
  713. if {$Option(-loadfile) eq {}} {return}
  714. set tmp [open $Option(-loadfile) r]
  715. if {[package vsatisfies [package provide Tcl] 8.7-]} {
  716. fconfigure $tmp -encoding utf-8
  717. }
  718. loadScript [read $tmp]
  719. close $tmp
  720. }
  721. Option -loadfile {} {
  722. Read the script to load the tested commands from the specified file.
  723. } AcceptLoadFile loadFile
  724. trace add variable Option(-loadfile) write [namespace code ReadLoadScript]
  725. proc AcceptOutFile { file } {
  726. if {[string equal stderr $file]} {return $file}
  727. if {[string equal stdout $file]} {return $file}
  728. return [file join [temporaryDirectory] $file]
  729. }
  730. # output goes to stdout by default
  731. Option -outfile stdout {
  732. Send output from test runs to the specified file.
  733. } AcceptOutFile outputFile
  734. trace add variable Option(-outfile) write \
  735. [namespace code {outputChannel $Option(-outfile) ;#}]
  736. # errors go to stderr by default
  737. Option -errfile stderr {
  738. Send errors from test runs to the specified file.
  739. } AcceptOutFile errorFile
  740. trace add variable Option(-errfile) write \
  741. [namespace code {errorChannel $Option(-errfile) ;#}]
  742. proc loadIntoChildInterpreter {child args} {
  743. variable Version
  744. interp eval $child [package ifneeded tcltest $Version]
  745. interp eval $child "tcltest::configure {*}{$args}"
  746. interp alias $child ::tcltest::ReportToParent \
  747. {} ::tcltest::ReportedFromChild
  748. }
  749. proc ReportedFromChild {total passed skipped failed because newfiles} {
  750. variable numTests
  751. variable skippedBecause
  752. variable createdNewFiles
  753. incr numTests(Total) $total
  754. incr numTests(Passed) $passed
  755. incr numTests(Skipped) $skipped
  756. incr numTests(Failed) $failed
  757. foreach {constraint count} $because {
  758. incr skippedBecause($constraint) $count
  759. }
  760. foreach {testfile created} $newfiles {
  761. lappend createdNewFiles($testfile) {*}$created
  762. }
  763. return
  764. }
  765. }
  766. #####################################################################
  767. # tcltest::Debug* --
  768. #
  769. # Internal helper procedures to write out debug information
  770. # dependent on the chosen level. A test shell may overide
  771. # them, f.e. to redirect the output into a different
  772. # channel, or even into a GUI.
  773. # tcltest::DebugPuts --
  774. #
  775. # Prints the specified string if the current debug level is
  776. # higher than the provided level argument.
  777. #
  778. # Arguments:
  779. # level The lowest debug level triggering the output
  780. # string The string to print out.
  781. #
  782. # Results:
  783. # Prints the string. Nothing else is allowed.
  784. #
  785. # Side Effects:
  786. # None.
  787. #
  788. proc tcltest::DebugPuts {level string} {
  789. variable debug
  790. if {$debug >= $level} {
  791. puts $string
  792. }
  793. return
  794. }
  795. # tcltest::DebugPArray --
  796. #
  797. # Prints the contents of the specified array if the current
  798. # debug level is higher than the provided level argument
  799. #
  800. # Arguments:
  801. # level The lowest debug level triggering the output
  802. # arrayvar The name of the array to print out.
  803. #
  804. # Results:
  805. # Prints the contents of the array. Nothing else is allowed.
  806. #
  807. # Side Effects:
  808. # None.
  809. #
  810. proc tcltest::DebugPArray {level arrayvar} {
  811. variable debug
  812. if {$debug >= $level} {
  813. catch {upvar 1 $arrayvar $arrayvar}
  814. parray $arrayvar
  815. }
  816. return
  817. }
  818. # Define our own [parray] in ::tcltest that will inherit use of the [puts]
  819. # defined in ::tcltest. NOTE: Ought to construct with [info args] and
  820. # [info default], but can't be bothered now. If [parray] changes, then
  821. # this will need changing too.
  822. auto_load ::parray
  823. proc tcltest::parray {a {pattern *}} [info body ::parray]
  824. # tcltest::DebugDo --
  825. #
  826. # Executes the script if the current debug level is greater than
  827. # the provided level argument
  828. #
  829. # Arguments:
  830. # level The lowest debug level triggering the execution.
  831. # script The tcl script executed upon a debug level high enough.
  832. #
  833. # Results:
  834. # Arbitrary side effects, dependent on the executed script.
  835. #
  836. # Side Effects:
  837. # None.
  838. #
  839. proc tcltest::DebugDo {level script} {
  840. variable debug
  841. if {$debug >= $level} {
  842. uplevel 1 $script
  843. }
  844. return
  845. }
  846. #####################################################################
  847. proc tcltest::Warn {msg} {
  848. puts [outputChannel] "WARNING: $msg"
  849. }
  850. # tcltest::mainThread
  851. #
  852. # Accessor command for tcltest variable mainThread.
  853. #
  854. proc tcltest::mainThread { {new ""} } {
  855. variable mainThread
  856. if {[llength [info level 0]] == 1} {
  857. return $mainThread
  858. }
  859. set mainThread $new
  860. }
  861. # tcltest::testConstraint --
  862. #
  863. # sets a test constraint to a value; to do multiple constraints,
  864. # call this proc multiple times. also returns the value of the
  865. # named constraint if no value was supplied.
  866. #
  867. # Arguments:
  868. # constraint - name of the constraint
  869. # value - new value for constraint (should be boolean) - if not
  870. # supplied, this is a query
  871. #
  872. # Results:
  873. # content of tcltest::testConstraints($constraint)
  874. #
  875. # Side effects:
  876. # none
  877. proc tcltest::testConstraint {constraint {value ""}} {
  878. variable testConstraints
  879. variable Option
  880. DebugPuts 3 "entering testConstraint $constraint $value"
  881. if {[llength [info level 0]] == 2} {
  882. return $testConstraints($constraint)
  883. }
  884. # Check for boolean values
  885. if {[catch {expr {$value && 1}} msg]} {
  886. return -code error $msg
  887. }
  888. if {[limitConstraints] && ($constraint ni $Option(-constraints))} {
  889. set value 0
  890. }
  891. set testConstraints($constraint) $value
  892. }
  893. # tcltest::interpreter --
  894. #
  895. # the interpreter name stored in tcltest::tcltest
  896. #
  897. # Arguments:
  898. # executable name
  899. #
  900. # Results:
  901. # content of tcltest::tcltest
  902. #
  903. # Side effects:
  904. # None.
  905. proc tcltest::interpreter { {interp ""} } {
  906. variable tcltest
  907. if {[llength [info level 0]] == 1} {
  908. return $tcltest
  909. }
  910. set tcltest $interp
  911. }
  912. #####################################################################
  913. # tcltest::AddToSkippedBecause --
  914. #
  915. # Increments the variable used to track how many tests were
  916. # skipped because of a particular constraint.
  917. #
  918. # Arguments:
  919. # constraint The name of the constraint to be modified
  920. #
  921. # Results:
  922. # Modifies tcltest::skippedBecause; sets the variable to 1 if
  923. # didn't previously exist - otherwise, it just increments it.
  924. #
  925. # Side effects:
  926. # None.
  927. proc tcltest::AddToSkippedBecause { constraint {value 1}} {
  928. # add the constraint to the list of constraints that kept tests
  929. # from running
  930. variable skippedBecause
  931. if {[info exists skippedBecause($constraint)]} {
  932. incr skippedBecause($constraint) $value
  933. } else {
  934. set skippedBecause($constraint) $value
  935. }
  936. return
  937. }
  938. # tcltest::PrintError --
  939. #
  940. # Prints errors to tcltest::errorChannel and then flushes that
  941. # channel, making sure that all messages are < 80 characters per
  942. # line.
  943. #
  944. # Arguments:
  945. # errorMsg String containing the error to be printed
  946. #
  947. # Results:
  948. # None.
  949. #
  950. # Side effects:
  951. # None.
  952. proc tcltest::PrintError {errorMsg} {
  953. set InitialMessage "Error: "
  954. set InitialMsgLen [string length $InitialMessage]
  955. puts -nonewline [errorChannel] $InitialMessage
  956. # Keep track of where the end of the string is.
  957. set endingIndex [string length $errorMsg]
  958. if {$endingIndex < (80 - $InitialMsgLen)} {
  959. puts [errorChannel] $errorMsg
  960. } else {
  961. # Print up to 80 characters on the first line, including the
  962. # InitialMessage.
  963. set beginningIndex [string last " " [string range $errorMsg 0 \
  964. [expr {80 - $InitialMsgLen}]]]
  965. puts [errorChannel] [string range $errorMsg 0 $beginningIndex]
  966. while {$beginningIndex ne "end"} {
  967. puts -nonewline [errorChannel] \
  968. [string repeat " " $InitialMsgLen]
  969. if {($endingIndex - $beginningIndex)
  970. < (80 - $InitialMsgLen)} {
  971. puts [errorChannel] [string trim \
  972. [string range $errorMsg $beginningIndex end]]
  973. break
  974. } else {
  975. set newEndingIndex [expr {[string last " " \
  976. [string range $errorMsg $beginningIndex \
  977. [expr {$beginningIndex
  978. + (80 - $InitialMsgLen)}]
  979. ]] + $beginningIndex}]
  980. if {($newEndingIndex <= 0)
  981. || ($newEndingIndex <= $beginningIndex)} {
  982. set newEndingIndex end
  983. }
  984. puts [errorChannel] [string trim \
  985. [string range $errorMsg \
  986. $beginningIndex $newEndingIndex]]
  987. set beginningIndex $newEndingIndex
  988. }
  989. }
  990. }
  991. flush [errorChannel]
  992. return
  993. }
  994. # tcltest::SafeFetch --
  995. #
  996. # The following trace procedure makes it so that we can safely
  997. # refer to non-existent members of the testConstraints array
  998. # without causing an error. Instead, reading a non-existent
  999. # member will return 0. This is necessary because tests are
  1000. # allowed to use constraint "X" without ensuring that
  1001. # testConstraints("X") is defined.
  1002. #
  1003. # Arguments:
  1004. # n1 - name of the array (testConstraints)
  1005. # n2 - array key value (constraint name)
  1006. # op - operation performed on testConstraints (generally r)
  1007. #
  1008. # Results:
  1009. # none
  1010. #
  1011. # Side effects:
  1012. # sets testConstraints($n2) to 0 if it's referenced but never
  1013. # before used
  1014. proc tcltest::SafeFetch {n1 n2 op} {
  1015. variable testConstraints
  1016. DebugPuts 3 "entering SafeFetch $n1 $n2 $op"
  1017. if {$n2 eq {}} {return}
  1018. if {![info exists testConstraints($n2)]} {
  1019. if {[catch {testConstraint $n2 [eval [ConstraintInitializer $n2]]}]} {
  1020. testConstraint $n2 0
  1021. }
  1022. }
  1023. }
  1024. # tcltest::ConstraintInitializer --
  1025. #
  1026. # Get or set a script that when evaluated in the tcltest namespace
  1027. # will return a boolean value with which to initialize the
  1028. # associated constraint.
  1029. #
  1030. # Arguments:
  1031. # constraint - name of the constraint initialized by the script
  1032. # script - the initializer script
  1033. #
  1034. # Results
  1035. # boolean value of the constraint - enabled or disabled
  1036. #
  1037. # Side effects:
  1038. # Constraint is initialized for future reference by [test]
  1039. proc tcltest::ConstraintInitializer {constraint {script ""}} {
  1040. variable ConstraintInitializer
  1041. DebugPuts 3 "entering ConstraintInitializer $constraint $script"
  1042. if {[llength [info level 0]] == 2} {
  1043. return $ConstraintInitializer($constraint)
  1044. }
  1045. # Check for boolean values
  1046. if {![info complete $script]} {
  1047. return -code error "ConstraintInitializer must be complete script"
  1048. }
  1049. set ConstraintInitializer($constraint) $script
  1050. }
  1051. # tcltest::InitConstraints --
  1052. #
  1053. # Call all registered constraint initializers to force initialization
  1054. # of all known constraints.
  1055. # See the tcltest man page for the list of built-in constraints defined
  1056. # in this procedure.
  1057. #
  1058. # Arguments:
  1059. # none
  1060. #
  1061. # Results:
  1062. # The testConstraints array is reset to have an index for each
  1063. # built-in test constraint.
  1064. #
  1065. # Side Effects:
  1066. # None.
  1067. #
  1068. proc tcltest::InitConstraints {} {
  1069. variable ConstraintInitializer
  1070. initConstraintsHook
  1071. foreach constraint [array names ConstraintInitializer] {
  1072. testConstraint $constraint
  1073. }
  1074. }
  1075. proc tcltest::DefineConstraintInitializers {} {
  1076. ConstraintInitializer singleTestInterp {singleProcess}
  1077. # All the 'pc' constraints are here for backward compatibility and
  1078. # are not documented. They have been replaced with equivalent 'win'
  1079. # constraints.
  1080. ConstraintInitializer unixOnly \
  1081. {string equal $::tcl_platform(platform) unix}
  1082. ConstraintInitializer macOnly \
  1083. {string equal $::tcl_platform(platform) macintosh}
  1084. ConstraintInitializer pcOnly \
  1085. {string equal $::tcl_platform(platform) windows}
  1086. ConstraintInitializer winOnly \
  1087. {string equal $::tcl_platform(platform) windows}
  1088. ConstraintInitializer unix {testConstraint unixOnly}
  1089. ConstraintInitializer mac {testConstraint macOnly}
  1090. ConstraintInitializer pc {testConstraint pcOnly}
  1091. ConstraintInitializer win {testConstraint winOnly}
  1092. ConstraintInitializer unixOrPc \
  1093. {expr {[testConstraint unix] || [testConstraint pc]}}
  1094. ConstraintInitializer macOrPc \
  1095. {expr {[testConstraint mac] || [testConstraint pc]}}
  1096. ConstraintInitializer unixOrWin \
  1097. {expr {[testConstraint unix] || [testConstraint win]}}
  1098. ConstraintInitializer macOrWin \
  1099. {expr {[testConstraint mac] || [testConstraint win]}}
  1100. ConstraintInitializer macOrUnix \
  1101. {expr {[testConstraint mac] || [testConstraint unix]}}
  1102. ConstraintInitializer nt {string equal $::tcl_platform(os) "Windows NT"}
  1103. ConstraintInitializer 95 {string equal $::tcl_platform(os) "Windows 95"}
  1104. ConstraintInitializer 98 {string equal $::tcl_platform(os) "Windows 98"}
  1105. # The following Constraints switches are used to mark tests that
  1106. # should work, but have been temporarily disabled on certain
  1107. # platforms because they don't and we haven't gotten around to
  1108. # fixing the underlying problem.
  1109. ConstraintInitializer tempNotPc {expr {![testConstraint pc]}}
  1110. ConstraintInitializer tempNotWin {expr {![testConstraint win]}}
  1111. ConstraintInitializer tempNotMac {expr {![testConstraint mac]}}
  1112. ConstraintInitializer tempNotUnix {expr {![testConstraint unix]}}
  1113. # The following Constraints switches are used to mark tests that
  1114. # crash on certain platforms, so that they can be reactivated again
  1115. # when the underlying problem is fixed.
  1116. ConstraintInitializer pcCrash {expr {![testConstraint pc]}}
  1117. ConstraintInitializer winCrash {expr {![testConstraint win]}}
  1118. ConstraintInitializer macCrash {expr {![testConstraint mac]}}
  1119. ConstraintInitializer unixCrash {expr {![testConstraint unix]}}
  1120. # Skip empty tests
  1121. ConstraintInitializer emptyTest {format 0}
  1122. # By default, tests that expose known bugs are skipped.
  1123. ConstraintInitializer knownBug {format 0}
  1124. # By default, non-portable tests are skipped.
  1125. ConstraintInitializer nonPortable {format 0}
  1126. # Some tests require user interaction.
  1127. ConstraintInitializer userInteraction {format 0}
  1128. # Some tests must be skipped if the interpreter is not in
  1129. # interactive mode
  1130. ConstraintInitializer interactive \
  1131. {expr {[info exists ::tcl_interactive] && $::tcl_interactive}}
  1132. # Some tests can only be run if the installation came from a CD
  1133. # image instead of a web image. Some tests must be skipped if you
  1134. # are running as root on Unix. Other tests can only be run if you
  1135. # are running as root on Unix.
  1136. ConstraintInitializer root {expr \
  1137. {($::tcl_platform(platform) eq "unix") &&
  1138. ($::tcl_platform(user) in {root {}})}}
  1139. ConstraintInitializer notRoot {expr {![testConstraint root]}}
  1140. # Set nonBlockFiles constraint: 1 means this platform supports
  1141. # setting files into nonblocking mode.
  1142. ConstraintInitializer nonBlockFiles {
  1143. set code [expr {[catch {set f [open defs r]}]
  1144. || [catch {fconfigure $f -blocking off}]}]
  1145. catch {close $f}
  1146. set code
  1147. }
  1148. # Set asyncPipeClose constraint: 1 means this platform supports
  1149. # async flush and async close on a pipe.
  1150. #
  1151. # Test for SCO Unix - cannot run async flushing tests because a
  1152. # potential problem with select is apparently interfering.
  1153. # (Mark Diekhans).
  1154. ConstraintInitializer asyncPipeClose {expr {
  1155. !([string equal unix $::tcl_platform(platform)]
  1156. && ([catch {exec uname -X | fgrep {Release = 3.2v}}] == 0))}}
  1157. # Test to see if we have a broken version of sprintf with respect
  1158. # to the "e" format of floating-point numbers.
  1159. ConstraintInitializer eformat {string equal [format %g 5e-5] 5e-05}
  1160. # Test to see if execed commands such as cat, echo, rm and so forth
  1161. # are present on this machine.
  1162. ConstraintInitializer unixExecs {
  1163. set code 1
  1164. if {$::tcl_platform(platform) eq "macintosh"} {
  1165. set code 0
  1166. }
  1167. if {$::tcl_platform(platform) eq "windows"} {
  1168. if {[catch {
  1169. set file _tcl_test_remove_me.txt
  1170. makeFile {hello} $file
  1171. }]} {
  1172. set code 0
  1173. } elseif {
  1174. [catch {exec cat $file}] ||
  1175. [catch {exec echo hello}] ||
  1176. [catch {exec sh -c echo hello}] ||
  1177. [catch {exec wc $file}] ||
  1178. [catch {exec sleep 1}] ||
  1179. [catch {exec echo abc > $file}] ||
  1180. [catch {exec chmod 644 $file}] ||
  1181. [catch {exec rm $file}] ||
  1182. [llength [auto_execok mkdir]] == 0 ||
  1183. [llength [auto_execok fgrep]] == 0 ||
  1184. [llength [auto_execok grep]] == 0 ||
  1185. [llength [auto_execok ps]] == 0
  1186. } {
  1187. set code 0
  1188. }
  1189. removeFile $file
  1190. }
  1191. set code
  1192. }
  1193. ConstraintInitializer stdio {
  1194. set code 0
  1195. if {![catch {set f [open "|[list [interpreter]]" w]}]} {
  1196. if {[package vsatisfies [package provide Tcl] 8.7-]} {
  1197. fconfigure $f -encoding utf-8
  1198. }
  1199. if {![catch {puts $f exit}]} {
  1200. if {![catch {close $f}]} {
  1201. set code 1
  1202. }
  1203. }
  1204. }
  1205. set code
  1206. }
  1207. # Deliberately call socket with the wrong number of arguments. The
  1208. # error message you get will indicate whether sockets are available
  1209. # on this system.
  1210. ConstraintInitializer socket {
  1211. catch {socket} msg
  1212. string compare $msg "sockets are not available on this system"
  1213. }
  1214. # Check for internationalization
  1215. ConstraintInitializer hasIsoLocale {
  1216. if {[llength [info commands testlocale]] == 0} {
  1217. set code 0
  1218. } else {
  1219. set code [string length [SetIso8859_1_Locale]]
  1220. RestoreLocale
  1221. }
  1222. set code
  1223. }
  1224. }
  1225. #####################################################################
  1226. # Usage and command line arguments processing.
  1227. # tcltest::PrintUsageInfo
  1228. #
  1229. # Prints out the usage information for package tcltest. This can
  1230. # be customized with the redefinition of [PrintUsageInfoHook].
  1231. #
  1232. # Arguments:
  1233. # none
  1234. #
  1235. # Results:
  1236. # none
  1237. #
  1238. # Side Effects:
  1239. # none
  1240. proc tcltest::PrintUsageInfo {} {
  1241. puts [Usage]
  1242. PrintUsageInfoHook
  1243. }
  1244. proc tcltest::Usage { {option ""} } {
  1245. variable Usage
  1246. variable Verify
  1247. if {[llength [info level 0]] == 1} {
  1248. set msg "Usage: [file tail [info nameofexecutable]] script "
  1249. append msg "?-help? ?flag value? ... \n"
  1250. append msg "Available flags (and valid input values) are:"
  1251. set max 0
  1252. set allOpts [concat -help [Configure]]
  1253. foreach opt $allOpts {
  1254. set foo [Usage $opt]
  1255. lassign $foo x type($opt) usage($opt)
  1256. set line($opt) " $opt $type($opt) "
  1257. set length($opt) [string length $line($opt)]
  1258. if {$length($opt) > $max} {set max $length($opt)}
  1259. }
  1260. set rest [expr {72 - $max}]
  1261. foreach opt $allOpts {
  1262. append msg \n$line($opt)
  1263. append msg [string repeat " " [expr {$max - $length($opt)}]]
  1264. set u [string trim $usage($opt)]
  1265. catch {append u " (default: \[[Configure $opt]])"}
  1266. regsub -all {\s*\n\s*} $u " " u
  1267. while {[string length $u] > $rest} {
  1268. set break [string wordstart $u $rest]
  1269. if {$break == 0} {
  1270. set break [string wordend $u 0]
  1271. }
  1272. append msg [string range $u 0 [expr {$break - 1}]]
  1273. set u [string trim [string range $u $break end]]
  1274. append msg \n[string repeat " " $max]
  1275. }
  1276. append msg $u
  1277. }
  1278. return $msg\n
  1279. } elseif {$option eq "-help"} {
  1280. return [list -help "" "Display this usage information."]
  1281. } else {
  1282. set type [lindex [info args $Verify($option)] 0]
  1283. return [list $option $type $Usage($option)]
  1284. }
  1285. }
  1286. # tcltest::ProcessFlags --
  1287. #
  1288. # process command line arguments supplied in the flagArray - this
  1289. # is called by processCmdLineArgs. Modifies tcltest variables
  1290. # according to the content of the flagArray.
  1291. #
  1292. # Arguments:
  1293. # flagArray - array containing name/value pairs of flags
  1294. #
  1295. # Results:
  1296. # sets tcltest variables according to their values as defined by
  1297. # flagArray
  1298. #
  1299. # Side effects:
  1300. # None.
  1301. proc tcltest::ProcessFlags {flagArray} {
  1302. # Process -help first
  1303. if {"-help" in $flagArray} {
  1304. PrintUsageInfo
  1305. exit 1
  1306. }
  1307. if {[llength $flagArray] == 0} {
  1308. RemoveAutoConfigureTraces
  1309. } else {
  1310. set args $flagArray
  1311. while {[llength $args] > 1 && [catch {configure {*}$args} msg]} {
  1312. # Something went wrong parsing $args for tcltest options
  1313. # Check whether the problem is "unknown option"
  1314. if {[regexp {^unknown option (\S+):} $msg -> option]} {
  1315. # Could be this is an option the Hook knows about
  1316. set moreOptions [processCmdLineArgsAddFlagsHook]
  1317. if {$option ni $moreOptions} {
  1318. # Nope. Report the error, including additional options,
  1319. # but keep going
  1320. if {[llength $moreOptions]} {
  1321. append msg ", "
  1322. append msg [join [lrange $moreOptions 0 end-1] ", "]
  1323. append msg "or [lindex $moreOptions end]"
  1324. }
  1325. Warn $msg
  1326. }
  1327. } else {
  1328. # error is something other than "unknown option"
  1329. # notify user of the error; and exit
  1330. puts [errorChannel] $msg
  1331. exit 1
  1332. }
  1333. # To recover, find that unknown option and remove up to it.
  1334. # then retry
  1335. while {[lindex $args 0] ne $option} {
  1336. set args [lrange $args 2 end]
  1337. }
  1338. set args [lrange $args 2 end]
  1339. }
  1340. if {[llength $args] == 1} {
  1341. puts [errorChannel] \
  1342. "missing value for option [lindex $args 0]"
  1343. exit 1
  1344. }
  1345. }
  1346. # Call the hook
  1347. catch {
  1348. array set flag $flagArray
  1349. processCmdLineArgsHook [array get flag]
  1350. }
  1351. return
  1352. }
  1353. # tcltest::ProcessCmdLineArgs --
  1354. #
  1355. # This procedure must be run after constraint initialization is
  1356. # set up (by [DefineConstraintInitializers]) because some constraints
  1357. # can be overridden.
  1358. #
  1359. # Perform configuration according to the command-line options.
  1360. #
  1361. # Arguments:
  1362. # none
  1363. #
  1364. # Results:
  1365. # Sets the above-named variables in the tcltest namespace.
  1366. #
  1367. # Side Effects:
  1368. # None.
  1369. #
  1370. proc tcltest::ProcessCmdLineArgs {} {
  1371. variable originalEnv
  1372. variable testConstraints
  1373. # The "argv" var doesn't exist in some cases, so use {}.
  1374. if {![info exists ::argv]} {
  1375. ProcessFlags {}
  1376. } else {
  1377. ProcessFlags $::argv
  1378. }
  1379. # Spit out everything you know if we're at a debug level 2 or
  1380. # greater
  1381. DebugPuts 2 "Flags passed into tcltest:"
  1382. if {[info exists ::env(TCLTEST_OPTIONS)]} {
  1383. DebugPuts 2 \
  1384. " ::env(TCLTEST_OPTIONS): $::env(TCLTEST_OPTIONS)"
  1385. }
  1386. if {[info exists ::argv]} {
  1387. DebugPuts 2 " argv: $::argv"
  1388. }
  1389. DebugPuts 2 "tcltest::debug = [debug]"
  1390. DebugPuts 2 "tcltest::testsDirectory = [testsDirectory]"
  1391. DebugPuts 2 "tcltest::workingDirectory = [workingDirectory]"
  1392. DebugPuts 2 "tcltest::temporaryDirectory = [temporaryDirectory]"
  1393. DebugPuts 2 "tcltest::outputChannel = [outputChannel]"
  1394. DebugPuts 2 "tcltest::errorChannel = [errorChannel]"
  1395. DebugPuts 2 "Original environment (tcltest::originalEnv):"
  1396. DebugPArray 2 originalEnv
  1397. DebugPuts 2 "Constraints:"
  1398. DebugPArray 2 testConstraints
  1399. }
  1400. #####################################################################
  1401. # Code to run the tests goes here.
  1402. # tcltest::TestPuts --
  1403. #
  1404. # Used to redefine puts in test environment. Stores whatever goes
  1405. # out on stdout in tcltest::outData and stderr in errData before
  1406. # sending it on to the regular puts.
  1407. #
  1408. # Arguments:
  1409. # same as standard puts
  1410. #
  1411. # Results:
  1412. # none
  1413. #
  1414. # Side effects:
  1415. # Intercepts puts; data that would otherwise go to stdout, stderr,
  1416. # or file channels specified in outputChannel and errorChannel
  1417. # does not get sent to the normal puts function.
  1418. namespace eval tcltest::Replace {
  1419. namespace export puts
  1420. }
  1421. proc tcltest::Replace::puts {args} {
  1422. variable [namespace parent]::outData
  1423. variable [namespace parent]::errData
  1424. switch [llength $args] {
  1425. 1 {
  1426. # Only the string to be printed is specified
  1427. append outData [lindex $args 0]\n
  1428. return
  1429. # return [Puts [lindex $args 0]]
  1430. }
  1431. 2 {
  1432. # Either -nonewline or channelId has been specified
  1433. if {[lindex $args 0] eq "-nonewline"} {
  1434. append outData [lindex $args end]
  1435. return
  1436. # return [Puts -nonewline [lindex $args end]]
  1437. } else {
  1438. set channel [lindex $args 0]
  1439. set newline \n
  1440. }
  1441. }
  1442. 3 {
  1443. if {[lindex $args 0] eq "-nonewline"} {
  1444. # Both -nonewline and channelId are specified, unless
  1445. # it's an error. -nonewline is supposed to be argv[0].
  1446. set channel [lindex $args 1]
  1447. set newline ""
  1448. }
  1449. }
  1450. }
  1451. if {[info exists channel]} {
  1452. if {$channel in [list [[namespace parent]::outputChannel] stdout]} {
  1453. append outData [lindex $args end]$newline
  1454. return
  1455. } elseif {$channel in [list [[namespace parent]::errorChannel] stderr]} {
  1456. append errData [lindex $args end]$newline
  1457. return
  1458. }
  1459. }
  1460. # If we haven't returned by now, we don't know how to handle the
  1461. # input. Let puts handle it.
  1462. return [Puts {*}$args]
  1463. }
  1464. # tcltest::Eval --
  1465. #
  1466. # Evaluate the script in the test environment. If ignoreOutput is
  1467. # false, store data sent to stderr and stdout in outData and
  1468. # errData. Otherwise, ignore this output altogether.
  1469. #
  1470. # Arguments:
  1471. # script Script to evaluate
  1472. # ?ignoreOutput? Indicates whether or not to ignore output
  1473. # sent to stdout & stderr
  1474. #
  1475. # Results:
  1476. # result from running the script
  1477. #
  1478. # Side effects:
  1479. # Empties the contents of outData and errData before running a
  1480. # test if ignoreOutput is set to 0.
  1481. proc tcltest::Eval {script {ignoreOutput 1}} {
  1482. variable outData
  1483. variable errData
  1484. DebugPuts 3 "[lindex [info level 0] 0] called"
  1485. if {!$ignoreOutput} {
  1486. set outData {}
  1487. set errData {}
  1488. rename ::puts [namespace current]::Replace::Puts
  1489. namespace eval :: [list namespace import [namespace origin Replace::puts]]
  1490. namespace import Replace::puts
  1491. }
  1492. set result [uplevel 1 $script]
  1493. if {!$ignoreOutput} {
  1494. namespace forget puts
  1495. namespace eval :: namespace forget puts
  1496. rename [namespace current]::Replace::Puts ::puts
  1497. }
  1498. return $result
  1499. }
  1500. # tcltest::CompareStrings --
  1501. #
  1502. # compares the expected answer to the actual answer, depending on
  1503. # the mode provided. Mode determines whether a regexp, exact,
  1504. # glob or custom comparison is done.
  1505. #
  1506. # Arguments:
  1507. # actual - string containing the actual result
  1508. # expected - pattern to be matched against
  1509. # mode - type of comparison to be done
  1510. #
  1511. # Results:
  1512. # result of the match
  1513. #
  1514. # Side effects:
  1515. # None.
  1516. proc tcltest::CompareStrings {actual expected mode} {
  1517. variable CustomMatch
  1518. if {![info exists CustomMatch($mode)]} {
  1519. return -code error "No matching command registered for `-match $mode'"
  1520. }
  1521. set match [namespace eval :: $CustomMatch($mode) [list $expected $actual]]
  1522. if {[catch {expr {$match && $match}} result]} {
  1523. return -code error "Invalid result from `-match $mode' command: $result"
  1524. }
  1525. return $match
  1526. }
  1527. # tcltest::customMatch --
  1528. #
  1529. # registers a command to be called when a particular type of
  1530. # matching is required.
  1531. #
  1532. # Arguments:
  1533. # nickname - Keyword for the type of matching
  1534. # cmd - Incomplete command that implements that type of matching
  1535. # when completed with expected string and actual string
  1536. # and then evaluated.
  1537. #
  1538. # Results:
  1539. # None.
  1540. #
  1541. # Side effects:
  1542. # Sets the variable tcltest::CustomMatch
  1543. proc tcltest::customMatch {mode script} {
  1544. variable CustomMatch
  1545. if {![info complete $script]} {
  1546. return -code error \
  1547. "invalid customMatch script; can't evaluate after completion"
  1548. }
  1549. set CustomMatch($mode) $script
  1550. }
  1551. # tcltest::SubstArguments list
  1552. #
  1553. # This helper function takes in a list of words, then perform a
  1554. # substitution on the list as though each word in the list is a separate
  1555. # argument to the Tcl function. For example, if this function is
  1556. # invoked as:
  1557. #
  1558. # SubstArguments {$a {$a}}
  1559. #
  1560. # Then it is as though the function is invoked as:
  1561. #
  1562. # SubstArguments $a {$a}
  1563. #
  1564. # This code is adapted from Paul Duffin's function "SplitIntoWords".
  1565. # The original function can be found on:
  1566. #
  1567. # http://purl.org/thecliff/tcl/wiki/858.html
  1568. #
  1569. # Results:
  1570. # a list containing the result of the substitution
  1571. #
  1572. # Exceptions:
  1573. # An error may occur if the list containing unbalanced quote or
  1574. # unknown variable.
  1575. #
  1576. # Side Effects:
  1577. # None.
  1578. #
  1579. proc tcltest::SubstArguments {argList} {
  1580. # We need to split the argList up into tokens but cannot use list
  1581. # operations as they throw away some significant quoting, and
  1582. # [split] ignores braces as it should. Therefore what we do is
  1583. # gradually build up a string out of whitespace seperated strings.
  1584. # We cannot use [split] to split the argList into whitespace
  1585. # separated strings as it throws away the whitespace which maybe
  1586. # important so we have to do it all by hand.
  1587. set result {}
  1588. set token ""
  1589. while {[string length $argList]} {
  1590. # Look for the next word containing a quote: " { }
  1591. if {[regexp -indices {[^ \t\n]*[\"\{\}]+[^ \t\n]*} \
  1592. $argList all]} {
  1593. # Get the text leading up to this word, but not including
  1594. # this word, from the argList.
  1595. set text [string range $argList 0 \
  1596. [expr {[lindex $all 0] - 1}]]
  1597. # Get the word with the quote
  1598. set word [string range $argList \
  1599. [lindex $all 0] [lindex $all 1]]
  1600. # Remove all text up to and including the word from the
  1601. # argList.
  1602. set argList [string range $argList \
  1603. [expr {[lindex $all 1] + 1}] end]
  1604. } else {
  1605. # Take everything up to the end of the argList.
  1606. set text $argList
  1607. set word {}
  1608. set argList {}
  1609. }
  1610. if {$token ne {}} {
  1611. # If we saw a word with quote before, then there is a
  1612. # multi-word token starting with that word. In this case,
  1613. # add the text and the current word to this token.
  1614. append token $text $word
  1615. } else {
  1616. # Add the text to the result. There is no need to parse
  1617. # the text because it couldn't be a part of any multi-word
  1618. # token. Then start a new multi-word token with the word
  1619. # because we need to pass this token to the Tcl parser to
  1620. # check for balancing quotes
  1621. append result $text
  1622. set token $word
  1623. }
  1624. if { [catch {llength $token} length] == 0 && $length == 1} {
  1625. # The token is a valid list so add it to the result.
  1626. # lappend result [string trim $token]
  1627. append result \{$token\}
  1628. set token {}
  1629. }
  1630. }
  1631. # If the last token has not been added to the list then there
  1632. # is a problem.
  1633. if { [string length $token] } {
  1634. error "incomplete token \"$token\""
  1635. }
  1636. return $result
  1637. }
  1638. # tcltest::test --
  1639. #
  1640. # This procedure runs a test and prints an error message if the test
  1641. # fails. If verbose has been set, it also prints a message even if the
  1642. # test succeeds. The test will be skipped if it doesn't match the
  1643. # match variable, if it matches an element in skip, or if one of the
  1644. # elements of "constraints" turns out not to be true.
  1645. #
  1646. # If testLevel is 1, then this is a top level test, and we record
  1647. # pass/fail information; otherwise, this information is not logged and
  1648. # is not added to running totals.
  1649. #
  1650. # Attributes:
  1651. # Only description is a required attribute. All others are optional.
  1652. # Default values are indicated.
  1653. #
  1654. # constraints - A list of one or more keywords, each of which
  1655. # must be the name of an element in the array
  1656. # "testConstraints". If any of these elements is
  1657. # zero, the test is skipped. This attribute is
  1658. # optional; default is {}
  1659. # body - Script to run to carry out the test. It must
  1660. # return a result that can be checked for
  1661. # correctness. This attribute is optional;
  1662. # default is {}
  1663. # result - Expected result from script. This attribute is
  1664. # optional; default is {}.
  1665. # output - Expected output sent to stdout. This attribute
  1666. # is optional; default is {}.
  1667. # errorOutput - Expected output sent to stderr. This attribute
  1668. # is optional; default is {}.
  1669. # returnCodes - Expected return codes. This attribute is
  1670. # optional; default is {0 2}.
  1671. # errorCode - Expected error code. This attribute is
  1672. # optional; default is {*}. It is a glob pattern.
  1673. # If given, returnCodes defaults to {1}.
  1674. # setup - Code to run before $script (above). This
  1675. # attribute is optional; default is {}.
  1676. # cleanup - Code to run after $script (above). This
  1677. # attribute is optional; default is {}.
  1678. # match - specifies type of matching to do on result,
  1679. # output, errorOutput; this must be a string
  1680. # previously registered by a call to [customMatch].
  1681. # The strings exact, glob, and regexp are pre-registered
  1682. # by the tcltest package. Default value is exact.
  1683. #
  1684. # Arguments:
  1685. # name - Name of test, in the form foo-1.2.
  1686. # description - Short textual description of the test, to
  1687. # help humans understand what it does.
  1688. #
  1689. # Results:
  1690. # None.
  1691. #
  1692. # Side effects:
  1693. # Just about anything is possible depending on the test.
  1694. #
  1695. proc tcltest::test {name description args} {
  1696. global tcl_platform
  1697. variable testLevel
  1698. variable coreModTime
  1699. DebugPuts 3 "test $name $args"
  1700. DebugDo 1 {
  1701. variable TestNames
  1702. catch {
  1703. puts "test name '$name' re-used; prior use in $TestNames($name)"
  1704. }
  1705. set TestNames($name) [info script]
  1706. }
  1707. FillFilesExisted
  1708. incr testLevel
  1709. # Pre-define everything to null except output and errorOutput. We
  1710. # determine whether or not to trap output based on whether or not
  1711. # these variables (output & errorOutput) are defined.
  1712. lassign {} constraints setup cleanup body result returnCodes errorCode match
  1713. # Set the default match mode
  1714. set match exact
  1715. # Set the default match values for return codes (0 is the standard
  1716. # expected return value if everything went well; 2 represents
  1717. # 'return' being used in the test script).
  1718. set returnCodes [list 0 2]
  1719. # Set the default error code pattern
  1720. set errorCode "*"
  1721. # The old test format can't have a 3rd argument (constraints or
  1722. # script) that starts with '-'.
  1723. if {[string match -* [lindex $args 0]] || ([llength $args] <= 1)} {
  1724. if {[llength $args] == 1} {
  1725. set list [SubstArguments [lindex $args 0]]
  1726. foreach {element value} $list {
  1727. set testAttributes($element) $value
  1728. }
  1729. foreach item {constraints match setup body cleanup \
  1730. result returnCodes errorCode output errorOutput} {
  1731. if {[info exists testAttributes(-$item)]} {
  1732. set testAttributes(-$item) [uplevel 1 \
  1733. ::concat $testAttributes(-$item)]
  1734. }
  1735. }
  1736. } else {
  1737. array set testAttributes $args
  1738. }
  1739. set validFlags {-setup -cleanup -body -result -returnCodes \
  1740. -errorCode -match -output -errorOutput -constraints}
  1741. foreach flag [array names testAttributes] {
  1742. if {$flag ni $validFlags} {
  1743. incr testLevel -1
  1744. set sorted [lsort $validFlags]
  1745. set options [join [lrange $sorted 0 end-1] ", "]
  1746. append options ", or [lindex $sorted end]"
  1747. return -code error "bad option \"$flag\": must be $options"
  1748. }
  1749. }
  1750. # store whatever the user gave us
  1751. foreach item [array names testAttributes] {
  1752. set [string trimleft $item "-"] $testAttributes($item)
  1753. }
  1754. # Check the values supplied for -match
  1755. variable CustomMatch
  1756. if {$match ni [array names CustomMatch]} {
  1757. incr testLevel -1
  1758. set sorted [lsort [array names CustomMatch]]
  1759. set values [join [lrange $sorted 0 end-1] ", "]
  1760. append values ", or [lindex $sorted end]"
  1761. return -code error "bad -match value \"$match\":\
  1762. must be $values"
  1763. }
  1764. # Replace symbolic valies supplied for -returnCodes
  1765. foreach {strcode numcode} {ok 0 normal 0 error 1 return 2 break 3 continue 4} {
  1766. set returnCodes [string map -nocase [list $strcode $numcode] $returnCodes]
  1767. }
  1768. # errorCode without returnCode 1 is meaningless
  1769. if {$errorCode ne "*" && 1 ni $returnCodes} {
  1770. set returnCodes 1
  1771. }
  1772. } else {
  1773. # This is parsing for the old test command format; it is here
  1774. # for backward compatibility.
  1775. set result [lindex $args end]
  1776. if {[llength $args] == 2} {
  1777. set body [lindex $args 0]
  1778. } elseif {[llength $args] == 3} {
  1779. set constraints [lindex $args 0]
  1780. set body [lindex $args 1]
  1781. } else {
  1782. incr testLevel -1
  1783. return -code error "wrong # args:\
  1784. should be \"test name desc ?options?\""
  1785. }
  1786. }
  1787. if {[Skipped $name $constraints]} {
  1788. incr testLevel -1
  1789. return
  1790. }
  1791. # Save information about the core file.
  1792. if {[preserveCore]} {
  1793. if {[file exists [file join [workingDirectory] core]]} {
  1794. set coreModTime [file mtime [file join [workingDirectory] core]]
  1795. }
  1796. }
  1797. # First, run the setup script (or a hook if it presents):
  1798. if {[set cmd [namespace which -command [namespace current]::SetupTest]] ne ""} {
  1799. set setup [list $cmd $setup]
  1800. }
  1801. set processTest 1
  1802. set code [catch {uplevel 1 $setup} setupMsg]
  1803. if {$code == 1} {
  1804. set errorInfo(setup) $::errorInfo
  1805. set errorCodeRes(setup) $::errorCode
  1806. if {$errorCodeRes(setup) eq "BYPASS-SKIPPED-TEST"} {
  1807. _noticeSkipped $name $setupMsg
  1808. set processTest [set code 0]
  1809. }
  1810. }
  1811. set setupFailure [expr {$code != 0}]
  1812. # Only run the test body if the setup was successful
  1813. if {$processTest && !$setupFailure} {
  1814. # Register startup time
  1815. if {[IsVerbose msec] || [IsVerbose usec]} {
  1816. set timeStart [clock microseconds]
  1817. }
  1818. # Verbose notification of $body start
  1819. if {[IsVerbose start]} {
  1820. puts [outputChannel] "---- $name start"
  1821. flush [outputChannel]
  1822. }
  1823. set command [list [namespace origin RunTest] $name $body]
  1824. if {[info exists output] || [info exists errorOutput]} {
  1825. set testResult [uplevel 1 [list [namespace origin Eval] $command 0]]
  1826. } else {
  1827. set testResult [uplevel 1 [list [namespace origin Eval] $command 1]]
  1828. }
  1829. lassign $testResult actualAnswer returnCode
  1830. if {$returnCode == 1} {
  1831. set errorInfo(body) $::errorInfo
  1832. set errorCodeRes(body) $::errorCode
  1833. if {$errorCodeRes(body) eq "BYPASS-SKIPPED-TEST"} {
  1834. _noticeSkipped $name $actualAnswer
  1835. set processTest [set returnCode 0]
  1836. }
  1837. }
  1838. }
  1839. # check if the return code matched the expected return code
  1840. set codeFailure 0
  1841. if {$processTest && !$setupFailure && ($returnCode ni $returnCodes)} {
  1842. set codeFailure 1
  1843. }
  1844. set errorCodeFailure 0
  1845. if {$processTest && !$setupFailure && !$codeFailure && $returnCode == 1 && \
  1846. ![string match $errorCode $errorCodeRes(body)]} {
  1847. set errorCodeFailure 1
  1848. }
  1849. # If expected output/error strings exist, we have to compare
  1850. # them. If the comparison fails, then so did the test.
  1851. set outputFailure 0
  1852. variable outData
  1853. if {$processTest && [info exists output] && !$codeFailure} {
  1854. if {[set outputCompare [catch {
  1855. CompareStrings $outData $output $match
  1856. } outputMatch]] == 0} {
  1857. set outputFailure [expr {!$outputMatch}]
  1858. } else {
  1859. set outputFailure 1
  1860. }
  1861. }
  1862. set errorFailure 0
  1863. variable errData
  1864. if {$processTest && [info exists errorOutput] && !$codeFailure} {
  1865. if {[set errorCompare [catch {
  1866. CompareStrings $errData $errorOutput $match
  1867. } errorMatch]] == 0} {
  1868. set errorFailure [expr {!$errorMatch}]
  1869. } else {
  1870. set errorFailure 1
  1871. }
  1872. }
  1873. # check if the answer matched the expected answer
  1874. # Only check if we ran the body of the test (no setup failure)
  1875. if {!$processTest} {
  1876. set scriptFailure 0
  1877. } elseif {$setupFailure || $codeFailure} {
  1878. set scriptFailure 0
  1879. } elseif {[set scriptCompare [catch {
  1880. CompareStrings $actualAnswer $result $match
  1881. } scriptMatch]] == 0} {
  1882. set scriptFailure [expr {!$scriptMatch}]
  1883. } else {
  1884. set scriptFailure 1
  1885. }
  1886. # Always run the cleanup script (or a hook if it presents):
  1887. if {[set cmd [namespace which -command [namespace current]::CleanupTest]] ne ""} {
  1888. set cleanup [list $cmd $cleanup]
  1889. }
  1890. set code [catch {uplevel 1 $cleanup} cleanupMsg]
  1891. if {$code == 1} {
  1892. set errorInfo(cleanup) $::errorInfo
  1893. set errorCodeRes(cleanup) $::errorCode
  1894. }
  1895. set cleanupFailure [expr {$code != 0}]
  1896. set coreFailure 0
  1897. set coreMsg ""
  1898. # check for a core file first - if one was created by the test,
  1899. # then the test failed
  1900. if {[preserveCore]} {
  1901. if {[file exists [file join [workingDirectory] core]]} {
  1902. # There's only a test failure if there is a core file
  1903. # and (1) there previously wasn't one or (2) the new
  1904. # one is different from the old one.
  1905. if {[info exists coreModTime]} {
  1906. if {$coreModTime != [file mtime \
  1907. [file join [workingDirectory] core]]} {
  1908. set coreFailure 1
  1909. }
  1910. } else {
  1911. set coreFailure 1
  1912. }
  1913. if {([preserveCore] > 1) && ($coreFailure)} {
  1914. append coreMsg "\nMoving file to:\
  1915. [file join [temporaryDirectory] core-$name]"
  1916. catch {file rename -force -- \
  1917. [file join [workingDirectory] core] \
  1918. [file join [temporaryDirectory] core-$name]
  1919. } msg
  1920. if {$msg ne {}} {
  1921. append coreMsg "\nError:\
  1922. Problem renaming core file: $msg"
  1923. }
  1924. }
  1925. }
  1926. }
  1927. if {[IsVerbose msec] || [IsVerbose usec]} {
  1928. set t [expr {[clock microseconds] - $timeStart}]
  1929. if {[IsVerbose usec]} {
  1930. puts [outputChannel] "++++ $name took $t \xB5s"
  1931. }
  1932. if {[IsVerbose msec]} {
  1933. puts [outputChannel] "++++ $name took [expr {round($t/1000.)}] ms"
  1934. }
  1935. }
  1936. # if skipped, it is safe to return here
  1937. if {!$processTest} {
  1938. incr testLevel -1
  1939. return
  1940. }
  1941. # if we didn't experience any failures, then we passed
  1942. variable numTests
  1943. if {!($setupFailure || $cleanupFailure || $coreFailure
  1944. || $outputFailure || $errorFailure || $codeFailure
  1945. || $errorCodeFailure || $scriptFailure)} {
  1946. if {$testLevel == 1} {
  1947. incr numTests(Passed)
  1948. if {[IsVerbose pass]} {
  1949. puts [outputChannel] "++++ $name PASSED"
  1950. }
  1951. }
  1952. incr testLevel -1
  1953. return
  1954. }
  1955. # We know the test failed, tally it...
  1956. if {$testLevel == 1} {
  1957. incr numTests(Failed)
  1958. }
  1959. # ... then report according to the type of failure
  1960. variable currentFailure true
  1961. if {![IsVerbose body]} {
  1962. set body ""
  1963. }
  1964. puts [outputChannel] "\n"
  1965. if {[IsVerbose line]} {
  1966. if {![catch {set testFrame [info frame -1]}] &&
  1967. [dict get $testFrame type] eq "source"} {
  1968. set testFile [dict get $testFrame file]
  1969. set testLine [dict get $testFrame line]
  1970. } else {
  1971. set testFile [file normalize [uplevel 1 {info script}]]
  1972. if {[file readable $testFile]} {
  1973. set testFd [open $testFile r]
  1974. if {[package vsatisfies [package provide Tcl] 8.7-]} {
  1975. fconfigure $testFd -encoding utf-8
  1976. }
  1977. set testLine [expr {[lsearch -regexp \
  1978. [split [read $testFd] "\n"] \
  1979. "^\[ \t\]*test [string map {. \\.} $name] "] + 1}]
  1980. close $testFd
  1981. }
  1982. }
  1983. if {[info exists testLine]} {
  1984. puts [outputChannel] "$testFile:$testLine: error: test failed:\
  1985. $name [string trim $description]"
  1986. }
  1987. }
  1988. puts [outputChannel] "==== $name\
  1989. [string trim $description] FAILED"
  1990. if {[string length $body]} {
  1991. puts [outputChannel] "==== Contents of test case:"
  1992. puts [outputChannel] $body
  1993. }
  1994. if {$setupFailure} {
  1995. puts [outputChannel] "---- Test setup\
  1996. failed:\n$setupMsg"
  1997. if {[info exists errorInfo(setup)]} {
  1998. puts [outputChannel] "---- errorInfo(setup): $errorInfo(setup)"
  1999. puts [outputChannel] "---- errorCode(setup): $errorCodeRes(setup)"
  2000. }
  2001. }
  2002. if {$processTest && $scriptFailure} {
  2003. if {$scriptCompare} {
  2004. puts [outputChannel] "---- Error testing result: $scriptMatch"
  2005. } else {
  2006. puts [outputChannel] "---- Result was:\n$actualAnswer"
  2007. puts [outputChannel] "---- Result should have been\
  2008. ($match matching):\n$result"
  2009. }
  2010. }
  2011. if {$errorCodeFailure} {
  2012. puts [outputChannel] "---- Error code was: '$errorCodeRes(body)'"
  2013. puts [outputChannel] "---- Error code should have been: '$errorCode'"
  2014. }
  2015. if {$codeFailure} {
  2016. switch -- $returnCode {
  2017. 0 { set msg "Test completed normally" }
  2018. 1 { set msg "Test generated error" }
  2019. 2 { set msg "Test generated return exception" }
  2020. 3 { set msg "Test generated break exception" }
  2021. 4 { set msg "Test generated continue exception" }
  2022. default { set msg "Test generated exception" }
  2023. }
  2024. puts [outputChannel] "---- $msg; Return code was: $returnCode"
  2025. puts [outputChannel] "---- Return code should have been\
  2026. one of: $returnCodes"
  2027. if {[IsVerbose error]} {
  2028. if {[info exists errorInfo(body)] && (1 ni $returnCodes)} {
  2029. puts [outputChannel] "---- errorInfo: $errorInfo(body)"
  2030. puts [outputChannel] "---- errorCode: $errorCodeRes(body)"
  2031. }
  2032. }
  2033. }
  2034. if {$outputFailure} {
  2035. if {$outputCompare} {
  2036. puts [outputChannel] "---- Error testing output: $outputMatch"
  2037. } else {
  2038. puts [outputChannel] "---- Output was:\n$outData"
  2039. puts [outputChannel] "---- Output should have been\
  2040. ($match matching):\n$output"
  2041. }
  2042. }
  2043. if {$errorFailure} {
  2044. if {$errorCompare} {
  2045. puts [outputChannel] "---- Error testing errorOutput: $errorMatch"
  2046. } else {
  2047. puts [outputChannel] "---- Error output was:\n$errData"
  2048. puts [outputChannel] "---- Error output should have\
  2049. been ($match matching):\n$errorOutput"
  2050. }
  2051. }
  2052. if {$cleanupFailure} {
  2053. puts [outputChannel] "---- Test cleanup failed:\n$cleanupMsg"
  2054. if {[info exists errorInfo(cleanup)]} {
  2055. puts [outputChannel] "---- errorInfo(cleanup): $errorInfo(cleanup)"
  2056. puts [outputChannel] "---- errorCode(cleanup): $errorCodeRes(cleanup)"
  2057. }
  2058. }
  2059. if {$coreFailure} {
  2060. puts [outputChannel] "---- Core file produced while running\
  2061. test! $coreMsg"
  2062. }
  2063. puts [outputChannel] "==== $name FAILED\n"
  2064. incr testLevel -1
  2065. return
  2066. }
  2067. # Skip --
  2068. #
  2069. # Skips a running test and add a reason to skipped "constraints". Can be used
  2070. # to conditional intended abort of the test.
  2071. #
  2072. # Side Effects: Maintains tally of total tests seen and tests skipped.
  2073. #
  2074. proc tcltest::Skip {reason} {
  2075. return -code error -errorcode BYPASS-SKIPPED-TEST $reason
  2076. }
  2077. proc tcltest::_noticeSkipped {name reason} {
  2078. variable testLevel
  2079. variable numTests
  2080. if {[IsVerbose skip]} {
  2081. puts [outputChannel] "++++ $name SKIPPED: $reason"
  2082. }
  2083. if {$testLevel == 1} {
  2084. incr numTests(Skipped)
  2085. AddToSkippedBecause $reason
  2086. }
  2087. }
  2088. # Skipped --
  2089. #
  2090. # Given a test name and it constraints, returns a boolean indicating
  2091. # whether the current configuration says the test should be skipped.
  2092. #
  2093. # Side Effects: Maintains tally of total tests seen and tests skipped.
  2094. #
  2095. proc tcltest::Skipped {name constraints} {
  2096. variable testLevel
  2097. variable numTests
  2098. variable testConstraints
  2099. if {$testLevel == 1} {
  2100. incr numTests(Total)
  2101. }
  2102. # skip the test if it's name matches an element of skip
  2103. foreach pattern [skip] {
  2104. if {[string match $pattern $name]} {
  2105. if {$testLevel == 1} {
  2106. incr numTests(Skipped)
  2107. DebugDo 1 {AddToSkippedBecause userSpecifiedSkip}
  2108. }
  2109. return 1
  2110. }
  2111. }
  2112. # skip the test if it's name doesn't match any element of match
  2113. set ok 0
  2114. foreach pattern [match] {
  2115. if {[string match $pattern $name]} {
  2116. set ok 1
  2117. break
  2118. }
  2119. }
  2120. if {!$ok} {
  2121. if {$testLevel == 1} {
  2122. incr numTests(Skipped)
  2123. DebugDo 1 {AddToSkippedBecause userSpecifiedNonMatch}
  2124. }
  2125. return 1
  2126. }
  2127. if {$constraints eq {}} {
  2128. # If we're limited to the listed constraints and there aren't
  2129. # any listed, then we shouldn't run the test.
  2130. if {[limitConstraints]} {
  2131. AddToSkippedBecause userSpecifiedLimitConstraint
  2132. if {$testLevel == 1} {
  2133. incr numTests(Skipped)
  2134. }
  2135. return 1
  2136. }
  2137. } else {
  2138. # "constraints" argument exists;
  2139. # make sure that the constraints are satisfied.
  2140. set doTest 0
  2141. if {[string match {*[$\[]*} $constraints] != 0} {
  2142. # full expression, e.g. {$foo > [info tclversion]}
  2143. catch {set doTest [uplevel #0 [list expr $constraints]]}
  2144. } elseif {[regexp {[^.:_a-zA-Z0-9 \n\r\t]+} $constraints] != 0} {
  2145. # something like {a || b} should be turned into
  2146. # $testConstraints(a) || $testConstraints(b).
  2147. regsub -all {[.\w]+} $constraints {$testConstraints(&)} c
  2148. catch {set doTest [eval [list expr $c]]}
  2149. } elseif {![catch {llength $constraints}]} {
  2150. # just simple constraints such as {unixOnly fonts}.
  2151. set doTest 1
  2152. foreach constraint $constraints {
  2153. if {(![info exists testConstraints($constraint)]) \
  2154. || (!$testConstraints($constraint))} {
  2155. set doTest 0
  2156. # store the constraint that kept the test from
  2157. # running
  2158. set constraints $constraint
  2159. break
  2160. }
  2161. }
  2162. }
  2163. if {!$doTest} {
  2164. _noticeSkipped $name $constraints
  2165. return 1
  2166. }
  2167. }
  2168. return 0
  2169. }
  2170. # RunTest --
  2171. #
  2172. # This is where the body of a test is evaluated. The combination of
  2173. # [RunTest] and [Eval] allows the output and error output of the test
  2174. # body to be captured for comparison against the expected values.
  2175. proc tcltest::RunTest {name script} {
  2176. DebugPuts 3 "Running $name {$script}"
  2177. # If there is no "memory" command (because memory debugging isn't
  2178. # enabled), then don't attempt to use the command.
  2179. if {[llength [info commands memory]] == 1} {
  2180. memory tag $name
  2181. }
  2182. # run the test script (or a hook if it presents):
  2183. if {[set cmd [namespace which -command [namespace current]::EvalTest]] ne ""} {
  2184. set script [list $cmd $script]
  2185. }
  2186. set code [catch {uplevel 1 $script} actualAnswer]
  2187. return [list $actualAnswer $code]
  2188. }
  2189. #####################################################################
  2190. # tcltest::cleanupTestsHook --
  2191. #
  2192. # This hook allows a harness that builds upon tcltest to specify
  2193. # additional things that should be done at cleanup.
  2194. #
  2195. if {[llength [info commands tcltest::cleanupTestsHook]] == 0} {
  2196. proc tcltest::cleanupTestsHook {} {}
  2197. }
  2198. # tcltest::cleanupTests --
  2199. #
  2200. # Remove files and dirs created using the makeFile and makeDirectory
  2201. # commands since the last time this proc was invoked.
  2202. #
  2203. # Print the names of the files created without the makeFile command
  2204. # since the tests were invoked.
  2205. #
  2206. # Print the number tests (total, passed, failed, and skipped) since the
  2207. # tests were invoked.
  2208. #
  2209. # Restore original environment (as reported by special variable env).
  2210. #
  2211. # Arguments:
  2212. # calledFromAllFile - if 0, behave as if we are running a single
  2213. # test file within an entire suite of tests. if we aren't running
  2214. # a single test file, then don't report status. check for new
  2215. # files created during the test run and report on them. if 1,
  2216. # report collated status from all the test file runs.
  2217. #
  2218. # Results:
  2219. # None.
  2220. #
  2221. # Side Effects:
  2222. # None
  2223. #
  2224. proc tcltest::cleanupTests {{calledFromAllFile 0}} {
  2225. variable filesMade
  2226. variable filesExisted
  2227. variable createdNewFiles
  2228. variable testSingleFile
  2229. variable numTests
  2230. variable numTestFiles
  2231. variable failFiles
  2232. variable skippedBecause
  2233. variable currentFailure
  2234. variable originalEnv
  2235. variable originalTclPlatform
  2236. variable coreModTime
  2237. FillFilesExisted
  2238. set testFileName [file tail [info script]]
  2239. # Hook to handle reporting to a parent interpreter
  2240. if {[llength [info commands [namespace current]::ReportToParent]]} {
  2241. ReportToParent $numTests(Total) $numTests(Passed) $numTests(Skipped) \
  2242. $numTests(Failed) [array get skippedBecause] \
  2243. [array get createdNewFiles]
  2244. set testSingleFile false
  2245. }
  2246. # Call the cleanup hook
  2247. cleanupTestsHook
  2248. # Remove files and directories created by the makeFile and
  2249. # makeDirectory procedures. Record the names of files in
  2250. # workingDirectory that were not pre-existing, and associate them
  2251. # with the test file that created them.
  2252. if {!$calledFromAllFile} {
  2253. foreach file $filesMade {
  2254. if {[file exists $file]} {
  2255. DebugDo 1 {Warn "cleanupTests deleting $file..."}
  2256. catch {file delete -force -- $file}
  2257. }
  2258. }
  2259. set currentFiles {}
  2260. foreach file [glob -nocomplain \
  2261. -directory [temporaryDirectory] *] {
  2262. lappend currentFiles [file tail $file]
  2263. }
  2264. set newFiles {}
  2265. foreach file $currentFiles {
  2266. if {$file ni $filesExisted} {
  2267. lappend newFiles $file
  2268. }
  2269. }
  2270. set filesExisted $currentFiles
  2271. if {[llength $newFiles] > 0} {
  2272. set createdNewFiles($testFileName) $newFiles
  2273. }
  2274. }
  2275. if {$calledFromAllFile || $testSingleFile} {
  2276. # print stats
  2277. puts -nonewline [outputChannel] "$testFileName:"
  2278. foreach index [list "Total" "Passed" "Skipped" "Failed"] {
  2279. puts -nonewline [outputChannel] \
  2280. "\t$index\t$numTests($index)"
  2281. }
  2282. puts [outputChannel] ""
  2283. # print number test files sourced
  2284. # print names of files that ran tests which failed
  2285. if {$calledFromAllFile} {
  2286. puts [outputChannel] \
  2287. "Sourced $numTestFiles Test Files."
  2288. set numTestFiles 0
  2289. if {[llength $failFiles] > 0} {
  2290. puts [outputChannel] \
  2291. "Files with failing tests: $failFiles"
  2292. set failFiles {}
  2293. }
  2294. }
  2295. # if any tests were skipped, print the constraints that kept
  2296. # them from running.
  2297. set constraintList [array names skippedBecause]
  2298. if {[llength $constraintList] > 0} {
  2299. puts [outputChannel] \
  2300. "Number of tests skipped for each constraint:"
  2301. foreach constraint [lsort $constraintList] {
  2302. puts [outputChannel] \
  2303. "\t$skippedBecause($constraint)\t$constraint"
  2304. unset skippedBecause($constraint)
  2305. }
  2306. }
  2307. # report the names of test files in createdNewFiles, and reset
  2308. # the array to be empty.
  2309. set testFilesThatTurded [lsort [array names createdNewFiles]]
  2310. if {[llength $testFilesThatTurded] > 0} {
  2311. puts [outputChannel] "Warning: files left behind:"
  2312. foreach testFile $testFilesThatTurded {
  2313. puts [outputChannel] \
  2314. "\t$testFile:\t$createdNewFiles($testFile)"
  2315. unset createdNewFiles($testFile)
  2316. }
  2317. }
  2318. # reset filesMade, filesExisted, and numTests
  2319. set filesMade {}
  2320. foreach index [list "Total" "Passed" "Skipped" "Failed"] {
  2321. set numTests($index) 0
  2322. }
  2323. # exit only if running Tk in non-interactive mode
  2324. # This should be changed to determine if an event
  2325. # loop is running, which is the real issue.
  2326. # Actually, this doesn't belong here at all. A package
  2327. # really has no business [exit]-ing an application.
  2328. if {![catch {package present Tk}] && ![testConstraint interactive]} {
  2329. exit
  2330. }
  2331. } else {
  2332. # if we're deferring stat-reporting until all files are sourced,
  2333. # then add current file to failFile list if any tests in this
  2334. # file failed
  2335. if {$currentFailure && ($testFileName ni $failFiles)} {
  2336. lappend failFiles $testFileName
  2337. }
  2338. set currentFailure false
  2339. # restore the environment to the state it was in before this package
  2340. # was loaded
  2341. set newEnv {}
  2342. set changedEnv {}
  2343. set removedEnv {}
  2344. foreach index [array names ::env] {
  2345. if {![info exists originalEnv($index)]} {
  2346. lappend newEnv $index
  2347. unset ::env($index)
  2348. }
  2349. }
  2350. foreach index [array names originalEnv] {
  2351. if {![info exists ::env($index)]} {
  2352. lappend removedEnv $index
  2353. set ::env($index) $originalEnv($index)
  2354. } elseif {$::env($index) ne $originalEnv($index)} {
  2355. lappend changedEnv $index
  2356. set ::env($index) $originalEnv($index)
  2357. }
  2358. }
  2359. if {[llength $newEnv] > 0} {
  2360. puts [outputChannel] \
  2361. "env array elements created:\t$newEnv"
  2362. }
  2363. if {[llength $changedEnv] > 0} {
  2364. puts [outputChannel] \
  2365. "env array elements changed:\t$changedEnv"
  2366. }
  2367. if {[llength $removedEnv] > 0} {
  2368. puts [outputChannel] \
  2369. "env array elements removed:\t$removedEnv"
  2370. }
  2371. set changedTclPlatform {}
  2372. foreach index [array names originalTclPlatform] {
  2373. if {$::tcl_platform($index) \
  2374. != $originalTclPlatform($index)} {
  2375. lappend changedTclPlatform $index
  2376. set ::tcl_platform($index) $originalTclPlatform($index)
  2377. }
  2378. }
  2379. if {[llength $changedTclPlatform] > 0} {
  2380. puts [outputChannel] "tcl_platform array elements\
  2381. changed:\t$changedTclPlatform"
  2382. }
  2383. if {[file exists [file join [workingDirectory] core]]} {
  2384. if {[preserveCore] > 1} {
  2385. puts "rename core file (> 1)"
  2386. puts [outputChannel] "produced core file! \
  2387. Moving file to: \
  2388. [file join [temporaryDirectory] core-$testFileName]"
  2389. catch {file rename -force -- \
  2390. [file join [workingDirectory] core] \
  2391. [file join [temporaryDirectory] core-$testFileName]
  2392. } msg
  2393. if {$msg ne {}} {
  2394. PrintError "Problem renaming file: $msg"
  2395. }
  2396. } else {
  2397. # Print a message if there is a core file and (1) there
  2398. # previously wasn't one or (2) the new one is different
  2399. # from the old one.
  2400. if {[info exists coreModTime]} {
  2401. if {$coreModTime != [file mtime \
  2402. [file join [workingDirectory] core]]} {
  2403. puts [outputChannel] "A core file was created!"
  2404. }
  2405. } else {
  2406. puts [outputChannel] "A core file was created!"
  2407. }
  2408. }
  2409. }
  2410. }
  2411. flush [outputChannel]
  2412. flush [errorChannel]
  2413. return
  2414. }
  2415. #####################################################################
  2416. # Procs that determine which tests/test files to run
  2417. # tcltest::GetMatchingFiles
  2418. #
  2419. # Looks at the patterns given to match and skip files and uses
  2420. # them to put together a list of the tests that will be run.
  2421. #
  2422. # Arguments:
  2423. # directory to search
  2424. #
  2425. # Results:
  2426. # The constructed list is returned to the user. This will
  2427. # primarily be used in 'all.tcl' files. It is used in
  2428. # runAllTests.
  2429. #
  2430. # Side Effects:
  2431. # None
  2432. # a lower case version is needed for compatibility with tcltest 1.0
  2433. proc tcltest::getMatchingFiles args {GetMatchingFiles {*}$args}
  2434. proc tcltest::GetMatchingFiles { args } {
  2435. if {[llength $args]} {
  2436. set dirList $args
  2437. } else {
  2438. # Finding tests only in [testsDirectory] is normal operation.
  2439. # This procedure is written to accept multiple directory arguments
  2440. # only to satisfy version 1 compatibility.
  2441. set dirList [list [testsDirectory]]
  2442. }
  2443. set matchingFiles [list]
  2444. foreach directory $dirList {
  2445. # List files in $directory that match patterns to run.
  2446. set matchFileList [list]
  2447. foreach match [matchFiles] {
  2448. set matchFileList [concat $matchFileList \
  2449. [glob -directory $directory -types {b c f p s} \
  2450. -nocomplain -- $match]]
  2451. }
  2452. # List files in $directory that match patterns to skip.
  2453. set skipFileList [list]
  2454. foreach skip [skipFiles] {
  2455. set skipFileList [concat $skipFileList \
  2456. [glob -directory $directory -types {b c f p s} \
  2457. -nocomplain -- $skip]]
  2458. }
  2459. # Add to result list all files in match list and not in skip list
  2460. foreach file $matchFileList {
  2461. if {$file ni $skipFileList} {
  2462. lappend matchingFiles $file
  2463. }
  2464. }
  2465. }
  2466. if {[llength $matchingFiles] == 0} {
  2467. PrintError "No test files remain after applying your match and\
  2468. skip patterns!"
  2469. }
  2470. return $matchingFiles
  2471. }
  2472. # tcltest::GetMatchingDirectories --
  2473. #
  2474. # Looks at the patterns given to match and skip directories and
  2475. # uses them to put together a list of the test directories that we
  2476. # should attempt to run. (Only subdirectories containing an
  2477. # "all.tcl" file are put into the list.)
  2478. #
  2479. # Arguments:
  2480. # root directory from which to search
  2481. #
  2482. # Results:
  2483. # The constructed list is returned to the user. This is used in
  2484. # the primary all.tcl file.
  2485. #
  2486. # Side Effects:
  2487. # None.
  2488. proc tcltest::GetMatchingDirectories {rootdir} {
  2489. # Determine the skip list first, to avoid [glob]-ing over subdirectories
  2490. # we're going to throw away anyway. Be sure we skip the $rootdir if it
  2491. # comes up to avoid infinite loops.
  2492. set skipDirs [list $rootdir]
  2493. foreach pattern [skipDirectories] {
  2494. set skipDirs [concat $skipDirs [glob -directory $rootdir -types d \
  2495. -nocomplain -- $pattern]]
  2496. }
  2497. # Now step through the matching directories, prune out the skipped ones
  2498. # as you go.
  2499. set matchDirs [list]
  2500. foreach pattern [matchDirectories] {
  2501. foreach path [glob -directory $rootdir -types d -nocomplain -- \
  2502. $pattern] {
  2503. if {$path ni $skipDirs} {
  2504. set matchDirs [concat $matchDirs [GetMatchingDirectories $path]]
  2505. if {[file exists [file join $path all.tcl]]} {
  2506. lappend matchDirs $path
  2507. }
  2508. }
  2509. }
  2510. }
  2511. if {[llength $matchDirs] == 0} {
  2512. DebugPuts 1 "No test directories remain after applying match\
  2513. and skip patterns!"
  2514. }
  2515. return [lsort $matchDirs]
  2516. }
  2517. # tcltest::runAllTests --
  2518. #
  2519. # prints output and sources test files according to the match and
  2520. # skip patterns provided. after sourcing test files, it goes on
  2521. # to source all.tcl files in matching test subdirectories.
  2522. #
  2523. # Arguments:
  2524. # shell being tested
  2525. #
  2526. # Results:
  2527. # Whether there were any failures.
  2528. #
  2529. # Side effects:
  2530. # None.
  2531. proc tcltest::runAllTests { {shell ""} } {
  2532. variable testSingleFile
  2533. variable numTestFiles
  2534. variable numTests
  2535. variable failFiles
  2536. variable DefaultValue
  2537. FillFilesExisted
  2538. if {[llength [info level 0]] == 1} {
  2539. set shell [interpreter]
  2540. }
  2541. set testSingleFile false
  2542. puts [outputChannel] "Tests running in interp: $shell"
  2543. puts [outputChannel] "Tests located in: [testsDirectory]"
  2544. puts [outputChannel] "Tests running in: [workingDirectory]"
  2545. puts [outputChannel] "Temporary files stored in\
  2546. [temporaryDirectory]"
  2547. # [file system] first available in Tcl 8.4
  2548. if {![catch {file system [testsDirectory]} result]
  2549. && ([lindex $result 0] ne "native")} {
  2550. # If we aren't running in the native filesystem, then we must
  2551. # run the tests in a single process (via 'source'), because
  2552. # trying to run then via a pipe will fail since the files don't
  2553. # really exist.
  2554. singleProcess 1
  2555. }
  2556. if {[singleProcess]} {
  2557. puts [outputChannel] \
  2558. "Test files sourced into current interpreter"
  2559. } else {
  2560. puts [outputChannel] \
  2561. "Test files run in separate interpreters"
  2562. }
  2563. if {[llength [skip]] > 0} {
  2564. puts [outputChannel] "Skipping tests that match: [skip]"
  2565. }
  2566. puts [outputChannel] "Running tests that match: [match]"
  2567. if {[llength [skipFiles]] > 0} {
  2568. puts [outputChannel] \
  2569. "Skipping test files that match: [skipFiles]"
  2570. }
  2571. if {[llength [matchFiles]] > 0} {
  2572. puts [outputChannel] \
  2573. "Only running test files that match: [matchFiles]"
  2574. }
  2575. set timeCmd {clock format [clock seconds]}
  2576. puts [outputChannel] "Tests began at [eval $timeCmd]"
  2577. # Run each of the specified tests
  2578. foreach file [lsort [GetMatchingFiles]] {
  2579. set tail [file tail $file]
  2580. puts [outputChannel] $tail
  2581. flush [outputChannel]
  2582. if {[singleProcess]} {
  2583. if {[catch {
  2584. incr numTestFiles
  2585. uplevel 1 [list ::source $file]
  2586. } msg]} {
  2587. puts [outputChannel] "Test file error: $msg"
  2588. # append the name of the test to a list to be reported
  2589. # later
  2590. lappend testFileFailures $file
  2591. }
  2592. if {$numTests(Failed) > 0} {
  2593. set failFilesSet 1
  2594. }
  2595. } else {
  2596. # Pass along our configuration to the child processes.
  2597. # EXCEPT for the -outfile, because the parent process
  2598. # needs to read and process output of children.
  2599. set childargv [list]
  2600. foreach opt [Configure] {
  2601. if {$opt eq "-outfile"} {continue}
  2602. set value [Configure $opt]
  2603. # Don't bother passing default configuration options
  2604. if {$value eq $DefaultValue($opt)} {
  2605. continue
  2606. }
  2607. lappend childargv $opt $value
  2608. }
  2609. set cmd [linsert $childargv 0 | $shell $file]
  2610. if {[catch {
  2611. incr numTestFiles
  2612. set pipeFd [open $cmd "r"]
  2613. if {[package vsatisfies [package provide Tcl] 8.7-]} {
  2614. fconfigure $pipeFd -encoding utf-8
  2615. }
  2616. while {[gets $pipeFd line] >= 0} {
  2617. if {[regexp [join {
  2618. {^([^:]+):\t}
  2619. {Total\t([0-9]+)\t}
  2620. {Passed\t([0-9]+)\t}
  2621. {Skipped\t([0-9]+)\t}
  2622. {Failed\t([0-9]+)}
  2623. } ""] $line null testFile \
  2624. Total Passed Skipped Failed]} {
  2625. foreach index {Total Passed Skipped Failed} {
  2626. incr numTests($index) [set $index]
  2627. }
  2628. if {$Failed > 0} {
  2629. lappend failFiles $testFile
  2630. set failFilesSet 1
  2631. }
  2632. } elseif {[regexp [join {
  2633. {^Number of tests skipped }
  2634. {for each constraint:}
  2635. {|^\t(\d+)\t(.+)$}
  2636. } ""] $line match skipped constraint]} {
  2637. if {[string match \t* $match]} {
  2638. AddToSkippedBecause $constraint $skipped
  2639. }
  2640. } else {
  2641. puts [outputChannel] $line
  2642. }
  2643. }
  2644. close $pipeFd
  2645. } msg]} {
  2646. puts [outputChannel] "Test file error: $msg"
  2647. # append the name of the test to a list to be reported
  2648. # later
  2649. lappend testFileFailures $file
  2650. }
  2651. }
  2652. }
  2653. # cleanup
  2654. puts [outputChannel] "\nTests ended at [eval $timeCmd]"
  2655. cleanupTests 1
  2656. if {[info exists testFileFailures]} {
  2657. puts [outputChannel] "\nTest files exiting with errors: \n"
  2658. foreach file $testFileFailures {
  2659. puts [outputChannel] " [file tail $file]\n"
  2660. }
  2661. }
  2662. # Checking for subdirectories in which to run tests
  2663. foreach directory [GetMatchingDirectories [testsDirectory]] {
  2664. set dir [file tail $directory]
  2665. puts [outputChannel] [string repeat ~ 44]
  2666. puts [outputChannel] "$dir test began at [eval $timeCmd]\n"
  2667. uplevel 1 [list ::source [file join $directory all.tcl]]
  2668. set endTime [eval $timeCmd]
  2669. puts [outputChannel] "\n$dir test ended at $endTime"
  2670. puts [outputChannel] ""
  2671. puts [outputChannel] [string repeat ~ 44]
  2672. }
  2673. return [expr {[info exists testFileFailures] || [info exists failFilesSet]}]
  2674. }
  2675. #####################################################################
  2676. # Test utility procs - not used in tcltest, but may be useful for
  2677. # testing.
  2678. # tcltest::loadTestedCommands --
  2679. #
  2680. # Uses the specified script to load the commands to test. Allowed to
  2681. # be empty, as the tested commands could have been compiled into the
  2682. # interpreter.
  2683. #
  2684. # Arguments
  2685. # none
  2686. #
  2687. # Results
  2688. # none
  2689. #
  2690. # Side Effects:
  2691. # none.
  2692. proc tcltest::loadTestedCommands {} {
  2693. return [uplevel 1 [loadScript]]
  2694. }
  2695. # tcltest::saveState --
  2696. #
  2697. # Save information regarding what procs and variables exist.
  2698. #
  2699. # Arguments:
  2700. # none
  2701. #
  2702. # Results:
  2703. # Modifies the variable saveState
  2704. #
  2705. # Side effects:
  2706. # None.
  2707. proc tcltest::saveState {} {
  2708. variable saveState
  2709. uplevel 1 [list ::set [namespace which -variable saveState]] \
  2710. {[::list [::info procs] [::info vars]]}
  2711. DebugPuts 2 "[lindex [info level 0] 0]: $saveState"
  2712. return
  2713. }
  2714. # tcltest::restoreState --
  2715. #
  2716. # Remove procs and variables that didn't exist before the call to
  2717. # [saveState].
  2718. #
  2719. # Arguments:
  2720. # none
  2721. #
  2722. # Results:
  2723. # Removes procs and variables from your environment if they don't
  2724. # exist in the saveState variable.
  2725. #
  2726. # Side effects:
  2727. # None.
  2728. proc tcltest::restoreState {} {
  2729. variable saveState
  2730. foreach p [uplevel 1 {::info procs}] {
  2731. if {($p ni [lindex $saveState 0]) && ("[namespace current]::$p" ne
  2732. [uplevel 1 [list ::namespace origin $p]])} {
  2733. DebugPuts 2 "[lindex [info level 0] 0]: Removing proc $p"
  2734. uplevel 1 [list ::catch [list ::rename $p {}]]
  2735. }
  2736. }
  2737. foreach p [uplevel 1 {::info vars}] {
  2738. if {$p ni [lindex $saveState 1]} {
  2739. DebugPuts 2 "[lindex [info level 0] 0]:\
  2740. Removing variable $p"
  2741. uplevel 1 [list ::catch [list ::unset $p]]
  2742. }
  2743. }
  2744. return
  2745. }
  2746. # tcltest::normalizeMsg --
  2747. #
  2748. # Removes "extra" newlines from a string.
  2749. #
  2750. # Arguments:
  2751. # msg String to be modified
  2752. #
  2753. # Results:
  2754. # string with extra newlines removed
  2755. #
  2756. # Side effects:
  2757. # None.
  2758. proc tcltest::normalizeMsg {msg} {
  2759. regsub "\n$" [string tolower $msg] "" msg
  2760. set msg [string map [list "\n\n" "\n"] $msg]
  2761. return [string map [list "\n\}" "\}"] $msg]
  2762. }
  2763. # tcltest::makeFile --
  2764. #
  2765. # Create a new file with the name <name>, and write <contents> to it.
  2766. #
  2767. # If this file hasn't been created via makeFile since the last time
  2768. # cleanupTests was called, add it to the $filesMade list, so it will be
  2769. # removed by the next call to cleanupTests.
  2770. #
  2771. # Arguments:
  2772. # contents content of the new file
  2773. # name name of the new file
  2774. # directory directory name for new file
  2775. #
  2776. # Results:
  2777. # absolute path to the file created
  2778. #
  2779. # Side effects:
  2780. # None.
  2781. proc tcltest::makeFile {contents name {directory ""}} {
  2782. variable filesMade
  2783. FillFilesExisted
  2784. if {[llength [info level 0]] == 3} {
  2785. set directory [temporaryDirectory]
  2786. }
  2787. set fullName [file join $directory $name]
  2788. DebugPuts 3 "[lindex [info level 0] 0]:\
  2789. putting ``$contents'' into $fullName"
  2790. set fd [open $fullName w]
  2791. fconfigure $fd -translation lf
  2792. if {[package vsatisfies [package provide Tcl] 8.7-]} {
  2793. fconfigure $fd -encoding utf-8
  2794. }
  2795. if {[string index $contents end] eq "\n"} {
  2796. puts -nonewline $fd $contents
  2797. } else {
  2798. puts $fd $contents
  2799. }
  2800. close $fd
  2801. if {$fullName ni $filesMade} {
  2802. lappend filesMade $fullName
  2803. }
  2804. return $fullName
  2805. }
  2806. # tcltest::removeFile --
  2807. #
  2808. # Removes the named file from the filesystem
  2809. #
  2810. # Arguments:
  2811. # name file to be removed
  2812. # directory directory from which to remove file
  2813. #
  2814. # Results:
  2815. # return value from [file delete]
  2816. #
  2817. # Side effects:
  2818. # None.
  2819. proc tcltest::removeFile {name {directory ""}} {
  2820. variable filesMade
  2821. FillFilesExisted
  2822. if {[llength [info level 0]] == 2} {
  2823. set directory [temporaryDirectory]
  2824. }
  2825. set fullName [file join $directory $name]
  2826. DebugPuts 3 "[lindex [info level 0] 0]: removing $fullName"
  2827. set idx [lsearch -exact $filesMade $fullName]
  2828. if {$idx < 0} {
  2829. DebugDo 1 {
  2830. Warn "removeFile removing \"$fullName\":\n not created by makeFile"
  2831. }
  2832. } else {
  2833. set filesMade [lreplace $filesMade $idx $idx]
  2834. }
  2835. if {![file isfile $fullName]} {
  2836. DebugDo 1 {
  2837. Warn "removeFile removing \"$fullName\":\n not a file"
  2838. }
  2839. }
  2840. if {[catch {file delete -- $fullName} msg ]} {
  2841. DebugDo 1 {
  2842. Warn "removeFile removing \"$fullName\":\n failed: $msg"
  2843. }
  2844. }
  2845. return
  2846. }
  2847. # tcltest::makeDirectory --
  2848. #
  2849. # Create a new dir with the name <name>.
  2850. #
  2851. # If this dir hasn't been created via makeDirectory since the last time
  2852. # cleanupTests was called, add it to the $directoriesMade list, so it
  2853. # will be removed by the next call to cleanupTests.
  2854. #
  2855. # Arguments:
  2856. # name name of the new directory
  2857. # directory directory in which to create new dir
  2858. #
  2859. # Results:
  2860. # absolute path to the directory created
  2861. #
  2862. # Side effects:
  2863. # None.
  2864. proc tcltest::makeDirectory {name {directory ""}} {
  2865. variable filesMade
  2866. FillFilesExisted
  2867. if {[llength [info level 0]] == 2} {
  2868. set directory [temporaryDirectory]
  2869. }
  2870. set fullName [file join $directory $name]
  2871. DebugPuts 3 "[lindex [info level 0] 0]: creating $fullName"
  2872. file mkdir $fullName
  2873. if {$fullName ni $filesMade} {
  2874. lappend filesMade $fullName
  2875. }
  2876. return $fullName
  2877. }
  2878. # tcltest::removeDirectory --
  2879. #
  2880. # Removes a named directory from the file system.
  2881. #
  2882. # Arguments:
  2883. # name Name of the directory to remove
  2884. # directory Directory from which to remove
  2885. #
  2886. # Results:
  2887. # return value from [file delete]
  2888. #
  2889. # Side effects:
  2890. # None
  2891. proc tcltest::removeDirectory {name {directory ""}} {
  2892. variable filesMade
  2893. FillFilesExisted
  2894. if {[llength [info level 0]] == 2} {
  2895. set directory [temporaryDirectory]
  2896. }
  2897. set fullName [file join $directory $name]
  2898. DebugPuts 3 "[lindex [info level 0] 0]: deleting $fullName"
  2899. set idx [lsearch -exact $filesMade $fullName]
  2900. set filesMade [lreplace $filesMade $idx $idx]
  2901. if {$idx < 0} {
  2902. DebugDo 1 {
  2903. Warn "removeDirectory removing \"$fullName\":\n not created\
  2904. by makeDirectory"
  2905. }
  2906. }
  2907. if {![file isdirectory $fullName]} {
  2908. DebugDo 1 {
  2909. Warn "removeDirectory removing \"$fullName\":\n not a directory"
  2910. }
  2911. }
  2912. return [file delete -force -- $fullName]
  2913. }
  2914. # tcltest::viewFile --
  2915. #
  2916. # reads the content of a file and returns it
  2917. #
  2918. # Arguments:
  2919. # name of the file to read
  2920. # directory in which file is located
  2921. #
  2922. # Results:
  2923. # content of the named file
  2924. #
  2925. # Side effects:
  2926. # None.
  2927. proc tcltest::viewFile {name {directory ""}} {
  2928. FillFilesExisted
  2929. if {[llength [info level 0]] == 2} {
  2930. set directory [temporaryDirectory]
  2931. }
  2932. set fullName [file join $directory $name]
  2933. set f [open $fullName]
  2934. if {[package vsatisfies [package provide Tcl] 8.7-]} {
  2935. fconfigure $f -encoding utf-8
  2936. }
  2937. set data [read -nonewline $f]
  2938. close $f
  2939. return $data
  2940. }
  2941. # tcltest::bytestring --
  2942. #
  2943. # Construct a string that consists of the requested sequence of bytes,
  2944. # as opposed to a string of properly formed UTF-8 characters.
  2945. # This allows the tester to
  2946. # 1. Create denormalized or improperly formed strings to pass to C
  2947. # procedures that are supposed to accept strings with embedded NULL
  2948. # bytes.
  2949. # 2. Confirm that a string result has a certain pattern of bytes, for
  2950. # instance to confirm that "\xE0\0" in a Tcl script is stored
  2951. # internally in UTF-8 as the sequence of bytes "\xC3\xA0\xC0\x80".
  2952. #
  2953. # Generally, it's a bad idea to examine the bytes in a Tcl string or to
  2954. # construct improperly formed strings in this manner, because it involves
  2955. # exposing that Tcl uses UTF-8 internally.
  2956. #
  2957. # This function doesn't work any more in Tcl 8.7, since the 'identity'
  2958. # is gone (TIP #345)
  2959. #
  2960. # Arguments:
  2961. # string being converted
  2962. #
  2963. # Results:
  2964. # result fom encoding
  2965. #
  2966. # Side effects:
  2967. # None
  2968. if {![package vsatisfies [package provide Tcl] 8.7-]} {
  2969. proc tcltest::bytestring {string} {
  2970. return [encoding convertfrom identity $string]
  2971. }
  2972. }
  2973. # tcltest::OpenFiles --
  2974. #
  2975. # used in io tests, uses testchannel
  2976. #
  2977. # Arguments:
  2978. # None.
  2979. #
  2980. # Results:
  2981. # ???
  2982. #
  2983. # Side effects:
  2984. # None.
  2985. proc tcltest::OpenFiles {} {
  2986. if {[catch {testchannel open} result]} {
  2987. return {}
  2988. }
  2989. return $result
  2990. }
  2991. # tcltest::LeakFiles --
  2992. #
  2993. # used in io tests, uses testchannel
  2994. #
  2995. # Arguments:
  2996. # None.
  2997. #
  2998. # Results:
  2999. # ???
  3000. #
  3001. # Side effects:
  3002. # None.
  3003. proc tcltest::LeakFiles {old} {
  3004. if {[catch {testchannel open} new]} {
  3005. return {}
  3006. }
  3007. set leak {}
  3008. foreach p $new {
  3009. if {$p ni $old} {
  3010. lappend leak $p
  3011. }
  3012. }
  3013. return $leak
  3014. }
  3015. #
  3016. # Internationalization / ISO support procs -- dl
  3017. #
  3018. # tcltest::SetIso8859_1_Locale --
  3019. #
  3020. # used in cmdIL.test, uses testlocale
  3021. #
  3022. # Arguments:
  3023. # None.
  3024. #
  3025. # Results:
  3026. # None.
  3027. #
  3028. # Side effects:
  3029. # None.
  3030. proc tcltest::SetIso8859_1_Locale {} {
  3031. variable previousLocale
  3032. variable isoLocale
  3033. if {[info commands testlocale] != ""} {
  3034. set previousLocale [testlocale ctype]
  3035. testlocale ctype $isoLocale
  3036. }
  3037. return
  3038. }
  3039. # tcltest::RestoreLocale --
  3040. #
  3041. # used in cmdIL.test, uses testlocale
  3042. #
  3043. # Arguments:
  3044. # None.
  3045. #
  3046. # Results:
  3047. # None.
  3048. #
  3049. # Side effects:
  3050. # None.
  3051. proc tcltest::RestoreLocale {} {
  3052. variable previousLocale
  3053. if {[info commands testlocale] != ""} {
  3054. testlocale ctype $previousLocale
  3055. }
  3056. return
  3057. }
  3058. # tcltest::threadReap --
  3059. #
  3060. # Kill all threads except for the main thread.
  3061. # Do nothing if testthread is not defined.
  3062. #
  3063. # Arguments:
  3064. # none.
  3065. #
  3066. # Results:
  3067. # Returns the number of existing threads.
  3068. #
  3069. # Side Effects:
  3070. # none.
  3071. #
  3072. proc tcltest::threadReap {} {
  3073. if {[info commands testthread] ne {}} {
  3074. # testthread built into tcltest
  3075. testthread errorproc ThreadNullError
  3076. while {[llength [testthread names]] > 1} {
  3077. foreach tid [testthread names] {
  3078. if {$tid != [mainThread]} {
  3079. catch {
  3080. testthread send -async $tid {testthread exit}
  3081. }
  3082. }
  3083. }
  3084. ## Enter a bit a sleep to give the threads enough breathing
  3085. ## room to kill themselves off, otherwise the end up with a
  3086. ## massive queue of repeated events
  3087. after 1
  3088. }
  3089. testthread errorproc ThreadError
  3090. return [llength [testthread names]]
  3091. } elseif {[info commands thread::id] ne {}} {
  3092. # Thread extension
  3093. thread::errorproc ThreadNullError
  3094. while {[llength [thread::names]] > 1} {
  3095. foreach tid [thread::names] {
  3096. if {$tid != [mainThread]} {
  3097. catch {thread::send -async $tid {thread::exit}}
  3098. }
  3099. }
  3100. ## Enter a bit a sleep to give the threads enough breathing
  3101. ## room to kill themselves off, otherwise the end up with a
  3102. ## massive queue of repeated events
  3103. after 1
  3104. }
  3105. thread::errorproc ThreadError
  3106. return [llength [thread::names]]
  3107. } else {
  3108. return 1
  3109. }
  3110. return 0
  3111. }
  3112. # Initialize the constraints and set up command line arguments
  3113. namespace eval tcltest {
  3114. # Define initializers for all the built-in contraint definitions
  3115. DefineConstraintInitializers
  3116. # Set up the constraints in the testConstraints array to be lazily
  3117. # initialized by a registered initializer, or by "false" if no
  3118. # initializer is registered.
  3119. trace add variable testConstraints read [namespace code SafeFetch]
  3120. # Only initialize constraints at package load time if an
  3121. # [initConstraintsHook] has been pre-defined. This is only
  3122. # for compatibility support. The modern way to add a custom
  3123. # test constraint is to just call the [testConstraint] command
  3124. # straight away, without all this "hook" nonsense.
  3125. if {[namespace current] eq
  3126. [namespace qualifiers [namespace which initConstraintsHook]]} {
  3127. InitConstraints
  3128. } else {
  3129. proc initConstraintsHook {} {}
  3130. }
  3131. # Define the standard match commands
  3132. customMatch exact [list string equal]
  3133. customMatch glob [list string match]
  3134. customMatch regexp [list regexp --]
  3135. # If the TCLTEST_OPTIONS environment variable exists, configure
  3136. # tcltest according to the option values it specifies. This has
  3137. # the effect of resetting tcltest's default configuration.
  3138. proc ConfigureFromEnvironment {} {
  3139. upvar #0 env(TCLTEST_OPTIONS) options
  3140. if {[catch {llength $options} msg]} {
  3141. Warn "invalid TCLTEST_OPTIONS \"$options\":\n invalid\
  3142. Tcl list: $msg"
  3143. return
  3144. }
  3145. if {[llength $options] % 2} {
  3146. Warn "invalid TCLTEST_OPTIONS: \"$options\":\n should be\
  3147. -option value ?-option value ...?"
  3148. return
  3149. }
  3150. if {[catch {Configure {*}$options} msg]} {
  3151. Warn "invalid TCLTEST_OPTIONS: \"$options\":\n $msg"
  3152. return
  3153. }
  3154. }
  3155. if {[info exists ::env(TCLTEST_OPTIONS)]} {
  3156. ConfigureFromEnvironment
  3157. }
  3158. proc LoadTimeCmdLineArgParsingRequired {} {
  3159. set required false
  3160. if {[info exists ::argv] && ("-help" in $::argv)} {
  3161. # The command line asks for -help, so give it (and exit)
  3162. # right now. ([configure] does not process -help)
  3163. set required true
  3164. }
  3165. foreach hook { PrintUsageInfoHook processCmdLineArgsHook
  3166. processCmdLineArgsAddFlagsHook } {
  3167. if {[namespace current] eq
  3168. [namespace qualifiers [namespace which $hook]]} {
  3169. set required true
  3170. } else {
  3171. proc $hook args {}
  3172. }
  3173. }
  3174. return $required
  3175. }
  3176. # Only initialize configurable options from the command line arguments
  3177. # at package load time if necessary for backward compatibility. This
  3178. # lets the tcltest user call [configure] for themselves if they wish.
  3179. # Traces are established for auto-configuration from the command line
  3180. # if any configurable options are accessed before the user calls
  3181. # [configure].
  3182. if {[LoadTimeCmdLineArgParsingRequired]} {
  3183. ProcessCmdLineArgs
  3184. } else {
  3185. EstablishAutoConfigureTraces
  3186. }
  3187. package provide [namespace tail [namespace current]] $Version
  3188. }