5b785a392ed0fbf172c22f37b106b31082ff59c2
[scons.git] / test / Program.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 os.path
28 import sys
29 import time
30 import TestSCons
31
32 _exe = TestSCons._exe
33
34 test = TestSCons.TestSCons()
35
36 foo1 = test.workpath('foo1' + _exe)
37 foo2 = test.workpath('foo2' + _exe)
38 foo3 = test.workpath('foo3' + _exe)
39 foo4 = test.workpath('foo4' + _exe)
40 foo5 = test.workpath('foo5' + _exe)
41 foo_args = 'foo1%s foo2%s foo3%s foo4%s foo5%s' % (_exe, _exe, _exe, _exe, _exe)
42
43 test.write('SConstruct', """
44 env = Environment()
45 env.Program(target = 'foo1', source = 'f1.c')
46 env.Program(target = 'foo2', source = Split('f2a.c f2b.c f2c.c'))
47 f3a = File('f3a.c')
48 f3b = File('f3b.c')
49 Program(target = 'foo3', source = [f3a, [f3b, 'f3c.c']])
50 env.Program('foo4', 'f4.c')
51 env.Program('foo5.c')
52 """)
53
54 test.write('f1.c', r"""
55 int
56 main(int argc, char *argv[])
57 {
58         argv[argc++] = "--";
59         printf("f1.c\n");
60         exit (0);
61 }
62 """)
63
64 test.write('f2a.c', r"""
65 void
66 f2a(void)
67 {
68         printf("f2a.c\n");
69 }
70 """)
71
72 test.write('f2b.c', r"""
73 void
74 f2b(void)
75 {
76         printf("f2b.c\n");
77 }
78 """)
79
80 test.write('f2c.c', r"""
81 extern void f2a(void);
82 extern void f2b(void);
83 int
84 main(int argc, char *argv[])
85 {
86         argv[argc++] = "--";
87         f2a();
88         f2b();
89         printf("f2c.c\n");
90         exit (0);
91 }
92 """)
93
94 test.write('f3a.c', r"""
95 void
96 f3a(void)
97 {
98         printf("f3a.c\n");
99 }
100 """)
101
102 test.write('f3b.c', r"""
103 void
104 f3b(void)
105 {
106         printf("f3b.c\n");
107 }
108 """)
109
110 test.write('f3c.c', r"""
111 extern void f3a(void);
112 extern void f3b(void);
113 int
114 main(int argc, char *argv[])
115 {
116         argv[argc++] = "--";
117         f3a();
118         f3b();
119         printf("f3c.c\n");
120         exit (0);
121 }
122 """)
123
124 test.write('f4.c', r"""
125 int
126 main(int argc, char *argv[])
127 {
128         argv[argc++] = "--";
129         printf("f4.c\n");
130         exit (0);
131 }
132 """)
133
134 test.write('foo5.c', r"""
135 int
136 main(int argc, char *argv[])
137 {
138         argv[argc++] = "--";
139         printf("foo5.c\n");
140         exit (0);
141 }
142 """)
143
144 test.run(arguments = '.')
145
146 test.run(program = foo1, stdout = "f1.c\n")
147 test.run(program = foo2, stdout = "f2a.c\nf2b.c\nf2c.c\n")
148 test.run(program = foo3, stdout = "f3a.c\nf3b.c\nf3c.c\n")
149 test.run(program = foo4, stdout = "f4.c\n")
150 test.run(program = foo5, stdout = "foo5.c\n")
151
152 test.up_to_date(arguments = '.')
153
154 test.write('f1.c', r"""
155 int
156 main(int argc, char *argv[])
157 {
158         argv[argc++] = "--";
159         printf("f1.c X\n");
160         exit (0);
161 }
162 """)
163
164 test.write('f3b.c', r"""
165 void
166 f3b(void)
167 {
168         printf("f3b.c X\n");
169 }
170 """)
171
172 test.write('f4.c', r"""
173 int
174 main(int argc, char *argv[])
175 {
176         argv[argc++] = "--";
177         printf("f4.c X\n");
178         exit (0);
179 }
180 """)
181
182 test.write('foo5.c', r"""
183 int
184 main(int argc, char *argv[])
185 {
186         argv[argc++] = "--";
187         printf("foo5.c X\n");
188         exit (0);
189 }
190 """)
191
192 test.run(arguments = '.')
193
194 test.run(program = foo1, stdout = "f1.c X\n")
195 test.run(program = foo2, stdout = "f2a.c\nf2b.c\nf2c.c\n")
196 test.run(program = foo3, stdout = "f3a.c\nf3b.c X\nf3c.c\n")
197 test.run(program = foo4, stdout = "f4.c X\n")
198 test.run(program = foo5, stdout = "foo5.c X\n")
199
200 test.up_to_date(arguments = '.')
201
202 # make sure the programs didn't get rebuilt, because nothing changed:
203 oldtime1 = os.path.getmtime(foo1)
204 oldtime2 = os.path.getmtime(foo2)
205 oldtime3 = os.path.getmtime(foo3)
206 oldtime4 = os.path.getmtime(foo4)
207 oldtime5 = os.path.getmtime(foo5)
208
209 time.sleep(2) # introduce a small delay, to make the test valid
210
211 test.run(arguments = foo_args)
212
213 test.fail_test(oldtime1 != os.path.getmtime(foo1))
214 test.fail_test(oldtime2 != os.path.getmtime(foo2))
215 test.fail_test(oldtime3 != os.path.getmtime(foo3))
216 test.fail_test(oldtime4 != os.path.getmtime(foo4))
217 test.fail_test(oldtime5 != os.path.getmtime(foo5))
218
219 test.write('f1.c', r"""
220 int
221 main(int argc, char *argv[])
222 {
223         argv[argc++] = "--";
224         printf("f1.c Y\n");
225         exit (0);
226 }
227 """)
228
229 test.write('f3b.c', r"""
230 void
231 f3b(void)
232 {
233         printf("f3b.c Y\n");
234 }
235 """)
236
237 test.write('f4.c', r"""
238 int
239 main(int argc, char *argv[])
240 {
241         argv[argc++] = "--";
242         printf("f4.c Y\n");
243         exit (0);
244 }
245 """)
246
247 test.write('foo5.c', r"""
248 int
249 main(int argc, char *argv[])
250 {
251         argv[argc++] = "--";
252         printf("foo5.c Y\n");
253         exit (0);
254 }
255 """)
256
257 test.run(arguments = foo_args)
258
259 test.run(program = foo1, stdout = "f1.c Y\n")
260 test.run(program = foo2, stdout = "f2a.c\nf2b.c\nf2c.c\n")
261 test.run(program = foo3, stdout = "f3a.c\nf3b.c Y\nf3c.c\n")
262 test.run(program = foo4, stdout = "f4.c Y\n")
263 test.run(program = foo5, stdout = "foo5.c Y\n")
264
265 test.up_to_date(arguments = foo_args)
266
267 test.write('f1.c', r"""
268 int
269 main(int argc, char *argv[])
270 {
271         argv[argc++] = "--";
272         printf("f1.c Z\n");
273         exit (0);
274 }
275 """)
276
277 test.write('f3b.c', r"""
278 void
279 f3b(void)
280 {
281         printf("f3b.c Z\n");
282 }
283 """)
284
285 test.write('f4.c', r"""
286 int
287 main(int argc, char *argv[])
288 {
289         argv[argc++] = "--";
290         printf("f4.c Z\n");
291         exit (0);
292 }
293 """)
294
295 test.write('foo5.c', r"""
296 int
297 main(int argc, char *argv[])
298 {
299         argv[argc++] = "--";
300         printf("foo5.c Z\n");
301         exit (0);
302 }
303 """)
304
305 test.run(arguments = foo_args)
306
307 test.run(program = foo1, stdout = "f1.c Z\n")
308 test.run(program = foo2, stdout = "f2a.c\nf2b.c\nf2c.c\n")
309 test.run(program = foo3, stdout = "f3a.c\nf3b.c Z\nf3c.c\n")
310 test.run(program = foo4, stdout = "f4.c Z\n")
311 test.run(program = foo5, stdout = "foo5.c Z\n")
312
313 test.up_to_date(arguments = foo_args)
314
315 # make sure the programs didn't get rebuilt, because nothing changed:
316 oldtime1 = os.path.getmtime(foo1)
317 oldtime2 = os.path.getmtime(foo2)
318 oldtime3 = os.path.getmtime(foo3)
319 oldtime4 = os.path.getmtime(foo4)
320 oldtime5 = os.path.getmtime(foo5)
321
322 time.sleep(2) # introduce a small delay, to make the test valid
323
324 test.run(arguments = foo_args)
325
326 test.fail_test(not (oldtime1 == os.path.getmtime(foo1)))
327 test.fail_test(not (oldtime2 == os.path.getmtime(foo2)))
328 test.fail_test(not (oldtime3 == os.path.getmtime(foo3)))
329 test.fail_test(not (oldtime4 == os.path.getmtime(foo4)))
330 test.fail_test(not (oldtime5 == os.path.getmtime(foo5)))
331
332 test.pass_test()