tests.py 8.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
##############################################################################
#
# Copyright (c) 2004 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""XXX short summary goes here.

$Id$
"""

19
import os, re, shutil, sys, unittest
Jim Fulton's avatar
Jim Fulton committed
20
from zope.testing import doctest, renormalizing
Jim Fulton's avatar
Jim Fulton committed
21
import zc.buildout.testing
22

23
def buildout_error_handling():
Jim Fulton's avatar
Jim Fulton committed
24
    r"""Buildout error handling
25 26 27 28 29 30

Asking for a section that doesn't exist, yields a key error:

    >>> import os
    >>> os.chdir(sample_buildout)
    >>> import zc.buildout.buildout
31
    >>> buildout = zc.buildout.buildout.Buildout('buildout.cfg', [])
32 33 34 35 36 37 38 39 40 41
    >>> buildout['eek']
    Traceback (most recent call last):
    ...
    KeyError: 'eek'

Asking for an option that doesn't exist, a MissingOption error is raised:

    >>> buildout['buildout']['eek']
    Traceback (most recent call last):
    ...
42
    MissingOption: Missing option: buildout:eek
43 44 45 46

It is an error to create a variable-reference cycle:

    >>> write(sample_buildout, 'buildout.cfg',
Jim Fulton's avatar
Jim Fulton committed
47
    ... '''
48 49 50 51 52 53
    ... [buildout]
    ... develop = recipes
    ... parts = data_dir debug
    ... x = ${buildout:y}
    ... y = ${buildout:z}
    ... z = ${buildout:x}
Jim Fulton's avatar
Jim Fulton committed
54
    ... ''')
55 56 57

    >>> print system(os.path.join(sample_buildout, 'bin', 'buildout')),
    ... # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    Error: Circular reference in substitutions.
    We're evaluating buildout:y, buildout:z, buildout:x
    and are referencing: buildout:y.

Al parts have to have a section:

    >>> write(sample_buildout, 'buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = x
    ... ''')

    >>> print system(os.path.join(sample_buildout, 'bin', 'buildout')),
    Error: No section was specified for part x

and all parts have to have a specified recipe:


    >>> write(sample_buildout, 'buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = x
80
    ...
81 82 83 84 85 86 87
    ... [x]
    ... foo = 1
    ... ''')

    >>> print system(os.path.join(sample_buildout, 'bin', 'buildout')),
    Error: Missing option: x:recipe

Jim Fulton's avatar
Jim Fulton committed
88
"""
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
 
def test_comparing_saved_options_with_funny_characters():
    """
    If an option has newlines, extra/odd spaces or a %, we need to make
    sure the comparison with the saved value works correctly.

    >>> mkdir(sample_buildout, 'recipes')
    >>> write(sample_buildout, 'recipes', 'debug.py', 
    ... '''
    ... class Debug:
    ...     def __init__(self, buildout, name, options):
    ...         options['debug'] = \"\"\"  <zodb>
    ...
    ...   <filestorage>
    ...     path foo
    ...   </filestorage>
    ...
    ... </zodb>  
    ...      \"\"\"
108 109 110 111 112 113 114 115 116
    ...         options['debug1'] = \"\"\"
    ... <zodb>
    ...
    ...   <filestorage>
    ...     path foo
    ...   </filestorage>
    ...
    ... </zodb>  
    ... \"\"\"
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
    ...         options['debug2'] = '  x  '
    ...         options['debug3'] = '42'
    ...         options['format'] = '%3d'
    ...
    ...     def install(self):
    ...         open('t', 'w').write('t')
    ...         return 't'
    ... ''')


    >>> write(sample_buildout, 'recipes', 'setup.py',
    ... '''
    ... from setuptools import setup
    ... setup(
    ...     name = "recipes",
    ...     entry_points = {'zc.buildout': ['default = debug:Debug']},
    ...     )
    ... ''')

    >>> write(sample_buildout, 'recipes', 'README.txt', " ")

    >>> write(sample_buildout, 'buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = recipes
    ... parts = debug
    ...
    ... [debug]
    ... recipe = recipes
    ... ''')

    >>> os.chdir(sample_buildout)
    >>> buildout = os.path.join(sample_buildout, 'bin', 'buildout')

    >>> print system(buildout+' -v'), # doctest: +ELLIPSIS
    buildout: Running ...setup.py -q develop ...
    buildout: Installing debug

If we run the buildout again, we shoudn't get a message about
uninstalling anything because the configuration hasn't changed.

    >>> print system(buildout+' -v'),
    buildout: Running setup.py -q develop ...
    buildout: Installing debug
"""

Jim Fulton's avatar
Jim Fulton committed
163 164

def linkerSetUp(test):
165 166
    zc.buildout.testing.buildoutSetUp(test, clear_home=False)
    zc.buildout.testing.multi_python(test)
167
    zc.buildout.testing.setUpServer(test, zc.buildout.testing.make_tree(test))
168

169 170 171 172 173 174
def easy_install_SetUp(test):
    zc.buildout.testing.buildoutSetUp(test, clear_home=False)
    zc.buildout.testing.multi_python(test)
    zc.buildout.testing.add_source_dist(test)
    zc.buildout.testing.setUpServer(test, zc.buildout.testing.make_tree(test))

175 176 177
class PythonNormalizing(renormalizing.RENormalizing):

    def _transform(self, want, got):
178
        if '/xyzsample-install/' in want:
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
            got = got.replace('-py2.4.egg', '-py2.3.egg')
            firstg = got.split('\n')[0]
            firstw = want.split('\n')[0]
            if firstg.startswith('#!') and firstw.startswith('#!'):
                firstg = ' '.join(firstg.split()[1:])
                got = firstg + '\n' + '\n'.join(got.split('\n')[1:])
                firstw = ' '.join(firstw.split()[1:])
                want = firstw + '\n' + '\n'.join(want.split('\n')[1:])
        
        for pattern, repl in self.patterns:
            want = pattern.sub(repl, want)
            got = pattern.sub(repl, got)

        return want, got

    def check_output(self, want, got, optionflags):
        if got == want:
            return True

        want, got = self._transform(want, got)
        if got == want:
            return True
            
        return doctest.OutputChecker.check_output(self, want, got, optionflags)

    def output_difference(self, example, got, optionflags):

        want = example.want

        # If want is empty, use original outputter. This is useful
        # when setting up tests for the first time.  In that case, we
        # generally use the differencer to display output, which we evaluate
        # by hand.
        if not want.strip():
            return doctest.OutputChecker.output_difference(
                self, example, got, optionflags)

        # Dang, this isn't as easy to override as we might wish
        original = want
        want, got = self._transform(want, got)

        # temporarily hack example with normalized want:
        example.want = want
        result = doctest.OutputChecker.output_difference(
            self, example, got, optionflags)
        example.want = original

        return result

Jim Fulton's avatar
Jim Fulton committed
228
    
229 230 231 232
def test_suite():
    return unittest.TestSuite((
        doctest.DocFileSuite(
            'buildout.txt',
Jim Fulton's avatar
Jim Fulton committed
233
            setUp=zc.buildout.testing.buildoutSetUp,
234
            tearDown=zc.buildout.testing.buildoutTearDown,
Jim Fulton's avatar
Jim Fulton committed
235 236 237
            checker=renormalizing.RENormalizing([
               (re.compile('__buildout_signature__ = recipes-\S+'),
                '__buildout_signature__ = recipes-SSSSSSSSSSS'),
238
               (re.compile('\S+sample-(\w+)%s(\S+)' % os.path.sep),
239 240 241 242
                r'/sample-\1/\2'),
               (re.compile('\S+sample-(\w+)'), r'/sample-\1'),
               (re.compile('executable = \S+python\S*'),
                'executable = python'),
243
               (re.compile('setuptools-\S+[.]egg'), 'setuptools.egg'),
244
               (re.compile('creating \S*setup.cfg'), 'creating setup.cfg'),
245
               ])
246
            ),
247
        
Jim Fulton's avatar
Jim Fulton committed
248
        doctest.DocFileSuite(
249
            'easy_install.txt', 
250 251
            setUp=easy_install_SetUp,
            tearDown=zc.buildout.testing.buildoutTearDown,
252 253

            checker=PythonNormalizing([
254
               (re.compile("'%(sep)s\S+sample-install%(sep)s(dist%(sep)s)?"
255 256
                           % dict(sep=os.path.sep)),
                '/sample-eggs/'),
257 258
               (re.compile("([d-]  ((ext)?demo(needed)?|other)"
                           "-\d[.]\d-py)\d[.]\d(-[^. \t\n]+)?[.]egg"),
259
                '\\1V.V.egg'),
Jim Fulton's avatar
Jim Fulton committed
260 261
               ]),
            ),
262
        doctest.DocTestSuite(
Jim Fulton's avatar
Jim Fulton committed
263
            setUp=zc.buildout.testing.buildoutSetUp,
264 265 266 267 268 269 270
            tearDown=zc.buildout.testing.buildoutTearDown,

            checker=PythonNormalizing([
               (re.compile("buildout: Running \S*setup.py"),
                'buildout: Running setup.py'),
               ]),
            )
271 272 273 274 275
        ))

if __name__ == '__main__':
    unittest.main(defaultTest='test_suite')