Issue 2255: Handle scanning of UTF-8 and UTF-16 files. (Greg Spencer)
[scons.git] / test / srcchange.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 """
28 Test changing the C source files based on an always-executed revision
29 extraction and substitution.
30
31 This makes sure we evaluate the content of intermediate files as
32 expected.  We used to configure this explicitly using
33 TargetSignatures('content') but we now rely on the default behavior
34 being the equivalent of Decider('content').
35 """
36
37 import os.path
38
39 import TestSCons
40
41 _python_ = TestSCons._python_
42
43 test = TestSCons.TestSCons()
44
45 test.write('getrevision', """
46 #!/usr/bin/env python
47 import string
48 print string.strip(open('revnum.in','rb').read())
49 """)
50
51 test.write('SConstruct', """
52 import re
53 import string
54
55 def subrevision(target, source ,env):
56     orig = target[0].get_text_contents()
57     new = re.sub('\$REV.*?\$',
58                  '$REV: %%s$'%%string.strip(source[0].get_text_contents()),
59                  target[0].get_text_contents())
60     outf = open(str(target[0]),'wb')
61     outf.write(new)
62     outf.close()
63
64 SubRevision = Action(subrevision)
65
66 env=Environment()
67 content_env=env.Clone()
68 content_env.Command('revision.in', [], '%(_python_)s getrevision > $TARGET')
69 content_env.AlwaysBuild('revision.in')
70 env.Precious('main.c')
71 env.Command('main.c', 'revision.in', SubRevision)
72 exe = env.Program('main.c')
73 env.Default(exe)
74 """ % locals())
75
76 test.write('main.c', """\
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <stdio.h>
80 int
81 main(int argc, char *argv[])
82 {
83     printf("Revision $REV$\\n");
84     exit (0);
85 }
86 """)
87
88 test.write('revnum.in', '3.2\n')
89
90 prog = 'main' + TestSCons._exe
91
92 light_build=test.wrap_stdout("""\
93 %(_python_)s getrevision > revision.in
94 """ % locals())
95
96 test.run(arguments='.')
97 test.must_exist(prog)
98 test.run(program=test.workpath(prog), stdout='Revision $REV: 3.2$\n')
99
100 test.run(arguments='.', stdout=light_build)
101 test.must_exist(prog)
102
103 test.run(arguments='.', stdout=light_build)
104 test.must_exist(prog)
105
106 test.write('revnum.in', '3.3\n')
107
108 test.run(arguments='.')
109 test.must_exist(prog)
110 test.run(program=test.workpath(prog), stdout='Revision $REV: 3.3$\n')
111
112 test.pass_test()