summaryrefslogtreecommitdiffstats
path: root/src/test_setup.py
blob: edf0de53e59f6d3869f7544d07f2568514aed77f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env python
#
# __COPYRIGHT__
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
from __future__ import print_function

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

"""
Test how the setup.py script installs SCons.

Note that this is an installation test, not a functional test, so the
name of this script doesn't end in *Tests.py.
"""

import os
import os.path
import shutil
import sys

try: WindowsError
except NameError: WindowsError = OSError

import TestSCons

version = TestSCons.TestSCons.scons_version

scons_version = 'scons-%s' % version

python = TestSCons.python

class MyTestSCons(TestSCons.TestSCons):

    _lib_modules = [
        # A representative smattering of build engine modules.
        '__init__.py',
        'Action.py',
        'Builder.py',
        'Environment.py',
        'Util.py',
    ]

    _base_scripts = [
        'scons',
        'sconsign',
    ]

    _version_scripts = [
        'scons-%s' % version,
        'sconsign-%s' % version,
    ]

    _bat_scripts = [
        'scons.bat',
    ]

    _bat_version_scripts = [
        'scons-%s.bat' % version,
    ]

    _man_pages = [
        'scons.1',
        'sconsign.1',
    ]

    def __init__(self):
        TestSCons.TestSCons.__init__(self)
        self.root = self.workpath('root')
        self.prefix = self.root + os.path.splitdrive(sys.prefix)[1]

        if sys.platform == 'win32':
            self.bin_dir = os.path.join(self.prefix, 'Scripts')
            self.bat_dir = self.prefix
            self.standalone_lib = os.path.join(self.prefix, 'scons')
            self.standard_lib = os.path.join(self.prefix,
                                             'Lib',
                                             'site-packages',
                                             '')
            self.version_lib = os.path.join(self.prefix, scons_version)
            self.man_dir = os.path.join(self.prefix, 'Doc')
        else:
            self.bin_dir = os.path.join(self.prefix, 'bin')
            self.bat_dir = self.bin_dir
            self.lib_dir = os.path.join(self.prefix, 'lib')
            self.standalone_lib = os.path.join(self.lib_dir, 'scons')
            self.standard_lib = os.path.join(self.lib_dir,
                                             'python%s' % sys.version[:3],
                                             'site-packages',
                                             '')
            self.version_lib = os.path.join(self.lib_dir, scons_version)
            self.man_dir = os.path.join(self.prefix, 'man', 'man1')

        self.prepend_bin_dir = lambda p: os.path.join(self.bin_dir, p)
        self.prepend_bat_dir = lambda p: os.path.join(self.bat_dir, p)
        self.prepend_man_dir = lambda p: os.path.join(self.man_dir, p)

    def run(self, *args, **kw):
        kw['chdir'] = scons_version
        kw['program'] = python
        kw['stderr'] = None
        return TestSCons.TestSCons.run(self, *args, **kw)

    def remove(self, dir):
        try: shutil.rmtree(dir)
        except (OSError, WindowsError): pass

    def stdout_lines(self):
        return self.stdout().split('\n')


    def lib_line(self, lib):
        return 'Installed SCons library modules into %s' % lib

    def lib_paths(self, lib_dir):
        return [os.path.join(lib_dir, 'SCons', p) for p in self._lib_modules]

    def scripts_line(self):
        return 'Installed SCons scripts into %s' % self.bin_dir

    def base_script_paths(self):
        scripts = self._base_scripts
        return list(map(self.prepend_bin_dir, scripts))

    def version_script_paths(self):
        scripts = self._version_scripts
        return list(map(self.prepend_bin_dir, scripts))

    def bat_script_paths(self):
        scripts = self._bat_scripts + self._bat_version_scripts
        return list(map(self.prepend_bat_dir, scripts))

    def man_page_line(self):
        return 'Installed SCons man pages into %s' % self.man_dir

    def man_page_paths(self):
        return list(map(self.prepend_man_dir, self._man_pages))


    def must_have_installed(self, paths):
        for p in paths:
            self.must_exist(p)

    def must_not_have_installed(self, paths):
        for p in paths:
            self.must_not_exist(p)

try:
    cwd = os.environ['SCONS_CWD']
except KeyError:
    cwd = os.getcwd()

test = MyTestSCons()

test.subdir(test.root)

tar_gz = os.path.join(cwd, 'build', 'dist', '%s.tar.gz' % scons_version)
zip = os.path.join(cwd, 'build', 'dist', '%s.zip' % scons_version)

if os.path.isfile(zip):
    try: import zipfile
    except ImportError: pass
    else:
        zf = zipfile.ZipFile(zip, 'r')

        for name in zf.namelist():
            dir = os.path.dirname(name)
            try: os.makedirs(dir)
            except: pass
            # if the file exists, then delete it before writing
            # to it so that we don't end up trying to write to a symlink:
            if os.path.isfile(name) or os.path.islink(name):
                os.unlink(name)
            if not os.path.isdir(name):
                open(name, 'w').write(zf.read(name))

if not os.path.isdir(scons_version) and os.path.isfile(tar_gz):
    # Unpack the .tar.gz file.  This should create the scons_version/
    # subdirectory from which we execute the setup.py script therein.
    os.system("gunzip -c %s | tar xf -" % tar_gz)

if not os.path.isdir(scons_version):
    print("Cannot test package installation, found none of the following packages:")
    print("\t" + tar_gz)
    print("\t" + zip)
    test.no_result(1)

# Verify that a virgin installation installs the version library,
# the scripts and (on UNIX/Linux systems) the man pages.
test.run(arguments = 'setup.py install --root=%s' % test.root)
test.fail_test(not test.lib_line(test.version_lib) in test.stdout_lines())
test.must_have_installed(test.lib_paths(test.version_lib))

# Verify that --standard-lib installs into the Python standard library.
test.run(arguments = 'setup.py install --root=%s --standard-lib' % test.root)
test.fail_test(not test.lib_line(test.standard_lib) in test.stdout_lines())
test.must_have_installed(test.lib_paths(test.standard_lib))

# Verify that --standalone-lib installs the standalone library.
test.run(arguments = 'setup.py install --root=%s --standalone-lib' % test.root)
test.fail_test(not test.lib_line(test.standalone_lib) in test.stdout_lines())
test.must_have_installed(test.lib_paths(test.standalone_lib))

# Verify that --version-lib installs into a version-specific library directory.
test.run(arguments = 'setup.py install --root=%s --version-lib' % test.root)
test.fail_test(not test.lib_line(test.version_lib) in test.stdout_lines())

# Now that all of the libraries are in place,
# verify that a default installation still installs the version library.
test.run(arguments = 'setup.py install --root=%s' % test.root)
test.fail_test(not test.lib_line(test.version_lib) in test.stdout_lines())

test.remove(test.version_lib)

# Now with only the standard and standalone libraries in place,
# verify that a default installation still installs the version library.
test.run(arguments = 'setup.py install --root=%s' % test.root)
test.fail_test(not test.lib_line(test.version_lib) in test.stdout_lines())

test.remove(test.version_lib)
test.remove(test.standalone_lib)

# Now with only the standard libraries in place,
# verify that a default installation still installs the version library.
test.run(arguments = 'setup.py install --root=%s' % test.root)
test.fail_test(not test.lib_line(test.version_lib) in test.stdout_lines())



#
test.run(arguments = 'setup.py install --root=%s' % test.root)
test.fail_test(not test.scripts_line() in test.stdout_lines())
if sys.platform == 'win32':
    test.must_have_installed(test.base_script_paths())
    test.must_have_installed(test.version_script_paths())
    test.must_have_installed(test.bat_script_paths())
else:
    test.must_have_installed(test.base_script_paths())
    test.must_have_installed(test.version_script_paths())
    test.must_not_have_installed(test.bat_script_paths())

test.remove(test.prefix)

test.run(arguments = 'setup.py install --root=%s --no-install-bat' % test.root)
test.fail_test(not test.scripts_line() in test.stdout_lines())
test.must_have_installed(test.base_script_paths())
test.must_have_installed(test.version_script_paths())
test.must_not_have_installed(test.bat_script_paths())

test.remove(test.prefix)

test.run(arguments = 'setup.py install --root=%s --install-bat' % test.root)
test.fail_test(not test.scripts_line() in test.stdout_lines())
test.must_have_installed(test.base_script_paths())
test.must_have_installed(test.version_script_paths())
test.must_have_installed(test.bat_script_paths())

test.remove(test.prefix)

test.run(arguments = 'setup.py install --root=%s --no-scons-script' % test.root)
test.fail_test(not test.scripts_line() in test.stdout_lines())
test.must_not_have_installed(test.base_script_paths())
test.must_have_installed(test.version_script_paths())
# Doesn't matter whether we installed the .bat scripts or not.

test.remove(test.prefix)

test.run(arguments = 'setup.py install --root=%s --no-version-script' % test.root)
test.fail_test(not test.scripts_line() in test.stdout_lines())
test.must_have_installed(test.base_script_paths())
test.must_not_have_installed(test.version_script_paths())
# Doesn't matter whether we installed the .bat scripts or not.



test.remove(test.man_dir)

test.run(arguments = 'setup.py install --root=%s' % test.root)
if sys.platform == 'win32':
    test.fail_test(test.man_page_line() in test.stdout_lines())
    test.must_not_have_installed(test.man_page_paths())
else:
    test.fail_test(not test.man_page_line() in test.stdout_lines())
    test.must_have_installed(test.man_page_paths())

test.remove(test.man_dir)

test.run(arguments = 'setup.py install --root=%s --no-install-man' % test.root)
test.fail_test(test.man_page_line() in test.stdout_lines())
test.must_not_have_installed(test.man_page_paths())

test.remove(test.man_dir)

test.run(arguments = 'setup.py install --root=%s --install-man' % test.root)
test.fail_test(not test.man_page_line() in test.stdout_lines())
test.must_have_installed(test.man_page_paths())



# Verify that we don't warn about the directory in which we've
# installed the modules when using a non-standard prefix.
other_prefix = test.workpath('other-prefix')
test.subdir(other_prefix)
test.run(arguments = 'setup.py install --prefix=%s' % other_prefix)
test.fail_test(test.stderr().find("you'll have to change the search path yourself")
               != -1)

# All done.
test.pass_test()

# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4: