36c5d8ae8e0664ea39bf0e4a877a73ddd5736474
[scons.git] / test / option-c.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2001, 2002 Steven Knight
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 TestSCons
30
31 python = TestSCons.python
32
33 test = TestSCons.TestSCons()
34
35 test.write('build.py', r"""
36 import sys
37 contents = open(sys.argv[2], 'rb').read()
38 file = open(sys.argv[1], 'wb')
39 file.write(contents)
40 file.close()
41 """)
42
43 test.write('SConstruct', """
44 B = Builder(action = r'%s build.py $TARGETS $SOURCES')
45 env = Environment(BUILDERS = { 'B' : B })
46 env.B(target = 'foo1.out', source = 'foo1.in')
47 env.B(target = 'foo2.out', source = 'foo2.xxx')
48 env.B(target = 'foo2.xxx', source = 'foo2.in')
49 env.B(target = 'foo3.out', source = 'foo3.in')
50 import os
51 if hasattr(os, 'symlink'):
52     def symlink1(env, target, source):
53         # symlink to a file that exists
54         os.symlink(str(source[0]), str(target[0]))
55     env.Command(target = 'symlink1', source = 'foo1.in', action = symlink1)
56     def symlink2(env, target, source):
57         # force symlink to a file that doesn't exist
58         os.symlink('does_not_exist', str(target[0]))
59     env.Command(target = 'symlink2', source = 'foo1.in', action = symlink2)
60 """ % python)
61
62 test.write('foo1.in', "foo1.in\n")
63
64 test.write('foo2.in', "foo2.in\n")
65
66 test.write('foo3.in', "foo3.in\n")
67
68 test.run(arguments = 'foo1.out foo2.out foo3.out')
69
70 test.fail_test(test.read(test.workpath('foo1.out')) != "foo1.in\n")
71 test.fail_test(test.read(test.workpath('foo2.xxx')) != "foo2.in\n")
72 test.fail_test(test.read(test.workpath('foo2.out')) != "foo2.in\n")
73 test.fail_test(test.read(test.workpath('foo3.out')) != "foo3.in\n")
74
75 test.run(arguments = '-c foo1.out',
76          stdout = test.wrap_stdout("Removed foo1.out\n"))
77
78 test.fail_test(os.path.exists(test.workpath('foo1.out')))
79 test.fail_test(not os.path.exists(test.workpath('foo2.xxx')))
80 test.fail_test(not os.path.exists(test.workpath('foo2.out')))
81 test.fail_test(not os.path.exists(test.workpath('foo3.out')))
82
83 test.run(arguments = '--clean foo2.out foo2.xxx',
84          stdout = test.wrap_stdout("Removed foo2.xxx\nRemoved foo2.out\n"))
85
86 test.fail_test(os.path.exists(test.workpath('foo1.out')))
87 test.fail_test(os.path.exists(test.workpath('foo2.xxx')))
88 test.fail_test(os.path.exists(test.workpath('foo2.out')))
89 test.fail_test(not os.path.exists(test.workpath('foo3.out')))
90
91 test.run(arguments = '--remove foo3.out',
92          stdout = test.wrap_stdout("Removed foo3.out\n"))
93
94 test.fail_test(os.path.exists(test.workpath('foo1.out')))
95 test.fail_test(os.path.exists(test.workpath('foo2.xxx')))
96 test.fail_test(os.path.exists(test.workpath('foo2.out')))
97 test.fail_test(os.path.exists(test.workpath('foo3.out')))
98
99 test.run(arguments = '.')
100
101 test.fail_test(test.read(test.workpath('foo1.out')) != "foo1.in\n")
102 test.fail_test(test.read(test.workpath('foo2.xxx')) != "foo2.in\n")
103 test.fail_test(test.read(test.workpath('foo2.out')) != "foo2.in\n")
104 test.fail_test(test.read(test.workpath('foo3.out')) != "foo3.in\n")
105
106 if hasattr(os, 'symlink'):
107     test.fail_test(not os.path.islink(test.workpath('symlink1')))
108     test.fail_test(not os.path.islink(test.workpath('symlink2')))
109
110 test.run(arguments = '-c foo2.xxx',
111          stdout = test.wrap_stdout("Removed foo2.xxx\n"))
112
113 test.fail_test(test.read(test.workpath('foo1.out')) != "foo1.in\n")
114 test.fail_test(os.path.exists(test.workpath('foo2.xxx')))
115 test.fail_test(test.read(test.workpath('foo2.out')) != "foo2.in\n")
116 test.fail_test(test.read(test.workpath('foo3.out')) != "foo3.in\n")
117
118 test.run(arguments = '-c .')
119
120 test.fail_test(os.path.exists(test.workpath('foo1.out')))
121 test.fail_test(os.path.exists(test.workpath('foo2.out')))
122 test.fail_test(os.path.exists(test.workpath('foo3.out')))
123
124 if hasattr(os, 'symlink'):
125     test.fail_test(os.path.islink(test.workpath('symlink1')))
126     test.fail_test(os.path.islink(test.workpath('symlink2')))
127
128 test.run(arguments = 'foo1.out foo2.out foo3.out')
129
130 expect = test.wrap_stdout("""Removed foo1.out
131 Removed foo2.xxx
132 Removed foo2.out
133 Removed foo3.out
134 """)
135
136 test.run(arguments = '-c -n foo1.out foo2.out foo3.out', stdout = expect)
137
138 test.run(arguments = '-n -c foo1.out foo2.out foo3.out', stdout = expect)
139
140 test.fail_test(test.read(test.workpath('foo1.out')) != "foo1.in\n")
141 test.fail_test(test.read(test.workpath('foo2.xxx')) != "foo2.in\n")
142 test.fail_test(test.read(test.workpath('foo2.out')) != "foo2.in\n")
143 test.fail_test(test.read(test.workpath('foo3.out')) != "foo3.in\n")
144
145 test.writable('.', 0)
146 f = open(test.workpath('foo1.out'))
147 test.run(arguments = '-c foo1.out',
148          stdout = test.wrap_stdout("scons: Could not remove 'foo1.out': Permission denied\n"))
149 test.fail_test(not os.path.exists(test.workpath('foo1.out')))
150 f.close()
151 test.writable('.', 1)
152
153 test.subdir('subd')
154 test.write(['subd', 'foon.in'], "foon.in\n")
155 test.write(['subd', 'foox.in'], "foox.in\n")
156 test.write('aux1.x', "aux1.x\n")
157 test.write('aux2.x', "aux2.x\n")
158 test.write('SConstruct', """
159 B = Builder(action = r'%s build.py $TARGETS $SOURCES')
160 env = Environment(BUILDERS = { 'B' : B })
161 env.B(target = 'foo1.out', source = 'foo1.in')
162 env.B(target = 'foo2.out', source = 'foo2.xxx')
163 env.B(target = 'foo2.xxx', source = 'foo2.in')
164 env.B(target = 'foo3.out', source = 'foo3.in')
165 SConscript('subd/SConscript')
166 Clean('foo2.xxx', ['aux1.x'])
167 Clean('foo2.xxx', ['aux2.x'])
168 Clean('.', ['subd'])
169 """ % python)
170
171 test.write(['subd', 'SConscript'], """
172 Clean('.', 'foox.in')
173 """)
174
175 expect = test.wrap_stdout("""Removed foo2.xxx
176 Removed aux1.x
177 Removed aux2.x
178 """)
179 test.run(arguments = '-c foo2.xxx', stdout=expect)
180 test.fail_test(test.read(test.workpath('foo1.out')) != "foo1.in\n")
181 test.fail_test(os.path.exists(test.workpath('foo2.xxx')))
182 test.fail_test(test.read(test.workpath('foo2.out')) != "foo2.in\n")
183 test.fail_test(test.read(test.workpath('foo3.out')) != "foo3.in\n")
184
185 expect = test.wrap_stdout("Removed %s\n" % os.path.join('subd', 'foox.in'))
186 test.run(arguments = '-c subd', stdout=expect)
187 test.fail_test(os.path.exists(test.workpath('foox.in')))
188
189 expect = test.wrap_stdout("""Removed foo1.out
190 Removed foo2.out
191 Removed foo3.out
192 Removed %s
193 Removed %s
194 Removed directory subd
195 """ % (os.path.join('subd','foon.in'), os.path.join('subd', 'SConscript')))
196 test.run(arguments = '-c .', stdout=expect)
197 test.fail_test(os.path.exists(test.workpath('subdir', 'foon.in')))
198 test.fail_test(os.path.exists(test.workpath('subdir')))
199
200 test.pass_test()