Change the double quotes around an up-to-date target to be like Make.
[scons.git] / test / Scanner.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 import sys
28 import TestSCons
29
30 python = TestSCons.python
31
32 test = TestSCons.TestSCons()
33
34 test.write('build.py', r"""
35 import sys
36 input = open(sys.argv[1], 'rb')
37 output = open(sys.argv[2], 'wb')
38
39 def process(infp, outfp):
40     for line in infp.readlines():
41         if line[:8] == 'include ':
42             file = line[8:-1]
43             process(open(file, 'rb'), outfp)
44         else:
45             outfp.write(line)
46
47 process(input, output)
48
49 sys.exit(0)
50 """)
51
52 # Execute a subsidiary SConscript just to make sure we can
53 # get at the Scanner keyword from there.
54
55 test.write('SConstruct', """
56 SConscript('SConscript')
57 """)
58
59 test.write('SConscript', """
60 import re
61
62 include_re = re.compile(r'^include\s+(\S+)$', re.M)
63
64 def kfile_scan(node, env, target, arg):
65     contents = node.get_contents()
66     includes = include_re.findall(contents)
67     return includes
68
69 kscan = Scanner(name = 'kfile',
70                 function = kfile_scan,
71                 argument = None,
72                 skeys = ['.k'])
73 scanners = Environment().Dictionary('SCANNERS')
74 env = Environment(SCANNERS = scanners + [kscan])
75
76 env.Command('foo', 'foo.k', r'%s build.py $SOURCES $TARGET')
77
78 bar_in = File('bar.in')
79 env.Command('bar', bar_in, r'%s build.py $SOURCES  $TARGET')
80 bar_in.source_scanner = kscan
81 """ % (python, python))
82
83 test.write('foo.k', 
84 """foo.k 1 line 1
85 include xxx
86 include yyy
87 foo.k 1 line 4
88 """)
89
90 test.write('bar.in', 
91 """include yyy
92 bar.in 1 line 2
93 bar.in 1 line 3
94 include zzz
95 """)
96
97 test.write('xxx', "xxx 1\n")
98
99 test.write('yyy', "yyy 1\n")
100
101 test.write('zzz', "zzz 1\n")
102
103 test.run(arguments = '.')
104
105 test.fail_test(test.read('foo') != "foo.k 1 line 1\nxxx 1\nyyy 1\nfoo.k 1 line 4\n")
106
107 test.fail_test(test.read('bar') != "yyy 1\nbar.in 1 line 2\nbar.in 1 line 3\nzzz 1\n")
108
109 test.up_to_date(arguments = '.')
110
111 test.write('xxx', "xxx 2\n")
112
113 test.run(arguments = '.')
114
115 test.fail_test(test.read('foo') != "foo.k 1 line 1\nxxx 2\nyyy 1\nfoo.k 1 line 4\n")
116
117 test.fail_test(test.read('bar') != "yyy 1\nbar.in 1 line 2\nbar.in 1 line 3\nzzz 1\n")
118
119 test.write('yyy', "yyy 2\n")
120
121 test.run(arguments = '.')
122
123 test.fail_test(test.read('foo') != "foo.k 1 line 1\nxxx 2\nyyy 2\nfoo.k 1 line 4\n")
124
125 test.fail_test(test.read('bar') != "yyy 2\nbar.in 1 line 2\nbar.in 1 line 3\nzzz 1\n")
126
127 test.write('zzz', "zzz 2\n")
128
129 test.run(arguments = '.')
130
131 test.fail_test(test.read('foo') != "foo.k 1 line 1\nxxx 2\nyyy 2\nfoo.k 1 line 4\n")
132
133 test.fail_test(test.read('bar') != "yyy 2\nbar.in 1 line 2\nbar.in 1 line 3\nzzz 2\n")
134
135 test.up_to_date(arguments = 'foo')
136
137 test.pass_test()