Merged revisions 2647-2719 via svnmerge from
[scons.git] / test / option-n.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 """
26 This test verifies:
27     1)  that we don't build files when we use the -n, --no-exec,
28         --just-print, --dry-run, and --recon options;
29     2)  that we don't remove built files when -n is used in
30         conjunction with -c;
31     3)  that files installed by the Install() method don't get
32         installed when -n is used;
33     4)  that source files don't get duplicated in a VariantDir
34         when -n is used.
35     5)  that Configure calls don't build any files. If a file
36         needs to be built (i.e. is not up-to-date), a ConfigureError
37         is raised.
38 """
39
40 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
41
42 import os
43 import os.path
44 import re
45 import string
46 import sys
47 import TestCmd
48 import TestSCons
49
50 _python_ = TestSCons._python_
51
52 test = TestSCons.TestSCons()
53
54 test.subdir('build', 'src')
55
56 test.write('build.py', r"""
57 import sys
58 file = open(sys.argv[1], 'wb')
59 file.write("build.py: %s\n" % sys.argv[1])
60 file.close()
61 """)
62
63 test.write('SConstruct', """
64 MyBuild = Builder(action = r'%(_python_)s build.py $TARGETS')
65 env = Environment(BUILDERS = { 'MyBuild' : MyBuild })
66 env.Tool('install')
67 env.MyBuild(target = 'f1.out', source = 'f1.in')
68 env.MyBuild(target = 'f2.out', source = 'f2.in')
69 env.Install('install', 'f3.in')
70 VariantDir('build', 'src', duplicate=1)
71 SConscript('build/SConscript', "env")
72 """ % locals())
73
74 test.write(['src', 'SConscript'], """
75 Import("env")
76 env.MyBuild(target = 'f4.out', source = 'f4.in')
77 """)
78
79 test.write('f1.in', "f1.in\n")
80 test.write('f2.in', "f2.in\n")
81 test.write('f3.in', "f3.in\n")
82 test.write(['src', 'f4.in'], "src/f4.in\n")
83
84 args = 'f1.out f2.out'
85 expect = test.wrap_stdout("""\
86 %(_python_)s build.py f1.out
87 %(_python_)s build.py f2.out
88 """ % locals())
89
90 test.run(arguments = args, stdout = expect)
91 test.fail_test(not os.path.exists(test.workpath('f1.out')))
92 test.fail_test(not os.path.exists(test.workpath('f2.out')))
93
94 test.unlink('f1.out')
95 test.unlink('f2.out')
96
97 test.run(arguments = '-n ' + args, stdout = expect)
98 test.fail_test(os.path.exists(test.workpath('f1.out')))
99 test.fail_test(os.path.exists(test.workpath('f2.out')))
100
101 test.run(arguments = '--no-exec ' + args, stdout = expect)
102 test.fail_test(os.path.exists(test.workpath('f1.out')))
103 test.fail_test(os.path.exists(test.workpath('f2.out')))
104
105 test.run(arguments = '--just-print ' + args, stdout = expect)
106 test.fail_test(os.path.exists(test.workpath('f1.out')))
107 test.fail_test(os.path.exists(test.workpath('f2.out')))
108
109 test.run(arguments = '--dry-run ' + args, stdout = expect)
110 test.fail_test(os.path.exists(test.workpath('f1.out')))
111 test.fail_test(os.path.exists(test.workpath('f2.out')))
112
113 test.run(arguments = '--recon ' + args, stdout = expect)
114 test.fail_test(os.path.exists(test.workpath('f1.out')))
115 test.fail_test(os.path.exists(test.workpath('f2.out')))
116
117 test.run(arguments = args)
118 test.fail_test(not os.path.exists(test.workpath('f1.out')))
119
120 # Test that SCons does not write a modified .sconsign when -n is used.
121 expect = test.wrap_stdout("""\
122 %(_python_)s build.py f1.out
123 """ % locals())
124 test.unlink('.sconsign.dblite')
125 test.write('f1.out', "X1.out\n")
126 test.run(arguments = '-n f1.out', stdout = expect)
127 test.run(arguments = '-n f1.out', stdout = expect)
128
129 expect = test.wrap_stdout("Removed f1.out\nRemoved f2.out\n", cleaning=1)
130
131 test.run(arguments = '-n -c ' + args, stdout = expect)
132
133 test.run(arguments = '-c -n ' + args, stdout = expect)
134
135 test.fail_test(not os.path.exists(test.workpath('f1.out')))
136 test.fail_test(not os.path.exists(test.workpath('f2.out')))
137
138 #
139 install_f3_in = os.path.join('install', 'f3.in')
140 expect = test.wrap_stdout('Install file: "f3.in" as "%s"\n' % install_f3_in)
141
142 test.run(arguments = '-n install', stdout = expect)
143 test.fail_test(os.path.exists(test.workpath('install', 'f3.in')))
144
145 test.run(arguments = 'install', stdout = expect)
146 test.fail_test(not os.path.exists(test.workpath('install', 'f3.in')))
147
148 test.write('f3.in', "f3.in again\n")
149
150 test.run(arguments = '-n install', stdout = expect)
151 test.fail_test(not os.path.exists(test.workpath('install', 'f3.in')))
152
153 # Make sure duplicate source files in a VariantDir aren't created
154 # when the -n option is used.
155
156 # First, make sure none of the previous non-dryrun invocations caused
157 # the build directory to be populated.  Processing of the
158 # src/SConscript (actually build/SConscript) will reference f4.in as a
159 # source, causing a Node object to be built for "build/f4.in".
160 # Creating the node won't cause "build/f4.in" to be created from
161 # "src/f4.in", but that *is* a side-effect of calling the exists()
162 # method on that node, which may happen via other processing.
163 # Therefore add this conditional removal to ensure  a clean setting
164 # before running this test.
165     
166 if os.path.exists(test.workpath('build', 'f4.in')):
167     test.unlink(test.workpath('build', 'f4.in'))
168
169 test.run(arguments = '-n build')
170 test.fail_test(os.path.exists(test.workpath('build', 'f4.in')))
171
172 # test Configure-calls in conjunction with -n
173 test.subdir('configure')
174 test.match_func = TestCmd.match_re_dotall
175 test.write('configure/SConstruct',
176 """def CustomTest(context):
177     def userAction(target,source,env):
178         import shutil
179         shutil.copyfile( str(source[0]), str(target[0]))
180     def strAction(target,source,env):
181         return "cp " + str(source[0]) + " " + str(target[0])
182     context.Message("Executing Custom Test ... " )
183     (ok, msg) = context.TryAction(Action(userAction,strAction),
184                                   "Hello World", ".in")
185     context.Result(ok)
186     return ok
187
188 env = Environment()
189 conf = Configure( env,
190                   custom_tests={'CustomTest':CustomTest},
191                   conf_dir="config.test",
192                   log_file="config.log" )
193 if not conf.CustomTest():
194     Exit(1)
195 else:
196     env = conf.Finish()
197 """)
198 # test that conf_dir isn't created and an error is raised
199 stderr=r"""
200 scons: \*\*\* Cannot create configure directory "config\.test" within a dry-run\.
201 File \S+, line \S+, in \S+
202 """
203 test.run(arguments="-n",stderr=stderr,status=2,
204          chdir=test.workpath("configure"))
205 test.fail_test(os.path.exists(test.workpath("configure", "config.test")))
206 test.fail_test(os.path.exists(test.workpath("configure", "config.log")))
207
208 # test that targets are not built, if conf_dir exists.
209 # verify that .cache and config.log are not created.
210 # an error should be raised
211 stderr=r"""
212 scons: \*\*\* Cannot update configure test "%s" within a dry-run\.
213 File \S+, line \S+, in \S+
214 """ % re.escape(os.path.join("config.test", "conftest_0.in"))
215 test.subdir(['configure','config.test'])
216 test.run(arguments="-n",stderr=stderr,status=2,
217          chdir=test.workpath("configure"))
218 test.fail_test(os.path.exists(test.workpath("configure", "config.test",
219                                             ".cache")))
220 test.fail_test(os.path.exists(test.workpath("configure", "config.test",
221                                             "conftest_0")))
222 test.fail_test(os.path.exists(test.workpath("configure", "config.test",
223                                             "conftest_0.in")))
224 test.fail_test(os.path.exists(test.workpath("configure", "config.log")))
225
226 # test that no error is raised, if all targets are up-to-date. In this
227 # case .cache and config.log shouldn't be created
228 stdout=test.wrap_stdout(build_str="scons: `.' is up to date.\n",
229                         read_str=r"""Executing Custom Test ... \(cached\) yes
230 """)
231 test.run(status=0,chdir=test.workpath("configure"))
232 log1_mtime = os.path.getmtime(test.workpath("configure","config.log"))
233 test.run(stdout=stdout,arguments="-n",status=0,
234          chdir=test.workpath("configure"))
235 log2_mtime = os.path.getmtime(test.workpath("configure","config.log"))
236 test.fail_test( log1_mtime != log2_mtime )
237
238 test.pass_test()