tcltest-2.4.0.tm 98 KB

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