summaryrefslogtreecommitdiffstats
path: root/src/engine/SCons/BuilderTests.py
blob: bd254b02e331b7ccdb363d80f64741fe1f911a70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
#
# __COPYRIGHT__
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

# Define a null function for use as a builder action.
# Where this is defined in the file seems to affect its
# byte-code contents, so try to minimize changes by
# defining it here, before we even import anything.
def Func():
    pass

import os.path
import sys
import types
import unittest

import TestCmd

import SCons.Action
import SCons.Builder
import SCons.Environment
import SCons.Errors

# Initial setup of the common environment for all tests,
# a temporary working directory containing a
# script for writing arguments to an output file.
#
# We don't do this as a setUp() method because it's
# unnecessary to create a separate directory and script
# for each test, they can just use the one.
test = TestCmd.TestCmd(workdir = '')

outfile = test.workpath('outfile')
outfile2 = test.workpath('outfile2')

show_string = None

scons_env = SCons.Environment.Environment()

env_arg2nodes_called = None

class Environment:
    def __init__(self, **kw):
        self.d = {}
        self.d['SHELL'] = scons_env['SHELL']
        self.d['SPAWN'] = scons_env['SPAWN']
        self.d['ESCAPE'] = scons_env['ESCAPE']
        for k, v in kw.items():
            self.d[k] = v
        global env_arg2nodes_called
        env_arg2nodes_called = None
        self.scanner = None
    def subst(self, s):
        if not SCons.Util.is_String(s):
            return s
        try:
            if s[0] == '$':
                return self.d.get(s[1:], '')
            if s[1] == '$':
                return s[0] + self.d.get(s[2:], '')
        except IndexError:
            pass
        return self.d.get(s, s)
    def arg2nodes(self, args, factory):
        global env_arg2nodes_called
        env_arg2nodes_called = 1
        if not SCons.Util.is_List(args):
            args = [args]
        list = []
        for a in args:
            if SCons.Util.is_String(a):
                a = factory(a)
            list.append(a)
        return list
    def get_scanner(self, ext):
        return self.scanner
    def Dictionary(self):
        return {}
    def autogenerate(self, dir=''):
        return {}
    def __setitem__(self, item, var):
        self.d[item] = var
    def __getitem__(self, item):
        return self.d[item]
    def has_key(self, item):
        return self.d.has_key(item)
    def keys(self):
        return self.d.keys()
    def get(self, key, value):
        return self.d.get(key, value)
    def Override(self, overrides):
        env = apply(Environment, (), self.d)
        env.d.update(overrides)
        return env
    def _update(self, dict):
        self.d.update(dict)
    def items(self):
        return self.d.items()
    def sig_dict(self):
        d = {}
        for k,v in self.items(): d[k] = v
        d['TARGETS'] = ['__t1__', '__t2__', '__t3__', '__t4__', '__t5__', '__t6__']
        d['TARGET'] = d['TARGETS'][0]
        d['SOURCES'] = ['__s1__', '__s2__', '__s3__', '__s4__', '__s5__', '__s6__']
        d['SOURCE'] = d['SOURCES'][0]
        return d

class MyNode_without_target_from_source:
    def __init__(self, name):
        self.name = name
        self.sources = []
        self.builder = None
        self.side_effect = 0
        self.source_scanner = None
    def __str__(self):
        return self.name
    def builder_set(self, builder):
        self.builder = builder
    def has_builder(self):
        return not self.builder is None
    def env_set(self, env, safe=0):
        self.env = env
    def add_source(self, source):
        self.sources.extend(source)
    def scanner_key(self):
        return self.name
    def is_derived(self):
        return self.has_builder()
    def generate_build_env(self, env):
        return env
    def get_build_env(self):
        return self.executor.get_build_env()
    def set_executor(self, executor):
        self.executor = executor
    def get_executor(self, create=1):
        return self.executor

class MyNode(MyNode_without_target_from_source):
    def target_from_source(self, prefix, suffix, stripext):
        return MyNode(prefix + stripext(str(self))[0] + suffix)

class BuilderTestCase(unittest.TestCase):

    def test__init__(self):
        """Test simple Builder creation
        """
        builder = SCons.Builder.Builder(action="foo")
        assert not builder is None, builder
        builder = SCons.Builder.Builder(action="foo", OVERRIDE='x')
        x = builder.overrides['OVERRIDE']
        assert x == 'x', x

    def test__nonzero__(self):
        """Test a builder raising an exception when __nonzero__ is called
        """
        builder = SCons.Builder.Builder(action="foo")
        exc_caught = None
        try:
            builder.__nonzero__()
        except SCons.Errors.InternalError:
            exc_caught = 1
        assert exc_caught, "did not catch expected InternalError exception"

        class Node:
             pass

        n = Node()
        n.builder = builder
        exc_caught = None
        try:
            if n.builder:
                pass
        except SCons.Errors.InternalError:
            exc_caught = 1
        assert exc_caught, "did not catch expected InternalError exception"

    def test__call__(self):
        """Test calling a builder to establish source dependencies
        """
        env = Environment()
        builder = SCons.Builder.Builder(action="foo", node_factory=MyNode)

        n1 = MyNode("n1");
        n2 = MyNode("n2");
        builder(env, target = n1, source = n2)
        assert env_arg2nodes_called
        assert n1.env == env, n1.env
        assert n1.builder == builder, n1.builder
        assert n1.sources == [n2], n1.sources
        assert n1.executor, "no executor found"
        assert not hasattr(n2, 'env')

        target = builder(env, target = 'n3', source = 'n4')
        assert target.name == 'n3'
        assert target.sources[0].name == 'n4'

        target = builder(env, target = 'n4 n5', source = ['n6 n7'])
        assert target.name == 'n4 n5'
        assert target.sources[0].name == 'n6 n7'

        target = builder(env, target = ['n8 n9'], source = 'n10 n11')
        assert target.name == 'n8 n9'
        assert target.sources[0].name == 'n10 n11'

        # A test to be uncommented when we freeze the environment
        # as part of calling the builder.
        #env1 = Environment(VAR='foo')
        #target = builder(env1, target = 'n12', source = 'n13')
        #env1['VAR'] = 'bar'
        #be = target.get_build_env()
        #assert be['VAR'] == 'foo', be['VAR']

        if not hasattr(types, 'UnicodeType'):
            uni = str
        else:
            uni = unicode

        target = builder(env, target = uni('n12 n13'),
                          source = [uni('n14 n15')])
        assert target.name == uni('n12 n13')
        assert target.sources[0].name == uni('n14 n15')

        target = builder(env, target = [uni('n16 n17')],
                         source = uni('n18 n19'))
        assert target.name == uni('n16 n17')
        assert target.sources[0].name == uni('n18 n19')

        n20 = MyNode_without_target_from_source('n20')
        flag = 0
        try:
            target = builder(env, source=n20)
        except SCons.Errors.UserError, e:
            flag = 1
        assert flag, "UserError should be thrown if a source node can't create a target."

        builder = SCons.Builder.Builder(action="foo",
                                        node_factory=MyNode,
                                        prefix='p-',
                                        suffix='.s')
        target = builder(env, source='n21')
        assert target.name == 'p-n21.s', target

    def test_mistaken_variables(self):
        """Test keyword arguments that are often mistakes
        """
        import SCons.Warnings
        env = Environment()
        builder = SCons.Builder.Builder(action="foo")

        save_warn = SCons.Warnings.warn
        warned = []
        def my_warn(exception, warning, warned=warned):
            warned.append(warning)
        SCons.Warnings.warn = my_warn

        try:
            target = builder(env, 'mistaken1', sources='mistaken1.c')
            assert warned == ["Did you mean to use `source' instead of `sources'?"], warned
            del warned[:]

            target = builder(env, 'mistaken2', targets='mistaken2.c')
            assert warned == ["Did you mean to use `target' instead of `targets'?"], warned
            del warned[:]

            target = builder(env, 'mistaken3', targets='mistaken3', sources='mistaken3.c')
            assert "Did you mean to use `source' instead of `sources'?" in warned, warned
            assert "Did you mean to use `target' instead of `targets'?" in warned, warned
            del warned[:]
        finally:
            SCons.Warnings.warn = save_warn

    def test_action(self):
        """Test Builder creation

        Verify that we can retrieve the supplied action attribute.
        """
        builder = SCons.Builder.Builder(action="foo")
        assert builder.action.cmd_list == "foo"

        def func():
            pass
        builder = SCons.Builder.Builder(action=func)
        assert isinstance(builder.action, SCons.Action.FunctionAction)
        # Preserve the following so that the baseline test will fail.
        # Remove it in favor of the previous test at some convenient
        # point in the future.
        assert builder.action.execfunction == func

    def test_generator(self):
        """Test Builder creation given a generator function."""

        def generator():
            pass

        builder = SCons.Builder.Builder(generator=generator)
        assert builder.action.generator == generator

    def test_cmp(self):
        """Test simple comparisons of Builder objects
        """
        b1 = SCons.Builder.Builder(src_suffix = '.o')
        b2 = SCons.Builder.Builder(src_suffix = '.o')
        assert b1 == b2
        b3 = SCons.Builder.Builder(src_suffix = '.x')
        assert b1 != b3
        assert b2 != b3

    def test_node_factory(self):
        """Test a Builder that creates nodes of a specified class
        """
        class Foo:
            pass
        def FooFactory(target):
            global Foo
            return Foo(target)
        builder = SCons.Builder.Builder(node_factory = FooFactory)
        assert builder.target_factory is FooFactory
        assert builder.source_factory is FooFactory

    def test_target_factory(self):
        """Test a Builder that creates target nodes of a specified class
        """
        class Foo:
            pass
        def FooFactory(target):
            global Foo
            return Foo(target)
        builder = SCons.Builder.Builder(target_factory = FooFactory)
        assert builder.target_factory is FooFactory
        assert not builder.source_factory is FooFactory

    def test_source_factory(self):
        """Test a Builder that creates source nodes of a specified class
        """
        class Foo:
            pass
        def FooFactory(source):
            global Foo
            return Foo(source)
        builder = SCons.Builder.Builder(source_factory = FooFactory)
        assert not builder.target_factory is FooFactory
        assert builder.source_factory is FooFactory

    def test_splitext(self):
        """Test the splitext() method attached to a Builder."""
        b = SCons.Builder.Builder()
        assert b.splitext('foo') == ('foo','')
        assert b.splitext('foo.bar') == ('foo','.bar')
        assert b.splitext(os.path.join('foo.bar', 'blat')) == (os.path.join('foo.bar', 'blat'),'')

        class MyBuilder(SCons.Builder.BuilderBase):
            def splitext(self, path):
                return "called splitext()"

        b = MyBuilder()
        ret = b.splitext('xyz.c')
        assert ret == "called splitext()", ret

    def test_adjust_suffix(self):
        """Test how a Builder adjusts file suffixes
        """
        b = SCons.Builder.Builder()
        assert b.adjust_suffix('.foo') == '.foo'
        assert b.adjust_suffix('foo') == '.foo'
        assert b.adjust_suffix('$foo') == '$foo'

        class MyBuilder(SCons.Builder.BuilderBase):
            def adjust_suffix(self, suff):
                return "called adjust_suffix()"

        b = MyBuilder()
        ret = b.adjust_suffix('.foo')
        assert ret == "called adjust_suffix()", ret

    def test_prefix(self):
        """Test Builder creation with a specified target prefix

        Make sure that there is no '.' separator appended.
        """
        env = Environment()
        builder = SCons.Builder.Builder(prefix = 'lib.')
        assert builder.get_prefix(env) == 'lib.'
        builder = SCons.Builder.Builder(prefix = 'lib')
        assert builder.get_prefix(env) == 'lib'
        tgt = builder(env, target = 'tgt1', source = 'src1')
        assert tgt.path == 'libtgt1', \
                "Target has unexpected name: %s" % tgt.path
        tgt = builder(env, target = 'tgt2a tgt2b', source = 'src2')
        assert tgt.path == 'libtgt2a tgt2b', \
                "Target has unexpected name: %s" % tgt.path
        tgt = builder(env, source = 'src3')
        assert tgt.path == 'libsrc3', \
                "Target has unexpected name: %s" % tgt.path
        tgt = builder(env, source = 'lib/src4')
        assert tgt.path == os.path.join('lib', 'libsrc4'), \
                "Target has unexpected name: %s" % tgt.path
        tgt = builder(env, target = 'lib/tgt5', source = 'lib/src5')
        assert tgt.path == os.path.join('lib', 'libtgt5'), \
                "Target has unexpected name: %s" % tgt.path

        def gen_prefix(env, sources):
            return "gen_prefix() says " + env['FOO']
        my_env = Environment(FOO = 'xyzzy')
        builder = SCons.Builder.Builder(prefix = gen_prefix)
        assert builder.get_prefix(my_env) == "gen_prefix() says xyzzy"
        my_env['FOO'] = 'abracadabra'
        assert builder.get_prefix(my_env) == "gen_prefix() says abracadabra"

        def my_emit(env, sources):
            return env.subst('$EMIT')
        my_env = Environment(FOO = '.foo', EMIT = 'emit-')
        builder = SCons.Builder.Builder(prefix = {None   : 'default-',
                                                  '.in'  : 'out-',
                                                  '.x'   : 'y-',
                                                  '$FOO' : 'foo-',
                                                  '.zzz' : my_emit})
        tgt = builder(my_env, source = 'f1')
        assert tgt.path == 'default-f1', tgt.path
        tgt = builder(my_env, source = 'f2.c')
        assert tgt.path == 'default-f2', tgt.path
        tgt = builder(my_env, source = 'f3.in')
        assert tgt.path == 'out-f3', tgt.path
        tgt = builder(my_env, source = 'f4.x')
        assert tgt.path == 'y-f4', tgt.path
        tgt = builder(my_env, source = 'f5.foo')
        assert tgt.path == 'foo-f5', tgt.path
        tgt = builder(my_env, source = 'f6.zzz')
        assert tgt.path == 'emit-f6', tgt.path

    def test_src_suffix(self):
        """Test Builder creation with a specified source file suffix
        
        Make sure that the '.' separator is appended to the
        beginning if it isn't already present.
        """
        env = Environment(XSUFFIX = '.x', YSUFFIX = '.y')

        b1 = SCons.Builder.Builder(src_suffix = '.c')
        assert b1.src_suffixes(env) == ['.c'], b1.src_suffixes(env)

        tgt = b1(env, target = 'tgt2', source = 'src2')
        assert tgt.sources[0].path == 'src2.c', \
                "Source has unexpected name: %s" % tgt.sources[0].path

        tgt = b1(env, target = 'tgt3', source = 'src3a src3b')
        assert len(tgt.sources) == 1
        assert tgt.sources[0].path == 'src3a src3b.c', \
                "Unexpected tgt.sources[0] name: %s" % tgt.sources[0].path

        b2 = SCons.Builder.Builder(src_suffix = '.2', src_builder = b1)
        assert b2.src_suffixes(env) == ['.2', '.c'], b2.src_suffixes(env)

        b3 = SCons.Builder.Builder(action = {'.3a' : '', '.3b' : ''})
        s = b3.src_suffixes(env)
        s.sort()
        assert s == ['.3a', '.3b'], s

        b4 = SCons.Builder.Builder(src_suffix = '$XSUFFIX')
        assert b4.src_suffixes(env) == ['.x'], b4.src_suffixes(env)

        b5 = SCons.Builder.Builder(action = { '.y' : ''})
        assert b5.src_suffixes(env) == ['.y'], b5.src_suffixes(env)

    def test_suffix(self):
        """Test Builder creation with a specified target suffix

        Make sure that the '.' separator is appended to the
        beginning if it isn't already present.
        """
        env = Environment()
        builder = SCons.Builder.Builder(suffix = '.o')
        assert builder.get_suffix(env) == '.o', builder.get_suffix(env)
        builder = SCons.Builder.Builder(suffix = 'o')
        assert builder.get_suffix(env) == '.o', builder.get_suffix(env)
        tgt = builder(env, target = 'tgt3', source = 'src3')
        assert tgt.path == 'tgt3.o', \
                "Target has unexpected name: %s" % tgt.path
        tgt = builder(env, target = 'tgt4a tgt4b', source = 'src4')
        assert tgt.path == 'tgt4a tgt4b.o', \
                "Target has unexpected name: %s" % tgt.path
        tgt = builder(env, source = 'src5')
        assert tgt.path == 'src5.o', \
                "Target has unexpected name: %s" % tgt.path

        def gen_suffix(env, sources):
            return "gen_suffix() says " + env['BAR']
        my_env = Environment(BAR = 'hocus pocus')
        builder = SCons.Builder.Builder(suffix = gen_suffix)
        assert builder.get_suffix(my_env) == "gen_suffix() says hocus pocus", builder.get_suffix(my_env)
        my_env['BAR'] = 'presto chango'
        assert builder.get_suffix(my_env) == "gen_suffix() says presto chango"

        def my_emit(env, sources):
            return env.subst('$EMIT')
        my_env = Environment(BAR = '.bar', EMIT = '.emit')
        builder = SCons.Builder.Builder(suffix = {None   : '.default',
                                                  '.in'  : '.out',
                                                  '.x'   : '.y',
                                                  '$BAR' : '.new',
                                                  '.zzz' : my_emit})
        tgt = builder(my_env, source = 'f1')
        assert tgt.path == 'f1.default', tgt.path
        tgt = builder(my_env, source = 'f2.c')
        assert tgt.path == 'f2.default', tgt.path
        tgt = builder(my_env, source = 'f3.in')
        assert tgt.path == 'f3.out', tgt.path
        tgt = builder(my_env, source = 'f4.x')
        assert tgt.path == 'f4.y', tgt.path
        tgt = builder(my_env, source = 'f5.bar')
        assert tgt.path == 'f5.new', tgt.path
        tgt = builder(my_env, source = 'f6.zzz')
        assert tgt.path == 'f6.emit', tgt.path

    def test_ListBuilder(self):
        """Testing ListBuilder class."""
        def function2(target, source, env, tlist = [outfile, outfile2], **kw):
            for t in target:
                open(str(t), 'w').write("function2\n")
            for t in tlist:
                if not t in map(str, target):
                    open(t, 'w').write("function2\n")
            return 1

        env = Environment()
        builder = SCons.Builder.Builder(action = function2)
        tgts = builder(env, target = [outfile, outfile2], source = 'foo')
        for t in tgts:
            t.prepare()
        try:
            tgts[0].build()
        except SCons.Errors.BuildError:
            pass
        c = test.read(outfile, 'r')
        assert c == "function2\n", c
        c = test.read(outfile2, 'r')
        assert c == "function2\n", c

        sub1_out = test.workpath('sub1', 'out')
        sub2_out = test.workpath('sub2', 'out')

        def function3(target, source, env, tlist = [sub1_out, sub2_out]):
            for t in target:
                open(str(t), 'w').write("function3\n")
            for t in tlist:
                if not t in map(str, target):
                    open(t, 'w').write("function3\n")
            return 1

        builder = SCons.Builder.Builder(action = function3)
        tgts = builder(env, target = [sub1_out, sub2_out], source = 'foo')
        for t in tgts:
            t.prepare()
        try:
            tgts[0].build()
        except SCons.Errors.BuildError:
            pass
        c = test.read(sub1_out, 'r')
        assert c == "function3\n", c
        c = test.read(sub2_out, 'r')
        assert c == "function3\n", c
        assert os.path.exists(test.workpath('sub1'))
        assert os.path.exists(test.workpath('sub2'))

    def test_MultiStepBuilder(self):
        """Testing MultiStepBuilder class."""
        env = Environment()
        builder1 = SCons.Builder.Builder(action='foo',
                                         src_suffix='.bar',
                                         suffix='.foo')
        builder2 = SCons.Builder.MultiStepBuilder(action='bar',
                                                  src_builder = builder1,
                                                  src_suffix = '.foo')

        tgt = builder2(env, target='baz', source=['test.bar', 'test2.foo', 'test3.txt'])
        assert str(tgt.sources[0]) == 'test.foo', str(tgt.sources[0])
        assert str(tgt.sources[0].sources[0]) == 'test.bar', \
               str(tgt.sources[0].sources[0])
        assert str(tgt.sources[1]) == 'test2.foo', str(tgt.sources[1])
        assert str(tgt.sources[2]) == 'test3.txt', str(tgt.sources[2])

        tgt = builder2(env, 'aaa.bar')
        assert str(tgt) == 'aaa', str(tgt)
        assert str(tgt.sources[0]) == 'aaa.foo', str(tgt.sources[0])
        assert str(tgt.sources[0].sources[0]) == 'aaa.bar', \
               str(tgt.sources[0].sources[0])

        builder3 = SCons.Builder.MultiStepBuilder(action = 'foo',
                                                  src_builder = 'xyzzy',
                                                  src_suffix = '.xyzzy')
        assert builder3.get_src_builders(Environment()) == []

        builder4 = SCons.Builder.Builder(action='bld4',
                                         src_suffix='.i',
                                         suffix='_wrap.c')
        builder5 = SCons.Builder.MultiStepBuilder(action='bld5',
                                                  src_builder=builder4,
                                                  suffix='.obj',
                                                  src_suffix='.c')
        builder6 = SCons.Builder.MultiStepBuilder(action='bld6',
                                                  src_builder=builder5,
                                                  suffix='.exe',
                                                  src_suffix='.obj')
        tgt = builder6(env, 'test', 'test.i')
        assert str(tgt) == 'test.exe', str(tgt)
        assert str(tgt.sources[0]) == 'test_wrap.obj', str(tgt.sources[0])
        assert str(tgt.sources[0].sources[0]) == 'test_wrap.c', \
               str(tgt.sources[0].sources[0])
        assert str(tgt.sources[0].sources[0].sources[0]) == 'test.i', \
               str(tgt.sources[0].sources[0].sources[0])
        
    def test_CompositeBuilder(self):
        """Testing CompositeBuilder class."""
        def func_action(target, source, env):
            return 0
        
        env = Environment(BAR_SUFFIX = '.BAR2', FOO_SUFFIX = '.FOO2')
        builder = SCons.Builder.Builder(action={ '.foo' : func_action,
                                                 '.bar' : func_action,
                                                 '$BAR_SUFFIX' : func_action,
                                                 '$FOO_SUFFIX' : func_action })
        
        assert isinstance(builder, SCons.Builder.CompositeBuilder)
        assert isinstance(builder.action, SCons.Action.CommandGeneratorAction)
        tgt = builder(env, target='test1', source='test1.foo')
        assert isinstance(tgt.builder, SCons.Builder.BuilderBase)
        assert tgt.builder.action is builder.action
        tgt = builder(env, target='test2', source='test1.bar')
        assert isinstance(tgt.builder, SCons.Builder.BuilderBase)
        assert tgt.builder.action is builder.action
        flag = 0
        tgt = builder(env, target='test3', source=['test2.bar', 'test1.foo'])
        try:
            tgt.build()
        except SCons.Errors.UserError, e:
            flag = 1
        assert flag, "UserError should be thrown when we build targets with files of different suffixes."
        match = str(e) == "While building `['test3']' from `test1.foo': Cannot build multiple sources with different extensions: .bar, .foo"
        assert match, e

        tgt = builder(env, target='test4', source=['test4.BAR2'])
        assert isinstance(tgt.builder, SCons.Builder.BuilderBase)
        try:
            tgt.build()
            flag = 1
        except SCons.Errors.UserError, e:
            print e
            flag = 0
        assert flag, "It should be possible to define actions in composite builders using variables."
        env['FOO_SUFFIX'] = '.BAR2'
        builder.add_action('$NEW_SUFFIX', func_action)
        flag = 0
        tgt = builder(env, target='test5', source=['test5.BAR2'])
        try:
            tgt.build()
        except SCons.Errors.UserError:
            flag = 1
        assert flag, "UserError should be thrown when we build targets with ambigous suffixes."
        del env.d['FOO_SUFFIX']
        del env.d['BAR_SUFFIX']

        foo_bld = SCons.Builder.Builder(action = 'a-foo',
                                        src_suffix = '.ina',
                                        suffix = '.foo')
        assert isinstance(foo_bld, SCons.Builder.BuilderBase)
        builder = SCons.Builder.Builder(action = { '.foo' : 'foo',
                                                   '.bar' : 'bar' },
                                        src_builder = foo_bld)
        assert isinstance(builder, SCons.Builder.CompositeBuilder)
        assert isinstance(builder.action, SCons.Action.CommandGeneratorAction)

        tgt = builder(env, target='t1', source='t1a.ina t1b.ina')
        assert isinstance(tgt.builder, SCons.Builder.BuilderBase)

        tgt = builder(env, target='t2', source='t2a.foo t2b.ina')
        assert isinstance(tgt.builder, SCons.Builder.MultiStepBuilder), tgt.builder.__dict__

        bar_bld = SCons.Builder.Builder(action = 'a-bar',
                                        src_suffix = '.inb',
                                        suffix = '.bar')
        assert isinstance(bar_bld, SCons.Builder.BuilderBase)
        builder = SCons.Builder.Builder(action = { '.foo' : 'foo'},
                                        src_builder = [foo_bld, bar_bld])
        assert isinstance(builder, SCons.Builder.CompositeBuilder)
        assert isinstance(builder.action, SCons.Action.CommandGeneratorAction)

        builder.add_action('.bar', 'bar')

        tgt = builder(env, target='t3-foo', source='t3a.foo t3b.ina')
        assert isinstance(tgt.builder, SCons.Builder.MultiStepBuilder)

        tgt = builder(env, target='t3-bar', source='t3a.bar t3b.inb')
        assert isinstance(tgt.builder, SCons.Builder.MultiStepBuilder)

        flag = 0
        tgt = builder(env, target='t5', source=['test5a.foo', 'test5b.inb'])
        try:
            tgt.build()
        except SCons.Errors.UserError, e:
            flag = 1
        assert flag, "UserError should be thrown when we build targets with files of different suffixes."
        match = str(e) == "While building `['t5']' from `test5b.bar': Cannot build multiple sources with different extensions: .foo, .bar"
        assert match, e

        flag = 0
        tgt = builder(env, target='t6', source=['test6a.bar', 'test6b.ina'])
        try:
            tgt.build()
        except SCons.Errors.UserError, e:
            flag = 1
        assert flag, "UserError should be thrown when we build targets with files of different suffixes."
        match = str(e) == "While building `['t6']' from `test6b.foo': Cannot build multiple sources with different extensions: .bar, .foo"
        assert match, e

        flag = 0
        tgt = builder(env, target='t4', source=['test4a.ina', 'test4b.inb'])
        try:
            tgt.build()
        except SCons.Errors.UserError, e:
            flag = 1
        assert flag, "UserError should be thrown when we build targets with files of different suffixes."
        match = str(e) == "While building `['t4']' from `test4b.bar': Cannot build multiple sources with different extensions: .foo, .bar"
        assert match, e

        flag = 0
        tgt = builder(env, target='t7', source=['test7'])
        try:
            tgt.build()
        except SCons.Errors.UserError, e:
            flag = 1
        assert flag, "UserError should be thrown when we build targets with files of different suffixes."
        match = str(e) == "While building `['t7']': Cannot deduce file extension from source files: ['test7']"
        assert match, e

        flag = 0
        tgt = builder(env, target='t8', source=['test8.unknown'])
        try:
            tgt.build()
        except SCons.Errors.UserError, e:
            flag = 1
        assert flag, "UserError should be thrown when we build targets with files of different suffixes."
        match = str(e) == "While building `['t8']': Don't know how to build a file with suffix `.unknown'."
        assert match, e

    def test_build_scanner(self):
        """Testing ability to set a target scanner through a builder."""
        global instanced
        class TestScanner:
            pass
        scn = TestScanner()
        env = Environment()
        builder = SCons.Builder.Builder(scanner=scn)
        tgt = builder(env, target='foo2', source='bar')
        assert tgt.target_scanner == scn, tgt.target_scanner

        builder1 = SCons.Builder.Builder(action='foo',
                                         src_suffix='.bar',
                                         suffix='.foo')
        builder2 = SCons.Builder.Builder(action='foo',
                                         src_builder = builder1,
                                         scanner = scn)
        tgt = builder2(env, target='baz2', source='test.bar test2.foo test3.txt')
        assert tgt.target_scanner == scn, tgt.target_scanner

    def test_src_scanner(slf):
        """Testing ability to set a source file scanner through a builder."""
        class TestScanner:
            def key(self, env):
                 return 'TestScannerkey'
            def instance(self, env):
                 return self

        scanner = TestScanner()
        builder = SCons.Builder.Builder(action='action')

        # With no scanner specified, source_scanner is None.
        env1 = Environment()
        tgt = builder(env1, target='foo1.x', source='bar.y')
        src = tgt.sources[0]
        assert tgt.target_scanner != scanner, tgt.target_scanner
        assert src.source_scanner is None, src.source_scanner

        # Later use of the same source file with an environment that
        # has a scanner must still set the scanner.
        env2 = Environment()
        env2.scanner = scanner
        tgt = builder(env2, target='foo2.x', source='bar.y')
        src = tgt.sources[0]
        assert tgt.target_scanner != scanner, tgt.target_scanner
        assert src.source_scanner == scanner, src.source_scanner

    def test_Builder_Args(self):
        """Testing passing extra args to a builder."""
        def buildFunc(target, source, env, s=self):
            s.foo=env['foo']
            s.bar=env['bar']
            assert env['CC'] == 'mycc'

        env=Environment(CC='cc')

        builder = SCons.Builder.Builder(action=buildFunc)
        tgt = builder(env, target='foo', source='bar', foo=1, bar=2, CC='mycc')
        tgt.build()
        assert self.foo == 1, self.foo
        assert self.bar == 2, self.bar

    def test_emitter(self):
        """Test emitter functions."""
        def emit(target, source, env):
            foo = env.get('foo', 0)
            bar = env.get('bar', 0)
            for t in target:
                assert isinstance(t, MyNode)
                assert t.has_builder()
            for s in source:
                assert isinstance(s, MyNode)
            if foo:
                target.append("bar%d"%foo)
            if bar:
                source.append("baz")
            return ( target, source )

        env = Environment()
        builder = SCons.Builder.Builder(action='foo',
                                        emitter=emit,
                                        node_factory=MyNode)
        tgt = builder(env, target='foo2', source='bar')
        assert str(tgt) == 'foo2', str(tgt)
        assert str(tgt.sources[0]) == 'bar', str(tgt.sources[0])

        tgt = builder(env, target='foo3', source='bar', foo=1)
        assert len(tgt) == 2, len(tgt)
        assert 'foo3' in map(str, tgt), map(str, tgt)
        assert 'bar1' in map(str, tgt), map(str, tgt)

        tgt = builder(env, target='foo4', source='bar', bar=1)
        assert str(tgt) == 'foo4', str(tgt)
        assert len(tgt.sources) == 2, len(tgt.sources)
        assert 'baz' in map(str, tgt.sources), map(str, tgt.sources)
        assert 'bar' in map(str, tgt.sources), map(str, tgt.sources)

        env2=Environment(FOO=emit)
        builder2=SCons.Builder.Builder(action='foo',
                                       emitter="$FOO",
                                       node_factory=MyNode)

        tgt = builder2(env2, target='foo5', source='bar')
        assert str(tgt) == 'foo5', str(tgt)
        assert str(tgt.sources[0]) == 'bar', str(tgt.sources[0])

        tgt = builder2(env2, target='foo6', source='bar', foo=2)
        assert len(tgt) == 2, len(tgt)
        assert 'foo6' in map(str, tgt), map(str, tgt)
        assert 'bar2' in map(str, tgt), map(str, tgt)

        tgt = builder2(env2, target='foo7', source='bar', bar=1)
        assert str(tgt) == 'foo7', str(tgt)
        assert len(tgt.sources) == 2, len(tgt.sources)
        assert 'baz' in map(str, tgt.sources), map(str, tgt.sources)
        assert 'bar' in map(str, tgt.sources), map(str, tgt.sources)

        builder2a=SCons.Builder.Builder(action='foo',
                                        emitter="$FOO",
                                        node_factory=MyNode)
        assert builder2 == builder2a, repr(builder2.__dict__) + "\n" + repr(builder2a.__dict__)

        # Test that, if an emitter sets a builder on the passed-in
        # targets and passes back new targets, the new builder doesn't
        # get overwritten.
        new_builder = SCons.Builder.Builder(action='new')
        node = MyNode('foo8')
        new_node = MyNode('foo8.new')
        def emit3(target, source, env, nb=new_builder, nn=new_node):
            for t in target:
                t.builder = nb
            return [nn], source
            
        builder3=SCons.Builder.Builder(action='foo',
                                       emitter=emit3,
                                       node_factory=MyNode)
        tgt = builder3(env, target=node, source='bar')
        assert tgt is new_node, tgt
        assert tgt.builder is builder3, tgt.builder
        assert node.builder is new_builder, node.builder

        # Test use of a dictionary mapping file suffixes to
        # emitter functions
        def emit4a(target, source, env):
            source = map(str, source)
            target = map(lambda x: 'emit4a-' + x[:-3], source)
            return (target, source)
        def emit4b(target, source, env):
            source = map(str, source)
            target = map(lambda x: 'emit4b-' + x[:-3], source)
            return (target, source)
        builder4 = SCons.Builder.Builder(action='foo',
                                         emitter={'.4a':emit4a,
                                                  '.4b':emit4b},
                                         node_factory=MyNode)
        tgt = builder4(env, source='aaa.4a')
        assert str(tgt) == 'emit4a-aaa', str(tgt)
        tgt = builder4(env, source='bbb.4b')
        assert str(tgt) == 'emit4b-bbb', str(tgt)
        tgt = builder4(env, source='ccc.4c')
        assert str(tgt) == 'ccc', str(tgt)

        def emit4c(target, source, env):
            source = map(str, source)
            target = map(lambda x: 'emit4c-' + x[:-3], source)
            return (target, source)
        builder4.add_emitter('.4c', emit4c)
        tgt = builder4(env, source='ccc.4c')
        assert str(tgt) == 'emit4c-ccc', str(tgt)

        # Test a list of emitter functions.
        def emit5a(target, source, env):
            source = map(str, source)
            target = target + map(lambda x: 'emit5a-' + x[:-2], source)
            return (target, source)
        def emit5b(target, source, env):
            source = map(str, source)
            target = target + map(lambda x: 'emit5b-' + x[:-2], source)
            return (target, source)
        builder5 = SCons.Builder.Builder(action='foo',
                                         emitter=[emit5a, emit5b],
                                         node_factory=MyNode)

        tgts = builder5(env, target='target-5', source='aaa.5')
        tgts = map(str, tgts)
        assert tgts == ['target-5', 'emit5a-aaa', 'emit5b-aaa'], tgts

        # Test a list of emitter functions through the environment.
        def emit6a(target, source, env):
            source = map(str, source)
            target = target + map(lambda x: 'emit6a-' + x[:-2], source)
            return (target, source)
        def emit6b(target, source, env):
            source = map(str, source)
            target = target + map(lambda x: 'emit6b-' + x[:-2], source)
            return (target, source)
        builder6 = SCons.Builder.Builder(action='foo',
                                         emitter='$EMITTERLIST',
                                         node_factory=MyNode)
                                         
        env = Environment(EMITTERLIST = [emit6a, emit6b])

        tgts = builder6(env, target='target-6', source='aaa.6')
        tgts = map(str, tgts)
        assert tgts == ['target-6', 'emit6a-aaa', 'emit6b-aaa'], tgts

    def test_no_target(self):
        """Test deducing the target from the source."""

        env = Environment()
        b = SCons.Builder.Builder(action='foo', suffix='.o')

        tgt = b(env, 'aaa')
        assert str(tgt) == 'aaa.o', str(tgt)
        assert len(tgt.sources) == 1, map(str, tgt.sources)
        assert str(tgt.sources[0]) == 'aaa', map(str, tgt.sources)

        tgt = b(env, 'bbb.c')
        assert str(tgt) == 'bbb.o', str(tgt)
        assert len(tgt.sources) == 1, map(str, tgt.sources)
        assert str(tgt.sources[0]) == 'bbb.c', map(str, tgt.sources)

        tgt = b(env, 'ccc.x.c')
        assert str(tgt) == 'ccc.x.o', str(tgt)
        assert len(tgt.sources) == 1, map(str, tgt.sources)
        assert str(tgt.sources[0]) == 'ccc.x.c', map(str, tgt.sources)

        tgt = b(env, ['d0.c', 'd1.c'])
        assert str(tgt) == 'd0.o', str(tgt)
        assert len(tgt.sources) == 2,  map(str, tgt.sources)
        assert str(tgt.sources[0]) == 'd0.c', map(str, tgt.sources)
        assert str(tgt.sources[1]) == 'd1.c', map(str, tgt.sources)

        tgt = b(env, source='eee')
        assert str(tgt) == 'eee.o', str(tgt)
        assert len(tgt.sources) == 1, map(str, tgt.sources)
        assert str(tgt.sources[0]) == 'eee', map(str, tgt.sources)

        tgt = b(env, source='fff.c')
        assert str(tgt) == 'fff.o', str(tgt)
        assert len(tgt.sources) == 1, map(str, tgt.sources)
        assert str(tgt.sources[0]) == 'fff.c', map(str, tgt.sources)

        tgt = b(env, source='ggg.x.c')
        assert str(tgt) == 'ggg.x.o', str(tgt)
        assert len(tgt.sources) == 1, map(str, tgt.sources)
        assert str(tgt.sources[0]) == 'ggg.x.c', map(str, tgt.sources)

        tgt = b(env, source=['h0.c', 'h1.c'])
        assert str(tgt) == 'h0.o', str(tgt)
        assert len(tgt.sources) == 2,  map(str, tgt.sources)
        assert str(tgt.sources[0]) == 'h0.c', map(str, tgt.sources)
        assert str(tgt.sources[1]) == 'h1.c', map(str, tgt.sources)

        w = b(env, target='i0.w', source=['i0.x'])
        y = b(env, target='i1.y', source=['i1.z'])
        tgt = b(env, source=[w, y])
        assert str(tgt) == 'i0.o', str(tgt)
        assert len(tgt.sources) == 2, map(str, tgt.sources)
        assert str(tgt.sources[0]) == 'i0.w', map(str, tgt.sources)
        assert str(tgt.sources[1]) == 'i1.y', map(str, tgt.sources)


if __name__ == "__main__":
    suite = unittest.makeSuite(BuilderTestCase, 'test_')
    if not unittest.TextTestRunner().run(suite).wasSuccessful():
        sys.exit(1)