tests.py 99 KB
Newer Older
1 2
##############################################################################
#
3
# Copyright (c) 2004-2009 Zope Foundation and Contributors.
4 5 6
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8 9 10 11 12 13
# 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.
#
##############################################################################
14
from zc.buildout.buildout import print_
15
from zope.testing import renormalizing
16

Jim Fulton's avatar
Jim Fulton committed
17
import doctest
18 19 20
import manuel.capture
import manuel.doctest
import manuel.testing
21
import os
22
import pkg_resources
23 24 25 26 27 28 29 30
import re
import shutil
import sys
import tempfile
import unittest
import zc.buildout.easy_install
import zc.buildout.testing
import zipfile
31

Jim Fulton's avatar
Jim Fulton committed
32 33 34 35
os_path_sep = os.path.sep
if os_path_sep == '\\':
    os_path_sep *= 2

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

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
52
    ... parts =
53 54
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
55
    >>> print_(system(join('bin', 'buildout')), end='')
56
    Develop: '/sample-buildout/foo'
57 58 59

    >>> ls('develop-eggs')
    -  foo.egg-link
60
    -  zc.recipe.egg.egg-link
61 62 63

    """

Jim Fulton's avatar
Jim Fulton committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
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
79
    ... parts =
Jim Fulton's avatar
Jim Fulton committed
80 81
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
82
    >>> print_(system(join('bin', 'buildout')+' -vv'), end='')
83
    ... # doctest: +ELLIPSIS
84 85
    Installing...
    Develop: '/sample-buildout/foo'
Jim Fulton's avatar
Jim Fulton committed
86 87 88 89 90 91
    ...
    Installed /sample-buildout/foo
    ...

    >>> ls('develop-eggs')
    -  foo.egg-link
92
    -  zc.recipe.egg.egg-link
Jim Fulton's avatar
Jim Fulton committed
93

Jim Fulton's avatar
Jim Fulton committed
94
    >>> print_(system(join('bin', 'buildout')+' -vvv'), end='')
95
    ... # doctest: +ELLIPSIS
96 97 98 99 100 101
    Installing...
    Develop: '/sample-buildout/foo'
    in: '/sample-buildout/foo'
    ... -q develop -mxN -d /sample-buildout/develop-eggs/...


Jim Fulton's avatar
Jim Fulton committed
102 103
    """

104
def buildout_error_handling():
Jim Fulton's avatar
Jim Fulton committed
105
    r"""Buildout error handling
106

107
Asking for a section that doesn't exist, yields a missing section error:
108 109 110 111

    >>> import os
    >>> os.chdir(sample_buildout)
    >>> import zc.buildout.buildout
112
    >>> buildout = zc.buildout.buildout.Buildout('buildout.cfg', [])
113 114 115
    >>> buildout['eek']
    Traceback (most recent call last):
    ...
116
    MissingSection: The referenced section, 'eek', was not defined.
117 118 119 120 121 122

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

    >>> buildout['buildout']['eek']
    Traceback (most recent call last):
    ...
123
    MissingOption: Missing option: buildout:eek
124 125 126 127

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

    >>> write(sample_buildout, 'buildout.cfg',
Jim Fulton's avatar
Jim Fulton committed
128
    ... '''
129
    ... [buildout]
130
    ... parts =
131 132 133
    ... x = ${buildout:y}
    ... y = ${buildout:z}
    ... z = ${buildout:x}
Jim Fulton's avatar
Jim Fulton committed
134
    ... ''')
135

136
    >>> print_(system(os.path.join(sample_buildout, 'bin', 'buildout')),
Jim Fulton's avatar
Jim Fulton committed
137
    ...        end='')
138
    ... # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
139
    While:
140 141 142
      Initializing.
      Getting section buildout.
      Initializing section buildout.
143
      Getting option buildout:x.
144 145 146
      Getting option buildout:y.
      Getting option buildout:z.
      Getting option buildout:x.
147 148
    Error: Circular reference in substitutions.

pombredanne's avatar
pombredanne committed
149
It is an error to use funny characters in variable references:
150 151 152 153 154 155 156 157 158

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

159
    >>> print_(system(os.path.join(sample_buildout, 'bin', 'buildout')),
Jim Fulton's avatar
Jim Fulton committed
160
    ...        end='')
161
    While:
162 163 164 165
      Initializing.
      Getting section buildout.
      Initializing section buildout.
      Getting option buildout:x.
166 167 168 169 170 171 172 173 174 175 176
    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}
    ... ''')

177
    >>> print_(system(os.path.join(sample_buildout, 'bin', 'buildout')),
Jim Fulton's avatar
Jim Fulton committed
178
    ...        end='')
179
    While:
180 181 182 183
      Initializing.
      Getting section buildout.
      Initializing section buildout.
      Getting option buildout:x.
184 185 186 187 188 189 190 191 192 193 194 195 196
    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}
    ... ''')

197
    >>> print_(system(os.path.join(sample_buildout, 'bin', 'buildout')),
Jim Fulton's avatar
Jim Fulton committed
198
    ...        end='')
199
    While:
200 201 202 203
      Initializing.
      Getting section buildout.
      Initializing section buildout.
      Getting option buildout:x.
204 205 206 207 208 209 210 211 212 213 214
    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}
    ... ''')

215
    >>> print_(system(os.path.join(sample_buildout, 'bin', 'buildout')),
Jim Fulton's avatar
Jim Fulton committed
216
    ...        end='')
217
    While:
218 219 220 221
      Initializing.
      Getting section buildout.
      Initializing section buildout.
      Getting option buildout:x.
222 223 224
    Error: The substitution, ${buildout:y:z},
    has too many colons.

pombredanne's avatar
pombredanne committed
225
All parts have to have a section:
226 227 228 229 230 231 232

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

233
    >>> print_(system(os.path.join(sample_buildout, 'bin', 'buildout')),
Jim Fulton's avatar
Jim Fulton committed
234
    ...        end='')
235
    While:
236 237
      Installing.
      Getting section x.
238
    Error: The referenced section, 'x', was not defined.
239 240 241 242 243 244 245 246

and all parts have to have a specified recipe:


    >>> write(sample_buildout, 'buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = x
247
    ...
248 249 250 251
    ... [x]
    ... foo = 1
    ... ''')

252
    >>> print_(system(os.path.join(sample_buildout, 'bin', 'buildout')),
Jim Fulton's avatar
Jim Fulton committed
253
    ...        end='')
254
    While:
255
      Installing.
256 257
    Error: Missing option: x:recipe

Jim Fulton's avatar
Jim Fulton committed
258
"""
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

make_dist_that_requires_setup_py_template = """
from setuptools import setup
setup(name=%r, version=%r,
      install_requires=%r,
      )
"""

def make_dist_that_requires(dest, name, requires=[], version=1, egg=''):
    os.mkdir(os.path.join(dest, name))
    open(os.path.join(dest, name, 'setup.py'), 'w').write(
        make_dist_that_requires_setup_py_template
        % (name, version, requires)
        )

def show_who_requires_when_there_is_a_conflict():
    """
276 277
It's a pain when we require eggs that have requirements that are
incompatible. We want the error we get to tell us what is missing.
278

279 280
Let's make a few develop distros, some of which have incompatible
requirements.
281 282

    >>> make_dist_that_requires(sample_buildout, 'sampley',
283
    ...                         ['demoneeded ==1.0'])
284
    >>> make_dist_that_requires(sample_buildout, 'samplez',
285
    ...                         ['demoneeded ==1.1'])
286

287
Now, let's create a buildout that requires y and z:
288 289 290 291 292 293 294 295 296 297 298 299 300 301

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = eggs
    ... develop = sampley samplez
    ... find-links = %(link_server)s
    ...
    ... [eggs]
    ... recipe = zc.recipe.egg
    ... eggs = sampley
    ...        samplez
    ... ''' % globals())

Jim Fulton's avatar
Jim Fulton committed
302
    >>> print_(system(buildout), end='')
303 304 305 306 307
    Develop: '/sample-buildout/sampley'
    Develop: '/sample-buildout/samplez'
    Installing eggs.
    Getting distribution for 'demoneeded==1.1'.
    Got demoneeded 1.1.
308
    While:
309
      Installing eggs.
310 311
    Error: There is a version conflict.
    We already have: demoneeded 1.1
312
    but sampley 1 requires 'demoneeded==1.0'.
313

314 315
Here, we see that sampley required an older version of demoneeded. What
if we hadn't required sampley ourselves:
316

317
    >>> make_dist_that_requires(sample_buildout, 'samplea', ['sampleb'])
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    >>> make_dist_that_requires(sample_buildout, 'sampleb',
    ...                         ['sampley', 'samplea'])
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = eggs
    ... develop = sampley samplez samplea sampleb
    ... find-links = %(link_server)s
    ...
    ... [eggs]
    ... recipe = zc.recipe.egg
    ... eggs = samplea
    ...        samplez
    ... ''' % globals())

333
If we use the verbose switch, we can see where requirements are coming from:
334

Jim Fulton's avatar
Jim Fulton committed
335
    >>> print_(system(buildout+' -v'), end='') # doctest: +ELLIPSIS
336
    Installing 'zc.buildout', 'setuptools'.
337
    We have a develop egg: zc.buildout 1.0.0
338 339
    We have the best distribution that satisfies 'setuptools'.
    Picked: setuptools = 0.7
340 341 342 343
    Develop: '/sample-buildout/sampley'
    Develop: '/sample-buildout/samplez'
    Develop: '/sample-buildout/samplea'
    Develop: '/sample-buildout/sampleb'
Shane Hathaway's avatar
Shane Hathaway committed
344
    ...Installing eggs.
345 346 347 348 349
    Installing 'samplea', 'samplez'.
    We have a develop egg: samplea 1
    We have a develop egg: samplez 1
    Getting required 'demoneeded==1.1'
      required by samplez 1.
350
    We have the distribution that satisfies 'demoneeded==1.1'.
351 352 353 354 355 356
    Getting required 'sampleb'
      required by samplea 1.
    We have a develop egg: sampleb 1
    Getting required 'sampley'
      required by sampleb 1.
    We have a develop egg: sampley 1
357
    While:
358
      Installing eggs.
359 360
    Error: There is a version conflict.
    We already have: demoneeded 1.1
361
    but sampley 1 requires 'demoneeded==1.0'.
362 363 364 365 366
    """

def show_who_requires_missing_distributions():
    """

367 368 369 370
When working with a lot of eggs, which require eggs recursively, it
can be hard to tell why we're requiring things we can't
find. Fortunately, buildout will tell us who's asking for something
that we can't find. when run in verbose mode
371

372 373
    >>> make_dist_that_requires(sample_buildout, 'sampley', ['demoneeded'])
    >>> make_dist_that_requires(sample_buildout, 'samplea', ['sampleb'])
374 375 376 377 378 379 380 381 382 383 384 385 386
    >>> make_dist_that_requires(sample_buildout, 'sampleb',
    ...                         ['sampley', 'samplea'])
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = eggs
    ... develop = sampley samplea sampleb
    ...
    ... [eggs]
    ... recipe = zc.recipe.egg
    ... eggs = samplea
    ... ''')

387 388 389 390 391 392 393 394 395 396 397 398 399 400
    >>> print_(system(buildout+' -v'), end='') # doctest: +ELLIPSIS
    Installing ...
    Installing 'samplea'.
    We have a develop egg: samplea 1
    Getting required 'sampleb'
      required by samplea 1.
    We have a develop egg: sampleb 1
    Getting required 'sampley'
      required by sampleb 1.
    We have a develop egg: sampley 1
    Getting required 'demoneeded'
      required by sampley 1.
    We have no distributions for demoneeded that satisfies 'demoneeded'.
    ...
401
    While:
402 403 404
      Installing eggs.
      Getting distribution for 'demoneeded'.
    Error: Couldn't find a distribution for 'demoneeded'.
405
    """
406

407 408 409 410 411 412 413
def show_who_requires_picked_versions():
    """

The show-picked-versions prints the versions, but it also prints who
required the picked distributions.
We do not need to run in verbose mode for that to work:

414
    >>> make_dist_that_requires(sample_buildout, 'sampley', ['setuptools'])
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
    >>> make_dist_that_requires(sample_buildout, 'samplea', ['sampleb'])
    >>> make_dist_that_requires(sample_buildout, 'sampleb',
    ...                         ['sampley', 'samplea'])
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = eggs
    ... show-picked-versions = true
    ... develop = sampley samplea sampleb
    ...
    ... [eggs]
    ... recipe = zc.recipe.egg
    ... eggs = samplea
    ... ''')

    >>> print_(system(buildout), end='') # doctest: +ELLIPSIS
    Develop: ...
    Installing eggs.
    Versions had to be automatically picked.
    The following part definition lists the versions picked:
    [versions]
    <BLANKLINE>
    # Required by:
    # sampley==1
439
    setuptools = 0.7
440 441
    """

442 443
def test_comparing_saved_options_with_funny_characters():
    """
444 445
If an option has newlines, extra/odd spaces or a %, we need to make sure
the comparison with the saved value works correctly.
446 447

    >>> mkdir(sample_buildout, 'recipes')
448
    >>> write(sample_buildout, 'recipes', 'debug.py',
449 450 451 452 453 454 455 456 457
    ... '''
    ... class Debug:
    ...     def __init__(self, buildout, name, options):
    ...         options['debug'] = \"\"\"  <zodb>
    ...
    ...   <filestorage>
    ...     path foo
    ...   </filestorage>
    ...
458
    ... </zodb>
459
    ...      \"\"\"
460 461 462 463 464 465 466
    ...         options['debug1'] = \"\"\"
    ... <zodb>
    ...
    ...   <filestorage>
    ...     path foo
    ...   </filestorage>
    ...
467
    ... </zodb>
468
    ... \"\"\"
469 470 471 472 473 474 475
    ...         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
476 477
    ...
    ...     update = install
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
    ... ''')


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

Jim Fulton's avatar
Jim Fulton committed
505
    >>> print_(system(buildout), end='')
506 507
    Develop: '/sample-buildout/recipes'
    Installing debug.
508 509 510 511

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

Jim Fulton's avatar
Jim Fulton committed
512
    >>> print_(system(buildout), end='')
513 514
    Develop: '/sample-buildout/recipes'
    Updating debug.
515 516
"""

Jim Fulton's avatar
Jim Fulton committed
517 518 519 520 521 522 523 524 525 526
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=[''],
527
    ...    zip_safe=False, version='1.0', author='bob', url='bob',
Jim Fulton's avatar
Jim Fulton committed
528 529 530 531 532 533 534 535 536 537 538
    ...    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(
539
    ...     ['demo'], d1, links=[join(src, 'dist')],
Jim Fulton's avatar
Jim Fulton committed
540 541 542 543 544 545 546 547 548
    ...     )

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

Then try to install it again:

    >>> d2 = tmpdir('d2')
    >>> ws = zc.buildout.easy_install.install(
549
    ...     ['demo'], d2, links=[d1],
Jim Fulton's avatar
Jim Fulton committed
550 551 552 553 554 555 556
    ...     )

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

    """

Jim Fulton's avatar
Jim Fulton committed
557 558 559 560 561 562 563 564 565
def create_sections_on_command_line():
    """
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts =
    ... x = ${foo:bar}
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
566
    >>> print_(system(buildout + ' foo:bar=1 -vv'), end='')
567
    ...        # doctest: +ELLIPSIS
568
    Installing 'zc.buildout', 'setuptools'.
Jim Fulton's avatar
Jim Fulton committed
569 570 571 572
    ...
    [foo]
    bar = 1
    ...
573

Jim Fulton's avatar
Jim Fulton committed
574 575
    """

576 577
def test_help():
    """
578
    >>> print_(system(os.path.join(sample_buildout, 'bin', 'buildout')+' -h'))
579 580 581 582 583 584 585 586
    ... # doctest: +ELLIPSIS
    Usage: buildout [options] [assignments] [command [command arguments]]
    <BLANKLINE>
    Options:
    <BLANKLINE>
      -h, --help
    ...

587 588
    >>> print_(system(os.path.join(sample_buildout, 'bin', 'buildout')
    ...              +' --help'))
589 590 591 592 593 594 595
    ... # doctest: +ELLIPSIS
    Usage: buildout [options] [assignments] [command [command arguments]]
    <BLANKLINE>
    Options:
    <BLANKLINE>
      -h, --help
    ...
596
    """
597

Marius Gedminas's avatar
Marius Gedminas committed
598 599 600 601 602 603 604 605 606
def test_version():
    """
    >>> buildout = os.path.join(sample_buildout, 'bin', 'buildout')
    >>> print_(system(buildout+' --version'))
    ... # doctest: +ELLIPSIS
    buildout version ...

    """

Jim Fulton's avatar
Jim Fulton committed
607 608 609 610 611 612
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.

613
    >>> d = tmpdir('sample-bootstrap')
614

Jim Fulton's avatar
Jim Fulton committed
615 616 617 618
    >>> write(d, 'buildout.cfg',
    ... '''
    ... [buildout]
    ... extensions = some_awsome_extension
619
    ... parts =
Jim Fulton's avatar
Jim Fulton committed
620 621 622
    ... ''')

    >>> os.chdir(d)
623
    >>> print_(system(os.path.join(sample_buildout, 'bin', 'buildout')
Jim Fulton's avatar
Jim Fulton committed
624
    ...              + ' bootstrap'), end='')
625 626 627 628 629
    Creating directory '/sample-bootstrap/bin'.
    Creating directory '/sample-bootstrap/parts'.
    Creating directory '/sample-bootstrap/eggs'.
    Creating directory '/sample-bootstrap/develop-eggs'.
    Generated script '/sample-bootstrap/bin/buildout'.
Jim Fulton's avatar
Jim Fulton committed
630
    """
631

632 633 634 635

def bug_92891_bootstrap_crashes_with_egg_recipe_in_buildout_section():
    """
    >>> d = tmpdir('sample-bootstrap')
636

637 638 639 640 641 642 643 644 645 646 647 648 649
    >>> write(d, 'buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = buildout
    ... eggs-directory = eggs
    ...
    ... [buildout]
    ... recipe = zc.recipe.egg
    ... eggs = zc.buildout
    ... scripts = buildout=buildout
    ... ''')

    >>> os.chdir(d)
650
    >>> print_(system(os.path.join(sample_buildout, 'bin', 'buildout')
Jim Fulton's avatar
Jim Fulton committed
651
    ...              + ' bootstrap'), end='')
652 653 654 655 656
    Creating directory '/sample-bootstrap/bin'.
    Creating directory '/sample-bootstrap/parts'.
    Creating directory '/sample-bootstrap/eggs'.
    Creating directory '/sample-bootstrap/develop-eggs'.
    Generated script '/sample-bootstrap/bin/buildout'.
657

Jim Fulton's avatar
Jim Fulton committed
658
    >>> print_(system(os.path.join('bin', 'buildout')), end='')
659
    Unused options for buildout: 'scripts' 'eggs'.
660 661 662

    """

663 664 665
def removing_eggs_from_develop_section_causes_egg_link_to_be_removed():
    '''
    >>> cd(sample_buildout)
666

667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
Create a develop egg:

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

Jim Fulton's avatar
Jim Fulton committed
682
    >>> print_(system(join('bin', 'buildout')), end='')
683
    Develop: '/sample-buildout/foo'
684 685 686

    >>> ls('develop-eggs')
    -  foox.egg-link
687
    -  zc.recipe.egg.egg-link
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703

Create another:

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

Jim Fulton's avatar
Jim Fulton committed
704
    >>> print_(system(join('bin', 'buildout')), end='')
705 706
    Develop: '/sample-buildout/foo'
    Develop: '/sample-buildout/bar'
707 708 709 710

    >>> ls('develop-eggs')
    -  foox.egg-link
    -  fooy.egg-link
711
    -  zc.recipe.egg.egg-link
712 713 714 715 716 717 718 719 720

Remove one:

    >>> write('buildout.cfg',
    ... """
    ... [buildout]
    ... develop = bar
    ... parts =
    ... """)
Jim Fulton's avatar
Jim Fulton committed
721
    >>> print_(system(join('bin', 'buildout')), end='')
722
    Develop: '/sample-buildout/bar'
723 724 725 726 727

It is gone

    >>> ls('develop-eggs')
    -  fooy.egg-link
728
    -  zc.recipe.egg.egg-link
729 730 731 732 733 734 735 736

Remove the other:

    >>> write('buildout.cfg',
    ... """
    ... [buildout]
    ... parts =
    ... """)
Jim Fulton's avatar
Jim Fulton committed
737
    >>> print_(system(join('bin', 'buildout')), end='')
738 739 740 741

All gone

    >>> ls('develop-eggs')
742
    -  zc.recipe.egg.egg-link
743 744 745
    '''


746
def add_setuptools_to_dependencies_when_namespace_packages():
747
    '''
748
Often, a package depends on setuptools solely by virtue of using
749
namespace packages. In this situation, package authors often forget to
750
declare setuptools as a dependency. This is a mistake, but,
751
unfortunately, a common one that we need to work around.  If an egg
752 753
uses namespace packages and does not include setuptools as a dependency,
we will still include setuptools in the working set.  If we see this for
pombredanne's avatar
pombredanne committed
754
a develop egg, we will also generate a warning.
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773

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

775 776 777 778
    >>> write('buildout.cfg',
    ... """
    ... [buildout]
    ... develop = foo
779
    ... parts =
780 781
    ... """)

Jim Fulton's avatar
Jim Fulton committed
782
    >>> print_(system(join('bin', 'buildout')), end='')
783
    Develop: '/sample-buildout/foo'
784 785

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

    >>> import logging, zope.testing.loggingsupport
    >>> handler = zope.testing.loggingsupport.InstalledHandler(
790 791
    ...        'zc.buildout.easy_install', level=logging.WARNING)
    >>> logging.getLogger('zc.buildout.easy_install').propagate = False
792

793 794 795 796 797 798
    >>> [dist.project_name
    ...  for dist in zc.buildout.easy_install.working_set(
    ...    ['foox'], sys.executable,
    ...    [join(sample_buildout, 'eggs'),
    ...     join(sample_buildout, 'develop-eggs'),
    ...     ])]
799
    ['foox', 'setuptools']
800

801
    >>> print_(handler)
802
    zc.buildout.easy_install WARNING
803
      Develop distribution: foox 0.0.0
804
    uses namespace packages but the distribution does not require setuptools.
805 806 807 808 809 810 811 812 813 814 815

    >>> 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')
816
    -  zc.recipe.egg.egg-link
817

818 819
    >>> ls('eggs') # doctest: +ELLIPSIS
    -  foox-0.0.0-py2.4.egg
820
    d  setuptools.eggpyN.N.egg
821
    ...
822

823
We do not get a warning, but we do get setuptools included in the working set:
824 825 826 827 828 829 830

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

Jim Fulton's avatar
Jim Fulton committed
833
    >>> print_(handler, end='')
834

pombredanne's avatar
pombredanne committed
835
We get the same behavior if the it is a dependency that uses a
Jim Fulton's avatar
Jim Fulton committed
836 837 838 839 840 841 842 843 844 845
namespace package.


    >>> mkdir('bar')
    >>> write('bar', 'setup.py',
    ... """
    ... from setuptools import setup
    ... setup(name='bar', install_requires = ['foox'])
    ... """)
    >>> write('bar', 'README.txt', '')
846

Jim Fulton's avatar
Jim Fulton committed
847 848 849 850
    >>> write('buildout.cfg',
    ... """
    ... [buildout]
    ... develop = foo bar
851
    ... parts =
Jim Fulton's avatar
Jim Fulton committed
852 853
    ... """)

Jim Fulton's avatar
Jim Fulton committed
854
    >>> print_(system(join('bin', 'buildout')), end='')
855 856
    Develop: '/sample-buildout/foo'
    Develop: '/sample-buildout/bar'
Jim Fulton's avatar
Jim Fulton committed
857 858 859 860 861 862 863

    >>> [dist.project_name
    ...  for dist in zc.buildout.easy_install.working_set(
    ...    ['bar'], sys.executable,
    ...    [join(sample_buildout, 'eggs'),
    ...     join(sample_buildout, 'develop-eggs'),
    ...     ])]
864
    ['bar', 'foox', 'setuptools']
Jim Fulton's avatar
Jim Fulton committed
865

Jim Fulton's avatar
Jim Fulton committed
866
    >>> print_(handler, end='')
Jim Fulton's avatar
Jim Fulton committed
867
    zc.buildout.easy_install WARNING
868
      Develop distribution: foox 0.0.0
869
    uses namespace packages but the distribution does not require setuptools.
Jim Fulton's avatar
Jim Fulton committed
870 871


872
    >>> logging.getLogger('zc.buildout.easy_install').propagate = True
873
    >>> handler.uninstall()
Jim Fulton's avatar
Jim Fulton committed
874

875
    '''
876 877 878

def develop_preserves_existing_setup_cfg():
    """
879

880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
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(
903
    ...   extdemo, dest,
904
    ...   {'include-dirs': os.path.join(sample_buildout, 'include')})
Jim Fulton's avatar
Jim Fulton committed
905
    '/dest/extdemo.egg-link'
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920

    >>> 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
921 922

def uninstall_recipes_used_for_removal():
923
    r"""
Jim Fulton's avatar
Jim Fulton committed
924 925 926 927 928 929 930 931 932 933 934 935 936 937
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",
938 939
    ... r'''
    ... import sys
Jim Fulton's avatar
Jim Fulton committed
940 941 942
    ... class Install:
    ...     def __init__(*args): pass
    ...     def install(self):
943
    ...         sys.stdout.write('installing\n')
Jim Fulton's avatar
Jim Fulton committed
944
    ...         return ()
945 946
    ... def uninstall(name, options):
    ...     sys.stdout.write('uninstalling\n')
Jim Fulton's avatar
Jim Fulton committed
947 948 949 950 951 952 953 954 955 956
    ... ''')

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

Jim Fulton's avatar
Jim Fulton committed
957
    >>> print_(system(join('bin', 'buildout')), end='')
958 959
    Develop: '/sample-buildout/recipes'
    Installing demo.
Jim Fulton's avatar
Jim Fulton committed
960 961 962 963 964 965 966 967 968 969 970 971
    installing


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

Jim Fulton's avatar
Jim Fulton committed
972
    >>> print_(system(join('bin', 'buildout')), end='')
973 974 975
    Develop: '/sample-buildout/recipes'
    Uninstalling demo.
    Running uninstall recipe.
Jim Fulton's avatar
Jim Fulton committed
976
    uninstalling
977
    Installing demo.
Jim Fulton's avatar
Jim Fulton committed
978 979 980 981 982 983
    installing


    >>> write('buildout.cfg', '''
    ... [buildout]
    ... develop = recipes
984
    ... parts =
Jim Fulton's avatar
Jim Fulton committed
985 986
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
987
    >>> print_(system(join('bin', 'buildout')), end='')
988 989 990
    Develop: '/sample-buildout/recipes'
    Uninstalling demo.
    Running uninstall recipe.
Jim Fulton's avatar
Jim Fulton committed
991 992 993 994
    uninstalling

"""

Jim Fulton's avatar
Jim Fulton committed
995 996 997 998
def extensions_installed_as_eggs_work_in_offline_mode():
    '''
    >>> mkdir('demo')

999
    >>> write('demo', 'demo.py',
Jim Fulton's avatar
Jim Fulton committed
1000 1001 1002 1003
    ... r"""
    ... import sys
    ... def print_(*args):
    ...     sys.stdout.write(' '.join(map(str, args)) + '\\n')
Jim Fulton's avatar
Jim Fulton committed
1004
    ... def ext(buildout):
1005
    ...     print_('ext', sorted(buildout))
Jim Fulton's avatar
Jim Fulton committed
1006 1007 1008 1009 1010
    ... """)

    >>> write('demo', 'setup.py',
    ... """
    ... from setuptools import setup
1011
    ...
Jim Fulton's avatar
Jim Fulton committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
    ... 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
    ... """)

Jim Fulton's avatar
Jim Fulton committed
1030
    >>> print_(system(join(sample_buildout, 'bin', 'buildout')), end='')
1031
    ext ['buildout', 'versions']
1032

Jim Fulton's avatar
Jim Fulton committed
1033 1034 1035

    '''

Jim Fulton's avatar
Jim Fulton committed
1036 1037
def changes_in_svn_or_CVS_dont_affect_sig():
    """
1038

Jim Fulton's avatar
Jim Fulton committed
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
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
    ... ''')
1056

Jim Fulton's avatar
Jim Fulton committed
1057 1058 1059 1060 1061
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = recipe
    ... parts = foo
1062
    ...
Jim Fulton's avatar
Jim Fulton committed
1063 1064 1065 1066 1067
    ... [foo]
    ... recipe = recipe
    ... ''')


Jim Fulton's avatar
Jim Fulton committed
1068
    >>> print_(system(join(sample_buildout, 'bin', 'buildout')), end='')
1069 1070
    Develop: '/sample-buildout/recipe'
    Installing foo.
Jim Fulton's avatar
Jim Fulton committed
1071 1072 1073

    >>> mkdir('recipe', '.svn')
    >>> mkdir('recipe', 'CVS')
Jim Fulton's avatar
Jim Fulton committed
1074
    >>> print_(system(join(sample_buildout, 'bin', 'buildout')), end='')
1075 1076
    Develop: '/sample-buildout/recipe'
    Updating foo.
Jim Fulton's avatar
Jim Fulton committed
1077 1078 1079 1080

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

Jim Fulton's avatar
Jim Fulton committed
1081
    >>> print_(system(join(sample_buildout, 'bin', 'buildout')), end='')
1082 1083
    Develop: '/sample-buildout/recipe'
    Updating foo.
Jim Fulton's avatar
Jim Fulton committed
1084 1085 1086

    """

1087 1088 1089
if hasattr(os, 'symlink'):
    def bug_250537_broken_symlink_doesnt_affect_sig():
        """
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
If we have a develop recipe, it's signature shouldn't be affected by
broken symlinks, and better yet, computing the hash should not break
because of the missing target file.

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

1109 1110 1111 1112 1113
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = recipe
    ... parts = foo
1114
    ...
1115 1116 1117 1118 1119
    ... [foo]
    ... recipe = recipe
    ... ''')


Jim Fulton's avatar
Jim Fulton committed
1120
    >>> print_(system(join(sample_buildout, 'bin', 'buildout')), end='')
1121 1122 1123 1124
    Develop: '/sample-buildout/recipe'
    Installing foo.

    >>> write('recipe', 'some-file', '1')
1125
    >>> os.symlink(join('recipe', 'some-file'),
1126 1127 1128
    ...            join('recipe', 'another-file'))
    >>> remove('recipe', 'some-file')

Jim Fulton's avatar
Jim Fulton committed
1129
    >>> print_(system(join(sample_buildout, 'bin', 'buildout')), end='')
1130 1131 1132 1133 1134
    Develop: '/sample-buildout/recipe'
    Updating foo.

    """

Jim Fulton's avatar
Jim Fulton committed
1135 1136
def o_option_sets_offline():
    """
Jim Fulton's avatar
Jim Fulton committed
1137
    >>> print_(system(join(sample_buildout, 'bin', 'buildout')+' -vvo'), end='')
Jim Fulton's avatar
Jim Fulton committed
1138 1139 1140 1141 1142 1143 1144
    ... # doctest: +ELLIPSIS
    <BLANKLINE>
    ...
    offline = true
    ...
    """

Jim Fulton's avatar
Jim Fulton committed
1145
def recipe_upgrade():
1146
    r"""
Jim Fulton's avatar
Jim Fulton committed
1147 1148 1149 1150 1151 1152 1153

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

Let's create a recipe egg

    >>> mkdir('recipe')
    >>> write('recipe', 'recipe.py',
1154 1155
    ... r'''
    ... import sys
Jim Fulton's avatar
Jim Fulton committed
1156 1157 1158
    ... class Recipe:
    ...     def __init__(*a): pass
    ...     def install(self):
1159
    ...         sys.stdout.write('recipe v1\n')
Jim Fulton's avatar
Jim Fulton committed
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
    ...         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', '')

1174
    >>> print_(system(buildout+' setup recipe bdist_egg')) # doctest: +ELLIPSIS
1175
    Running setup script 'recipe/setup.py'.
Jim Fulton's avatar
Jim Fulton committed
1176 1177
    ...

Jim Fulton's avatar
Jim Fulton committed
1178 1179
    >>> rmdir('recipe', 'build')

Jim Fulton's avatar
Jim Fulton committed
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
And update our buildout to use it.

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

Jim Fulton's avatar
Jim Fulton committed
1192
    >>> print_(system(buildout), end='')
1193 1194 1195
    Getting distribution for 'recipe'.
    Got recipe 1.
    Installing foo.
Jim Fulton's avatar
Jim Fulton committed
1196 1197 1198 1199 1200
    recipe v1

Now, if we update the recipe egg:

    >>> write('recipe', 'recipe.py',
1201 1202
    ... r'''
    ... import sys
Jim Fulton's avatar
Jim Fulton committed
1203 1204 1205
    ... class Recipe:
    ...     def __init__(*a): pass
    ...     def install(self):
1206
    ...         sys.stdout.write('recipe v2\n')
Jim Fulton's avatar
Jim Fulton committed
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
    ...         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']},
    ...       )
    ... ''')


1220
    >>> print_(system(buildout+' setup recipe bdist_egg')) # doctest: +ELLIPSIS
1221
    Running setup script 'recipe/setup.py'.
Jim Fulton's avatar
Jim Fulton committed
1222 1223 1224 1225
    ...

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

Jim Fulton's avatar
Jim Fulton committed
1226
    >>> print_(system(buildout+' -N'), end='')
1227
    Updating foo.
Jim Fulton's avatar
Jim Fulton committed
1228 1229 1230 1231
    recipe v1

or if we use -o:

Jim Fulton's avatar
Jim Fulton committed
1232
    >>> print_(system(buildout+' -o'), end='')
1233
    Updating foo.
Jim Fulton's avatar
Jim Fulton committed
1234 1235 1236 1237
    recipe v1

But we will if we use neither of these:

Jim Fulton's avatar
Jim Fulton committed
1238
    >>> print_(system(buildout), end='')
1239 1240 1241 1242
    Getting distribution for 'recipe'.
    Got recipe 2.
    Uninstalling foo.
    Installing foo.
Jim Fulton's avatar
Jim Fulton committed
1243
    recipe v2
Jim Fulton's avatar
Jim Fulton committed
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256

We can also select a particular recipe version:

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

Jim Fulton's avatar
Jim Fulton committed
1257
    >>> print_(system(buildout), end='')
1258 1259
    Uninstalling foo.
    Installing foo.
Jim Fulton's avatar
Jim Fulton committed
1260
    recipe v1
1261

Jim Fulton's avatar
Jim Fulton committed
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 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
    """

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

Jim Fulton's avatar
Jim Fulton committed
1306
    >>> print_(system(buildout), end='')
Jim Fulton's avatar
Jim Fulton committed
1307
    Develop: '/sample-buildout/recipe'
1308
    Installing foo.
Jim Fulton's avatar
Jim Fulton committed
1309

Jim Fulton's avatar
Jim Fulton committed
1310
    >>> print_(system(buildout), end='')
Jim Fulton's avatar
Jim Fulton committed
1311
    Develop: '/sample-buildout/recipe'
1312
    Updating foo.
Jim Fulton's avatar
Jim Fulton committed
1313 1314 1315 1316 1317

    >>> cat('.installed.cfg') # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
    [buildout]
    ...
    [foo]
1318 1319 1320
    __buildout_installed__ = a
    	b
    	c
Jim Fulton's avatar
Jim Fulton committed
1321 1322 1323 1324 1325 1326
    	d
    	e
    __buildout_signature__ = ...

"""

1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
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/')

1342
    >>> print_(handler) # doctest: +ELLIPSIS
1343
    zc.buildout.easy_install DEBUG
1344
      Installing 'demo==0.2'.
1345
    zc.buildout.easy_install DEBUG
1346
      We have no distributions for demo that satisfies 'demo==0.2'.
1347 1348 1349 1350
    ...

    >>> handler.uninstall()
    >>> logger.propagate = old_propogate
1351

1352
    """
Jim Fulton's avatar
Jim Fulton committed
1353

1354 1355 1356 1357 1358
def internal_errors():
    """Internal errors are clearly marked and don't generate tracebacks:

    >>> mkdir(sample_buildout, 'recipes')

1359
    >>> write(sample_buildout, 'recipes', 'mkdir.py',
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
    ... '''
    ... 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
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
1388
    >>> print_(system(buildout), end='') # doctest: +ELLIPSIS
1389
    Develop: '/sample-buildout/recipes'
1390
    While:
1391 1392
      Installing.
      Getting section data-dir.
Jim Fulton's avatar
Jim Fulton committed
1393
      Initializing section data-dir.
1394
    <BLANKLINE>
pombredanne's avatar
pombredanne committed
1395
    An internal error occurred due to a bug in either zc.buildout or in a
1396
    recipe being used:
1397 1398 1399
    Traceback (most recent call last):
    ...
    NameError: global name 'os' is not defined
1400
    """
1401

1402 1403 1404
def whine_about_unused_options():
    '''

1405
    >>> write('foo.py',
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
    ... """
    ... class Foo:
    ...
    ...     def __init__(self, buildout, name, options):
    ...         self.name, self.options = name, options
    ...         options['x']
    ...
    ...     def install(self):
    ...         self.options['y']
    ...         return ()
    ... """)

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

    >>> write('buildout.cfg',
    ... """
    ... [buildout]
    ... develop = .
    ... parts = foo
    ... a = 1
    ...
    ... [foo]
    ... recipe = foo
    ... x = 1
    ... y = 1
    ... z = 1
    ... """)

Jim Fulton's avatar
Jim Fulton committed
1440
    >>> print_(system(buildout), end='')
Jim Fulton's avatar
Jim Fulton committed
1441
    Develop: '/sample-buildout/.'
1442 1443 1444
    Unused options for buildout: 'a'.
    Installing foo.
    Unused options for foo: 'z'.
1445
    '''
1446

1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 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 1503 1504 1505 1506 1507
def abnormal_exit():
    """
People sometimes hit control-c while running a builout. We need to make
sure that the installed database Isn't corrupted.  To test this, we'll create
some evil recipes that exit uncleanly:

    >>> mkdir('recipes')
    >>> write('recipes', 'recipes.py',
    ... '''
    ... import os
    ...
    ... class Clean:
    ...     def __init__(*_): pass
    ...     def install(_): return ()
    ...     def update(_): pass
    ...
    ... class EvilInstall(Clean):
    ...     def install(_): os._exit(1)
    ...
    ... class EvilUpdate(Clean):
    ...     def update(_): os._exit(1)
    ... ''')

    >>> write('recipes', 'setup.py',
    ... '''
    ... import setuptools
    ... setuptools.setup(name='recipes',
    ...    entry_points = {
    ...      'zc.buildout': [
    ...          'clean = recipes:Clean',
    ...          'evil_install = recipes:EvilInstall',
    ...          'evil_update = recipes:EvilUpdate',
    ...          'evil_uninstall = recipes:Clean',
    ...          ],
    ...       },
    ...     )
    ... ''')

Now let's look at 3 cases:

1. We exit during installation after installing some other parts:

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = recipes
    ... parts = p1 p2 p3 p4
    ...
    ... [p1]
    ... recipe = recipes:clean
    ...
    ... [p2]
    ... recipe = recipes:clean
    ...
    ... [p3]
    ... recipe = recipes:evil_install
    ...
    ... [p4]
    ... recipe = recipes:clean
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
1508
    >>> print_(system(buildout), end='')
1509 1510 1511 1512
    Develop: '/sample-buildout/recipes'
    Installing p1.
    Installing p2.
    Installing p3.
1513

Jim Fulton's avatar
Jim Fulton committed
1514
    >>> print_(system(buildout), end='')
1515 1516 1517 1518
    Develop: '/sample-buildout/recipes'
    Updating p1.
    Updating p2.
    Installing p3.
1519

Jim Fulton's avatar
Jim Fulton committed
1520
    >>> print_(system(buildout+' buildout:parts='), end='')
1521 1522 1523
    Develop: '/sample-buildout/recipes'
    Uninstalling p2.
    Uninstalling p1.
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545

2. We exit while updating:

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = recipes
    ... parts = p1 p2 p3 p4
    ...
    ... [p1]
    ... recipe = recipes:clean
    ...
    ... [p2]
    ... recipe = recipes:clean
    ...
    ... [p3]
    ... recipe = recipes:evil_update
    ...
    ... [p4]
    ... recipe = recipes:clean
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
1546
    >>> print_(system(buildout), end='')
1547 1548 1549 1550 1551
    Develop: '/sample-buildout/recipes'
    Installing p1.
    Installing p2.
    Installing p3.
    Installing p4.
1552

Jim Fulton's avatar
Jim Fulton committed
1553
    >>> print_(system(buildout), end='')
1554 1555 1556 1557
    Develop: '/sample-buildout/recipes'
    Updating p1.
    Updating p2.
    Updating p3.
1558

Jim Fulton's avatar
Jim Fulton committed
1559
    >>> print_(system(buildout+' buildout:parts='), end='')
1560 1561 1562 1563 1564
    Develop: '/sample-buildout/recipes'
    Uninstalling p2.
    Uninstalling p1.
    Uninstalling p4.
    Uninstalling p3.
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586

3. We exit while installing or updating after uninstalling:

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = recipes
    ... parts = p1 p2 p3 p4
    ...
    ... [p1]
    ... recipe = recipes:evil_update
    ...
    ... [p2]
    ... recipe = recipes:clean
    ...
    ... [p3]
    ... recipe = recipes:clean
    ...
    ... [p4]
    ... recipe = recipes:clean
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
1587
    >>> print_(system(buildout), end='')
1588 1589 1590 1591 1592
    Develop: '/sample-buildout/recipes'
    Installing p1.
    Installing p2.
    Installing p3.
    Installing p4.
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = recipes
    ... parts = p1 p2 p3 p4
    ...
    ... [p1]
    ... recipe = recipes:evil_update
    ...
    ... [p2]
    ... recipe = recipes:clean
    ...
    ... [p3]
    ... recipe = recipes:clean
    ...
    ... [p4]
    ... recipe = recipes:clean
    ... x = 1
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
1614
    >>> print_(system(buildout), end='')
1615 1616 1617
    Develop: '/sample-buildout/recipes'
    Uninstalling p4.
    Updating p1.
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = recipes
    ... parts = p1 p2 p3 p4
    ...
    ... [p1]
    ... recipe = recipes:clean
    ...
    ... [p2]
    ... recipe = recipes:clean
    ...
    ... [p3]
    ... recipe = recipes:clean
    ...
    ... [p4]
    ... recipe = recipes:clean
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
1638
    >>> print_(system(buildout), end='')
1639 1640 1641 1642 1643 1644
    Develop: '/sample-buildout/recipes'
    Uninstalling p1.
    Installing p1.
    Updating p2.
    Updating p3.
    Installing p4.
1645 1646

    """
1647 1648

def install_source_dist_with_bad_py():
1649
    r"""
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670

    >>> mkdir('badegg')
    >>> mkdir('badegg', 'badegg')
    >>> write('badegg', 'badegg', '__init__.py', '#\\n')
    >>> mkdir('badegg', 'badegg', 'scripts')
    >>> write('badegg', 'badegg', 'scripts', '__init__.py', '#\\n')
    >>> write('badegg', 'badegg', 'scripts', 'one.py',
    ... '''
    ... return 1
    ... ''')

    >>> write('badegg', 'setup.py',
    ... '''
    ... from setuptools import setup, find_packages
    ... setup(
    ...     name='badegg',
    ...     version='1',
    ...     packages = find_packages('.'),
    ...     zip_safe=False)
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
1671
    >>> print_(system(buildout+' setup badegg sdist'), end='') # doctest: +ELLIPSIS
1672
    Running setup script 'badegg/setup.py'.
1673
    ...
1674

1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
    >>> dist = join('badegg', 'dist')

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = eggs bo
    ... find-links = %(dist)s
    ...
    ... [eggs]
    ... recipe = zc.recipe.egg
    ... eggs = badegg
    ...
    ... [bo]
    ... recipe = zc.recipe.egg
    ... eggs = zc.buildout
    ... scripts = buildout=bo
    ... ''' % globals())

1693
    >>> print_(system(buildout));print_('X') # doctest: +ELLIPSIS
1694 1695
    Installing eggs.
    Getting distribution for 'badegg'.
1696 1697
    Got badegg 1.
    Installing bo.
1698 1699 1700 1701 1702
    ...
    SyntaxError: ...'return' outside function...
    ...
    SyntaxError: ...'return' outside function...
    ...
1703

1704
    >>> ls('eggs') # doctest: +ELLIPSIS
1705
    d  badegg-1-py2.4.egg
1706
    ...
1707

1708 1709 1710 1711
    >>> ls('bin')
    -  bo
    -  buildout
    """
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724

def version_requirements_in_build_honored():
    '''

    >>> update_extdemo()
    >>> dest = tmpdir('sample-install')
    >>> mkdir('include')
    >>> write('include', 'extdemo.h',
    ... """
    ... #define EXTDEMO 42
    ... """)

    >>> zc.buildout.easy_install.build(
1725
    ...   'extdemo ==1.4', dest,
1726 1727 1728 1729 1730 1731
    ...   {'include-dirs': os.path.join(sample_buildout, 'include')},
    ...   links=[link_server], index=link_server+'index/',
    ...   newest=False)
    ['/sample-install/extdemo-1.4-py2.4-linux-i686.egg']

    '''
1732

1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
def bug_105081_Specific_egg_versions_are_ignored_when_newer_eggs_are_around():
    """
    Buildout might ignore a specific egg requirement for a recipe:

    - Have a newer version of an egg in your eggs directory
    - Use 'recipe==olderversion' in your buildout.cfg to request an
      older version

    Buildout will go and fetch the older version, but it will *use*
    the newer version when installing a part with this recipe.

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = x
    ... find-links = %(sample_eggs)s
    ...
    ... [x]
    ... recipe = zc.recipe.egg
    ... eggs = demo
    ... ''' % globals())

Jim Fulton's avatar
Jim Fulton committed
1755
    >>> print_(system(buildout), end='')
1756 1757
    Installing x.
    Getting distribution for 'demo'.
Jim Fulton's avatar
Jim Fulton committed
1758
    Got demo 0.3.
1759
    Getting distribution for 'demoneeded'.
Jim Fulton's avatar
Jim Fulton committed
1760
    Got demoneeded 1.1.
1761
    Generated script '/sample-buildout/bin/demo'.
1762

Jim Fulton's avatar
Jim Fulton committed
1763
    >>> print_(system(join('bin', 'demo')), end='')
Jim Fulton's avatar
Jim Fulton committed
1764
    3 1
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = x
    ... find-links = %(sample_eggs)s
    ...
    ... [x]
    ... recipe = zc.recipe.egg
    ... eggs = demo ==0.1
    ... ''' % globals())
1776

Jim Fulton's avatar
Jim Fulton committed
1777
    >>> print_(system(buildout), end='')
1778 1779 1780 1781 1782
    Uninstalling x.
    Installing x.
    Getting distribution for 'demo==0.1'.
    Got demo 0.1.
    Generated script '/sample-buildout/bin/demo'.
1783

Jim Fulton's avatar
Jim Fulton committed
1784
    >>> print_(system(join('bin', 'demo')), end='')
Jim Fulton's avatar
Jim Fulton committed
1785
    1 1
1786 1787
    """

1788 1789 1790 1791 1792 1793 1794 1795
if sys.version_info > (2, 4):
    def test_exit_codes():
        """
        >>> import subprocess
        >>> def call(s):
        ...     p = subprocess.Popen(s, stdin=subprocess.PIPE,
        ...                 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        ...     p.stdin.close()
1796 1797
        ...     print_(p.stdout.read().decode())
        ...     print_('Exit:', bool(p.wait()))
1798

1799 1800 1801 1802 1803 1804 1805 1806 1807 1808
        >>> call(buildout)
        <BLANKLINE>
        Exit: False

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

1809
        >>> call(buildout) # doctest: +NORMALIZE_WHITESPACE
1810
        While:
1811 1812
          Installing.
          Getting section x.
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
        Error: The referenced section, 'x', was not defined.
        <BLANKLINE>
        Exit: True

        >>> write('setup.py',
        ... '''
        ... from setuptools import setup
        ... setup(name='zc.buildout.testexit', entry_points={
        ...    'zc.buildout': ['default = testexitrecipe:x']})
        ... ''')

        >>> write('testexitrecipe.py',
        ... '''
        ... x y
        ... ''')

        >>> write('buildout.cfg',
        ... '''
        ... [buildout]
        ... parts = x
        ... develop = .
        ...
        ... [x]
        ... recipe = zc.buildout.testexit
        ... ''')

1839
        >>> call(buildout) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
1840
        Develop: '/sample-buildout/.'
1841
        While:
1842 1843 1844 1845
          Installing.
          Getting section x.
          Initializing section x.
          Loading zc.buildout recipe entry zc.buildout.testexit:default.
1846
        <BLANKLINE>
pombredanne's avatar
pombredanne committed
1847
        An internal error occurred due to a bug in either zc.buildout or in a
1848
        recipe being used:
1849 1850 1851 1852 1853
        Traceback (most recent call last):
        ...
             x y
               ^
         SyntaxError: invalid syntax
1854 1855 1856 1857
        <BLANKLINE>
        Exit: True
        """

1858
def bug_59270_recipes_always_start_in_buildout_dir():
1859
    r"""
1860 1861 1862 1863
    Recipes can rely on running from buildout directory

    >>> mkdir('bad_start')
    >>> write('bad_recipe.py',
1864 1865
    ... r'''
    ... import os, sys
Jim Fulton's avatar
Jim Fulton committed
1866
    ... def print_(*args):
Jim Fulton's avatar
Jim Fulton committed
1867
    ...     sys.stdout.write(' '.join(map(str, args)) + '\n')
1868 1869
    ... class Bad:
    ...     def __init__(self, *_):
Jim Fulton's avatar
Jim Fulton committed
1870
    ...         print_(os.getcwd())
1871
    ...     def install(self):
1872
    ...         sys.stdout.write(os.getcwd()+'\n')
1873
    ...         os.chdir('bad_start')
1874
    ...         sys.stdout.write(os.getcwd()+'\n')
1875 1876 1877 1878 1879 1880 1881 1882 1883
    ...         return ()
    ... ''')

    >>> write('setup.py',
    ... '''
    ... from setuptools import setup
    ... setup(name='bad.test',
    ...       entry_points={'zc.buildout': ['default=bad_recipe:Bad']},)
    ... ''')
1884

1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = .
    ... parts = b1 b2
    ... [b1]
    ... recipe = bad.test
    ... [b2]
    ... recipe = bad.test
    ... ''')

    >>> os.chdir('bad_start')
1897
    >>> print_(system(join(sample_buildout, 'bin', 'buildout')
Jim Fulton's avatar
Jim Fulton committed
1898
    ...              +' -c '+join(sample_buildout, 'buildout.cfg')), end='')
Jim Fulton's avatar
Jim Fulton committed
1899
    Develop: '/sample-buildout/.'
1900 1901
    /sample-buildout
    /sample-buildout
1902
    Installing b1.
1903 1904
    /sample-buildout
    /sample-buildout/bad_start
1905
    Installing b2.
1906 1907 1908 1909
    /sample-buildout
    /sample-buildout/bad_start
    """

1910 1911
def bug_61890_file_urls_dont_seem_to_work_in_find_dash_links():
    """
1912

1913
    This bug arises from the fact that setuptools is overly restrictive
1914 1915 1916 1917 1918
    about file urls, requiring that file urls pointing at directories
    must end in a slash.

    >>> dest = tmpdir('sample-install')
    >>> import zc.buildout.easy_install
1919
    >>> sample_eggs = sample_eggs.replace(os.path.sep, '/')
1920 1921 1922 1923 1924 1925
    >>> ws = zc.buildout.easy_install.install(
    ...     ['demo==0.2'], dest,
    ...     links=['file://'+sample_eggs], index=link_server+'index/')


    >>> for dist in ws:
1926
    ...     print_(dist)
1927 1928 1929 1930
    demo 0.2
    demoneeded 1.1

    >>> ls(dest)
Jim Fulton's avatar
Jim Fulton committed
1931
    d  demo-0.2-py2.4.egg
Jim Fulton's avatar
Jim Fulton committed
1932
    d  demoneeded-1.1-py2.4.egg
1933

1934 1935 1936 1937 1938
    """

def bug_75607_buildout_should_not_run_if_it_creates_an_empty_buildout_cfg():
    """
    >>> remove('buildout.cfg')
Jim Fulton's avatar
Jim Fulton committed
1939
    >>> print_(system(buildout), end='')
1940
    While:
1941
      Initializing.
1942 1943 1944
    Error: Couldn't open /sample-buildout/buildout.cfg


1945

1946
    """
1947

1948 1949
def dealing_with_extremely_insane_dependencies():
    r"""
1950

1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
    There was a problem with analysis of dependencies taking a long
    time, in part because the analysis would get repeated every time a
    package was encountered in a dependency list.  Now, we don't do
    the analysis any more:

    >>> import os
    >>> for i in range(5):
    ...     p = 'pack%s' % i
    ...     deps = [('pack%s' % j) for j in range(5) if j is not i]
    ...     if i == 4:
    ...         deps.append('pack5')
    ...     mkdir(p)
    ...     write(p, 'setup.py',
    ...           'from setuptools import setup\n'
    ...           'setup(name=%r, install_requires=%r,\n'
    ...           '      url="u", author="a", author_email="e")\n'
    ...           % (p, deps))

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
1972
    ... develop = pack0 pack1 pack2 pack3 pack4
1973 1974 1975 1976 1977 1978 1979
    ... parts = pack1
    ...
    ... [pack1]
    ... recipe = zc.recipe.egg:eggs
    ... eggs = pack0
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
1980
    >>> print_(system(buildout), end='') # doctest: +ELLIPSIS
1981 1982 1983 1984 1985 1986
    Develop: '/sample-buildout/pack0'
    Develop: '/sample-buildout/pack1'
    Develop: '/sample-buildout/pack2'
    Develop: '/sample-buildout/pack3'
    Develop: '/sample-buildout/pack4'
    Installing pack1.
Jim Fulton's avatar
Jim Fulton committed
1987
    ...
1988 1989 1990 1991 1992 1993 1994
    While:
      Installing pack1.
      Getting distribution for 'pack5'.
    Error: Couldn't find a distribution for 'pack5'.

    However, if we run in verbose mode, we can see why packages were included:

Jim Fulton's avatar
Jim Fulton committed
1995
    >>> print_(system(buildout+' -v'), end='') # doctest: +ELLIPSIS
1996
    Installing 'zc.buildout', 'setuptools'.
1997
    We have a develop egg: zc.buildout 1.0.0
1998 1999
    We have the best distribution that satisfies 'setuptools'.
    Picked: setuptools = 0.7
2000 2001 2002 2003 2004
    Develop: '/sample-buildout/pack0'
    Develop: '/sample-buildout/pack1'
    Develop: '/sample-buildout/pack2'
    Develop: '/sample-buildout/pack3'
    Develop: '/sample-buildout/pack4'
Shane Hathaway's avatar
Shane Hathaway committed
2005
    ...Installing pack1.
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
    Installing 'pack0'.
    We have a develop egg: pack0 0.0.0
    Getting required 'pack4'
      required by pack0 0.0.0.
    We have a develop egg: pack4 0.0.0
    Getting required 'pack3'
      required by pack0 0.0.0.
      required by pack4 0.0.0.
    We have a develop egg: pack3 0.0.0
    Getting required 'pack2'
      required by pack0 0.0.0.
      required by pack3 0.0.0.
      required by pack4 0.0.0.
    We have a develop egg: pack2 0.0.0
    Getting required 'pack1'
      required by pack0 0.0.0.
      required by pack2 0.0.0.
      required by pack3 0.0.0.
      required by pack4 0.0.0.
    We have a develop egg: pack1 0.0.0
    Getting required 'pack5'
      required by pack4 0.0.0.
    We have no distributions for pack5 that satisfies 'pack5'.
Jim Fulton's avatar
Jim Fulton committed
2029
    ...
2030 2031 2032 2033 2034 2035
    While:
      Installing pack1.
      Getting distribution for 'pack5'.
    Error: Couldn't find a distribution for 'pack5'.
    """

2036
def read_find_links_to_load_extensions():
Jim Fulton's avatar
Jim Fulton committed
2037 2038
    r"""
We'll create a wacky buildout extension that just announces itself when used:
2039 2040 2041 2042

    >>> src = tmpdir('src')
    >>> write(src, 'wacky_handler.py',
    ... '''
Jim Fulton's avatar
Jim Fulton committed
2043
    ... import sys
2044
    ... def install(buildout=None):
Jim Fulton's avatar
Jim Fulton committed
2045
    ...     sys.stdout.write("I am a wacky extension\\n")
2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
    ... ''')
    >>> write(src, 'setup.py',
    ... '''
    ... from setuptools import setup
    ... setup(name='wackyextension', version='1',
    ...       py_modules=['wacky_handler'],
    ...       entry_points = {'zc.buildout.extension':
    ...             ['default = wacky_handler:install']
    ...             },
    ...       )
    ... ''')
Jim Fulton's avatar
Jim Fulton committed
2057
    >>> print_(system(buildout+' setup '+src+' bdist_egg'), end='')
2058 2059 2060 2061 2062 2063 2064
    ... # doctest: +ELLIPSIS
    Running setup ...
    creating 'dist/wackyextension-1-...

Now we'll create a buildout that uses this extension to load other packages:

    >>> wacky_server = link_server.replace('http', 'wacky')
2065
    >>> dist = 'file://' + join(src, 'dist').replace(os.path.sep, '/')
2066 2067 2068
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
Jim Fulton's avatar
Jim Fulton committed
2069
    ... parts =
2070
    ... extensions = wackyextension
Jim Fulton's avatar
Jim Fulton committed
2071
    ... find-links = %(dist)s
2072 2073 2074 2075 2076
    ... ''' % globals())

When we run the buildout. it will load the extension from the dist
directory and then use the wacky extension to load the demo package

Jim Fulton's avatar
Jim Fulton committed
2077
    >>> print_(system(buildout), end='')
2078 2079
    Getting distribution for 'wackyextension'.
    Got wackyextension 1.
Jim Fulton's avatar
Jim Fulton committed
2080
    I am a wacky extension
2081

2082
    """
2083

Jim Fulton's avatar
Jim Fulton committed
2084 2085 2086
def distributions_from_local_find_links_make_it_to_download_cache():
    """

pombredanne's avatar
pombredanne committed
2087
If we specify a local directory in find links, distros found there
Jim Fulton's avatar
Jim Fulton committed
2088 2089 2090 2091 2092 2093 2094 2095 2096
need to make it to the download cache.

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

2097
    >>> print_(system(buildout+' setup test bdist_egg')) # doctest: +ELLIPSIS
Jim Fulton's avatar
Jim Fulton committed
2098 2099 2100 2101 2102 2103 2104 2105 2106
    Running setup script 'test/setup.py'.
    ...


    >>> mkdir('cache')
    >>> old_cache = zc.buildout.easy_install.download_cache('cache')
    >>> list(zc.buildout.easy_install.install(['foo'], 'eggs',
    ...          links=[join('test', 'dist')])) # doctest: +ELLIPSIS
    [foo 0.0.0 ...
2107

Jim Fulton's avatar
Jim Fulton committed
2108 2109 2110 2111
    >>> ls('cache')
    -  foo-0.0.0-py2.4.egg

    >>> _ = zc.buildout.easy_install.download_cache(old_cache)
2112

Jim Fulton's avatar
Jim Fulton committed
2113
    """
2114

2115 2116
def create_egg(name, version, dest, install_requires=None,
               dependency_links=None):
2117 2118 2119 2120 2121
    d = tempfile.mkdtemp()
    if dest=='available':
        extras = dict(x=['x'])
    else:
        extras = {}
2122 2123 2124 2125 2126 2127 2128 2129
    if dependency_links:
        links = 'dependency_links = %s, ' % dependency_links
    else:
        links = ''
    if install_requires:
        requires = 'install_requires = %s, ' % install_requires
    else:
        requires = ''
2130 2131 2132 2133
    try:
        open(os.path.join(d, 'setup.py'), 'w').write(
            'from setuptools import setup\n'
            'setup(name=%r, version=%r, extras_require=%r, zip_safe=True,\n'
2134 2135
            '      %s %s py_modules=["setup"]\n)'
            % (name, str(version), extras, requires, links)
2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
            )
        zc.buildout.testing.bdist_egg(d, sys.executable, os.path.abspath(dest))
    finally:
        shutil.rmtree(d)

def prefer_final_permutation(existing, available):
    for d in ('existing', 'available'):
        if os.path.exists(d):
            shutil.rmtree(d)
        os.mkdir(d)
    for version in existing:
        create_egg('spam', version, 'existing')
    for version in available:
        create_egg('spam', version, 'available')

    zc.buildout.easy_install.clear_index_cache()
    [dist] = list(
Jim Fulton's avatar
Jim Fulton committed
2153
        zc.buildout.easy_install.install(['spam'], 'existing', ['available'])
2154 2155 2156
        )

    if dist.extras:
2157
        print_('downloaded', dist.version)
2158
    else:
2159
        print_('had', dist.version)
2160 2161 2162 2163 2164 2165
    sys.path_importer_cache.clear()

def prefer_final():
    """
This test tests several permutations:

2166
Using different version numbers to work around zip importer cache problems. :(
2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254

- With prefer final:

    - no existing and newer dev available
    >>> prefer_final_permutation((), [1, '2a1'])
    downloaded 1

    - no existing and only dev available
    >>> prefer_final_permutation((), ['3a1'])
    downloaded 3a1

    - final existing and only dev acailable
    >>> prefer_final_permutation([4], ['5a1'])
    had 4

    - final existing and newer final available
    >>> prefer_final_permutation([6], [7])
    downloaded 7

    - final existing and same final available
    >>> prefer_final_permutation([8], [8])
    had 8

    - final existing and older final available
    >>> prefer_final_permutation([10], [9])
    had 10

    - only dev existing and final available
    >>> prefer_final_permutation(['12a1'], [11])
    downloaded 11

    - only dev existing and no final available newer dev available
    >>> prefer_final_permutation(['13a1'], ['13a2'])
    downloaded 13a2

    - only dev existing and no final available older dev available
    >>> prefer_final_permutation(['15a1'], ['14a1'])
    had 15a1

    - only dev existing and no final available same dev available
    >>> prefer_final_permutation(['16a1'], ['16a1'])
    had 16a1

- Without prefer final:

    >>> _ = zc.buildout.easy_install.prefer_final(False)

    - no existing and newer dev available
    >>> prefer_final_permutation((), [18, '19a1'])
    downloaded 19a1

    - no existing and only dev available
    >>> prefer_final_permutation((), ['20a1'])
    downloaded 20a1

    - final existing and only dev acailable
    >>> prefer_final_permutation([21], ['22a1'])
    downloaded 22a1

    - final existing and newer final available
    >>> prefer_final_permutation([23], [24])
    downloaded 24

    - final existing and same final available
    >>> prefer_final_permutation([25], [25])
    had 25

    - final existing and older final available
    >>> prefer_final_permutation([27], [26])
    had 27

    - only dev existing and final available
    >>> prefer_final_permutation(['29a1'], [28])
    had 29a1

    - only dev existing and no final available newer dev available
    >>> prefer_final_permutation(['30a1'], ['30a2'])
    downloaded 30a2

    - only dev existing and no final available older dev available
    >>> prefer_final_permutation(['32a1'], ['31a1'])
    had 32a1

    - only dev existing and no final available same dev available
    >>> prefer_final_permutation(['33a1'], ['33a1'])
    had 33a1

    >>> _ = zc.buildout.easy_install.prefer_final(True)
2255

2256 2257 2258 2259 2260
    """

def buildout_prefer_final_option():
    """
The prefer-final buildout option can be used for override the default
2261
preference for newer distributions.
2262

Jim Fulton's avatar
Jim Fulton committed
2263
The default is prefer-final = true:
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = eggs
    ... find-links = %(link_server)s
    ...
    ... [eggs]
    ... recipe = zc.recipe.egg:eggs
    ... eggs = demo
    ... ''' % globals())

Jim Fulton's avatar
Jim Fulton committed
2276
    >>> print_(system(buildout+' -v'), end='') # doctest: +ELLIPSIS
2277
    Installing 'zc.buildout', 'setuptools'.
2278
    ...
Jim Fulton's avatar
Jim Fulton committed
2279
    Picked: demo = 0.3
2280
    ...
Jim Fulton's avatar
Jim Fulton committed
2281
    Picked: demoneeded = 1.1
2282 2283

Here we see that the final versions of demo and demoneeded are used.
Jim Fulton's avatar
Jim Fulton committed
2284
We get the same behavior if we add prefer-final = true
2285 2286 2287 2288 2289 2290

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = eggs
    ... find-links = %(link_server)s
Jim Fulton's avatar
Jim Fulton committed
2291
    ... prefer-final = true
2292 2293 2294 2295 2296 2297
    ...
    ... [eggs]
    ... recipe = zc.recipe.egg:eggs
    ... eggs = demo
    ... ''' % globals())

Jim Fulton's avatar
Jim Fulton committed
2298
    >>> print_(system(buildout+' -v'), end='') # doctest: +ELLIPSIS
2299
    Installing 'zc.buildout', 'setuptools'.
2300
    ...
Jim Fulton's avatar
Jim Fulton committed
2301
    Picked: demo = 0.3
2302
    ...
Jim Fulton's avatar
Jim Fulton committed
2303
    Picked: demoneeded = 1.1
2304

Jim Fulton's avatar
Jim Fulton committed
2305
If we specify prefer-final = false, we'll get the newest
2306 2307 2308 2309 2310 2311 2312
distributions:

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = eggs
    ... find-links = %(link_server)s
Jim Fulton's avatar
Jim Fulton committed
2313
    ... prefer-final = false
2314 2315 2316 2317 2318 2319
    ...
    ... [eggs]
    ... recipe = zc.recipe.egg:eggs
    ... eggs = demo
    ... ''' % globals())

Jim Fulton's avatar
Jim Fulton committed
2320
    >>> print_(system(buildout+' -v'), end='') # doctest: +ELLIPSIS
2321
    Installing 'zc.buildout', 'setuptools'.
2322
    ...
Jim Fulton's avatar
Jim Fulton committed
2323
    Picked: demo = 0.4c1
2324
    ...
Jim Fulton's avatar
Jim Fulton committed
2325
    Picked: demoneeded = 1.2c1
2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340

We get an error if we specify anything but true or false:

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = eggs
    ... find-links = %(link_server)s
    ... prefer-final = no
    ...
    ... [eggs]
    ... recipe = zc.recipe.egg:eggs
    ... eggs = demo
    ... ''' % globals())

Jim Fulton's avatar
Jim Fulton committed
2341
    >>> print_(system(buildout+' -v'), end='') # doctest: +ELLIPSIS
2342 2343
    While:
      Initializing.
Jim Fulton's avatar
Jim Fulton committed
2344
    Error: Invalid value for 'prefer-final' option: 'no'
2345 2346 2347 2348 2349 2350
    """

def wont_downgrade_due_to_prefer_final():
    r"""
    If we install a non-final buildout version, we don't want to
    downgrade just bcause we prefer-final.  If a buildout version
2351 2352
    isn't specified using a versions entry, then buildout's version
    requirement gets set to >=CURRENT_VERSION.
2353 2354 2355 2356 2357 2358

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts =
    ... ''')
2359 2360

    >>> [v] = [l.split('= >=', 1)[1].strip()
2361
    ...        for l in system(buildout+' -vv').split('\n')
2362 2363
    ...        if l.startswith('zc.buildout = >=')]
    >>> v == pkg_resources.working_set.find(
2364 2365 2366 2367 2368 2369 2370 2371
    ...         pkg_resources.Requirement.parse('zc.buildout')
    ...         ).version
    True

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts =
2372 2373
    ... [versions]
    ... zc.buildout = >.1
2374
    ... ''')
2375
    >>> [str(l.split('= >', 1)[1].strip())
2376
    ...        for l in system(buildout+' -vv').split('\n')
2377 2378
    ...        if l.startswith('zc.buildout =')]
    ['.1']
2379 2380 2381 2382 2383 2384 2385 2386 2387

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts =
    ... versions = versions
    ... [versions]
    ... zc.buildout = 43
    ... ''')
2388 2389 2390
    >>> print_(system(buildout), end='') # doctest: +ELLIPSIS
    Getting distribution for 'zc.buildout==43'.
    ...
2391 2392 2393

    """

2394 2395 2396
def develop_with_modules():
    """
Distribution setup scripts can import modules in the distribution directory:
2397

2398 2399 2400 2401
    >>> mkdir('foo')
    >>> write('foo', 'bar.py',
    ... '''# empty
    ... ''')
2402

2403 2404 2405 2406 2407 2408
    >>> write('foo', 'setup.py',
    ... '''
    ... import bar
    ... from setuptools import setup
    ... setup(name="foo")
    ... ''')
2409

2410 2411 2412 2413
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = foo
2414
    ... parts =
2415 2416
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
2417
    >>> print_(system(join('bin', 'buildout')), end='')
2418 2419 2420 2421 2422 2423 2424
    Develop: '/sample-buildout/foo'

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

    """
2425

2426
def dont_pick_setuptools_if_version_is_specified_when_required_by_src_dist():
Jim Fulton's avatar
Jim Fulton committed
2427
    """
2428
When installing a source distribution, we got setuptools without
Jim Fulton's avatar
Jim Fulton committed
2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448
honoring our version specification.

    >>> mkdir('dist')
    >>> write('setup.py',
    ... '''
    ... from setuptools import setup
    ... setup(name='foo', version='1', py_modules=['foo'], zip_safe=True)
    ... ''')
    >>> write('foo.py', '')
    >>> _ = system(buildout+' setup . sdist')

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = foo
    ... find-links = dist
    ... versions = versions
    ... allow-picked-versions = false
    ...
    ... [versions]
2449
    ... setuptools = %s
Jim Fulton's avatar
Jim Fulton committed
2450 2451 2452 2453 2454 2455
    ... foo = 1
    ...
    ... [foo]
    ... recipe = zc.recipe.egg
    ... eggs = foo
    ... ''' % pkg_resources.working_set.find(
2456
    ...    pkg_resources.Requirement.parse('setuptools')).version)
Jim Fulton's avatar
Jim Fulton committed
2457

Jim Fulton's avatar
Jim Fulton committed
2458
    >>> print_(system(buildout), end='')
Jim Fulton's avatar
Jim Fulton committed
2459 2460 2461
    Installing foo.
    Getting distribution for 'foo==1'.
    Got foo 1.
2462

Jim Fulton's avatar
Jim Fulton committed
2463
    """
2464

2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482
def pyc_and_pyo_files_have_correct_paths():
    r"""

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = eggs
    ... find-links = %(link_server)s
    ...
    ... [eggs]
    ... recipe = zc.recipe.egg
    ... eggs = demo
    ... interpreter = py
    ... ''' % globals())

    >>> _ = system(buildout)

    >>> write('t.py',
2483 2484
    ... r'''
    ... import eggrecipedemo, eggrecipedemoneeded, sys
Jim Fulton's avatar
Jim Fulton committed
2485 2486 2487 2488 2489 2490
    ... if sys.version_info > (3,):
    ...     code = lambda f: f.__code__
    ... else:
    ...     code = lambda f: f.func_code
    ... sys.stdout.write(code(eggrecipedemo.main).co_filename+'\n')
    ... sys.stdout.write(code(eggrecipedemoneeded.f).co_filename+'\n')
2491 2492
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
2493
    >>> print_(system(join('bin', 'py')+ ' t.py'), end='')
Jim Fulton's avatar
Jim Fulton committed
2494 2495
    /sample-buildout/eggs/demo-0.3-py2.4.egg/eggrecipedemo.py
    /sample-buildout/eggs/demoneeded-1.1-py2.4.egg/eggrecipedemoneeded.py
2496 2497
    """

2498 2499 2500 2501 2502 2503
def dont_mess_with_standard_dirs_with_variable_refs():
    """
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... eggs-directory = ${buildout:directory}/develop-eggs
2504
    ... parts =
2505
    ... ''' % globals())
Jim Fulton's avatar
Jim Fulton committed
2506
    >>> print_(system(buildout), end='')
2507

2508 2509
    """

2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536
def expand_shell_patterns_in_develop_paths():
    """
    Sometimes we want to include a number of eggs in some directory as
    develop eggs, without explicitly listing all of them in our
    buildout.cfg

    >>> make_dist_that_requires(sample_buildout, 'sampley')
    >>> make_dist_that_requires(sample_buildout, 'samplez')

    Now, let's create a buildout that has a shell pattern that matches
    both:

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = eggs
    ... develop = sample*
    ... find-links = %(link_server)s
    ...
    ... [eggs]
    ... recipe = zc.recipe.egg
    ... eggs = sampley
    ...        samplez
    ... ''' % globals())

    We can see that both eggs were found:

Jim Fulton's avatar
Jim Fulton committed
2537
    >>> print_(system(buildout), end='')
2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568
    Develop: '/sample-buildout/sampley'
    Develop: '/sample-buildout/samplez'
    Installing eggs.

    """

def warn_users_when_expanding_shell_patterns_yields_no_results():
    """
    Sometimes shell patterns do not match anything, so we want to warn
    our users about it...

    >>> make_dist_that_requires(sample_buildout, 'samplea')

    So if we have 2 patterns, one that has a matching directory, and
    another one that does not

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = eggs
    ... develop = samplea grumble*
    ... find-links = %(link_server)s
    ...
    ... [eggs]
    ... recipe = zc.recipe.egg
    ... eggs = samplea
    ... ''' % globals())

    We should get one of the eggs, and a warning for the pattern that
    did not match anything.

Jim Fulton's avatar
Jim Fulton committed
2569
    >>> print_(system(buildout), end='')
2570 2571 2572 2573 2574 2575
    Develop: '/sample-buildout/samplea'
    Couldn't develop '/sample-buildout/grumble*' (not found)
    Installing eggs.

    """

2576 2577 2578 2579
def make_sure_versions_dont_cancel_extras():
    """
    There was a bug that caused extras in requirements to be lost.

Jim Fulton's avatar
Jim Fulton committed
2580
    >>> _ = open('setup.py', 'w').write('''
2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598
    ... from setuptools import setup
    ... setup(name='extraversiondemo', version='1.0',
    ...       url='x', author='x', author_email='x',
    ...       extras_require=dict(foo=['demo']), py_modules=['t'])
    ... ''')
    >>> open('README', 'w').close()
    >>> open('t.py', 'w').close()

    >>> sdist('.', sample_eggs)
    >>> mkdir('dest')
    >>> ws = zc.buildout.easy_install.install(
    ...     ['extraversiondemo[foo]'], 'dest', links=[sample_eggs],
    ...     versions = dict(extraversiondemo='1.0')
    ... )
    >>> sorted(dist.key for dist in ws)
    ['demo', 'demoneeded', 'extraversiondemo']
    """

Jim Fulton's avatar
Jim Fulton committed
2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623
def increment_buildout_options():
    r"""
    >>> write('b1.cfg', '''
    ... [buildout]
    ... parts = p1
    ... x = 1
    ... y = a
    ...     b
    ...
    ... [p1]
    ... recipe = zc.buildout:debug
    ... foo = ${buildout:x} ${buildout:y}
    ... ''')

    >>> write('buildout.cfg', '''
    ... [buildout]
    ... extends = b1.cfg
    ... parts += p2
    ... x += 2
    ... y -= a
    ...
    ... [p2]
    ... <= p1
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
2624
    >>> print_(system(buildout), end='')
Jim Fulton's avatar
Jim Fulton committed
2625 2626 2627 2628 2629 2630 2631 2632
    Installing p1.
      foo='1\n2 b'
      recipe='zc.buildout:debug'
    Installing p2.
      foo='1\n2 b'
      recipe='zc.buildout:debug'
    """

Jim Fulton's avatar
Jim Fulton committed
2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663
def increment_buildout_with_multiple_extended_files_421022():
    r"""
    >>> write('foo.cfg', '''
    ... [buildout]
    ... foo-option = foo
    ... [other]
    ... foo-option = foo
    ... ''')
    >>> write('bar.cfg', '''
    ... [buildout]
    ... bar-option = bar
    ... [other]
    ... bar-option = bar
    ... ''')
    >>> write('buildout.cfg', '''
    ... [buildout]
    ... parts = p other
    ... extends = bar.cfg foo.cfg
    ... bar-option += baz
    ... foo-option += ham
    ...
    ... [other]
    ... recipe = zc.buildout:debug
    ... bar-option += baz
    ... foo-option += ham
    ...
    ... [p]
    ... recipe = zc.buildout:debug
    ... x = ${buildout:bar-option} ${buildout:foo-option}
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
2664
    >>> print_(system(buildout), end='')
Jim Fulton's avatar
Jim Fulton committed
2665 2666 2667 2668 2669 2670 2671 2672 2673
    Installing p.
      recipe='zc.buildout:debug'
      x='bar\nbaz foo\nham'
    Installing other.
      bar-option='bar\nbaz'
      foo-option='foo\nham'
      recipe='zc.buildout:debug'
    """

Jim Fulton's avatar
Jim Fulton committed
2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689
def increment_on_command_line():
    r"""
    >>> write('buildout.cfg', '''
    ... [buildout]
    ... parts = p1
    ... x = 1
    ... y = a
    ...     b
    ...
    ... [p1]
    ... recipe = zc.buildout:debug
    ... foo = ${buildout:x} ${buildout:y}
    ...
    ... [p2]
    ... <= p1
    ... ''')
Jim Fulton's avatar
Jim Fulton committed
2690

Jim Fulton's avatar
Jim Fulton committed
2691
    >>> print_(system(buildout+' buildout:parts+=p2 p1:foo+=bar'), end='')
Jim Fulton's avatar
Jim Fulton committed
2692 2693 2694 2695 2696 2697 2698
    Installing p1.
      foo='1 a\nb\nbar'
      recipe='zc.buildout:debug'
    Installing p2.
      foo='1 a\nb\nbar'
      recipe='zc.buildout:debug'
    """
2699

2700 2701
def test_constrained_requirement():
    """
pombredanne's avatar
pombredanne committed
2702
    zc.buildout.easy_install._constrained_requirement(constraint, requirement)
2703 2704 2705 2706 2707 2708 2709 2710 2711

    Transforms an environment by applying a constraint.

    Here's a table of examples:

    >>> from zc.buildout.easy_install import IncompatibleConstraintError
    >>> examples = [
    ... # original, constraint, transformed
    ... ('x',        '1',        'x==1'),
2712
    ... ('x>1',      '2',        'x==2'),
2713
    ... ('x>3',      '2',        IncompatibleConstraintError),
2714
    ... ('x>1',      '>2',       'x>1,>2'),
2715 2716 2717 2718 2719 2720 2721 2722 2723 2724
    ... ]
    >>> from zc.buildout.easy_install import _constrained_requirement
    >>> for o, c, e in examples:
    ...     try:
    ...         o = pkg_resources.Requirement.parse(o)
    ...         if isinstance(e, str):
    ...             e = pkg_resources.Requirement.parse(e)
    ...         g = _constrained_requirement(c, o)
    ...     except IncompatibleConstraintError:
    ...         g = IncompatibleConstraintError
2725
    ...     if str(g) != str(e):
2726 2727 2728
    ...         print_('failed', o, c, g, '!=', e)
    """

2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799
def test_distutils_scripts_using_import_are_properly_parsed():
    """
    zc.buildout.easy_install._distutils_script(path, dest, script_content, initialization, rsetup):

    Creates a script for a distutils based project. In this example for a
    hypothetical code quality checker called 'pyflint' that uses an import
    statement to import its code.

    >>> pyflint_script = '''#!/path/to/bin/python
    ... import pyflint.do_something
    ... pyflint.do_something()
    ... '''
    >>> import sys
    >>> original_executable = sys.executable
    >>> sys.executable = 'python'

    >>> from zc.buildout.easy_install import _distutils_script
    >>> _distutils_script('\\'/path/test/\\'', 'bin/pyflint', pyflint_script, '', '')
    ['bin/pyflint']
    >>> cat('bin/pyflint')
    #!python
    <BLANKLINE>
    <BLANKLINE>
    import sys
    sys.path[0:0] = [
      '/path/test/',
      ]
    <BLANKLINE>
    <BLANKLINE>
    import pyflint.do_something
    pyflint.do_something()

    >>> sys.executable = original_executable
    """

def test_distutils_scripts_using_from_are_properly_parsed():
    """
    zc.buildout.easy_install._distutils_script(path, dest, script_content, initialization, rsetup):

    Creates a script for a distutils based project. In this example for a
    hypothetical code quality checker called 'pyflint' that uses a from
    statement to import its code.

    >>> pyflint_script = '''#!/path/to/bin/python
    ... from pyflint import do_something
    ... do_something()
    ... '''
    >>> import sys
    >>> original_executable = sys.executable
    >>> sys.executable = 'python'

    >>> from zc.buildout.easy_install import _distutils_script
    >>> _distutils_script('\\'/path/test/\\'', 'bin/pyflint', pyflint_script, '', '')
    ['bin/pyflint']
    >>> cat('bin/pyflint')
    #!python
    <BLANKLINE>
    <BLANKLINE>
    import sys
    sys.path[0:0] = [
      '/path/test/',
      ]
    <BLANKLINE>
    <BLANKLINE>
    from pyflint import do_something
    do_something()

    >>> sys.executable = original_executable
    """


2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810
def want_new_zcrecipeegg():
    """
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = egg
    ... [egg]
    ... recipe = zc.recipe.egg <2dev
    ... eggs = demo
    ... ''')
    >>> print_(system(join('bin', 'buildout')), end='') # doctest: +ELLIPSIS
2811
    Getting distribution for 'zc.recipe.egg<2dev,>=2.0.0a3'.
2812 2813 2814 2815 2816
    While:
      Installing.
      Getting section egg.
      Initializing section egg.
      Installing recipe zc.recipe.egg <2dev.
2817 2818
      Getting distribution for 'zc.recipe.egg<2dev,>=2.0.0a3'.
    Error: Couldn't find a distribution for 'zc.recipe.egg<2dev,>=2.0.0a3'.
2819 2820
    """

2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853
def macro_inheritance_bug():
    """

There was a bug preventing a section from using another section as a macro
if that section was extended with macros, and both sections were listed as
parts (phew!).  The following contrived example demonstrates that this
now works.

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = foo bar
    ... [base]
    ... recipe = zc.recipe.egg
    ... [foo]
    ... <=base
    ... eggs = zc.buildout
    ... interpreter = python
    ... [bar]
    ... <=foo
    ... interpreter = py
    ... ''')
    >>> print_(system(join('bin', 'buildout')), end='') # doctest: +ELLIPSIS
    Installing foo.
    ...
    Installing bar.
    ...
    >>> ls("./bin")
    -  buildout
    -  py
    -  python
    """

2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864
def bootstrap_honors_relative_paths():
    """
    >>> working = tmpdir('working')
    >>> cd(working)
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts =
    ... relative-paths = true
    ... ''')
    >>> _ = system(buildout+' bootstrap')
2865 2866
    >>> cat('bin', 'buildout') # doctest: +ELLIPSIS
    #!/usr/local/bin/python2.7
2867 2868 2869 2870 2871 2872 2873 2874 2875
    <BLANKLINE>
    import os
    <BLANKLINE>
    join = os.path.join
    base = os.path.dirname(os.path.abspath(os.path.realpath(__file__)))
    base = os.path.dirname(base)
    <BLANKLINE>
    import sys
    sys.path[0:0] = [
2876
      join(base, 'eggs/setuptools-0.7-py2.7.egg'),
2877
      ...
2878 2879 2880 2881 2882 2883 2884
      ]
    <BLANKLINE>
    import zc.buildout.buildout
    <BLANKLINE>
    if __name__ == '__main__':
        sys.exit(zc.buildout.buildout.main())
    """
2885

2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898
def cant_use_install_from_cache_and_offline_together():
    r"""
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts =
    ... offline = true
    ... install-from-cache = true
    ... ''')
    >>> print_(system(join('bin', 'buildout')), end='') # doctest: +ELLIPSIS
    While:
      Initializing.
    Error: install-from-cache can't be used with offline mode.
David Glick's avatar
David Glick committed
2899
    Nothing is installed, even from cache, in offline
2900 2901 2902 2903
    mode, which might better be called 'no-install mode'.
    <BLANKLINE>
    """

2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928
def error_installing_in_offline_mode_if_dont_have_needed_dist():
    r"""
    >>> import zc.buildout.easy_install
    >>> ws = zc.buildout.easy_install.install(
    ...     ['demo==0.2'], None,
    ...     links=[link_server], index=link_server+'index/')
    Traceback (most recent call last):
    ...
    UserError: We don't have a distribution for demo==0.2
    and can't install one in offline (no-install) mode.
    <BLANKLINE>
    """

def error_building_in_offline_mode_if_dont_have_needed_dist():
    r"""
    >>> zc.buildout.easy_install.build(
    ...   'extdemo', None,
    ...   {}, links=[link_server], index=link_server+'index/')
    Traceback (most recent call last):
    ...
    UserError: We don't have a distribution for extdemo
    and can't build one in offline (no-install) mode.
    <BLANKLINE>
    """

2929 2930 2931 2932 2933 2934
def test_buildout_section_shorthand_for_command_line_assignments():
    r"""
    >>> write('buildout.cfg', '')
    >>> print_(system(buildout+' parts='), end='') # doctest: +ELLIPSIS
    """

Jim Fulton's avatar
Jim Fulton committed
2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948
def buildout_honors_umask():
    """

    For setting the executable permission, the user's umask is honored:

    >>> orig_umask = os.umask(0o077)  # Only user gets permissions.
    >>> zc.buildout.easy_install._execute_permission() == 0o700
    True
    >>> tmp = os.umask(0o022)  # User can write, the rest not.
    >>> zc.buildout.easy_install._execute_permission() == 0o755
    True
    >>> tmp = os.umask(orig_umask)  # Reset umask to the original value.
    """

2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968
def parse_with_section_expr():
    r"""
    >>> class Recipe:
    ...     def __init__(self, buildout, *_):
    ...         buildout.parse('''
    ...             [foo : sys.version_info[0] > 0]
    ...             x = 1
    ...             ''')

    >>> buildout = zc.buildout.testing.Buildout()
    >>> buildout.parse('''
    ...     [foo : sys.version_info[0] > 0]
    ...     x = 1
    ...     ''')
    >>> buildout.print_options()
    [foo]
    x = 1

    """

Jim Fulton's avatar
Jim Fulton committed
2969 2970
if sys.platform == 'win32':
    del buildout_honors_umask # umask on dohs is academic
2971

Jim Fulton's avatar
Jim Fulton committed
2972
######################################################################
2973

2974
def create_sample_eggs(test, executable=sys.executable):
Jim Fulton's avatar
Jim Fulton committed
2975
    assert executable == sys.executable, (executable, sys.executable)
2976 2977 2978 2979 2980 2981
    write = test.globs['write']
    dest = test.globs['sample_eggs']
    tmp = tempfile.mkdtemp()
    try:
        write(tmp, 'README.txt', '')

2982
        for i in (0, 1, 2):
2983
            write(tmp, 'eggrecipedemoneeded.py', 'y=%s\ndef f():\n  pass' % i)
2984
            c1 = i==2 and 'c1' or ''
2985 2986 2987
            write(
                tmp, 'setup.py',
                "from setuptools import setup\n"
2988
                "setup(name='demoneeded', py_modules=['eggrecipedemoneeded'],"
2989
                " zip_safe=True, version='1.%s%s', author='bob', url='bob', "
2990
                "author_email='bob')\n"
2991
                % (i, c1)
2992 2993
                )
            zc.buildout.testing.sdist(tmp, dest)
2994

2995 2996 2997
        write(
            tmp, 'distutilsscript',
            '#!/usr/bin/python\n'
Reinout van Rees's avatar
Reinout van Rees committed
2998
            '# -*- coding: utf-8 -*-\n'
2999 3000
            '"""Module docstring."""\n'
            'from __future__ import print_statement\n'
3001
            'import os\n'
3002 3003
            'import sys; sys.stdout.write("distutils!\\n")\n'
            )
3004 3005 3006
        write(
            tmp, 'setup.py',
            "from setuptools import setup\n"
Jim Fulton's avatar
Jim Fulton committed
3007
            "setup(name='other', zip_safe=False, version='1.0', "
3008
            "scripts=['distutilsscript'],"
3009
            "py_modules=['eggrecipedemoneeded'])\n"
3010
            )
Jim Fulton's avatar
Jim Fulton committed
3011
        zc.buildout.testing.bdist_egg(tmp, sys.executable, dest)
3012

3013 3014 3015 3016 3017 3018 3019 3020 3021
        write(
            tmp, 'setup.py',
            "from setuptools import setup\n"
            "setup(name='du_zipped', zip_safe=True, version='1.0', "
            "scripts=['distutilsscript'],"
            "py_modules=['eggrecipedemoneeded'])\n"
            )
        zc.buildout.testing.bdist_egg(tmp, executable, dest)

3022
        os.remove(os.path.join(tmp, 'distutilsscript'))
3023
        os.remove(os.path.join(tmp, 'eggrecipedemoneeded.py'))
3024

3025
        for i in (1, 2, 3, 4):
3026 3027
            write(
                tmp, 'eggrecipedemo.py',
3028 3029 3030
                'import eggrecipedemoneeded, sys\n'
                'def print_(*a):\n'
                '    sys.stdout.write(" ".join(map(str, a))+"\\n")\n'
3031
                'x=%s\n'
3032 3033
                'def main():\n'
                '   print_(x, eggrecipedemoneeded.y)\n'
3034
                % i)
3035
            c1 = i==4 and 'c1' or ''
3036 3037 3038 3039 3040 3041 3042
            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']},"
3043
                " zip_safe=True, version='0.%s%s')\n" % (i, c1)
3044
                )
Jim Fulton's avatar
Jim Fulton committed
3045
            zc.buildout.testing.bdist_egg(tmp, dest)
3046 3047 3048 3049 3050 3051 3052 3053 3054 3055

        write(tmp, 'eggrecipebigdemo.py', 'import eggrecipedemo')
        write(
            tmp, 'setup.py',
            "from setuptools import setup\n"
            "setup(name='bigdemo', "
            " install_requires = 'demo',"
            " py_modules=['eggrecipebigdemo'], "
            " zip_safe=True, version='0.1')\n"
            )
Jim Fulton's avatar
Jim Fulton committed
3056
        zc.buildout.testing.bdist_egg(tmp, sys.executable, dest)
3057

3058 3059 3060
    finally:
        shutil.rmtree(tmp)

3061
extdemo_c2 = """
3062 3063 3064 3065 3066 3067 3068 3069
#include <Python.h>
#include <extdemo.h>

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

PyMODINIT_FUNC
initextdemo(void)
{
3070 3071 3072 3073 3074 3075 3076
    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
3077 3078
}
"""
3079

3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112
extdemo_c3 = """
#include <Python.h>
#include <extdemo.h>

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

#define MOD_DEF(ob, name, doc, methods) \
	  static struct PyModuleDef moduledef = { \
	    PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
	  ob = PyModule_Create(&moduledef);

#define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)

MOD_INIT(extdemo)
{
    PyObject *m;

    MOD_DEF(m, "extdemo", "", methods);

#ifdef TWO
    PyModule_AddObject(m, "val", PyLong_FromLong(2));
#else
    PyModule_AddObject(m, "val", PyLong_FromLong(EXTDEMO));
#endif

    return m;
}
"""

extdemo_c = sys.version_info[0] < 3 and extdemo_c2 or extdemo_c3

extdemo_setup_py = r"""
import os, sys
3113
from distutils.core import setup, Extension
3114

3115
if os.environ.get('test-variable'):
3116
    print("Have environment test-variable: %%s" %% os.environ['test-variable'])
3117

3118
setup(name = "extdemo", version = "%s", url="http://www.zope.org",
3119 3120 3121 3122
      author="Demo", author_email="demo@demo.com",
      ext_modules = [Extension('extdemo', ['extdemo.c'])],
      )
"""
3123

3124 3125 3126 3127 3128
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']
3129 3130 3131
    write = test.globs['write']
    try:
        write(tmp, 'extdemo.c', extdemo_c);
3132
        write(tmp, 'setup.py', extdemo_setup_py % version);
3133 3134 3135 3136 3137
        write(tmp, 'README', "");
        write(tmp, 'MANIFEST.in', "include *.c\n");
        test.globs['sdist'](tmp, test.globs['sample_eggs'])
    except:
        shutil.rmtree(tmp)
3138

3139 3140 3141 3142 3143 3144 3145 3146 3147
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'])
3148
    test.globs['update_extdemo'] = lambda : add_source_dist(test, 1.5)
3149
    zc.buildout.testing.install_develop('zc.recipe.egg', test)
3150

3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173
def buildout_txt_setup(test):
    zc.buildout.testing.buildoutSetUp(test)
    mkdir = test.globs['mkdir']
    eggs = os.environ['buildout-testing-index-url'][7:]
    test.globs['sample_eggs'] = eggs
    create_sample_eggs(test)

    for name in os.listdir(eggs):
        if '-' in name:
            pname = name.split('-')[0]
            if not os.path.exists(os.path.join(eggs, pname)):
                mkdir(eggs, pname)
            shutil.move(os.path.join(eggs, name),
                        os.path.join(eggs, pname, name))

    dist = pkg_resources.working_set.find(
        pkg_resources.Requirement.parse('zc.recipe.egg'))
    mkdir(eggs, 'zc.recipe.egg')
    zc.buildout.testing.sdist(
        os.path.dirname(dist.location),
        os.path.join(eggs, 'zc.recipe.egg'),
        )

3174 3175
egg_parse = re.compile('([0-9a-zA-Z_.]+)-([0-9a-zA-Z_.]+)-py(\d[.]\d).egg$'
                       ).match
Gary Poster's avatar
Gary Poster committed
3176
def makeNewRelease(project, ws, dest, version='99.99'):
3177 3178 3179 3180
    dist = ws.find(pkg_resources.Requirement.parse(project))
    eggname, oldver, pyver = egg_parse(
        os.path.basename(dist.location)
        ).groups()
Gary Poster's avatar
Gary Poster committed
3181
    dest = os.path.join(dest, "%s-%s-py%s.egg" % (eggname, version, pyver))
3182 3183 3184 3185 3186
    if os.path.isfile(dist.location):
        shutil.copy(dist.location, dest)
        zip = zipfile.ZipFile(dest, 'a')
        zip.writestr(
            'EGG-INFO/PKG-INFO',
3187
            ((zip.read('EGG-INFO/PKG-INFO').decode('ISO-8859-1')
Jim Fulton's avatar
Jim Fulton committed
3188 3189
              ).replace("Version: %s" % oldver,
                        "Version: %s" % version)
3190
             ).encode('ISO-8859-1')
3191 3192 3193
            )
        zip.close()
    else:
3194
        shutil.copytree(dist.location, dest)
3195
        info_path = os.path.join(dest, 'EGG-INFO', 'PKG-INFO')
3196
        info = open(info_path).read().replace("Version: %s" % oldver,
Gary Poster's avatar
Gary Poster committed
3197
                                              "Version: %s" % version)
3198 3199
        open(info_path, 'w').write(info)

Gary Poster's avatar
Gary Poster committed
3200
def getWorkingSetWithBuildoutEgg(test):
3201 3202 3203
    sample_buildout = test.globs['sample_buildout']
    eggs = os.path.join(sample_buildout, 'eggs')

Gary Poster's avatar
Gary Poster committed
3204
    # If the zc.buildout dist is a develop dist, convert it to a
3205 3206 3207 3208 3209 3210 3211
    # 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))
3212 3213 3214 3215 3216 3217
        zc.buildout.easy_install.call_subprocess(
            [sys.executable,
             os.path.join(os.path.dirname(dist.location), 'setup.py'),
             '-q', 'bdist_egg', '-d', eggs],
            env=dict(os.environ,
                     PYTHONPATH=pkg_resources.working_set.find(
3218
                         pkg_resources.Requirement.parse('setuptools')
3219 3220 3221
                         ).location,
                     ),
            )
3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232
        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
Gary Poster's avatar
Gary Poster committed
3233
    return ws
3234

Gary Poster's avatar
Gary Poster committed
3235 3236 3237 3238 3239
def updateSetup(test):
    zc.buildout.testing.buildoutSetUp(test)
    new_releases = test.globs['tmpdir']('new_releases')
    test.globs['new_releases'] = new_releases
    ws = getWorkingSetWithBuildoutEgg(test)
3240 3241 3242
    # now let's make the new releases
    makeNewRelease('zc.buildout', ws, new_releases)
    os.mkdir(os.path.join(new_releases, 'zc.buildout'))
3243 3244
    makeNewRelease('setuptools', ws, new_releases)
    os.mkdir(os.path.join(new_releases, 'setuptools'))
3245

Jim Fulton's avatar
Jim Fulton committed
3246 3247 3248 3249 3250
bootstrap_py = os.path.join(
    os.path.dirname(os.path.dirname(os.path.dirname(
        os.path.dirname(__file__)))),
    'bootstrap', 'bootstrap.py')

Gary Poster's avatar
Gary Poster committed
3251
def bootstrapSetup(test):
Jim Fulton's avatar
Jim Fulton committed
3252 3253 3254
    buildout_txt_setup(test)
    test.globs['link_server'] = test.globs['start_server'](
        test.globs['sample_eggs'])
Gary Poster's avatar
Gary Poster committed
3255 3256
    sample_eggs = test.globs['sample_eggs']
    ws = getWorkingSetWithBuildoutEgg(test)
Jim Fulton's avatar
Jim Fulton committed
3257
    makeNewRelease('zc.buildout', ws, sample_eggs, '2.0.0')
Jim Fulton's avatar
Jim Fulton committed
3258
    makeNewRelease('zc.buildout', ws, sample_eggs, '22.0.0')
Gary Poster's avatar
Gary Poster committed
3259
    os.environ['bootstrap-testing-find-links'] = test.globs['link_server']
Jim Fulton's avatar
Jim Fulton committed
3260
    test.globs['bootstrap_py'] = bootstrap_py
's avatar
committed
3261

3262
normalize_bang = (
's avatar
committed
3263 3264
    re.compile(re.escape('#!'+
                         zc.buildout.easy_install._safe_arg(sys.executable))),
Jim Fulton's avatar
Jim Fulton committed
3265
    '#!/usr/local/bin/python2.7',
3266 3267
    )

Jim Fulton's avatar
Jim Fulton committed
3268 3269 3270 3271 3272
normalize_S = (
    re.compile(r'#!/usr/local/bin/python2.7 -S'),
    '#!/usr/local/bin/python2.7',
    )

3273
def test_suite():
3274
    test_suite = [
3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285
        manuel.testing.TestSuite(
            manuel.doctest.Manuel() + manuel.capture.Manuel(),
            'configparser.test'),
        manuel.testing.TestSuite(
            manuel.doctest.Manuel(
                checker=renormalizing.RENormalizing([
                    zc.buildout.testing.normalize_path,
                    zc.buildout.testing.normalize_endings,
                    zc.buildout.testing.normalize_script,
                    zc.buildout.testing.normalize_egg_py,
                    zc.buildout.testing.not_found,
3286
                    zc.buildout.testing.adding_find_link,
3287
                    # (re.compile(r"Installing 'zc.buildout >=\S+"), ''),
3288 3289 3290 3291
                    (re.compile('__buildout_signature__ = recipes-\S+'),
                     '__buildout_signature__ = recipes-SSSSSSSSSSS'),
                    (re.compile('executable = [\S ]+python\S*', re.I),
                     'executable = python'),
3292
                    (re.compile('[-d]  (setuptools|setuptools)-\S+[.]egg'),
3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307
                     'setuptools.egg'),
                    (re.compile('zc.buildout(-\S+)?[.]egg(-link)?'),
                     'zc.buildout.egg'),
                    (re.compile('creating \S*setup.cfg'), 'creating setup.cfg'),
                    (re.compile('hello\%ssetup' % os.path.sep), 'hello/setup'),
                    (re.compile('Picked: (\S+) = \S+'),
                     'Picked: \\1 = V.V'),
                    (re.compile(r'We have a develop egg: zc.buildout (\S+)'),
                     'We have a develop egg: zc.buildout X.X.'),
                    (re.compile(r'\\[\\]?'), '/'),
                    (re.compile('WindowsError'), 'OSError'),
                    (re.compile(r'\[Error \d+\] Cannot create a file '
                                r'when that file already exists: '),
                     '[Errno 17] File exists: '
                     ),
3308
                    (re.compile('setuptools'), 'setuptools'),
3309
                    (re.compile('Got zc.recipe.egg \S+'), 'Got zc.recipe.egg'),
3310 3311
                    (re.compile(r'zc\.(buildout|recipe\.egg)\s*= >=\S+'),
                     'zc.\1 = >=1.99'),
3312 3313
                    ])
                ) + manuel.capture.Manuel(),
Jim Fulton's avatar
Jim Fulton committed
3314
            'buildout.txt', 'meta-recipes.txt',
3315 3316 3317 3318 3319
            setUp=buildout_txt_setup,
            tearDown=zc.buildout.testing.buildoutTearDown,
            ),
        doctest.DocFileSuite(
            'runsetup.txt', 'repeatable.txt', 'setup.txt',
Jim Fulton's avatar
Jim Fulton committed
3320
            setUp=zc.buildout.testing.buildoutSetUp,
3321
            tearDown=zc.buildout.testing.buildoutTearDown,
Jim Fulton's avatar
Jim Fulton committed
3322
            checker=renormalizing.RENormalizing([
3323
               zc.buildout.testing.normalize_path,
3324
               zc.buildout.testing.normalize_endings,
3325 3326
               zc.buildout.testing.normalize_script,
               zc.buildout.testing.normalize_egg_py,
3327
               zc.buildout.testing.not_found,
3328
               zc.buildout.testing.adding_find_link,
3329 3330 3331
               # (re.compile(r"Installing 'zc.buildout >=\S+"), ''),
               # (re.compile(r"Getting distribution for 'zc.buildout >=\S+"),
               #  ''),
Jim Fulton's avatar
Jim Fulton committed
3332 3333
               (re.compile('__buildout_signature__ = recipes-\S+'),
                '__buildout_signature__ = recipes-SSSSSSSSSSS'),
3334
               (re.compile('[-d]  setuptools-\S+[.]egg'), 'setuptools.egg'),
Jim Fulton's avatar
Jim Fulton committed
3335 3336
               (re.compile('zc.buildout(-\S+)?[.]egg(-link)?'),
                'zc.buildout.egg'),
3337
               (re.compile('creating \S*setup.cfg'), 'creating setup.cfg'),
Jim Fulton's avatar
Jim Fulton committed
3338
               (re.compile('hello\%ssetup' % os.path.sep), 'hello/setup'),
3339 3340 3341 3342
               (re.compile('Picked: (\S+) = \S+'),
                'Picked: \\1 = V.V'),
               (re.compile(r'We have a develop egg: zc.buildout (\S+)'),
                'We have a develop egg: zc.buildout X.X.'),
3343
               (re.compile(r'\\[\\]?'), '/'),
3344
               (re.compile('WindowsError'), 'OSError'),
3345
               (re.compile('setuptools = \S+'), 'setuptools = 0.7.99'),
3346 3347 3348 3349
               (re.compile(r'\[Error 17\] Cannot create a file '
                           r'when that file already exists: '),
                '[Errno 17] File exists: '
                ),
3350 3351
               (re.compile('executable = %s' % re.escape(sys.executable)),
                'executable = python'),
3352 3353 3354
               (re.compile(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{6}'),
                'YYYY-MM-DD hh:mm:ss.dddddd'),
               ]),
3355
            ),
3356 3357 3358 3359 3360
        doctest.DocFileSuite(
            'debugging.txt',
            setUp=zc.buildout.testing.buildoutSetUp,
            tearDown=zc.buildout.testing.buildoutTearDown,
            checker=renormalizing.RENormalizing([
Jim Fulton's avatar
Jim Fulton committed
3361 3362 3363
                zc.buildout.testing.normalize_path,
                zc.buildout.testing.normalize_endings,
                zc.buildout.testing.normalize_exception_type_for_python_2_and_3,
3364
                zc.buildout.testing.not_found,
3365
                zc.buildout.testing.adding_find_link,
Jim Fulton's avatar
Jim Fulton committed
3366 3367 3368 3369 3370 3371
                (re.compile('zc.buildout.buildout.MissingOption'),
                 'MissingOption'),
                (re.compile(r'\S+buildout.py'), 'buildout.py'),
                (re.compile(r'line \d+'), 'line NNN'),
                (re.compile(r'py\(\d+\)'), 'py(NNN)'),
                ])
3372
            ),
3373 3374 3375 3376 3377 3378

        doctest.DocFileSuite(
            'update.txt',
            setUp=updateSetup,
            tearDown=zc.buildout.testing.buildoutTearDown,
            checker=renormalizing.RENormalizing([
3379
                (re.compile(r'(zc.buildout|setuptools)-\d+[.]\d+\S*'
Jim Fulton's avatar
Jim Fulton committed
3380 3381 3382 3383 3384 3385
                            '-py\d.\d.egg'),
                 '\\1.egg'),
                zc.buildout.testing.normalize_path,
                zc.buildout.testing.normalize_endings,
                zc.buildout.testing.normalize_script,
                zc.buildout.testing.normalize_egg_py,
3386
                zc.buildout.testing.not_found,
3387
                zc.buildout.testing.adding_find_link,
Jim Fulton's avatar
Jim Fulton committed
3388 3389
                normalize_bang,
                normalize_S,
3390
                # (re.compile(r"Installing 'zc.buildout >=\S+"), ''),
3391 3392
                (re.compile(r"Getting distribution for 'zc.buildout>=\S+"),
                 ''),
Jim Fulton's avatar
Jim Fulton committed
3393 3394
                (re.compile('99[.]99'), 'NINETYNINE.NINETYNINE'),
                (re.compile(
3395
                    r'(zc.buildout|setuptools)( version)? \d+[.]\d+\S*'),
Jim Fulton's avatar
Jim Fulton committed
3396
                 '\\1 V.V'),
3397
                (re.compile('[-d]  setuptools'), '-  setuptools'),
3398
                (re.compile(re.escape(os.path.sep)+'+'), '/'),
3399 3400
               ])
            ),
3401

Jim Fulton's avatar
Jim Fulton committed
3402
        doctest.DocFileSuite(
3403
            'easy_install.txt', 'downloadcache.txt', 'dependencylinks.txt',
Jim Fulton's avatar
Jim Fulton committed
3404
            'allowhosts.txt',
3405 3406
            setUp=easy_install_SetUp,
            tearDown=zc.buildout.testing.buildoutTearDown,
3407
            checker=renormalizing.RENormalizing([
3408
                zc.buildout.testing.normalize_script,
Jim Fulton's avatar
Jim Fulton committed
3409 3410 3411
                zc.buildout.testing.normalize_path,
                zc.buildout.testing.normalize_endings,
                zc.buildout.testing.normalize_egg_py,
3412
                zc.buildout.testing.normalize_exception_type_for_python_2_and_3,
3413
                zc.buildout.testing.adding_find_link,
3414
                zc.buildout.testing.not_found,
Jim Fulton's avatar
Jim Fulton committed
3415 3416
                normalize_bang,
                normalize_S,
3417
                (re.compile('[-d]  setuptools-\S+[.]egg'), 'setuptools.egg'),
Jim Fulton's avatar
Jim Fulton committed
3418
                (re.compile(r'\\[\\]?'), '/'),
3419 3420
                (re.compile('(\n?)-  ([a-zA-Z_.-]+)\n-  \\2.exe\n'),
                 '\\1-  \\2\n'),
Jim Fulton's avatar
Jim Fulton committed
3421
                ]+(sys.version_info < (2, 5) and [
3422 3423 3424 3425
                  (re.compile('.*No module named runpy.*', re.S), ''),
                  (re.compile('.*usage: pdb.py scriptfile .*', re.S), ''),
                  (re.compile('.*Error: what does not exist.*', re.S), ''),
                  ] or [])),
3426 3427


Jim Fulton's avatar
Jim Fulton committed
3428
            ),
3429 3430 3431 3432 3433 3434 3435

        doctest.DocFileSuite(
            'download.txt', 'extends-cache.txt',
            setUp=easy_install_SetUp,
            tearDown=zc.buildout.testing.buildoutTearDown,
            optionflags=doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS,
            checker=renormalizing.RENormalizing([
3436 3437
              zc.buildout.testing.normalize_exception_type_for_python_2_and_3,
              zc.buildout.testing.not_found,
3438
              zc.buildout.testing.adding_find_link,
3439 3440 3441 3442 3443
              (re.compile(' at -?0x[^>]+'), '<MEM ADDRESS>'),
              (re.compile('http://localhost:[0-9]{4,5}/'),
               'http://localhost/'),
              (re.compile('[0-9a-f]{32}'), '<MD5 CHECKSUM>'),
              zc.buildout.testing.normalize_path,
3444
              zc.buildout.testing.ignore_not_upgrading,
3445
              ]),
3446 3447
            ),

3448
        doctest.DocTestSuite(
3449
            setUp=easy_install_SetUp,
3450
            tearDown=zc.buildout.testing.buildoutTearDown,
3451
            checker=renormalizing.RENormalizing([
Jim Fulton's avatar
Jim Fulton committed
3452 3453 3454 3455 3456
                zc.buildout.testing.normalize_path,
                zc.buildout.testing.normalize_endings,
                zc.buildout.testing.normalize_script,
                zc.buildout.testing.normalize_egg_py,
                zc.buildout.testing.normalize___pycache__,
3457
                zc.buildout.testing.not_found,
3458
                zc.buildout.testing.normalize_exception_type_for_python_2_and_3,
3459
                zc.buildout.testing.adding_find_link,
3460
                normalize_bang,
Jim Fulton's avatar
Jim Fulton committed
3461 3462 3463
                (re.compile(r'^(\w+\.)*(Missing\w+: )'), '\2'),
                (re.compile("buildout: Running \S*setup.py"),
                 'buildout: Running setup.py'),
3464 3465
                (re.compile('setuptools-\S+-'),
                 'setuptools.egg'),
Jim Fulton's avatar
Jim Fulton committed
3466 3467
                (re.compile('zc.buildout-\S+-'),
                 'zc.buildout.egg'),
3468
                (re.compile('setuptools = \S+'), 'setuptools = 0.7.99'),
Jim Fulton's avatar
Jim Fulton committed
3469 3470 3471 3472
                (re.compile('File "\S+one.py"'),
                 'File "one.py"'),
                (re.compile(r'We have a develop egg: (\S+) (\S+)'),
                 r'We have a develop egg: \1 V'),
3473 3474
                (re.compile('Picked: setuptools = \S+'),
                 'Picked: setuptools = V'),
3475
                (re.compile('[-d]  setuptools'), '-  setuptools'),
Jim Fulton's avatar
Jim Fulton committed
3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486
                (re.compile(r'\\[\\]?'), '/'),
                (re.compile(
                    '-q develop -mxN -d "/sample-buildout/develop-eggs'),
                 '-q develop -mxN -d /sample-buildout/develop-eggs'
                 ),
                (re.compile(r'^[*]...'), '...'),
                # for
                # bug_92891
                # bootstrap_crashes_with_egg_recipe_in_buildout_section
                (re.compile(r"Unused options for buildout: 'eggs' 'scripts'\."),
                 "Unused options for buildout: 'scripts' 'eggs'."),
3487 3488
                # Python 3.4 changed the wording of NameErrors
                (re.compile('NameError: global name'), 'NameError: name'),
Jim Fulton's avatar
Jim Fulton committed
3489
                ]),
3490
            ),
3491
        zc.buildout.rmtree.test_suite(),
3492 3493 3494 3495 3496 3497
        doctest.DocFileSuite(
            'windows.txt',
            setUp=zc.buildout.testing.buildoutSetUp,
            tearDown=zc.buildout.testing.buildoutTearDown,
            checker=renormalizing.RENormalizing([
               zc.buildout.testing.normalize_path,
3498
               zc.buildout.testing.normalize_endings,
3499 3500
               zc.buildout.testing.normalize_script,
               zc.buildout.testing.normalize_egg_py,
3501
               zc.buildout.testing.not_found,
3502
               zc.buildout.testing.adding_find_link,
3503 3504
               (re.compile('__buildout_signature__ = recipes-\S+'),
                '__buildout_signature__ = recipes-SSSSSSSSSSS'),
3505
               (re.compile('[-d]  setuptools-\S+[.]egg'), 'setuptools.egg'),
3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520
               (re.compile('zc.buildout(-\S+)?[.]egg(-link)?'),
                'zc.buildout.egg'),
               (re.compile('creating \S*setup.cfg'), 'creating setup.cfg'),
               (re.compile('hello\%ssetup' % os.path.sep), 'hello/setup'),
               (re.compile('Picked: (\S+) = \S+'),
                'Picked: \\1 = V.V'),
               (re.compile(r'We have a develop egg: zc.buildout (\S+)'),
                'We have a develop egg: zc.buildout X.X.'),
               (re.compile(r'\\[\\]?'), '/'),
               (re.compile('WindowsError'), 'OSError'),
               (re.compile(r'\[Error 17\] Cannot create a file '
                           r'when that file already exists: '),
                '[Errno 17] File exists: '
                ),
               ])
3521 3522 3523
            ),
        doctest.DocFileSuite(
            'testing_bugfix.txt'),
3524 3525
    ]

3526
    # adding bootstrap.txt doctest to the suite
3527 3528 3529
    # only if bootstrap.py is present
    if os.path.exists(bootstrap_py):
        test_suite.append(doctest.DocFileSuite(
Jim Fulton's avatar
Jim Fulton committed
3530
            'bootstrap.txt', 'bootstrap_cl_settings.test',
Gary Poster's avatar
Gary Poster committed
3531
            setUp=bootstrapSetup,
3532 3533 3534
            tearDown=zc.buildout.testing.buildoutTearDown,
            checker=renormalizing.RENormalizing([
               zc.buildout.testing.normalize_path,
3535
               zc.buildout.testing.normalize_endings,
3536
               zc.buildout.testing.normalize_script,
3537
               zc.buildout.testing.not_found,
3538
               normalize_bang,
3539
               zc.buildout.testing.adding_find_link,
3540
               (re.compile('Downloading.*setuptools.*egg\n'), ''),
3541 3542 3543 3544
               ]),
            ))

    return unittest.TestSuite(test_suite)