Added fix for TeX includes with same name as subdirs.
[scons.git] / test / chdir.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 that the chdir argument to Builder creation, Action creation,
29 Command() calls and execution work1s correctly.
30 """
31
32 import TestSCons
33
34 _python_ = TestSCons._python_
35
36 test = TestSCons.TestSCons()
37
38 test.subdir('other1',
39             'other2',
40             'other3',
41             'other4',
42             'other5',
43             'other6',
44             'other7',
45             'other8',
46             'other9',
47             'work1',
48             ['work1', 'sub1'],
49             ['work1', 'sub2'],
50             ['work1', 'sub3'],
51             ['work1', 'sub4'],
52             ['work1', 'sub5'],
53             ['work1', 'sub6'],
54             ['work1', 'sub7'],
55             ['work1', 'sub8'],
56             ['work1', 'sub9'],
57             ['work1', 'sub20'],
58             ['work1', 'sub21'],
59             ['work1', 'sub22'],
60             ['work1', 'sub23'])
61
62 cat_py = test.workpath('cat.py')
63
64 other1 = test.workpath('other1')
65 other1_f11_out = test.workpath('other1', 'f11.out')
66 other1_f11_in = test.workpath('other1', 'f11.in')
67 other2 = test.workpath('other2')
68 other2_f12_out = test.workpath('other2', 'f12.out')
69 other2_f12_in = test.workpath('other2', 'f12.in')
70 other3 = test.workpath('other3')
71 other3_f13_out = test.workpath('other3', 'f13.out')
72 other3_f13_in = test.workpath('other3', 'f13.in')
73 other4 = test.workpath('other4')
74 other4_f14_out = test.workpath('other4', 'f14.out')
75 other4_f14_in = test.workpath('other4', 'f14.in')
76 other5 = test.workpath('other5')
77 other5_f15_out = test.workpath('other5', 'f15.out')
78 other5_f15_in = test.workpath('other5', 'f15.in')
79 other6 = test.workpath('other6')
80 other6_f16_out = test.workpath('other6', 'f16.out')
81 other6_f16_in = test.workpath('other6', 'f16.in')
82 other7 = test.workpath('other7')
83 other7_f17_out = test.workpath('other7', 'f17.out')
84 other7_f17_in = test.workpath('other7', 'f17.in')
85 other8 = test.workpath('other8')
86 other8_f18_out = test.workpath('other8', 'f18.out')
87 other8_f18_in = test.workpath('other8', 'f18.in')
88 other9 = test.workpath('other9')
89 other9_f19_out = test.workpath('other9', 'f19.out')
90 other9_f19_in = test.workpath('other9', 'f19.in')
91
92 test.write(cat_py, """\
93 import sys
94 ofp = open(sys.argv[1], 'wb')
95 for f in sys.argv[2:]:
96     ifp = open(f, 'rb')
97     ofp.write(ifp.read())
98 ofp.close
99 """)
100
101 test.write(['work1', 'SConstruct'], """
102 cat_command = r'%(_python_)s %(cat_py)s ${TARGET.file} ${SOURCE.file}'
103
104 no_chdir_act = Action(cat_command)
105 chdir_sub4_act = Action(cat_command, chdir=1)
106 chdir_sub5_act = Action(cat_command, chdir='sub5')
107 chdir_sub6_act = Action(cat_command, chdir=Dir('sub6'))
108
109 env = Environment(BUILDERS = {
110     'Chdir4' : Builder(action = chdir_sub4_act),
111     'Chdir5' : Builder(action = chdir_sub5_act),
112     'Chdir6' : Builder(action = chdir_sub6_act),
113     'Chdir7' : Builder(action = no_chdir_act, chdir=1),
114     'Chdir8' : Builder(action = no_chdir_act, chdir='sub8'),
115     'Chdir9' : Builder(action = no_chdir_act, chdir=Dir('sub9')),
116 })
117
118 env.Command('f0.out', 'f0.in', cat_command)
119
120 env.Command('sub1/f1.out', 'sub1/f1.in', cat_command,
121             chdir=1)
122 env.Command('sub2/f2.out', 'sub2/f2.in', cat_command,
123             chdir='sub2')
124 env.Command('sub3/f3.out', 'sub3/f3.in', cat_command,
125             chdir=Dir('sub3'))
126
127 env.Chdir4('sub4/f4.out', 'sub4/f4.in')
128 env.Chdir5('sub5/f5.out', 'sub5/f5.in')
129 env.Chdir6('sub6/f6.out', 'sub6/f6.in')
130
131 env.Chdir7('sub7/f7.out', 'sub7/f7.in')
132 env.Chdir8('sub8/f8.out', 'sub8/f8.in')
133 env.Chdir9('sub9/f9.out', 'sub9/f9.in')
134
135 env.Command(r'%(other1_f11_out)s', r'%(other1_f11_in)s', cat_command,
136             chdir=1)
137 env.Command(r'%(other2_f12_out)s', r'%(other2_f12_in)s', cat_command,
138             chdir=r'%(other2)s')
139 env.Command(r'%(other3_f13_out)s', r'%(other3_f13_in)s', cat_command,
140             chdir=Dir(r'%(other3)s'))
141
142 env.Chdir4(r'%(other4_f14_out)s', r'%(other4_f14_in)s')
143 env.Chdir5(r'%(other5_f15_out)s', r'%(other5_f15_in)s',
144            chdir=r'%(other5)s')
145 env.Chdir6(r'%(other6_f16_out)s', r'%(other6_f16_in)s',
146            chdir=Dir(r'%(other6)s'))
147
148 env.Chdir7(r'%(other7_f17_out)s', r'%(other7_f17_in)s')
149 env.Chdir8(r'%(other8_f18_out)s', r'%(other8_f18_in)s',
150            chdir=r'%(other8)s')
151 env.Chdir9(r'%(other9_f19_out)s', r'%(other9_f19_in)s',
152            chdir=Dir(r'%(other9)s'))
153
154 Command('f20.out', 'f20.in', cat_command)
155
156 Command('sub21/f21.out', 'sub21/f21.in', cat_command,
157         chdir=1)
158 Command('sub22/f22.out', 'sub22/f22.in', cat_command,
159         chdir='sub22')
160 Command('sub23/f23.out', 'sub23/f23.in', cat_command,
161         chdir=Dir('sub23'))
162 """ % locals())
163
164 test.write(['work1', 'f0.in'], "work1/f0.in\n")
165
166 test.write(['work1', 'sub1', 'f1.in'], "work1/sub1/f1.in\n")
167 test.write(['work1', 'sub2', 'f2.in'], "work1/sub2/f2.in\n")
168 test.write(['work1', 'sub3', 'f3.in'], "work1/sub3/f3.in\n")
169 test.write(['work1', 'sub4', 'f4.in'], "work1/sub4/f4.in\n")
170 test.write(['work1', 'sub5', 'f5.in'], "work1/sub5/f5.in\n")
171 test.write(['work1', 'sub6', 'f6.in'], "work1/sub6/f6.in\n")
172 test.write(['work1', 'sub7', 'f7.in'], "work1/sub7/f7.in\n")
173 test.write(['work1', 'sub8', 'f8.in'], "work1/sub8/f8.in\n")
174 test.write(['work1', 'sub9', 'f9.in'], "work1/sub9/f9.in\n")
175
176 test.write(['other1', 'f11.in'], "other1/f11.in\n")
177 test.write(['other2', 'f12.in'], "other2/f12.in\n")
178 test.write(['other3', 'f13.in'], "other3/f13.in\n")
179 test.write(['other4', 'f14.in'], "other4/f14.in\n")
180 test.write(['other5', 'f15.in'], "other5/f15.in\n")
181 test.write(['other6', 'f16.in'], "other6/f16.in\n")
182 test.write(['other7', 'f17.in'], "other7/f17.in\n")
183 test.write(['other8', 'f18.in'], "other8/f18.in\n")
184 test.write(['other9', 'f19.in'], "other9/f19.in\n")
185
186 test.write(['work1', 'f20.in'], "work1/f20.in\n")
187
188 test.write(['work1', 'sub21', 'f21.in'], "work1/sub21/f21.in\n")
189 test.write(['work1', 'sub22', 'f22.in'], "work1/sub22/f22.in\n")
190 test.write(['work1', 'sub23', 'f23.in'], "work1/sub23/f23.in\n")
191
192 test.run(chdir='work1', arguments='..')
193
194 test.must_match(['work1', 'f0.out'], "work1/f0.in\n")
195
196 test.must_match(['work1', 'sub1', 'f1.out'], "work1/sub1/f1.in\n")
197 test.must_match(['work1', 'sub2', 'f2.out'], "work1/sub2/f2.in\n")
198 test.must_match(['work1', 'sub3', 'f3.out'], "work1/sub3/f3.in\n")
199 test.must_match(['work1', 'sub4', 'f4.out'], "work1/sub4/f4.in\n")
200 test.must_match(['work1', 'sub5', 'f5.out'], "work1/sub5/f5.in\n")
201 test.must_match(['work1', 'sub6', 'f6.out'], "work1/sub6/f6.in\n")
202 test.must_match(['work1', 'sub7', 'f7.out'], "work1/sub7/f7.in\n")
203 test.must_match(['work1', 'sub8', 'f8.out'], "work1/sub8/f8.in\n")
204 test.must_match(['work1', 'sub9', 'f9.out'], "work1/sub9/f9.in\n")
205
206 test.must_match(['other1', 'f11.out'], "other1/f11.in\n")
207 test.must_match(['other2', 'f12.out'], "other2/f12.in\n")
208 test.must_match(['other3', 'f13.out'], "other3/f13.in\n")
209 test.must_match(['other4', 'f14.out'], "other4/f14.in\n")
210 test.must_match(['other5', 'f15.out'], "other5/f15.in\n")
211 test.must_match(['other6', 'f16.out'], "other6/f16.in\n")
212 test.must_match(['other7', 'f17.out'], "other7/f17.in\n")
213 test.must_match(['other8', 'f18.out'], "other8/f18.in\n")
214 test.must_match(['other9', 'f19.out'], "other9/f19.in\n")
215
216 test.must_match(['work1', 'f20.out'], "work1/f20.in\n")
217
218 test.must_match(['work1', 'sub21', 'f21.out'], "work1/sub21/f21.in\n")
219 test.must_match(['work1', 'sub22', 'f22.out'], "work1/sub22/f22.in\n")
220 test.must_match(['work1', 'sub23', 'f23.out'], "work1/sub23/f23.in\n")
221
222
223
224 test.subdir('work2',
225             ['work2', 'sub'])
226
227 work2 = repr(test.workpath('work2'))
228 work2_sub_f1_out = test.workpath('work2', 'sub', 'f1.out')
229 work2_sub_f2_out = test.workpath('work2', 'sub', 'f2.out')
230
231 test.write(['work2', 'SConstruct'], """\
232 cat_command = r'%(_python_)s %(cat_py)s ${TARGET.file} ${SOURCE.file}'
233 env = Environment()
234 env.Command('sub/f1.out', 'sub/f1.in', cat_command,
235             chdir=1)
236 env.Command('sub/f2.out', 'sub/f2.in',
237             [
238               r'%(_python_)s %(cat_py)s .temp ${SOURCE.file}',
239               r'%(_python_)s %(cat_py)s ${TARGET.file} .temp',
240             ],
241             chdir=1)
242 """ % locals())
243
244 test.write(['work2', 'sub', 'f1.in'], "work2/sub/f1.in")
245 test.write(['work2', 'sub', 'f2.in'], "work2/sub/f2.in")
246
247 expect = test.wrap_stdout("""\
248 os.chdir('sub')
249 %(_python_)s %(cat_py)s f1.out f1.in
250 os.chdir(%(work2)s)
251 os.chdir('sub')
252 %(_python_)s %(cat_py)s .temp f2.in
253 os.chdir(%(work2)s)
254 os.chdir('sub')
255 %(_python_)s %(cat_py)s f2.out .temp
256 os.chdir(%(work2)s)
257 """ % locals())
258
259 test.run(chdir='work2', arguments='-n .', stdout=expect)
260
261 test.must_not_exist(work2_sub_f1_out)
262 test.must_not_exist(work2_sub_f2_out)
263
264 test.run(chdir='work2', arguments='.', stdout=expect)
265
266 test.must_match(work2_sub_f1_out, "work2/sub/f1.in")
267 test.must_match(work2_sub_f2_out, "work2/sub/f2.in")
268
269 test.run(chdir='work2', arguments='-c .')
270
271 test.must_not_exist(work2_sub_f1_out)
272 test.must_not_exist(work2_sub_f2_out)
273
274 test.run(chdir='work2', arguments='-s .', stdout="")
275
276 test.must_match(work2_sub_f1_out, "work2/sub/f1.in")
277 test.must_match(work2_sub_f2_out, "work2/sub/f2.in")
278
279 test.pass_test()
280
281 # Local Variables:
282 # tab-width:4
283 # indent-tabs-mode:nil
284 # End:
285 # vim: set expandtab tabstop=4 shiftwidth=4: