File: testing.jam

package info (click to toggle)
boost-build 2.0-m12-2
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 8,692 kB
  • ctags: 6,963
  • sloc: ansic: 39,914; sh: 9,086; python: 6,120; xml: 5,524; cpp: 1,467; yacc: 456; asm: 353; makefile: 184
file content (518 lines) | stat: -rw-r--r-- 16,842 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# Copyright 2005 Dave Abrahams 
# Copyright 2002, 2003, 2004, 2005, 2006 Vladimir Prus 
# Distributed under the Boost Software License, Version 1.0. 
# (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) 

#  This module implements regression testing framework. It declares a number of 
#  main target rules, which perform some action, and if the results are ok,
#  creates an output file.
#  
#  The exact list of rules is:
#  'compile'       -- creates .test file if compilation of sources was successfull
#  'compile-fail'  -- creates .test file if compilation of sources failed
#  'run'           -- creates .test file is running of executable produced from
#                     sources was successfull. Also leaves behing .output file
#                     with the output from program run.
#  'run-fail'      -- same as above, but .test file is created if running fails.
#
#  In all cases, presense of .test file is an incication that
#  the test passed. For more convenient reporting, you might want to use C++ Boost
#  regression testing utilities, see
#  http://www.boost.org/more/regression.html
#
#  For historical reason, a 'unit-test' rule is available which
#  has the same syntax as 'exe' and behaves just like 'run'.

# Things to do:
#  - Teach compiler_status handle Jamfile.v2.
# Notes:
#  - <no-warn> is not implemented, since in Como-specific, and it's not clear how
#     to implement it
#  - std::locale-support is not impelemted (it's used in one test).

  
import targets ;
import "class" : new ;
import property ;
import feature ;
import toolset ;
import alias ;
import type ;
import generators ;
import project ;
import property-set ;
import virtual-target ;
import path ;
import os ;
import common ;
import sequence ;
import errors ;

rule init ( ) { }

# The feature which controls the name of program used to
# lanch test programs. 
feature.feature testing.launcher : : optional free ;
feature.feature test-info : : free incidental ;
feature.feature testing.arg : : free incidental ;
feature.feature testing.input-file : : free dependency ;

# Register target types.
type.register TEST : test ;
type.register COMPILE : : TEST ;
type.register COMPILE_FAIL : : TEST ;
type.register RUN_OUTPUT : run ;
type.register RUN : : TEST ;
type.register RUN_FAIL : : TEST ;
type.register LINK_FAIL : : TEST ;
type.register LINK : : TEST ;
type.register UNIT_TEST : passed : TEST ;

# Declare the rules which create main targets. 
# While the 'type' module already creates rules with the same names for us,
# we need extra convenience: default name of main target, so write
# our own versions.

# Helper rule. Create a test target, using basename of first source if no
# target name is explicitly passed. Remembers the created target in
# a global variable.
rule make-test ( target-type : sources + : requirements * : target-name ? )
{
    target-name ?= $(sources[1]:D=:S=) ;

    local project = [ project.current ] ;
    # The <location-prefix> forces the build system for generate paths in the form
    # $build_dir/array1.test/gcc/debug
    # This is necessary to allow post-processing tools to work.
    local t = 
      [ targets.create-typed-target 
          [ type.type-from-rule-name $(target-type) ] : $(project)
            : $(target-name) : $(sources) 
            : $(requirements) <location-prefix>$(target-name).test ] ;
    
    # Remember the test (for --dump-test). 
    # A good way would be to collect all given a project.
    # This has some technical problems: e.g. we can't call this dump from
    # Jamfile since projects referred by 'build-project' are not available until
    # whole Jamfile is loaded.
    .all-tests += $(t) ;
    return $(t) ;    
}

# Note: passing more that one cpp file here is know to
# fail. Passing a cpp file and a library target works.
rule compile ( sources + : requirements * : target-name ? )
{    
    return [ make-test compile : $(sources) : $(requirements) : $(target-name) ] ;
}

rule compile-fail ( sources + : requirements * : target-name ? )
{    
    return [ make-test compile-fail : $(sources) : $(requirements) : $(target-name) ] ;
}

rule link ( sources + : requirements * : target-name ? )
{    
    return [ make-test link : $(sources) : $(requirements) : $(target-name) ] ;
}


rule link-fail ( sources + : requirements * : target-name ? )
{    
    return [ make-test link-fail : $(sources) : $(requirements) : $(target-name) ] ;
}


rule handle-input-files ( input-files * )
{
    if $(input-files[2])
    {
        # Check that sorting made when creating property-set instance
        # won't change the ordering.
        if [ sequence.insertion-sort $(input-files) ] != $(input-files)
        {
            errors.user-error "Names of input files must be sorted alphabetically"
              : "due to internal limitations" ;
        }        
    }
    return <testing.input-file>$(input-files) ;
}


rule run ( sources + : args * : input-files * : requirements * : target-name ? 
    : default-build * )
{      
    requirements += <testing.arg>$(args:J=" ") ;
    requirements += [ handle-input-files $(input-files) ] ;
    return [ make-test run : $(sources) : $(requirements) : $(target-name) ] ;
}

rule run-fail ( sources + : args * : input-files * : requirements * : target-name ? 
    : default-build * )
{    
    requirements += <testing.arg>$(args:J=" ") ;    
    requirements += [ handle-input-files $(input-files) ] ;
    return [ make-test run-fail : $(sources) : $(requirements) : $(target-name) ] ;
}


# Use 'test-suite' as synonym for 'alias', for backward compatibility.
IMPORT : alias : : test-suite ;



# For all main target in 'project-module', 
# which are typed target with type derived from 'TEST', 
# produce some interesting information.
rule dump-tests # ( project-module )
{
    for local t in $(.all-tests)
    {
        dump-test $(t) ;
    }    
}

# Given a project location, compute the name of Boost library
local rule get-library-name ( path )
{
    # Path is in normalized form, so all slashes are forward.

    local match1 = [ MATCH /libs/(.*)/(test|example) : $(path) ] ;
    local match2 = [ MATCH /libs/(.*)$ : $(path) ] ;
    local match3 = [ MATCH (/status$) : $(path) ] ;

    if $(match1) { return $(match1[0]) ; }
    else if $(match2) { return $(match2[0]) ; }
    else if $(match3) { return "" ; }
    else if --dump-tests in [ modules.peek : ARGV ] 
    { 
        # The 'run' rule and others might be used outside
        # boost. In that case, just return the path,
        # since the 'library name' makes no sense.
        return $(path) ;
    }
}


# Take a target (instance of 'basic-target') and prints 
# - its type
# - its name
# - comments specified via the <test-info> property
# - relative location of all source from the project root.
rule dump-test ( target )
{
    local type = [ $(target).type ] ;
    local name = [ $(target).name ] ;
    local project = [ $(target).project ] ;
    
    local project-root = [ $(project).get project-root ] ;
    local library = [ get-library-name 
        [ path.root [ $(project).get location ] [ path.pwd ] ] ] ;
    if $(library)
    {
        name = $(library)/$(name) ;
    }
        
    local sources = [ $(target).sources ] ;
    local source-files ;
    for local s in $(sources)
    {
        if [ class.is-a $(s) : file-reference ] 
        {
            local location = 
              [ path.root 
                  [ path.root [ $(s).name ] [ $(s).location ] ]
                  [ path.pwd ] ] ;  
                       
            source-files += 
              [ path.relative 
                  $(location)
                  [ path.root $(project-root) [ path.pwd ] ] ] ;
        }                
    }
          
    local r = [ $(target).requirements ] ;
    # Extract values of the <test-info> feature
    local test-info = [ $(r).get <test-info> ] ;
    
    # Format them into a single string of quoted strings
    test-info = \"$(test-info:J=\"\ \")\" ;
    
    ECHO boost-test($(type)) \"$(name)\" 
      [$(test-info)]            
        ":" \"$(source-files)\"
          ;            
}

# Register generators. Depending on target type, either
# 'expect-success' or 'expect-failure' rule will be used.
generators.register-standard testing.expect-success : OBJ : COMPILE ;
generators.register-standard testing.expect-failure : OBJ : COMPILE_FAIL ;
generators.register-standard testing.expect-success : RUN_OUTPUT : RUN ;
generators.register-standard testing.expect-failure : RUN_OUTPUT : RUN_FAIL ;
generators.register-standard testing.expect-failure : EXE : LINK_FAIL ;
generators.register-standard testing.expect-success : EXE : LINK ;

# Generator which runs an EXE and captures output.
generators.register-standard testing.capture-output : EXE : RUN_OUTPUT ;

# Generator which creates target if sources runs successfully.
# Differers from RUN in that run output is not captured.
# The reason why it exists is that the 'run' rule is much better for
# automated testing, but is not user-friendly. See
# http://article.gmane.org/gmane.comp.lib.boost.build/6353/
generators.register-standard testing.unit-test : EXE : UNIT_TEST ;


# The action rules called by generators.

# Causes the 'target' to exist after bjam invocation if and only if all the 
# dependencies were successfully built.
rule expect-success ( target : dependency + : requirements * )
{
    **passed** $(target) : $(sources) ;    
}

# Causes the 'target' to exist after bjam invocation if and only if all some
# of the dependencies were not successfully built.
rule expect-failure ( target : dependency + : properties * )
{
    local grist = [ MATCH ^<(.*)> : $(dependency:G) ] ;
    local marker = $(dependency:G=$(grist)*fail) ;
    (failed-as-expected) $(marker) ;
    FAIL_EXPECTED $(dependency) ;    
    LOCATE on $(marker) = [ on $(dependency) return $(LOCATE) ] ;
    RMOLD $(marker) ;
    DEPENDS $(marker) : $(dependency) ;
    
    DEPENDS $(target) : $(marker) ;
    **passed** $(target) : $(marker) ;
}

# The rule/action combination used to report successfull passing
# of a test.
rule **passed**
{
    # Dump all the tests, if needed.
    # We do it here, since dump should happen after all Jamfiles are read,
    # and there's no such place currently defined (but should).
    if ! $(.dumped-tests) && --dump-tests in [ modules.peek : ARGV ] 
    {
        .dumped-tests = true ;
        dump-tests ;
    }
                 
    # Force deletion of the target, in case any dependencies failed
    # to build.
    RMOLD $(<) ;
}

actions **passed**
{
    echo passed > $(<)
}

actions (failed-as-expected)
{
    echo failed as expected > $(<)
}

rule run-path-setup ( target : source : properties * )
{
    # For testing, we need to make sure that all dynamic libraries needed by
    # the test are found. So, we collect all paths from dependency libraries
    # (via xdll-path property) and add whatever explicit dll-path user has
    # specified. The resulting paths are added to environment on each test
    # invocation.
    local dll-paths = [ feature.get-values <dll-path> : $(properties) ] ;
    dll-paths += [ feature.get-values <xdll-path> : $(properties) ] ;
    dll-paths += [ on $(source) return $(RUN_PATH) ] ;
    dll-paths = [ sequence.unique $(dll-paths) ] ;
    if $(dll-paths)
    {    
        dll-paths = [ sequence.transform path.native : $(dll-paths) ] ;
        
        PATH_SETUP on $(target) = 
          [ common.prepend-path-variable-command
              [ os.shared-library-path-variable ] : $(dll-paths) ] ;
    }            
}

local argv = [ modules.peek : ARGV ] ;
if --preserve-test-targets in $(argv)
{
    preserve-test-targets = true ;
}

toolset.flags testing.capture-output ARGS <testing.arg> ;
toolset.flags testing.capture-output INPUT_FILES <testing.input-file> ;
toolset.flags testing.capture-output LAUNCHER <testing.launcher> ;

# Runs executable 'sources' and stores stdout in file 'target'.
# If --preserve-test-targets command line option, removes the executable.
# The 'target-to-remove' parameter controls what should be removed:
#   - if 'none', does not remove anything, ever
#   - if empty, removes 'source'
#   - if non-empty and not 'none', contains a list of sources to remove.
rule capture-output ( target : source : properties * : targets-to-remove * )
{
    output-file on $(target) = $(target:S=.output) ;
    LOCATE on $(target:S=.output) = [ on $(target) return $(LOCATE) ] ;
    
    # The INCLUDES kill a warning about independent target...
    INCLUDES $(target) : $(target:S=.output) ;
    # but it also puts .output into dependency graph, so we must tell jam
    # it's OK if it cannot find the target or updating rule.
    NOCARE $(target:S=.output) ;        
    
    # This has two-fold effect. First it adds input files to the dependendency
    # graph, preventing a warning. Second, it causes input files to be bound
    # before target is created. Therefore, they are bound using SEARCH setting 
    # on them and not LOCATE setting of $(target), as in other case (due to jam bug).
    DEPENDS $(target) : [ on $(target) return $(INPUT_FILES) ] ;
    
    if $(targets-to-remove) = none
    {
        targets-to-remove = ;
    }
    else if ! $(targets-to-remove)
    {
        targets-to-remove = $(source) ;
    }
            
    run-path-setup $(target) : $(source) : $(properties) ;
    
    if ! $(preserve-test-targets)
    {
        TEMPORARY $(targets-to-remove) ;
        # Set a second action on target that will
        # be executed after capture output action.
        # The 'RmTemps' rule has the 'ignore' modifier
        # so it's always considered succeeded.
        # This is needed for 'run-fail' test. For that
        # test the target will be marked with FAIL_EXPECTED,
        # and without 'ingore' successfull execution 
        # will be negated and be reported as failure.
        # With 'ignore' we don't detect a case where
        # removing files, but it's not likely to
        # happen.
        RmTemps $(target) : $(targets-to-remove) ;
    }
}


if [ os.name ] = NT
{
    STATUS = %status% ;
    SET_STATUS = "set status=%ERRORLEVEL%" ;
    RUN_OUTPUT_NL = "echo." ;
    STATUS_0 = "%status% EQU 0 (" ;
    STATUS_NOT_0 = "%status% NEQ 0 (" ;
    VERBOSE = "%verbose% EQU 1 (" ;
    ENDIF = ")" ;
    SHELL_SET = "set " ;
    
    CATENATE = type ;
    CP = copy ;
}
else
{
    STATUS = "$status" ;
    SET_STATUS = "status=$?" ;
    RUN_OUTPUT_NL = "echo" ;
    STATUS_0 = "test $status -eq 0 ; then" ;
    STATUS_NOT_0 = "test $status -ne 0 ; then" ;
    VERBOSE = "test $verbose -eq 1 ; then" ;
    ENDIF = "fi" ;
    SHELL_SET = "" ;
    
    CATENATE = cat ;
    CP = cp ;
}

if --verbose-test in [ modules.peek : ARGV ] 
{
    VERBOSE_TEST = 1 ;
}
else
{
    VERBOSE_TEST = 0 ;
}


RM = [ common.rm-command ] ;

actions capture-output bind INPUT_FILES output-file
{
    $(PATH_SETUP)
    $(LAUNCHER) "$(>)" $(ARGS) "$(INPUT_FILES)" > "$(output-file)" 2>&1      
    $(SET_STATUS)
    $(RUN_OUTPUT_NL) >> "$(output-file)"
    echo EXIT STATUS: $(STATUS) >> "$(output-file)"
    if $(STATUS_0)
        $(CP) "$(output-file)" "$(<)"
    $(ENDIF)
    $(SHELL_SET)verbose=$(VERBOSE_TEST)
    if $(STATUS_NOT_0)
        $(SHELL_SET)verbose=1
    $(ENDIF)
    if $(VERBOSE)
        echo ====== BEGIN OUTPUT ======
        $(CATENATE) "$(output-file)"
        echo ====== END OUTPUT ======
    $(ENDIF)    
    exit $(STATUS)      
}

actions quietly updated ignore piecemeal together RmTemps
{
    $(RM) "$(>)"
}

MAKE_FILE = [ common.file-creation-command ] ;

toolset.flags testing.unit-test LAUNCHER <testing.launcher> ;
rule unit-test ( target : source : properties * )
{
    run-path-setup $(target) : $(source) : $(properties) ;
}

actions unit-test 
{
    $(PATH_SETUP)
    $(LAUNCHER) $(>) && $(MAKE_FILE) $(<)
}

IMPORT $(__name__) : compile compile-fail run run-fail link link-fail 
  : : compile compile-fail run run-fail link link-fail ;


type.register TIME : time ;
generators.register-standard testing.time : : TIME ;

rule record-time ( target source : user : system )
{
    local src-string = [$(source:G=:J=",")"] " ;
    USER_TIME on $(target) += $(src-string)$(user) ;
    SYSTEM_TIME on $(target) += $(src-string)$(system) ;
}

IMPORT testing : record-time : : testing.record-time ;
rule time ( target : source : properties *  )
{
    # Set up rule for recording timing information
    __TIMING_RULE__ on $(source) = testing.record-time $(target) ;
    
    # Make sure that the source is rebuilt any time we need to
    # retrieve that information
    REBUILDS $(target) : $(source) ;
}

actions time
{
    echo user: $(USER_TIME)
    echo system: $(SYSTEM_TIME)
      
    echo user: $(USER_TIME)" seconds" > $(<)
    echo system: $(SYSTEM_TIME)" seconds" > $(<)
}