[ci skip] update generated files
[scons.git] / test / Scanner / dictionary.py
blobf80c7e55d6766540a12278e61dc1b6a92462233f
1 #!/usr/bin/env python
3 # __COPYRIGHT__
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:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
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.
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
27 """
28 Verify
29 """
31 import TestSCons
33 _python_ = TestSCons._python_
35 test = TestSCons.TestSCons()
37 test.write('build.py', r"""
38 import sys
40 include_prefix = 'include%s ' % sys.argv[1][-1]
42 def process(infp, outfp):
43 for line in infp.readlines():
44 if line[:len(include_prefix)] == include_prefix:
45 file = line[len(include_prefix):-1]
46 with open(file, 'r') as f:
47 process(f, outfp)
48 else:
49 outfp.write(line)
51 with open(sys.argv[2], 'w') as ofp, open(sys.argv[1], 'r') as ifp:
52 process(ifp, ofp)
54 sys.exit(0)
55 """)
57 # Execute a subsidiary SConscript just to make sure we can
58 # get at the Scanner keyword from there.
60 test.write('SConstruct', """
61 SConscript('SConscript')
62 """)
64 test.write('SConscript', r"""
65 import re
67 include1_re = re.compile(r'^include1\s+(\S+)$', re.M)
68 include2_re = re.compile(r'^include2\s+(\S+)$', re.M)
69 include3_re = re.compile(r'^include3\s+(\S+)$', re.M)
71 def kfile_scan1(node, env, scanpaths, arg=None):
72 contents = node.get_text_contents()
73 includes = include1_re.findall(contents)
74 return includes
76 def kfile_scan2(node, env, scanpaths, arg=None):
77 contents = node.get_text_contents()
78 includes = include2_re.findall(contents)
79 return includes
81 def kfile_scan3(node, env, scanpaths, arg=None):
82 contents = node.get_text_contents()
83 includes = include3_re.findall(contents)
84 return includes
86 scan1 = Scanner(kfile_scan1)
88 scan2 = Scanner(kfile_scan2)
90 scan3 = Scanner(kfile_scan3)
92 kscanner = Scanner({'.k1' : scan1, '.k2': scan2})
94 env = Environment(SCANNERS = [kscanner])
96 kscanner.add_scanner('.k3', scan3)
98 env.Command('aaa', 'aaa.k1', r'%(_python_)s build.py $SOURCES $TARGET')
99 env.Command('bbb', 'bbb.k2', r'%(_python_)s build.py $SOURCES $TARGET')
100 env.Command('ccc', 'ccc.k3', r'%(_python_)s build.py $SOURCES $TARGET')
101 """ % locals())
103 test.write('aaa.k1',
104 """aaa.k1 1
105 line 2
106 include1 xxx
107 include2 yyy
108 include3 zzz
109 line 6
110 """)
112 test.write('bbb.k2',
113 """bbb.k2 1
114 line 2
115 include1 xxx
116 include2 yyy
117 include3 zzz
118 line 6
119 """)
121 test.write('ccc.k3',
122 """ccc.k3 1
123 line 2
124 include1 xxx
125 include2 yyy
126 include3 zzz
127 line 6
128 """)
130 test.write('xxx', "xxx 1\n")
131 test.write('yyy', "yyy 1\n")
132 test.write('zzz', "zzz 1\n")
137 expect = test.wrap_stdout("""\
138 %(_python_)s build.py aaa.k1 aaa
139 %(_python_)s build.py bbb.k2 bbb
140 %(_python_)s build.py ccc.k3 ccc
141 """ % locals())
143 test.run(stdout=expect)
145 expect_aaa = 'aaa.k1 1\nline 2\nxxx 1\ninclude2 yyy\ninclude3 zzz\nline 6\n'
146 expect_bbb = 'bbb.k2 1\nline 2\ninclude1 xxx\nyyy 1\ninclude3 zzz\nline 6\n'
147 expect_ccc = 'ccc.k3 1\nline 2\ninclude1 xxx\ninclude2 yyy\nzzz 1\nline 6\n'
149 test.must_match('aaa', expect_aaa, mode='r')
150 test.must_match('bbb', expect_bbb, mode='r')
151 test.must_match('ccc', expect_ccc, mode='r')
153 test.up_to_date(arguments = '.')
157 test.write('zzz', "zzz 2\n")
159 expect = test.wrap_stdout("""\
160 %(_python_)s build.py ccc.k3 ccc
161 """ % locals())
163 test.run(stdout=expect)
165 expect_ccc = 'ccc.k3 1\nline 2\ninclude1 xxx\ninclude2 yyy\nzzz 2\nline 6\n'
167 test.must_match('bbb', expect_bbb, mode='r')
171 test.write('yyy', "yyy 2\n")
173 expect = test.wrap_stdout("""\
174 %(_python_)s build.py bbb.k2 bbb
175 """ % locals())
177 test.run(stdout=expect)
179 expect_bbb = 'bbb.k2 1\nline 2\ninclude1 xxx\nyyy 2\ninclude3 zzz\nline 6\n'
181 test.must_match('bbb', expect_bbb, mode='r')
185 test.write('xxx', "xxx 2\n")
187 expect = test.wrap_stdout("""\
188 %(_python_)s build.py aaa.k1 aaa
189 """ % locals())
191 test.run(stdout=expect)
193 expect_aaa = 'aaa.k1 1\nline 2\nxxx 2\ninclude2 yyy\ninclude3 zzz\nline 6\n'
195 test.must_match('bbb', expect_bbb, mode='r')
199 test.pass_test()
201 # Local Variables:
202 # tab-width:4
203 # indent-tabs-mode:nil
204 # End:
205 # vim: set expandtab tabstop=4 shiftwidth=4: