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