tests.py 5.56 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 24 25 26 27 28 29 30
def buildout_error_handling():
    r'''Buildout error handling

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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    >>> 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):
    ...
    MissingOption: ('Missing option', 'buildout', 'eek')

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

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = data_dir debug
    ... x = ${buildout:y}
    ... y = ${buildout:z}
    ... z = ${buildout:x}
    ... """)

    >>> print system(os.path.join(sample_buildout, 'bin', 'buildout')),
    ... # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
    Traceback (most recent call last):
    ...
    ValueError: ('Circular references',
           [('buildout', 'y'), ('buildout', 'z'), ('buildout', 'x')],
           ('buildout', 'y'))

64
'''    
Jim Fulton's avatar
Jim Fulton committed
65 66

def linkerSetUp(test):
67 68
    zc.buildout.testing.buildoutSetUp(test, clear_home=False)
    zc.buildout.testing.multi_python(test)
Jim Fulton's avatar
Jim Fulton committed
69 70 71 72
        
def linkerTearDown(test):
    shutil.rmtree(test.globs['_sample_eggs_container'])
    zc.buildout.testing.buildoutTearDown(test)
73

74
def buildoutTearDown(test):
75 76 77
    shutil.rmtree(test.globs['extensions'])
    shutil.rmtree(test.globs['home'])
    zc.buildout.testing.buildoutTearDown(test)
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


class PythonNormalizing(renormalizing.RENormalizing):

    def _transform(self, want, got):
        if '/xyzsample-eggs/' in want:
            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
133
    
134 135 136 137
def test_suite():
    return unittest.TestSuite((
        doctest.DocFileSuite(
            'buildout.txt',
Jim Fulton's avatar
Jim Fulton committed
138
            setUp=zc.buildout.testing.buildoutSetUp,
139
            tearDown=buildoutTearDown,
Jim Fulton's avatar
Jim Fulton committed
140 141 142
            checker=renormalizing.RENormalizing([
               (re.compile('__buildout_signature__ = recipes-\S+'),
                '__buildout_signature__ = recipes-SSSSSSSSSSS'),
143
               (re.compile('\S+sample-(\w+)%s(\S+)' % os.path.sep),
144 145 146 147
                r'/sample-\1/\2'),
               (re.compile('\S+sample-(\w+)'), r'/sample-\1'),
               (re.compile('executable = \S+python\S*'),
                'executable = python'),
148
               ])
149
            ),
150
        
Jim Fulton's avatar
Jim Fulton committed
151
        doctest.DocFileSuite(
152
            'egglinker.txt', 'easy_install.txt', 
Jim Fulton's avatar
Jim Fulton committed
153
            setUp=linkerSetUp, tearDown=linkerTearDown,
154 155 156 157 158 159 160

            checker=PythonNormalizing([
               (re.compile("'%(sep)s\S+sample-eggs%(sep)s(dist%(sep)s)?"
                           % dict(sep=os.path.sep)),
                '/sample-eggs/'),
               (re.compile("(-  demo(needed)?-\d[.]\d-py)\d[.]\d[.]egg"),
                '\\1V.V.egg'),
Jim Fulton's avatar
Jim Fulton committed
161 162
               ]),
            ),
163
        doctest.DocTestSuite(
Jim Fulton's avatar
Jim Fulton committed
164 165
            setUp=zc.buildout.testing.buildoutSetUp,
            tearDown=zc.buildout.testing.buildoutTearDown),
166 167 168 169 170
        ))

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