tests.py 44 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, tempfile, unittest, zipfile
Jim Fulton's avatar
Jim Fulton committed
20
from zope.testing import doctest, renormalizing
21 22
import pkg_resources
import zc.buildout.testing, zc.buildout.easy_install
23

Jim Fulton's avatar
Jim Fulton committed
24 25 26 27
os_path_sep = os.path.sep
if os_path_sep == '\\':
    os_path_sep *= 2

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

def develop_w_non_setuptools_setup_scripts():
    """
We should be able to deal with setup scripts that aren't setuptools based.

    >>> mkdir('foo')
    >>> write('foo', 'setup.py',
    ... '''
    ... from distutils.core import setup
    ... setup(name="foo")
    ... ''')

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = foo
    ... parts = 
    ... ''')

    >>> print system(join('bin', 'buildout')),
48
    buildout: Develop: /sample-buildout/foo
49 50 51 52 53 54

    >>> ls('develop-eggs')
    -  foo.egg-link

    """

Jim Fulton's avatar
Jim Fulton committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
def develop_verbose():
    """
We should be able to deal with setup scripts that aren't setuptools based.

    >>> mkdir('foo')
    >>> write('foo', 'setup.py',
    ... '''
    ... from setuptools import setup
    ... setup(name="foo")
    ... ''')

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = foo
    ... parts = 
    ... ''')

73
    >>> print system(join('bin', 'buildout')+' -vv'), # doctest: +ELLIPSIS
74 75
    zc.buildout...
    buildout: Develop: /sample-buildout/foo
Jim Fulton's avatar
Jim Fulton committed
76 77 78 79 80 81 82 83 84
    ...
    Installed /sample-buildout/foo
    ...

    >>> ls('develop-eggs')
    -  foo.egg-link

    """

85
def buildout_error_handling():
Jim Fulton's avatar
Jim Fulton committed
86
    r"""Buildout error handling
87

88
Asking for a section that doesn't exist, yields a missing section error:
89 90 91 92

    >>> import os
    >>> os.chdir(sample_buildout)
    >>> import zc.buildout.buildout
93
    >>> buildout = zc.buildout.buildout.Buildout('buildout.cfg', [])
94 95 96
    >>> buildout['eek']
    Traceback (most recent call last):
    ...
97
    MissingSection: The referenced section, 'eek', was not defined.
98 99 100 101 102 103

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

    >>> buildout['buildout']['eek']
    Traceback (most recent call last):
    ...
104
    MissingOption: Missing option: buildout:eek
105 106 107 108

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

    >>> write(sample_buildout, 'buildout.cfg',
Jim Fulton's avatar
Jim Fulton committed
109
    ... '''
110
    ... [buildout]
111
    ... parts =
112 113 114
    ... x = ${buildout:y}
    ... y = ${buildout:z}
    ... z = ${buildout:x}
Jim Fulton's avatar
Jim Fulton committed
115
    ... ''')
116 117 118

    >>> print system(os.path.join(sample_buildout, 'bin', 'buildout')),
    ... # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
119 120 121 122 123 124 125 126
    While:
      Initializing
      Getting section buildout
      Initializing section buildout
      Getting option buildout:y
      Getting option buildout:z
      Getting option buildout:x
      Getting option buildout:y
127 128
    Error: Circular reference in substitutions.

129 130 131 132 133 134 135 136 137 138 139
It is an error to use funny characters in variable refereces:

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

    >>> print system(os.path.join(sample_buildout, 'bin', 'buildout')),
140 141 142 143 144
    While:
      Initializing
      Getting section buildout
      Initializing section buildout
      Getting option buildout:x
145 146 147 148 149 150 151 152 153 154 155 156
    Error: The section name in substitution, ${bui$ldout:y},
    has invalid characters.

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

    >>> print system(os.path.join(sample_buildout, 'bin', 'buildout')),
157 158 159 160 161
    While:
      Initializing
      Getting section buildout
      Initializing section buildout
      Getting option buildout:x
162 163 164 165 166 167 168 169 170 171 172 173 174 175
    Error: The option name in substitution, ${buildout:y{z},
    has invalid characters.

and too have too many or too few colons:

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

    >>> print system(os.path.join(sample_buildout, 'bin', 'buildout')),
176 177 178 179 180
    While:
      Initializing
      Getting section buildout
      Initializing section buildout
      Getting option buildout:x
181 182 183 184 185 186 187 188 189 190 191 192
    Error: The substitution, ${parts},
    doesn't contain a colon.

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

    >>> print system(os.path.join(sample_buildout, 'bin', 'buildout')),
193 194 195 196 197
    While:
      Initializing
      Getting section buildout
      Initializing section buildout
      Getting option buildout:x
198 199 200
    Error: The substitution, ${buildout:y:z},
    has too many colons.

201 202 203 204 205 206 207 208 209
Al parts have to have a section:

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

    >>> print system(os.path.join(sample_buildout, 'bin', 'buildout')),
210 211 212
    While:
      Installing
      Getting section x
213
    Error: The referenced section, 'x', was not defined.
214 215 216 217 218 219 220 221

and all parts have to have a specified recipe:


    >>> write(sample_buildout, 'buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = x
222
    ...
223 224 225 226 227
    ... [x]
    ... foo = 1
    ... ''')

    >>> print system(os.path.join(sample_buildout, 'bin', 'buildout')),
228 229
    While:
      Installing
230 231
    Error: Missing option: x:recipe

Jim Fulton's avatar
Jim Fulton committed
232
"""
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
 
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>  
    ...      \"\"\"
252 253 254 255 256 257 258 259 260
    ...         options['debug1'] = \"\"\"
    ... <zodb>
    ...
    ...   <filestorage>
    ...     path foo
    ...   </filestorage>
    ...
    ... </zodb>  
    ... \"\"\"
261 262 263 264 265 266 267
    ...         options['debug2'] = '  x  '
    ...         options['debug3'] = '42'
    ...         options['format'] = '%3d'
    ...
    ...     def install(self):
    ...         open('t', 'w').write('t')
    ...         return 't'
Jim Fulton's avatar
Jim Fulton committed
268 269
    ...
    ...     update = install
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
    ... ''')


    >>> 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')

297 298
    >>> print system(buildout),
    buildout: Develop: /sample-buildout/recipes
299 300 301 302 303
    buildout: Installing debug

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

304 305
    >>> print system(buildout),
    buildout: Develop: /sample-buildout/recipes
Jim Fulton's avatar
Jim Fulton committed
306
    buildout: Updating debug
307 308
"""

Jim Fulton's avatar
Jim Fulton committed
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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
def finding_eggs_as_local_directories():
    r"""
It is possible to set up find-links so that we could install from
a local directory that may contained unzipped eggs.

    >>> src = tmpdir('src')
    >>> write(src, 'setup.py',
    ... '''
    ... from setuptools import setup
    ... setup(name='demo', py_modules=[''],
    ...    zip_safe=False, version='1.0', author='bob', url='bob', 
    ...    author_email='bob')
    ... ''')

    >>> write(src, 't.py', '#\n')
    >>> write(src, 'README.txt', '')
    >>> _ = system(join('bin', 'buildout')+' setup ' + src + ' bdist_egg')

Install it so it gets unzipped:

    >>> d1 = tmpdir('d1')
    >>> ws = zc.buildout.easy_install.install(
    ...     ['demo'], d1, links=[join(src, 'dist')], 
    ...     )

    >>> ls(d1)
    d  demo-1.0-py2.4.egg

Then try to install it again:

    >>> d2 = tmpdir('d2')
    >>> ws = zc.buildout.easy_install.install(
    ...     ['demo'], d2, links=[d1], 
    ...     )

    >>> ls(d2)
    d  demo-1.0-py2.4.egg

    """

Jim Fulton's avatar
Jim Fulton committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
def make_sure__get_version_works_with_2_digit_python_versions():
    """

This is a test of an internal function used by higher-level machinery.

We'll start by creating a faux 'python' that executable that prints a
2-digit version. This is a bit of a pain to do portably. :(

    >>> mkdir('demo')
    >>> write('demo', 'setup.py',
    ... '''
    ... from setuptools import setup
    ... setup(name='demo',
    ...       entry_points = {'console_scripts': ['demo = demo:main']},
    ...       )
    ... ''')
    >>> write('demo', 'demo.py',
    ... '''
    ... def main():
    ...     print 'Python 2.5'
    ... ''')

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = demo
    ... parts = 
    ... ''')

    >>> print system(join('bin', 'buildout')),
    buildout: Develop: /sample-buildout/demo

    >>> import zc.buildout.easy_install
    >>> ws = zc.buildout.easy_install.working_set(
    ...    ['demo'], sys.executable, ['develop-eggs'])
    >>> zc.buildout.easy_install.scripts(
    ...    ['demo'], ws, sys.executable, 'bin')
    ['bin/demo']

    >>> print system(join('bin', 'demo')),
    Python 2.5

Now, finally, let's test _get_version:

    >>> zc.buildout.easy_install._get_version(join('bin', 'demo'))
    '2.5'

    """

398 399 400 401 402
# Why?
## def error_for_undefined_install_parts():
##     """
## Any parts we pass to install on the command line must be
## listed in the configuration.
Jim Fulton's avatar
Jim Fulton committed
403

404 405 406
##     >>> print system(join('bin', 'buildout') + ' install foo'),
##     buildout: Invalid install parts: foo.
##     Install parts must be listed in the configuration.
Jim Fulton's avatar
Jim Fulton committed
407

408 409 410
##     >>> print system(join('bin', 'buildout') + ' install foo bar'),
##     buildout: Invalid install parts: foo bar.
##     Install parts must be listed in the configuration.
Jim Fulton's avatar
Jim Fulton committed
411
    
412
##     """
Jim Fulton's avatar
Jim Fulton committed
413

414 415 416 417 418 419 420 421 422 423 424 425 426 427

bootstrap_py = os.path.join(
       os.path.dirname(
          os.path.dirname(
             os.path.dirname(
                os.path.dirname(zc.buildout.__file__)
                )
             )
          ),
       'bootstrap', 'bootstrap.py')
if os.path.exists(bootstrap_py):
    def test_bootstrap_py():
        """Make sure the bootstrap script actually works

428
    >>> sample_buildout = tmpdir('sample')
429 430 431 432 433
    >>> os.chdir(sample_buildout)
    >>> write('bootstrap.py', open(bootstrap_py).read())
    >>> print system(sys.executable+' '+'bootstrap.py'), # doctest: +ELLIPSIS
    Downloading ...
    Warning: creating ...buildout.cfg
434 435 436 437 438
    buildout: Creating directory ...bin
    buildout: Creating directory ...parts
    buildout: Creating directory ...eggs
    buildout: Creating directory ...develop-eggs

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
    >>> ls(sample_buildout)
    d  bin
    -  bootstrap.py
    -  buildout.cfg
    d  develop-eggs
    d  eggs
    d  parts


    >>> ls(sample_buildout, 'bin')
    -  buildout

    >>> ls(sample_buildout, 'eggs')
    -  setuptools-0.6-py2.4.egg
    d  zc.buildout-1.0-py2.4.egg

    """

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
def test_help():
    """
>>> print system(os.path.join(sample_buildout, 'bin', 'buildout')+' -h'),
Usage: buildout [options] [assignments] [command [command arguments]]
<BLANKLINE>
Options:
<BLANKLINE>
  -h, --help
<BLANKLINE>
     Print this message and exit.
<BLANKLINE>
  -v
<BLANKLINE>
     Increase the level of verbosity.  This option can be used multiple times.
<BLANKLINE>
  -q
<BLANKLINE>
Jim Fulton's avatar
Jim Fulton committed
474
     Decrease the level of verbosity.  This option can be used multiple times.
475 476 477 478
<BLANKLINE>
  -c config_file
<BLANKLINE>
     Specify the path to the buildout configuration file to be used.
Jim Fulton's avatar
Jim Fulton committed
479 480
     This defaults to the file named "buildout.cfg" in the current
     working directory.
Jim Fulton's avatar
Jim Fulton committed
481 482 483 484
<BLANKLINE>
  -U
<BLANKLINE>
     Don't read user defaults.
Jim Fulton's avatar
Jim Fulton committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
<BLANKLINE>
  -o
<BLANKLINE>
    Run in off-line mode.  This is equivalent to the assignment 
    buildout:offline=true.
<BLANKLINE>
  -O
<BLANKLINE>
    Run in non-off-line mode.  This is equivalent to the assignment 
    buildout:offline=false.  This is the default buildout mode.  The
    -O option would normally be used to override a true offline
    setting in a configuration file.
<BLANKLINE>
  -n
<BLANKLINE>
    Run in newest mode.  This is equivalent to the assignment
    buildout:newest=true.  With this setting, which is the default,
    buildout will try to find the newest versions of distributions
    available that satisfy its requirements.
<BLANKLINE>
  -N
<BLANKLINE>
    Run in non-newest mode.  This is equivalent to the assignment 
    buildout:newest=false.  With this setting, buildout will not seek
    new distributions if installed distributions satisfy it's
    requirements. 
511 512 513 514 515 516
<BLANKLINE>
  -D
<BLANKLINE>
    Debug errors.  If an error occurs, then the post-mortem debugger
    will be started. This is especially useful for debuging recipe
    problems.
517 518
<BLANKLINE>
Assignments are of the form: section:option=value and are used to
Jim Fulton's avatar
Jim Fulton committed
519
provide configuration options that override those given in the
520 521 522 523 524
configuration file.  For example, to run the buildout in offline mode,
use buildout:offline=true.
<BLANKLINE>
Options and assignments can be interspersed.
<BLANKLINE>
Jim Fulton's avatar
Jim Fulton committed
525
Commands:
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
<BLANKLINE>
  install [parts]
<BLANKLINE>
    Install parts.  If no command arguments are given, then the parts
    definition from the configuration file is used.  Otherwise, the
    arguments specify the parts to be installed.
<BLANKLINE>
  bootstrap
<BLANKLINE>
    Create a new buildout in the current working directory, copying
    the buildout and setuptools eggs and, creating a basic directory
    structure and a buildout-local buildout script.
<BLANKLINE>
<BLANKLINE>

>>> print system(os.path.join(sample_buildout, 'bin', 'buildout')
...              +' --help'),
Usage: buildout [options] [assignments] [command [command arguments]]
<BLANKLINE>
Options:
<BLANKLINE>
  -h, --help
<BLANKLINE>
     Print this message and exit.
<BLANKLINE>
  -v
<BLANKLINE>
     Increase the level of verbosity.  This option can be used multiple times.
<BLANKLINE>
  -q
<BLANKLINE>
Jim Fulton's avatar
Jim Fulton committed
557
     Decrease the level of verbosity.  This option can be used multiple times.
558 559 560 561
<BLANKLINE>
  -c config_file
<BLANKLINE>
     Specify the path to the buildout configuration file to be used.
Jim Fulton's avatar
Jim Fulton committed
562 563
     This defaults to the file named "buildout.cfg" in the current
     working directory.
Jim Fulton's avatar
Jim Fulton committed
564 565 566 567
<BLANKLINE>
  -U
<BLANKLINE>
     Don't read user defaults.
Jim Fulton's avatar
Jim Fulton committed
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
<BLANKLINE>
  -o
<BLANKLINE>
    Run in off-line mode.  This is equivalent to the assignment 
    buildout:offline=true.
<BLANKLINE>
  -O
<BLANKLINE>
    Run in non-off-line mode.  This is equivalent to the assignment 
    buildout:offline=false.  This is the default buildout mode.  The
    -O option would normally be used to override a true offline
    setting in a configuration file.
<BLANKLINE>
  -n
<BLANKLINE>
    Run in newest mode.  This is equivalent to the assignment
    buildout:newest=true.  With this setting, which is the default,
    buildout will try to find the newest versions of distributions
    available that satisfy its requirements.
<BLANKLINE>
  -N
<BLANKLINE>
    Run in non-newest mode.  This is equivalent to the assignment 
    buildout:newest=false.  With this setting, buildout will not seek
    new distributions if installed distributions satisfy it's
    requirements. 
594 595 596 597 598 599
<BLANKLINE>
  -D
<BLANKLINE>
    Debug errors.  If an error occurs, then the post-mortem debugger
    will be started. This is especially useful for debuging recipe
    problems.
600 601
<BLANKLINE>
Assignments are of the form: section:option=value and are used to
Jim Fulton's avatar
Jim Fulton committed
602
provide configuration options that override those given in the
603 604 605 606 607
configuration file.  For example, to run the buildout in offline mode,
use buildout:offline=true.
<BLANKLINE>
Options and assignments can be interspersed.
<BLANKLINE>
Jim Fulton's avatar
Jim Fulton committed
608
Commands:
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
<BLANKLINE>
  install [parts]
<BLANKLINE>
    Install parts.  If no command arguments are given, then the parts
    definition from the configuration file is used.  Otherwise, the
    arguments specify the parts to be installed.
<BLANKLINE>
  bootstrap
<BLANKLINE>
    Create a new buildout in the current working directory, copying
    the buildout and setuptools eggs and, creating a basic directory
    structure and a buildout-local buildout script.
<BLANKLINE>
<BLANKLINE>
    """
624

Jim Fulton's avatar
Jim Fulton committed
625 626 627 628 629 630
def test_bootstrap_with_extension():
    """
We had a problem running a bootstrap with an extension.  Let's make
sure it is fixed.  Basically, we don't load extensions when
bootstrapping.

631
    >>> d = tmpdir('sample-bootstrap')
Jim Fulton's avatar
Jim Fulton committed
632 633 634 635 636 637 638 639 640 641 642
    
    >>> write(d, 'buildout.cfg',
    ... '''
    ... [buildout]
    ... extensions = some_awsome_extension
    ... parts = 
    ... ''')

    >>> os.chdir(d)
    >>> print system(os.path.join(sample_buildout, 'bin', 'buildout')
    ...              + ' bootstrap'),
643 644 645 646
    buildout: Creating directory /sample-bootstrap/bin
    buildout: Creating directory /sample-bootstrap/parts
    buildout: Creating directory /sample-bootstrap/eggs
    buildout: Creating directory /sample-bootstrap/develop-eggs
Jim Fulton's avatar
Jim Fulton committed
647
    """
648

649 650 651
def removing_eggs_from_develop_section_causes_egg_link_to_be_removed():
    '''
    >>> cd(sample_buildout)
652

653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
Create a develop egg:

    >>> mkdir('foo')
    >>> write('foo', 'setup.py',
    ... """
    ... from setuptools import setup
    ... setup(name='foox')
    ... """)
    >>> write('buildout.cfg',
    ... """
    ... [buildout]
    ... develop = foo
    ... parts =
    ... """)

    >>> print system(join('bin', 'buildout')),
669
    buildout: Develop: /sample-buildout/foo
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689

    >>> ls('develop-eggs')
    -  foox.egg-link

Create another:

    >>> mkdir('bar')
    >>> write('bar', 'setup.py',
    ... """
    ... from setuptools import setup
    ... setup(name='fooy')
    ... """)
    >>> write('buildout.cfg',
    ... """
    ... [buildout]
    ... develop = foo bar
    ... parts =
    ... """)

    >>> print system(join('bin', 'buildout')),
690 691
    buildout: Develop: /sample-buildout/foo
    buildout: Develop: /sample-buildout/bar
692 693 694 695 696 697 698 699 700 701 702 703 704 705

    >>> ls('develop-eggs')
    -  foox.egg-link
    -  fooy.egg-link

Remove one:

    >>> write('buildout.cfg',
    ... """
    ... [buildout]
    ... develop = bar
    ... parts =
    ... """)
    >>> print system(join('bin', 'buildout')),
706
    buildout: Develop: /sample-buildout/bar
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734

It is gone

    >>> ls('develop-eggs')
    -  fooy.egg-link

Remove the other:

    >>> write('buildout.cfg',
    ... """
    ... [buildout]
    ... parts =
    ... """)
    >>> print system(join('bin', 'buildout')),

All gone

    >>> ls('develop-eggs')
    '''


def add_setuptools_to_dependencies_when_namespace_packages():
    '''    
Often, a package depends on setuptools soley by virtue of using
namespace packages. In this situation, package authors often forget to
declare setuptools as a dependency. This is a mistake, but,
unfortunately, a common one that we need to work around.  If an egg
uses namespace packages and does not include setuptools as a depenency,
Jim Fulton's avatar
Jim Fulton committed
735
we will still include setuptools in the working set.  If we see this for
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
a devlop egg, we will also generate a warning.

    >>> mkdir('foo')
    >>> mkdir('foo', 'src')
    >>> mkdir('foo', 'src', 'stuff')
    >>> write('foo', 'src', 'stuff', '__init__.py',
    ... """__import__('pkg_resources').declare_namespace(__name__)
    ... """)
    >>> mkdir('foo', 'src', 'stuff', 'foox')
    >>> write('foo', 'src', 'stuff', 'foox', '__init__.py', '')
    >>> write('foo', 'setup.py',
    ... """
    ... from setuptools import setup
    ... setup(name='foox',
    ...       namespace_packages = ['stuff'],
    ...       package_dir = {'': 'src'},
    ...       packages = ['stuff', 'stuff.foox'],
    ...       )
    ... """)
    >>> write('foo', 'README.txt', '')
    
    >>> write('buildout.cfg',
    ... """
    ... [buildout]
    ... develop = foo
    ... parts = 
    ... """)

    >>> print system(join('bin', 'buildout')),
765
    buildout: Develop: /sample-buildout/foo
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814

Now, if we generate a working set using the egg link, we will get a warning
and we will get setuptools included in the working set.

    >>> import logging, zope.testing.loggingsupport
    >>> handler = zope.testing.loggingsupport.InstalledHandler(
    ...        'zc.buildout', level=logging.WARNING)
    >>> logging.getLogger('zc').propagate = False
    
    >>> [dist.project_name
    ...  for dist in zc.buildout.easy_install.working_set(
    ...    ['foox'], sys.executable,
    ...    [join(sample_buildout, 'eggs'),
    ...     join(sample_buildout, 'develop-eggs'),
    ...     ])]
    ['foox', 'setuptools']

    >>> print handler
    zc.buildout.easy_install WARNING
      Develop distribution for foox 0.0.0
    uses namespace packages but the distribution does not require setuptools.

    >>> handler.clear()

On the other hand, if we have a regular egg, rather than a develop egg:

    >>> os.remove(join('develop-eggs', 'foox.egg-link'))

    >>> _ = system(join('bin', 'buildout') + ' setup foo bdist_egg -d'
    ...            + join(sample_buildout, 'eggs'))

    >>> ls('develop-eggs')
    
    >>> ls('eggs') # doctest: +ELLIPSIS
    -  foox-0.0.0-py2.4.egg
    ...
    
We do not get a warning, but we do get setuptools included in the working set:

    >>> [dist.project_name
    ...  for dist in zc.buildout.easy_install.working_set(
    ...    ['foox'], sys.executable,
    ...    [join(sample_buildout, 'eggs'),
    ...     join(sample_buildout, 'develop-eggs'),
    ...     ])]
    ['foox', 'setuptools']

    >>> print handler,

Jim Fulton's avatar
Jim Fulton committed
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
We get the same behavior if the it is a depedency that uses a
namespace package.


    >>> mkdir('bar')
    >>> write('bar', 'setup.py',
    ... """
    ... from setuptools import setup
    ... setup(name='bar', install_requires = ['foox'])
    ... """)
    >>> write('bar', 'README.txt', '')
    
    >>> write('buildout.cfg',
    ... """
    ... [buildout]
    ... develop = foo bar
    ... parts = 
    ... """)

    >>> print system(join('bin', 'buildout')),
835 836
    buildout: Develop: /sample-buildout/foo
    buildout: Develop: /sample-buildout/bar
Jim Fulton's avatar
Jim Fulton committed
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851

    >>> [dist.project_name
    ...  for dist in zc.buildout.easy_install.working_set(
    ...    ['bar'], sys.executable,
    ...    [join(sample_buildout, 'eggs'),
    ...     join(sample_buildout, 'develop-eggs'),
    ...     ])]
    ['bar', 'foox', 'setuptools']

    >>> print handler,
    zc.buildout.easy_install WARNING
      Develop distribution for foox 0.0.0
    uses namespace packages but the distribution does not require setuptools.


852 853
    >>> logging.getLogger('zc').propagate = True
    >>> handler.uninstall()
Jim Fulton's avatar
Jim Fulton committed
854

855
    '''
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884

def develop_preserves_existing_setup_cfg():
    """
    
See "Handling custom build options for extensions in develop eggs" in
easy_install.txt.  This will be very similar except that we'll have an
existing setup.cfg:

    >>> write(extdemo, "setup.cfg",
    ... '''
    ... # sampe cfg file
    ...
    ... [foo]
    ... bar = 1
    ...
    ... [build_ext]
    ... define = X,Y
    ... ''')

    >>> mkdir('include')
    >>> write('include', 'extdemo.h',
    ... '''
    ... #define EXTDEMO 42
    ... ''')

    >>> dest = tmpdir('dest')
    >>> zc.buildout.easy_install.develop(
    ...   extdemo, dest, 
    ...   {'include-dirs': os.path.join(sample_buildout, 'include')})
Jim Fulton's avatar
Jim Fulton committed
885
    '/dest/extdemo.egg-link'
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900

    >>> ls(dest)
    -  extdemo.egg-link

    >>> cat(extdemo, "setup.cfg")
    <BLANKLINE>
    # sampe cfg file
    <BLANKLINE>
    [foo]
    bar = 1
    <BLANKLINE>
    [build_ext]
    define = X,Y

"""
Jim Fulton's avatar
Jim Fulton committed
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935

def uninstall_recipes_used_for_removal():
    """
Uninstall recipes need to be called when a part is removed too:

    >>> mkdir("recipes")
    >>> write("recipes", "setup.py",
    ... '''
    ... from setuptools import setup
    ... setup(name='recipes',
    ...       entry_points={
    ...          'zc.buildout': ["demo=demo:Install"],
    ...          'zc.buildout.uninstall': ["demo=demo:uninstall"],
    ...          })
    ... ''')

    >>> write("recipes", "demo.py",
    ... '''
    ... class Install:
    ...     def __init__(*args): pass
    ...     def install(self):
    ...         print 'installing'
    ...         return ()
    ... def uninstall(name, options): print 'uninstalling'
    ... ''')

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

    >>> print system(join('bin', 'buildout')),
Jim Fulton's avatar
Jim Fulton committed
936
    buildout: Develop: /sample-buildout/recipes
Jim Fulton's avatar
Jim Fulton committed
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
    buildout: Installing demo
    installing


    >>> write('buildout.cfg', '''
    ... [buildout]
    ... develop = recipes
    ... parts = demo
    ... [demo]
    ... recipe = recipes:demo
    ... x = 1
    ... ''')

    >>> print system(join('bin', 'buildout')),
    buildout: Develop: /sample-buildout/recipes
    buildout: Uninstalling demo
    buildout: Running uninstall recipe
    uninstalling
    buildout: Installing demo
    installing


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

    >>> print system(join('bin', 'buildout')),
    buildout: Develop: /sample-buildout/recipes
    buildout: Uninstalling demo
    buildout: Running uninstall recipe
    uninstalling

"""

Jim Fulton's avatar
Jim Fulton committed
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
def extensions_installed_as_eggs_work_in_offline_mode():
    '''
    >>> mkdir('demo')

    >>> write('demo', 'demo.py', 
    ... """
    ... def ext(buildout):
    ...     print 'ext', list(buildout)
    ... """)

    >>> write('demo', 'setup.py',
    ... """
    ... from setuptools import setup
    ... 
    ... setup(
    ...     name = "demo",
    ...     py_modules=['demo'],
    ...     entry_points = {'zc.buildout.extension': ['ext = demo:ext']},
    ...     )
    ... """)

    >>> bdist_egg(join(sample_buildout, "demo"), sys.executable,
    ...           join(sample_buildout, "eggs"))

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... extensions = demo
    ... parts =
    ... offline = true
    ... """)

    >>> print system(join(sample_buildout, 'bin', 'buildout')),
    ext ['buildout']
    

    '''

Jim Fulton's avatar
Jim Fulton committed
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
def changes_in_svn_or_CVS_dont_affect_sig():
    """
    
If we have a develop recipe, it's signature shouldn't be affected to
changes in .svn or CVS directories.

    >>> mkdir('recipe')
    >>> write('recipe', 'setup.py',
    ... '''
    ... from setuptools import setup
    ... setup(name='recipe',
    ...       entry_points={'zc.buildout': ['default=foo:Foo']})
    ... ''')
    >>> write('recipe', 'foo.py',
    ... '''
    ... class Foo:
    ...     def __init__(*args): pass
    ...     def install(*args): return ()
    ...     update = install
    ... ''')
    
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = recipe
    ... parts = foo
    ... 
    ... [foo]
    ... recipe = recipe
    ... ''')


    >>> print system(join(sample_buildout, 'bin', 'buildout')),
    buildout: Develop: /sample-buildout/recipe
    buildout: Installing foo

    >>> mkdir('recipe', '.svn')
    >>> mkdir('recipe', 'CVS')
    >>> print system(join(sample_buildout, 'bin', 'buildout')),
    buildout: Develop: /sample-buildout/recipe
    buildout: Updating foo

    >>> write('recipe', '.svn', 'x', '1')
    >>> write('recipe', 'CVS', 'x', '1')

    >>> print system(join(sample_buildout, 'bin', 'buildout')),
    buildout: Develop: /sample-buildout/recipe
    buildout: Updating foo

    """

def o_option_sets_offline():
    """
    >>> print system(join(sample_buildout, 'bin', 'buildout')+' -vvo'),
    ... # doctest: +ELLIPSIS
    <BLANKLINE>
    ...
    offline = true
    ...
    """

Jim Fulton's avatar
Jim Fulton committed
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
def recipe_upgrade():
    """

The buildout will upgrade recipes in newest (and non-offline) mode.

Let's create a recipe egg

    >>> mkdir('recipe')
    >>> write('recipe', 'recipe.py',
    ... '''
    ... class Recipe:
    ...     def __init__(*a): pass
    ...     def install(self):
    ...         print 'recipe v1'
    ...         return ()
    ...     update = install
    ... ''')

    >>> write('recipe', 'setup.py',
    ... '''
    ... from setuptools import setup
    ... setup(name='recipe', version='1', py_modules=['recipe'],
    ...       entry_points={'zc.buildout': ['default = recipe:Recipe']},
    ...       )
    ... ''')

    >>> write('recipe', 'README', '')

    >>> print system(buildout+' setup recipe bdist_egg'), # doctest: +ELLIPSIS
    buildout: Running setup script recipe/setup.py
    ...

Jim Fulton's avatar
Jim Fulton committed
1104 1105
    >>> rmdir('recipe', 'build')

Jim Fulton's avatar
Jim Fulton committed
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
And update our buildout to use it.

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = foo
    ... find-links = %s
    ...
    ... [foo]
    ... recipe = recipe
    ... ''' % join('recipe', 'dist'))

    >>> print system(buildout),
    zc.buildout.easy_install: Getting new distribution for recipe
    zc.buildout.easy_install: Got recipe 1
    buildout: Installing foo
    recipe v1

Now, if we update the recipe egg:

    >>> write('recipe', 'recipe.py',
    ... '''
    ... class Recipe:
    ...     def __init__(*a): pass
    ...     def install(self):
    ...         print 'recipe v2'
    ...         return ()
    ...     update = install
    ... ''')

    >>> write('recipe', 'setup.py',
    ... '''
    ... from setuptools import setup
    ... setup(name='recipe', version='2', py_modules=['recipe'],
    ...       entry_points={'zc.buildout': ['default = recipe:Recipe']},
    ...       )
    ... ''')


    >>> print system(buildout+' setup recipe bdist_egg'), # doctest: +ELLIPSIS
    buildout: Running setup script recipe/setup.py
    ...

We won't get the update if we specify -N:

    >>> print system(buildout+' -N'),
    buildout: Updating foo
    recipe v1

or if we use -o:

    >>> print system(buildout+' -o'),
    buildout: Updating foo
    recipe v1

But we will if we use neither of these:

    >>> print system(buildout),
    zc.buildout.easy_install: Getting new distribution for recipe
    zc.buildout.easy_install: Got recipe 2
    buildout: Uninstalling foo
    buildout: Installing foo
Jim Fulton's avatar
Jim Fulton committed
1168
    recipe v2
Jim Fulton's avatar
Jim Fulton committed
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251

We can also select a particular recipe version:

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = foo
    ... find-links = %s
    ...
    ... [foo]
    ... recipe = recipe ==1
    ... ''' % join('recipe', 'dist'))

    >>> print system(buildout),
    buildout: Uninstalling foo
    buildout: Installing foo
    recipe v1
    
    """

def update_adds_to_uninstall_list():
    """

Paths returned by the update method are added to the list of paths to
uninstall

    >>> mkdir('recipe')
    >>> write('recipe', 'setup.py',
    ... '''
    ... from setuptools import setup
    ... setup(name='recipe',
    ...       entry_points={'zc.buildout': ['default = recipe:Recipe']},
    ...       )
    ... ''')

    >>> write('recipe', 'recipe.py',
    ... '''
    ... import os
    ... class Recipe:
    ...     def __init__(*_): pass
    ...     def install(self):
    ...         r = ('a', 'b', 'c')
    ...         for p in r: os.mkdir(p)
    ...         return r
    ...     def update(self):
    ...         r = ('c', 'd', 'e')
    ...         for p in r:
    ...             if not os.path.exists(p):
    ...                os.mkdir(p)
    ...         return r
    ... ''')

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = recipe
    ... parts = foo
    ...
    ... [foo]
    ... recipe = recipe
    ... ''')

    >>> print system(buildout),
    buildout: Develop: /tmp/tmpbHOHnU/_TEST_/sample-buildout/recipe
    buildout: Installing foo

    >>> print system(buildout),
    buildout: Develop: /tmp/tmpbHOHnU/_TEST_/sample-buildout/recipe
    buildout: Updating foo

    >>> cat('.installed.cfg') # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
    [buildout]
    ...
    [foo]
    __buildout_installed__ = c
    	d
    	e
    	a
    	b
    __buildout_signature__ = ...

"""

1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
def log_when_there_are_not_local_distros():
    """
    >>> from zope.testing.loggingsupport import InstalledHandler
    >>> handler = InstalledHandler('zc.buildout.easy_install')
    >>> import logging
    >>> logger = logging.getLogger('zc.buildout.easy_install')
    >>> old_propogate = logger.propagate
    >>> logger.propagate = False

    >>> dest = tmpdir('sample-install')
    >>> import zc.buildout.easy_install
    >>> ws = zc.buildout.easy_install.install(
    ...     ['demo==0.2'], dest,
    ...     links=[link_server], index=link_server+'index/')

    >>> print handler # doctest: +ELLIPSIS
    zc.buildout.easy_install DEBUG
      Installing ['demo==0.2']
    zc.buildout.easy_install DEBUG
      We have no distributions for demo that satisfies demo==0.2.
    ...

    >>> handler.uninstall()
    >>> logger.propagate = old_propogate
    
    """
Jim Fulton's avatar
Jim Fulton committed
1278

1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
def internal_errors():
    """Internal errors are clearly marked and don't generate tracebacks:

    >>> mkdir(sample_buildout, 'recipes')

    >>> write(sample_buildout, 'recipes', 'mkdir.py', 
    ... '''
    ... class Mkdir:
    ...     def __init__(self, buildout, name, options):
    ...         self.name, self.options = name, options
    ...         options['path'] = os.path.join(
    ...                               buildout['buildout']['directory'],
    ...                               options['path'],
    ...                               )
    ... ''')

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

    >>> write(sample_buildout, 'buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = recipes
    ... parts = data-dir
    ...
    ... [data-dir]
    ... recipe = recipes:mkdir
    ... ''')

    >>> print system(buildout),
    buildout: Develop: /sample-buildout/recipes
    While:
      Installing
      Getting section data-dir
      Initializing part data-dir
    <BLANKLINE>
    An internal error occured due to a bug in either zc.buildout or in a
    recipe being used:
    <BLANKLINE>
    NameError:
    global name 'os' is not defined
    """
    

Jim Fulton's avatar
Jim Fulton committed
1328
######################################################################
1329
    
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
def create_sample_eggs(test, executable=sys.executable):
    write = test.globs['write']
    dest = test.globs['sample_eggs']
    tmp = tempfile.mkdtemp()
    try:
        write(tmp, 'README.txt', '')

        for i in (0, 1):
            write(tmp, 'eggrecipedemobeeded.py', 'y=%s\n' % i)
            write(
                tmp, 'setup.py',
                "from setuptools import setup\n"
                "setup(name='demoneeded', py_modules=['eggrecipedemobeeded'],"
                " zip_safe=True, version='1.%s', author='bob', url='bob', "
                "author_email='bob')\n"
                % i
                )
            zc.buildout.testing.sdist(tmp, dest)
1348

1349 1350 1351
        write(
            tmp, 'setup.py',
            "from setuptools import setup\n"
Jim Fulton's avatar
Jim Fulton committed
1352
            "setup(name='other', zip_safe=False, version='1.0', "
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
            "py_modules=['eggrecipedemobeeded'])\n"
            )
        zc.buildout.testing.bdist_egg(tmp, executable, dest)

        os.remove(os.path.join(tmp, 'eggrecipedemobeeded.py'))

        for i in (1, 2, 3):
            write(
                tmp, 'eggrecipedemo.py',
                'import eggrecipedemobeeded\n'
                'x=%s\n'
                'def main(): print x, eggrecipedemobeeded.y\n'
                % i)
            write(
                tmp, 'setup.py',
                "from setuptools import setup\n"
                "setup(name='demo', py_modules=['eggrecipedemo'],"
                " install_requires = 'demoneeded',"
                " entry_points={'console_scripts': "
                     "['demo = eggrecipedemo:main']},"
                " zip_safe=True, version='0.%s')\n" % i
                )
            zc.buildout.testing.bdist_egg(tmp, executable, dest)
    finally:
        shutil.rmtree(tmp)

extdemo_c = """
#include <Python.h>
#include <extdemo.h>

static PyMethodDef methods[] = {{NULL}};

PyMODINIT_FUNC
initextdemo(void)
{
1388 1389 1390 1391 1392 1393 1394
    PyObject *m;
    m = Py_InitModule3("extdemo", methods, "");
#ifdef TWO
    PyModule_AddObject(m, "val", PyInt_FromLong(2));
#else
    PyModule_AddObject(m, "val", PyInt_FromLong(EXTDEMO));
#endif
1395 1396
}
"""
1397

1398 1399
extdemo_setup_py = """
from distutils.core import setup, Extension
1400

1401
setup(name = "extdemo", version = "%s", url="http://www.zope.org",
1402 1403 1404 1405
      author="Demo", author_email="demo@demo.com",
      ext_modules = [Extension('extdemo', ['extdemo.c'])],
      )
"""
1406

1407 1408 1409 1410 1411 1412
def add_source_dist(test, version=1.4):

    if 'extdemo' not in test.globs:
        test.globs['extdemo'] = test.globs['tmpdir']('extdemo')

    tmp = test.globs['extdemo']
1413 1414 1415
    write = test.globs['write']
    try:
        write(tmp, 'extdemo.c', extdemo_c);
1416
        write(tmp, 'setup.py', extdemo_setup_py % version);
1417 1418 1419 1420 1421
        write(tmp, 'README', "");
        write(tmp, 'MANIFEST.in', "include *.c\n");
        test.globs['sdist'](tmp, test.globs['sample_eggs'])
    except:
        shutil.rmtree(tmp)
1422

1423 1424 1425 1426 1427 1428 1429 1430 1431
def easy_install_SetUp(test):
    zc.buildout.testing.buildoutSetUp(test)
    sample_eggs = test.globs['tmpdir']('sample_eggs')
    test.globs['sample_eggs'] = sample_eggs
    os.mkdir(os.path.join(sample_eggs, 'index'))
    create_sample_eggs(test)
    add_source_dist(test)
    test.globs['link_server'] = test.globs['start_server'](
        test.globs['sample_eggs'])
1432
    test.globs['update_extdemo'] = lambda : add_source_dist(test, 1.5)
1433

1434
        
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
egg_parse = re.compile('([0-9a-zA-Z_.]+)-([0-9a-zA-Z_.]+)-py(\d[.]\d).egg$'
                       ).match
def makeNewRelease(project, ws, dest):
    dist = ws.find(pkg_resources.Requirement.parse(project))
    eggname, oldver, pyver = egg_parse(
        os.path.basename(dist.location)
        ).groups()
    dest = os.path.join(dest, "%s-99.99-py%s.egg" % (eggname, pyver)) 
    if os.path.isfile(dist.location):
        shutil.copy(dist.location, dest)
        zip = zipfile.ZipFile(dest, 'a')
        zip.writestr(
            'EGG-INFO/PKG-INFO',
            zip.read('EGG-INFO/PKG-INFO').replace("Version: %s" % oldver, 
                                                  "Version: 99.99")
            )
        zip.close()
    else:
1453
        shutil.copytree(dist.location, dest)
1454 1455 1456 1457 1458 1459 1460 1461
        info_path = os.path.join(dest, 'EGG-INFO', 'PKG-INFO')
        info = open(info_path).read().replace("Version: %s" % oldver, 
                                              "Version: 99.99")
        open(info_path, 'w').write(info)


def updateSetup(test):
    zc.buildout.testing.buildoutSetUp(test)
1462 1463
    new_releases = test.globs['tmpdir']('new_releases')
    test.globs['new_releases'] = new_releases
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
    sample_buildout = test.globs['sample_buildout']
    eggs = os.path.join(sample_buildout, 'eggs')

    # If the zc.buildout dist is a develo dist, convert it to a
    # regular egg in the sample buildout
    req = pkg_resources.Requirement.parse('zc.buildout')
    dist = pkg_resources.working_set.find(req)
    if dist.precedence == pkg_resources.DEVELOP_DIST:
        # We have a develop egg, create a real egg for it:
        here = os.getcwd()
        os.chdir(os.path.dirname(dist.location))
        assert os.spawnle(
            os.P_WAIT, sys.executable, sys.executable,
            os.path.join(os.path.dirname(dist.location), 'setup.py'),
            '-q', 'bdist_egg', '-d', eggs,
            dict(os.environ,
                 PYTHONPATH=pkg_resources.working_set.find(
                               pkg_resources.Requirement.parse('setuptools')
                               ).location,
                 ),
            ) == 0
        os.chdir(here)
        os.remove(os.path.join(eggs, 'zc.buildout.egg-link'))

        # Rebuild the buildout script
        ws = pkg_resources.WorkingSet([eggs])
        ws.require('zc.buildout')
        zc.buildout.easy_install.scripts(
            ['zc.buildout'], ws, sys.executable,
            os.path.join(sample_buildout, 'bin'))
    else:
        ws = pkg_resources.working_set

    # now let's make the new releases
    makeNewRelease('zc.buildout', ws, new_releases)
    makeNewRelease('setuptools', ws, new_releases)

    os.mkdir(os.path.join(new_releases, 'zc.buildout'))
    os.mkdir(os.path.join(new_releases, 'setuptools'))
1503 1504

    
Jim Fulton's avatar
Jim Fulton committed
1505
    
1506 1507 1508 1509 1510
normalize_bang = (
    re.compile(re.escape('#!'+sys.executable)),
    '#!/usr/local/bin/python2.4',
    )

1511
def test_suite():
1512
    import zc.buildout.testselectingpython
1513
    suite = unittest.TestSuite((
1514
        doctest.DocFileSuite(
1515
            'buildout.txt', 'runsetup.txt', 'repeatable.txt',
Jim Fulton's avatar
Jim Fulton committed
1516
            setUp=zc.buildout.testing.buildoutSetUp,
1517
            tearDown=zc.buildout.testing.buildoutTearDown,
Jim Fulton's avatar
Jim Fulton committed
1518
            checker=renormalizing.RENormalizing([
1519 1520 1521
               zc.buildout.testing.normalize_path,
               zc.buildout.testing.normalize_script,
               zc.buildout.testing.normalize_egg_py,
Jim Fulton's avatar
Jim Fulton committed
1522 1523
               (re.compile('__buildout_signature__ = recipes-\S+'),
                '__buildout_signature__ = recipes-SSSSSSSSSSS'),
1524 1525
               (re.compile('executable = \S+python\S*'),
                'executable = python'),
1526
               (re.compile('[-d]  setuptools-\S+[.]egg'), 'setuptools.egg'),
Jim Fulton's avatar
Jim Fulton committed
1527 1528
               (re.compile('zc.buildout(-\S+)?[.]egg(-link)?'),
                'zc.buildout.egg'),
1529
               (re.compile('creating \S*setup.cfg'), 'creating setup.cfg'),
Jim Fulton's avatar
Jim Fulton committed
1530
               (re.compile('hello\%ssetup' % os.path.sep), 'hello/setup'),
1531 1532
               (re.compile('zc.buildout.easy_install.picked: (\S+) = \S+'),
                'picked \\1 = V.V'),
1533
               ])
1534
            ),
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
        doctest.DocFileSuite(
            'debugging.txt',
            setUp=zc.buildout.testing.buildoutSetUp,
            tearDown=zc.buildout.testing.buildoutTearDown,
            checker=renormalizing.RENormalizing([
               zc.buildout.testing.normalize_path,
               (re.compile(r'\S+buildout.py'), 'buildout.py'),
               (re.compile(r'line \d+'), 'line NNN'),
               (re.compile(r'py\(\d+\)'), 'py(NNN)'),
               ])
            ),
1546 1547 1548 1549 1550 1551

        doctest.DocFileSuite(
            'update.txt',
            setUp=updateSetup,
            tearDown=zc.buildout.testing.buildoutTearDown,
            checker=renormalizing.RENormalizing([
1552 1553 1554 1555
               zc.buildout.testing.normalize_path,
               zc.buildout.testing.normalize_script,
               zc.buildout.testing.normalize_egg_py,
               normalize_bang,
1556 1557 1558 1559 1560 1561
               (re.compile('99[.]99'), 'NINETYNINE.NINETYNINE'),
               (re.compile('(zc.buildout|setuptools)-\d+[.]\d+\S*'
                           '-py\d.\d.egg'),
                '\\1.egg'),
               (re.compile('(zc.buildout|setuptools)( version)? \d+[.]\d+\S*'),
                '\\1 V.V'),
1562
               (re.compile('[-d]  setuptools'), '-  setuptools'),
1563 1564
               ])
            ),
1565
        
Jim Fulton's avatar
Jim Fulton committed
1566
        doctest.DocFileSuite(
1567
            'easy_install.txt', 
1568 1569
            setUp=easy_install_SetUp,
            tearDown=zc.buildout.testing.buildoutTearDown,
1570

1571 1572 1573 1574 1575
            checker=renormalizing.RENormalizing([
               zc.buildout.testing.normalize_path,
               zc.buildout.testing.normalize_script,
               zc.buildout.testing.normalize_egg_py,
               normalize_bang,
Jim Fulton's avatar
Jim Fulton committed
1576
               (re.compile('extdemo[.]pyd'), 'extdemo.so')
Jim Fulton's avatar
Jim Fulton committed
1577 1578
               ]),
            ),
1579
        doctest.DocTestSuite(
1580
            setUp=easy_install_SetUp,
1581
            tearDown=zc.buildout.testing.buildoutTearDown,
1582 1583 1584 1585
            checker=renormalizing.RENormalizing([
               zc.buildout.testing.normalize_path,
               zc.buildout.testing.normalize_script,
               zc.buildout.testing.normalize_egg_py,
1586 1587
               (re.compile("buildout: Running \S*setup.py"),
                'buildout: Running setup.py'),
1588
               (re.compile('setuptools-\S+-'),
1589
                'setuptools.egg'),
1590
               (re.compile('zc.buildout-\S+-'),
1591
                'zc.buildout.egg'),
1592
               ]),
1593
            ),
1594
        ))
1595 1596 1597 1598 1599 1600 1601

    if sys.version_info[:2] != (2, 3):
        # Only run selecting python tests if not 2.3, since
        # 2.3 is the alternate python used in the tests.
        suite.addTest(zc.buildout.testselectingpython.test_suite())

    return suite