REF: remove duplicated function msvc_setup_env_once.
[scons.git] / src / test_setup.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 how the setup.py script installs SCons.
29
30 Note that this is an installation test, not a functional test, so the
31 name of this script doesn't end in *Tests.py.
32 """
33
34 import os
35 import os.path
36 import shutil
37 import string
38 import sys
39
40 try: WindowsError
41 except NameError: WindowsError = OSError
42
43 import TestSCons
44
45 version = TestSCons.TestSCons.scons_version
46
47 scons_version = 'scons-%s' % version
48
49 python = TestSCons.python
50
51 class MyTestSCons(TestSCons.TestSCons):
52
53     _lib_modules = [
54         # A representative smattering of build engine modules.
55         '__init__.py',
56         'Action.py',
57         'Builder.py',
58         'Environment.py',
59         'Util.py',
60     ]
61
62     _base_scripts = [
63         'scons',
64         'sconsign',
65     ]
66
67     _version_scripts = [
68         'scons-%s' % version,
69         'sconsign-%s' % version,
70     ]
71
72     _bat_scripts = [
73         'scons.bat',
74     ]
75
76     _bat_version_scripts = [
77         'scons-%s.bat' % version,
78     ]
79
80     _man_pages = [
81         'scons.1',
82         'sconsign.1',
83     ]
84
85     def __init__(self):
86         TestSCons.TestSCons.__init__(self)
87         self.root = self.workpath('root')
88         self.prefix = self.root + os.path.splitdrive(sys.prefix)[1]
89
90         if sys.platform == 'win32':
91             self.bin_dir = os.path.join(self.prefix, 'Scripts')
92             self.bat_dir = self.prefix
93             self.standalone_lib = os.path.join(self.prefix, 'scons')
94             self.standard_lib = os.path.join(self.prefix,
95                                              'Lib',
96                                              'site-packages',
97                                              '')
98             self.version_lib = os.path.join(self.prefix, scons_version)
99             self.man_dir = os.path.join(self.prefix, 'Doc')
100         else:
101             self.bin_dir = os.path.join(self.prefix, 'bin')
102             self.bat_dir = self.bin_dir
103             self.lib_dir = os.path.join(self.prefix, 'lib')
104             self.standalone_lib = os.path.join(self.lib_dir, 'scons')
105             self.standard_lib = os.path.join(self.lib_dir,
106                                              'python%s' % sys.version[:3],
107                                              'site-packages',
108                                              '')
109             self.version_lib = os.path.join(self.lib_dir, scons_version)
110             self.man_dir = os.path.join(self.prefix, 'man', 'man1')
111
112         self.prepend_bin_dir = lambda p, d=self.bin_dir: os.path.join(d, p)
113         self.prepend_bat_dir = lambda p, d=self.bat_dir: os.path.join(d, p)
114         self.prepend_man_dir = lambda p, d=self.man_dir: os.path.join(d, p)
115
116     def run(self, *args, **kw):
117         kw['chdir'] = scons_version
118         kw['program'] = python
119         kw['stderr'] = None
120         return apply(TestSCons.TestSCons.run, (self,)+args, kw)
121
122     def remove(self, dir):
123         try: shutil.rmtree(dir)
124         except (OSError, WindowsError): pass
125
126     def stdout_lines(self):
127         return string.split(self.stdout(), '\n')
128
129
130     def lib_line(self, lib):
131         return 'Installed SCons library modules into %s' % lib
132
133     def lib_paths(self, lib_dir):
134         prepend_lib_dir = lambda p, d=lib_dir: os.path.join(d, 'SCons', p)
135         return map(prepend_lib_dir, self._lib_modules)
136
137     def scripts_line(self):
138         return 'Installed SCons scripts into %s' % self.bin_dir
139
140     def base_script_paths(self):
141         scripts = self._base_scripts
142         return map(self.prepend_bin_dir, scripts)
143
144     def version_script_paths(self):
145         scripts = self._version_scripts
146         return map(self.prepend_bin_dir, scripts)
147
148     def bat_script_paths(self):
149         scripts = self._bat_scripts + self._bat_version_scripts
150         return map(self.prepend_bat_dir, scripts)
151
152     def man_page_line(self):
153         return 'Installed SCons man pages into %s' % self.man_dir
154
155     def man_page_paths(self):
156         return map(self.prepend_man_dir, self._man_pages)
157
158
159     def must_have_installed(self, paths):
160         for p in paths:
161             self.must_exist(p)
162
163     def must_not_have_installed(self, paths):
164         for p in paths:
165             self.must_not_exist(p)
166
167 try:
168     cwd = os.environ['SCONS_CWD']
169 except KeyError:
170     cwd = os.getcwd()
171
172 test = MyTestSCons()
173
174 test.subdir(test.root)
175
176 tar_gz = os.path.join(cwd, 'build', 'dist', '%s.tar.gz' % scons_version)
177 zip = os.path.join(cwd, 'build', 'dist', '%s.zip' % scons_version)
178
179 if os.path.isfile(zip):
180     try: import zipfile
181     except ImportError: pass
182     else:
183         zf = zipfile.ZipFile(zip, 'r')
184
185         for name in zf.namelist():
186             dir = os.path.dirname(name)
187             try: os.makedirs(dir)
188             except: pass
189             # if the file exists, then delete it before writing
190             # to it so that we don't end up trying to write to a symlink:
191             if os.path.isfile(name) or os.path.islink(name):
192                 os.unlink(name)
193             if not os.path.isdir(name):
194                 open(name, 'w').write(zf.read(name))
195
196 if not os.path.isdir(scons_version) and os.path.isfile(tar_gz):
197     # Unpack the .tar.gz file.  This should create the scons_version/
198     # subdirectory from which we execute the setup.py script therein.
199     os.system("gunzip -c %s | tar xf -" % tar_gz)
200
201 if not os.path.isdir(scons_version):
202     print "Cannot test package installation, found none of the following packages:"
203     print "\t" + tar_gz
204     print "\t" + zip
205     test.no_result(1)
206
207 # Verify that a virgin installation installs the version library,
208 # the scripts and (on UNIX/Linux systems) the man pages.
209 test.run(arguments = 'setup.py install --root=%s' % test.root)
210 test.fail_test(not test.lib_line(test.version_lib) in test.stdout_lines())
211 test.must_have_installed(test.lib_paths(test.version_lib))
212
213 # Verify that --standard-lib installs into the Python standard library.
214 test.run(arguments = 'setup.py install --root=%s --standard-lib' % test.root)
215 test.fail_test(not test.lib_line(test.standard_lib) in test.stdout_lines())
216 test.must_have_installed(test.lib_paths(test.standard_lib))
217
218 # Verify that --standalone-lib installs the standalone library.
219 test.run(arguments = 'setup.py install --root=%s --standalone-lib' % test.root)
220 test.fail_test(not test.lib_line(test.standalone_lib) in test.stdout_lines())
221 test.must_have_installed(test.lib_paths(test.standalone_lib))
222
223 # Verify that --version-lib installs into a version-specific library directory.
224 test.run(arguments = 'setup.py install --root=%s --version-lib' % test.root)
225 test.fail_test(not test.lib_line(test.version_lib) in test.stdout_lines())
226
227 # Now that all of the libraries are in place,
228 # verify that a default installation still installs the version library.
229 test.run(arguments = 'setup.py install --root=%s' % test.root)
230 test.fail_test(not test.lib_line(test.version_lib) in test.stdout_lines())
231
232 test.remove(test.version_lib)
233
234 # Now with only the standard and standalone libraries in place,
235 # verify that a default installation still installs the version library.
236 test.run(arguments = 'setup.py install --root=%s' % test.root)
237 test.fail_test(not test.lib_line(test.version_lib) in test.stdout_lines())
238
239 test.remove(test.version_lib)
240 test.remove(test.standalone_lib)
241
242 # Now with only the standard libraries in place,
243 # verify that a default installation still installs the version library.
244 test.run(arguments = 'setup.py install --root=%s' % test.root)
245 test.fail_test(not test.lib_line(test.version_lib) in test.stdout_lines())
246
247
248
249 #
250 test.run(arguments = 'setup.py install --root=%s' % test.root)
251 test.fail_test(not test.scripts_line() in test.stdout_lines())
252 if sys.platform == 'win32':
253     test.must_have_installed(test.base_script_paths())
254     test.must_have_installed(test.version_script_paths())
255     test.must_have_installed(test.bat_script_paths())
256 else:
257     test.must_have_installed(test.base_script_paths())
258     test.must_have_installed(test.version_script_paths())
259     test.must_not_have_installed(test.bat_script_paths())
260
261 test.remove(test.prefix)
262
263 test.run(arguments = 'setup.py install --root=%s --no-install-bat' % test.root)
264 test.fail_test(not test.scripts_line() in test.stdout_lines())
265 test.must_have_installed(test.base_script_paths())
266 test.must_have_installed(test.version_script_paths())
267 test.must_not_have_installed(test.bat_script_paths())
268
269 test.remove(test.prefix)
270
271 test.run(arguments = 'setup.py install --root=%s --install-bat' % test.root)
272 test.fail_test(not test.scripts_line() in test.stdout_lines())
273 test.must_have_installed(test.base_script_paths())
274 test.must_have_installed(test.version_script_paths())
275 test.must_have_installed(test.bat_script_paths())
276
277 test.remove(test.prefix)
278
279 test.run(arguments = 'setup.py install --root=%s --no-scons-script' % test.root)
280 test.fail_test(not test.scripts_line() in test.stdout_lines())
281 test.must_not_have_installed(test.base_script_paths())
282 test.must_have_installed(test.version_script_paths())
283 # Doesn't matter whether we installed the .bat scripts or not.
284
285 test.remove(test.prefix)
286
287 test.run(arguments = 'setup.py install --root=%s --no-version-script' % test.root)
288 test.fail_test(not test.scripts_line() in test.stdout_lines())
289 test.must_have_installed(test.base_script_paths())
290 test.must_not_have_installed(test.version_script_paths())
291 # Doesn't matter whether we installed the .bat scripts or not.
292
293
294
295 test.remove(test.man_dir)
296
297 test.run(arguments = 'setup.py install --root=%s' % test.root)
298 if sys.platform == 'win32':
299     test.fail_test(test.man_page_line() in test.stdout_lines())
300     test.must_not_have_installed(test.man_page_paths())
301 else:
302     test.fail_test(not test.man_page_line() in test.stdout_lines())
303     test.must_have_installed(test.man_page_paths())
304
305 test.remove(test.man_dir)
306
307 test.run(arguments = 'setup.py install --root=%s --no-install-man' % test.root)
308 test.fail_test(test.man_page_line() in test.stdout_lines())
309 test.must_not_have_installed(test.man_page_paths())
310
311 test.remove(test.man_dir)
312
313 test.run(arguments = 'setup.py install --root=%s --install-man' % test.root)
314 test.fail_test(not test.man_page_line() in test.stdout_lines())
315 test.must_have_installed(test.man_page_paths())
316
317
318
319 # Verify that we don't warn about the directory in which we've
320 # installed the modules when using a non-standard prefix.
321 other_prefix = test.workpath('other-prefix')
322 test.subdir(other_prefix)
323 test.run(arguments = 'setup.py install --prefix=%s' % other_prefix)
324 test.fail_test(string.find(test.stderr(),
325                            "you'll have to change the search path yourself")
326                != -1)
327
328 # All done.
329 test.pass_test()
330
331 # Local Variables:
332 # tab-width:4
333 # indent-tabs-mode:nil
334 # End:
335 # vim: set expandtab tabstop=4 shiftwidth=4: