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

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

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

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

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

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

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

    """

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

and all parts have to have a specified recipe:


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

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

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

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():
    """
277 278
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.
279

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

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

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

    >>> 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
303
    >>> print_(system(buildout), end='')
304 305 306 307 308
    Develop: '/sample-buildout/sampley'
    Develop: '/sample-buildout/samplez'
    Installing eggs.
    Getting distribution for 'demoneeded==1.1'.
    Got demoneeded 1.1.
309
    While:
310
      Installing eggs.
311 312
    Error: There is a version conflict.
    We already have: demoneeded 1.1
313
    but sampley 1 requires 'demoneeded==1.0'.
314

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

318
    >>> make_dist_that_requires(sample_buildout, 'samplea', ['sampleb'])
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    >>> 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())

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

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

365
def version_conflict_rendering():
Reinout van Rees's avatar
Reinout van Rees committed
366 367 368
    """

We use the arguments passed by pkg_resources.VersionConflict to construct a
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
nice error message:

    >>> error = pkg_resources.VersionConflict('pkg1 2.1', 'pkg1 1.0')
    >>> ws = []  # Not relevant for this test
    >>> print_(zc.buildout.easy_install.VersionConflict(
    ...     error, ws)) # doctest: +ELLIPSIS
    There is a version conflict...

But sometimes pkg_resources passes a nicely formatted string itself already.
Extracting the original arguments fails in that case, so we just show the string.

    >>> error = pkg_resources.VersionConflict('pkg1 2.1 is simply wrong')
    >>> ws = []  # Not relevant for this test
    >>> print_(zc.buildout.easy_install.VersionConflict(
    ...     error, ws)) # doctest: +ELLIPSIS
    There is a version conflict.
    pkg1 2.1 is simply wrong

    """

389 390 391
def show_who_requires_missing_distributions():
    """

392 393 394 395
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
396

397 398
    >>> make_dist_that_requires(sample_buildout, 'sampley', ['demoneeded'])
    >>> make_dist_that_requires(sample_buildout, 'samplea', ['sampleb'])
399 400 401 402 403 404 405 406 407 408 409 410 411
    >>> 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
    ... ''')

412 413 414 415 416 417 418 419 420 421 422 423 424 425
    >>> 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'.
    ...
426
    While:
427 428 429
      Installing eggs.
      Getting distribution for 'demoneeded'.
    Error: Couldn't find a distribution for 'demoneeded'.
430
    """
431

432 433 434 435 436 437 438
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:

439
    >>> make_dist_that_requires(sample_buildout, 'sampley', ['setuptools'])
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
    >>> 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
464
    setuptools = 0.7
465 466
    """

467 468
def test_comparing_saved_options_with_funny_characters():
    """
469 470
If an option has newlines, extra/odd spaces or a %, we need to make sure
the comparison with the saved value works correctly.
471 472

    >>> mkdir(sample_buildout, 'recipes')
473
    >>> write(sample_buildout, 'recipes', 'debug.py',
474 475 476 477 478 479 480 481 482
    ... '''
    ... class Debug:
    ...     def __init__(self, buildout, name, options):
    ...         options['debug'] = \"\"\"  <zodb>
    ...
    ...   <filestorage>
    ...     path foo
    ...   </filestorage>
    ...
483
    ... </zodb>
484
    ...      \"\"\"
485 486 487 488 489 490 491
    ...         options['debug1'] = \"\"\"
    ... <zodb>
    ...
    ...   <filestorage>
    ...     path foo
    ...   </filestorage>
    ...
492
    ... </zodb>
493
    ... \"\"\"
494 495 496 497 498 499 500
    ...         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
501 502
    ...
    ...     update = install
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    ... ''')


    >>> 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
530
    >>> print_(system(buildout), end='')
531 532
    Develop: '/sample-buildout/recipes'
    Installing debug.
533 534 535 536

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
537
    >>> print_(system(buildout), end='')
538 539
    Develop: '/sample-buildout/recipes'
    Updating debug.
540 541
"""

Jim Fulton's avatar
Jim Fulton committed
542 543 544 545 546 547 548 549 550 551
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=[''],
552
    ...    zip_safe=False, version='1.0', author='bob', url='bob',
Jim Fulton's avatar
Jim Fulton committed
553 554 555 556 557 558 559 560 561 562 563
    ...    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(
564
    ...     ['demo'], d1, links=[join(src, 'dist')],
Jim Fulton's avatar
Jim Fulton committed
565 566 567 568 569 570 571 572 573
    ...     )

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

Then try to install it again:

    >>> d2 = tmpdir('d2')
    >>> ws = zc.buildout.easy_install.install(
574
    ...     ['demo'], d2, links=[d1],
Jim Fulton's avatar
Jim Fulton committed
575 576 577 578 579 580 581
    ...     )

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

    """

Jim Fulton's avatar
Jim Fulton committed
582 583 584 585 586 587 588 589 590
def create_sections_on_command_line():
    """
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts =
    ... x = ${foo:bar}
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
591
    >>> print_(system(buildout + ' foo:bar=1 -vv'), end='')
592
    ...        # doctest: +ELLIPSIS
593
    Installing 'zc.buildout', 'setuptools'.
Jim Fulton's avatar
Jim Fulton committed
594 595 596 597
    ...
    [foo]
    bar = 1
    ...
598

Jim Fulton's avatar
Jim Fulton committed
599 600
    """

601 602
def test_help():
    """
603
    >>> print_(system(os.path.join(sample_buildout, 'bin', 'buildout')+' -h'))
604 605 606 607 608 609 610 611
    ... # doctest: +ELLIPSIS
    Usage: buildout [options] [assignments] [command [command arguments]]
    <BLANKLINE>
    Options:
    <BLANKLINE>
      -h, --help
    ...

612 613
    >>> print_(system(os.path.join(sample_buildout, 'bin', 'buildout')
    ...              +' --help'))
614 615 616 617 618 619 620
    ... # doctest: +ELLIPSIS
    Usage: buildout [options] [assignments] [command [command arguments]]
    <BLANKLINE>
    Options:
    <BLANKLINE>
      -h, --help
    ...
621
    """
622

Marius Gedminas's avatar
Marius Gedminas committed
623 624 625 626 627 628 629 630 631
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
632 633 634 635 636 637
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.

638
    >>> d = tmpdir('sample-bootstrap')
639

Jim Fulton's avatar
Jim Fulton committed
640 641 642 643
    >>> write(d, 'buildout.cfg',
    ... '''
    ... [buildout]
    ... extensions = some_awsome_extension
644
    ... parts =
Jim Fulton's avatar
Jim Fulton committed
645 646 647
    ... ''')

    >>> os.chdir(d)
648
    >>> print_(system(os.path.join(sample_buildout, 'bin', 'buildout')
Jim Fulton's avatar
Jim Fulton committed
649
    ...              + ' bootstrap'), end='')
650 651 652 653 654
    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
655
    """
656

657 658 659 660

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

662 663 664 665 666 667 668 669 670 671 672 673 674
    >>> write(d, 'buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = buildout
    ... eggs-directory = eggs
    ...
    ... [buildout]
    ... recipe = zc.recipe.egg
    ... eggs = zc.buildout
    ... scripts = buildout=buildout
    ... ''')

    >>> os.chdir(d)
675
    >>> print_(system(os.path.join(sample_buildout, 'bin', 'buildout')
Jim Fulton's avatar
Jim Fulton committed
676
    ...              + ' bootstrap'), end='')
677 678 679 680 681
    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'.
682

Jim Fulton's avatar
Jim Fulton committed
683
    >>> print_(system(os.path.join('bin', 'buildout')), end='')
684
    Unused options for buildout: 'scripts' 'eggs'.
685 686 687

    """

688 689 690
def removing_eggs_from_develop_section_causes_egg_link_to_be_removed():
    '''
    >>> cd(sample_buildout)
691

692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
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
707
    >>> print_(system(join('bin', 'buildout')), end='')
708
    Develop: '/sample-buildout/foo'
709 710 711

    >>> ls('develop-eggs')
    -  foox.egg-link
712
    -  zc.recipe.egg.egg-link
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728

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
729
    >>> print_(system(join('bin', 'buildout')), end='')
730 731
    Develop: '/sample-buildout/foo'
    Develop: '/sample-buildout/bar'
732 733 734 735

    >>> ls('develop-eggs')
    -  foox.egg-link
    -  fooy.egg-link
736
    -  zc.recipe.egg.egg-link
737 738 739 740 741 742 743 744 745

Remove one:

    >>> write('buildout.cfg',
    ... """
    ... [buildout]
    ... develop = bar
    ... parts =
    ... """)
Jim Fulton's avatar
Jim Fulton committed
746
    >>> print_(system(join('bin', 'buildout')), end='')
747
    Develop: '/sample-buildout/bar'
748 749 750 751 752

It is gone

    >>> ls('develop-eggs')
    -  fooy.egg-link
753
    -  zc.recipe.egg.egg-link
754 755 756 757 758 759 760 761

Remove the other:

    >>> write('buildout.cfg',
    ... """
    ... [buildout]
    ... parts =
    ... """)
Jim Fulton's avatar
Jim Fulton committed
762
    >>> print_(system(join('bin', 'buildout')), end='')
763 764 765 766

All gone

    >>> ls('develop-eggs')
767
    -  zc.recipe.egg.egg-link
768 769 770
    '''


771
def add_setuptools_to_dependencies_when_namespace_packages():
772
    '''
773
Often, a package depends on setuptools solely by virtue of using
774
namespace packages. In this situation, package authors often forget to
775
declare setuptools as a dependency. This is a mistake, but,
776
unfortunately, a common one that we need to work around.  If an egg
777 778
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
779
a develop egg, we will also generate a warning.
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798

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

800 801 802 803
    >>> write('buildout.cfg',
    ... """
    ... [buildout]
    ... develop = foo
804
    ... parts =
805 806
    ... """)

Jim Fulton's avatar
Jim Fulton committed
807
    >>> print_(system(join('bin', 'buildout')), end='')
808
    Develop: '/sample-buildout/foo'
809 810

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

    >>> import logging, zope.testing.loggingsupport
    >>> handler = zope.testing.loggingsupport.InstalledHandler(
815 816
    ...        'zc.buildout.easy_install', level=logging.WARNING)
    >>> logging.getLogger('zc.buildout.easy_install').propagate = False
817

818 819 820 821 822 823
    >>> [dist.project_name
    ...  for dist in zc.buildout.easy_install.working_set(
    ...    ['foox'], sys.executable,
    ...    [join(sample_buildout, 'eggs'),
    ...     join(sample_buildout, 'develop-eggs'),
    ...     ])]
824
    ['foox', 'setuptools']
825

826
    >>> print_(handler)
827
    zc.buildout.easy_install WARNING
828
      Develop distribution: foox 0.0.0
829
    uses namespace packages but the distribution does not require setuptools.
830 831 832 833 834 835 836 837 838 839 840

    >>> 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')
841
    -  zc.recipe.egg.egg-link
842

843 844
    >>> ls('eggs') # doctest: +ELLIPSIS
    -  foox-0.0.0-py2.4.egg
845
    d  setuptools.eggpyN.N.egg
846
    ...
847

848
We do not get a warning, but we do get setuptools included in the working set:
849 850 851 852 853 854 855

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

Jim Fulton's avatar
Jim Fulton committed
858
    >>> print_(handler, end='')
859

pombredanne's avatar
pombredanne committed
860
We get the same behavior if the it is a dependency that uses a
Jim Fulton's avatar
Jim Fulton committed
861 862 863 864 865 866 867 868 869 870
namespace package.


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

Jim Fulton's avatar
Jim Fulton committed
872 873 874 875
    >>> write('buildout.cfg',
    ... """
    ... [buildout]
    ... develop = foo bar
876
    ... parts =
Jim Fulton's avatar
Jim Fulton committed
877 878
    ... """)

Jim Fulton's avatar
Jim Fulton committed
879
    >>> print_(system(join('bin', 'buildout')), end='')
880 881
    Develop: '/sample-buildout/foo'
    Develop: '/sample-buildout/bar'
Jim Fulton's avatar
Jim Fulton committed
882 883 884 885 886 887 888

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

Jim Fulton's avatar
Jim Fulton committed
891
    >>> print_(handler, end='')
Jim Fulton's avatar
Jim Fulton committed
892
    zc.buildout.easy_install WARNING
893
      Develop distribution: foox 0.0.0
894
    uses namespace packages but the distribution does not require setuptools.
Jim Fulton's avatar
Jim Fulton committed
895 896


897
    >>> logging.getLogger('zc.buildout.easy_install').propagate = True
898
    >>> handler.uninstall()
Jim Fulton's avatar
Jim Fulton committed
899

900
    '''
901 902 903

def develop_preserves_existing_setup_cfg():
    """
904

905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
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(
928
    ...   extdemo, dest,
929
    ...   {'include-dirs': os.path.join(sample_buildout, 'include')})
Jim Fulton's avatar
Jim Fulton committed
930
    '/dest/extdemo.egg-link'
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945

    >>> 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
946 947

def uninstall_recipes_used_for_removal():
948
    r"""
Jim Fulton's avatar
Jim Fulton committed
949 950 951 952 953 954 955 956 957 958 959 960 961 962
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",
963 964
    ... r'''
    ... import sys
Jim Fulton's avatar
Jim Fulton committed
965 966 967
    ... class Install:
    ...     def __init__(*args): pass
    ...     def install(self):
968
    ...         sys.stdout.write('installing\n')
Jim Fulton's avatar
Jim Fulton committed
969
    ...         return ()
970 971
    ... def uninstall(name, options):
    ...     sys.stdout.write('uninstalling\n')
Jim Fulton's avatar
Jim Fulton committed
972 973 974 975 976 977 978 979 980 981
    ... ''')

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

Jim Fulton's avatar
Jim Fulton committed
982
    >>> print_(system(join('bin', 'buildout')), end='')
983 984
    Develop: '/sample-buildout/recipes'
    Installing demo.
Jim Fulton's avatar
Jim Fulton committed
985 986 987 988 989 990 991 992 993 994 995 996
    installing


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

Jim Fulton's avatar
Jim Fulton committed
997
    >>> print_(system(join('bin', 'buildout')), end='')
998 999 1000
    Develop: '/sample-buildout/recipes'
    Uninstalling demo.
    Running uninstall recipe.
Jim Fulton's avatar
Jim Fulton committed
1001
    uninstalling
1002
    Installing demo.
Jim Fulton's avatar
Jim Fulton committed
1003 1004 1005 1006 1007 1008
    installing


    >>> write('buildout.cfg', '''
    ... [buildout]
    ... develop = recipes
1009
    ... parts =
Jim Fulton's avatar
Jim Fulton committed
1010 1011
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
1012
    >>> print_(system(join('bin', 'buildout')), end='')
1013 1014 1015
    Develop: '/sample-buildout/recipes'
    Uninstalling demo.
    Running uninstall recipe.
Jim Fulton's avatar
Jim Fulton committed
1016 1017 1018 1019
    uninstalling

"""

Jim Fulton's avatar
Jim Fulton committed
1020 1021 1022 1023
def extensions_installed_as_eggs_work_in_offline_mode():
    '''
    >>> mkdir('demo')

1024
    >>> write('demo', 'demo.py',
Jim Fulton's avatar
Jim Fulton committed
1025 1026 1027 1028
    ... r"""
    ... import sys
    ... def print_(*args):
    ...     sys.stdout.write(' '.join(map(str, args)) + '\\n')
Jim Fulton's avatar
Jim Fulton committed
1029
    ... def ext(buildout):
1030
    ...     print_('ext', sorted(buildout))
Jim Fulton's avatar
Jim Fulton committed
1031 1032 1033 1034 1035
    ... """)

    >>> write('demo', 'setup.py',
    ... """
    ... from setuptools import setup
1036
    ...
Jim Fulton's avatar
Jim Fulton committed
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
    ... 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
1055
    >>> print_(system(join(sample_buildout, 'bin', 'buildout')), end='')
1056
    ext ['buildout', 'versions']
1057

Jim Fulton's avatar
Jim Fulton committed
1058 1059 1060

    '''

Jim Fulton's avatar
Jim Fulton committed
1061 1062
def changes_in_svn_or_CVS_dont_affect_sig():
    """
1063

Jim Fulton's avatar
Jim Fulton committed
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
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
    ... ''')
1081

Jim Fulton's avatar
Jim Fulton committed
1082 1083 1084 1085 1086
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = recipe
    ... parts = foo
1087
    ...
Jim Fulton's avatar
Jim Fulton committed
1088 1089 1090 1091 1092
    ... [foo]
    ... recipe = recipe
    ... ''')


Jim Fulton's avatar
Jim Fulton committed
1093
    >>> print_(system(join(sample_buildout, 'bin', 'buildout')), end='')
1094 1095
    Develop: '/sample-buildout/recipe'
    Installing foo.
Jim Fulton's avatar
Jim Fulton committed
1096 1097 1098

    >>> mkdir('recipe', '.svn')
    >>> mkdir('recipe', 'CVS')
Jim Fulton's avatar
Jim Fulton committed
1099
    >>> print_(system(join(sample_buildout, 'bin', 'buildout')), end='')
1100 1101
    Develop: '/sample-buildout/recipe'
    Updating foo.
Jim Fulton's avatar
Jim Fulton committed
1102 1103 1104 1105

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

Jim Fulton's avatar
Jim Fulton committed
1106
    >>> print_(system(join(sample_buildout, 'bin', 'buildout')), end='')
1107 1108
    Develop: '/sample-buildout/recipe'
    Updating foo.
Jim Fulton's avatar
Jim Fulton committed
1109 1110 1111

    """

1112 1113 1114
if hasattr(os, 'symlink'):
    def bug_250537_broken_symlink_doesnt_affect_sig():
        """
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
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
    ... ''')
1133

1134 1135 1136 1137 1138
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = recipe
    ... parts = foo
1139
    ...
1140 1141 1142 1143 1144
    ... [foo]
    ... recipe = recipe
    ... ''')


Jim Fulton's avatar
Jim Fulton committed
1145
    >>> print_(system(join(sample_buildout, 'bin', 'buildout')), end='')
1146 1147 1148 1149
    Develop: '/sample-buildout/recipe'
    Installing foo.

    >>> write('recipe', 'some-file', '1')
1150
    >>> os.symlink(join('recipe', 'some-file'),
1151 1152 1153
    ...            join('recipe', 'another-file'))
    >>> remove('recipe', 'some-file')

Jim Fulton's avatar
Jim Fulton committed
1154
    >>> print_(system(join(sample_buildout, 'bin', 'buildout')), end='')
1155 1156 1157 1158 1159
    Develop: '/sample-buildout/recipe'
    Updating foo.

    """

1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
def unicode_filename_doesnt_break_hash():
    """
Buildout's _dir_hash() used to break on non-ascii filenames.

    >>> mkdir('héhé')
    >>> write('héhé', 'héhé.py',
    ... '''
    ... print('Example filename from pyramid tests')
    ... ''')
    >>> from zc.buildout.buildout import _dir_hash
    >>> _dir_hash('héhé')

    """

Jim Fulton's avatar
Jim Fulton committed
1174 1175
def o_option_sets_offline():
    """
Jim Fulton's avatar
Jim Fulton committed
1176
    >>> print_(system(join(sample_buildout, 'bin', 'buildout')+' -vvo'), end='')
Jim Fulton's avatar
Jim Fulton committed
1177 1178 1179 1180 1181 1182 1183
    ... # doctest: +ELLIPSIS
    <BLANKLINE>
    ...
    offline = true
    ...
    """

Jim Fulton's avatar
Jim Fulton committed
1184
def recipe_upgrade():
1185
    r"""
Jim Fulton's avatar
Jim Fulton committed
1186 1187 1188 1189 1190 1191 1192

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

Let's create a recipe egg

    >>> mkdir('recipe')
    >>> write('recipe', 'recipe.py',
1193 1194
    ... r'''
    ... import sys
Jim Fulton's avatar
Jim Fulton committed
1195 1196 1197
    ... class Recipe:
    ...     def __init__(*a): pass
    ...     def install(self):
1198
    ...         sys.stdout.write('recipe v1\n')
Jim Fulton's avatar
Jim Fulton committed
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
    ...         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', '')

1213
    >>> print_(system(buildout+' setup recipe bdist_egg')) # doctest: +ELLIPSIS
1214
    Running setup script 'recipe/setup.py'.
Jim Fulton's avatar
Jim Fulton committed
1215 1216
    ...

Jim Fulton's avatar
Jim Fulton committed
1217 1218
    >>> rmdir('recipe', 'build')

Jim Fulton's avatar
Jim Fulton committed
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
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
1231
    >>> print_(system(buildout), end='')
1232 1233 1234
    Getting distribution for 'recipe'.
    Got recipe 1.
    Installing foo.
Jim Fulton's avatar
Jim Fulton committed
1235 1236 1237 1238 1239
    recipe v1

Now, if we update the recipe egg:

    >>> write('recipe', 'recipe.py',
1240 1241
    ... r'''
    ... import sys
Jim Fulton's avatar
Jim Fulton committed
1242 1243 1244
    ... class Recipe:
    ...     def __init__(*a): pass
    ...     def install(self):
1245
    ...         sys.stdout.write('recipe v2\n')
Jim Fulton's avatar
Jim Fulton committed
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
    ...         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']},
    ...       )
    ... ''')


1259
    >>> print_(system(buildout+' setup recipe bdist_egg')) # doctest: +ELLIPSIS
1260
    Running setup script 'recipe/setup.py'.
Jim Fulton's avatar
Jim Fulton committed
1261 1262 1263 1264
    ...

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

Jim Fulton's avatar
Jim Fulton committed
1265
    >>> print_(system(buildout+' -N'), end='')
1266
    Updating foo.
Jim Fulton's avatar
Jim Fulton committed
1267 1268 1269 1270
    recipe v1

or if we use -o:

Jim Fulton's avatar
Jim Fulton committed
1271
    >>> print_(system(buildout+' -o'), end='')
1272
    Updating foo.
Jim Fulton's avatar
Jim Fulton committed
1273 1274 1275 1276
    recipe v1

But we will if we use neither of these:

Jim Fulton's avatar
Jim Fulton committed
1277
    >>> print_(system(buildout), end='')
1278 1279 1280 1281
    Getting distribution for 'recipe'.
    Got recipe 2.
    Uninstalling foo.
    Installing foo.
Jim Fulton's avatar
Jim Fulton committed
1282
    recipe v2
Jim Fulton's avatar
Jim Fulton committed
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295

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
1296
    >>> print_(system(buildout), end='')
1297 1298
    Uninstalling foo.
    Installing foo.
Jim Fulton's avatar
Jim Fulton committed
1299
    recipe v1
1300

Jim Fulton's avatar
Jim Fulton committed
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
    """

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
1345
    >>> print_(system(buildout), end='')
Jim Fulton's avatar
Jim Fulton committed
1346
    Develop: '/sample-buildout/recipe'
1347
    Installing foo.
Jim Fulton's avatar
Jim Fulton committed
1348

Jim Fulton's avatar
Jim Fulton committed
1349
    >>> print_(system(buildout), end='')
Jim Fulton's avatar
Jim Fulton committed
1350
    Develop: '/sample-buildout/recipe'
1351
    Updating foo.
Jim Fulton's avatar
Jim Fulton committed
1352 1353 1354 1355 1356

    >>> cat('.installed.cfg') # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
    [buildout]
    ...
    [foo]
1357 1358 1359
    __buildout_installed__ = a
    	b
    	c
Jim Fulton's avatar
Jim Fulton committed
1360 1361 1362 1363 1364 1365
    	d
    	e
    __buildout_signature__ = ...

"""

1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
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/')

1381
    >>> print_(handler) # doctest: +ELLIPSIS
1382
    zc.buildout.easy_install DEBUG
1383
      Installing 'demo==0.2'.
1384
    zc.buildout.easy_install DEBUG
1385
      We have no distributions for demo that satisfies 'demo==0.2'.
1386 1387 1388 1389
    ...

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

1391
    """
Jim Fulton's avatar
Jim Fulton committed
1392

1393 1394 1395 1396 1397
def internal_errors():
    """Internal errors are clearly marked and don't generate tracebacks:

    >>> mkdir(sample_buildout, 'recipes')

1398
    >>> write(sample_buildout, 'recipes', 'mkdir.py',
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
    ... '''
    ... 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
1427
    >>> print_(system(buildout), end='') # doctest: +ELLIPSIS
1428
    Develop: '/sample-buildout/recipes'
1429
    While:
1430 1431
      Installing.
      Getting section data-dir.
Jim Fulton's avatar
Jim Fulton committed
1432
      Initializing section data-dir.
1433
    <BLANKLINE>
pombredanne's avatar
pombredanne committed
1434
    An internal error occurred due to a bug in either zc.buildout or in a
1435
    recipe being used:
1436 1437 1438
    Traceback (most recent call last):
    ...
    NameError: global name 'os' is not defined
1439
    """
1440

1441 1442 1443
def whine_about_unused_options():
    '''

1444
    >>> write('foo.py',
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
    ... """
    ... 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
1479
    >>> print_(system(buildout), end='')
Jim Fulton's avatar
Jim Fulton committed
1480
    Develop: '/sample-buildout/.'
1481 1482 1483
    Unused options for buildout: 'a'.
    Installing foo.
    Unused options for foo: 'z'.
1484
    '''
1485

1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
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
1547
    >>> print_(system(buildout), end='')
1548 1549 1550 1551
    Develop: '/sample-buildout/recipes'
    Installing p1.
    Installing p2.
    Installing p3.
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.
    Installing p3.
1558

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

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
1585
    >>> print_(system(buildout), end='')
1586 1587 1588 1589 1590
    Develop: '/sample-buildout/recipes'
    Installing p1.
    Installing p2.
    Installing p3.
    Installing p4.
1591

Jim Fulton's avatar
Jim Fulton committed
1592
    >>> print_(system(buildout), end='')
1593 1594 1595 1596
    Develop: '/sample-buildout/recipes'
    Updating p1.
    Updating p2.
    Updating p3.
1597

Jim Fulton's avatar
Jim Fulton committed
1598
    >>> print_(system(buildout+' buildout:parts='), end='')
1599 1600 1601 1602 1603
    Develop: '/sample-buildout/recipes'
    Uninstalling p2.
    Uninstalling p1.
    Uninstalling p4.
    Uninstalling p3.
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625

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
1626
    >>> print_(system(buildout), end='')
1627 1628 1629 1630 1631
    Develop: '/sample-buildout/recipes'
    Installing p1.
    Installing p2.
    Installing p3.
    Installing p4.
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652

    >>> 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
1653
    >>> print_(system(buildout), end='')
1654 1655 1656
    Develop: '/sample-buildout/recipes'
    Uninstalling p4.
    Updating p1.
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676

    >>> 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
1677
    >>> print_(system(buildout), end='')
1678 1679 1680 1681 1682 1683
    Develop: '/sample-buildout/recipes'
    Uninstalling p1.
    Installing p1.
    Updating p2.
    Updating p3.
    Installing p4.
1684 1685

    """
1686 1687

def install_source_dist_with_bad_py():
1688
    r"""
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709

    >>> 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
1710
    >>> print_(system(buildout+' setup badegg sdist'), end='') # doctest: +ELLIPSIS
1711
    Running setup script 'badegg/setup.py'.
1712
    ...
1713

1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
    >>> 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())

1732
    >>> print_(system(buildout));print_('X') # doctest: +ELLIPSIS
1733 1734
    Installing eggs.
    Getting distribution for 'badegg'.
1735 1736
    Got badegg 1.
    Installing bo.
1737 1738 1739 1740 1741
    ...
    SyntaxError: ...'return' outside function...
    ...
    SyntaxError: ...'return' outside function...
    ...
1742

1743
    >>> ls('eggs') # doctest: +ELLIPSIS
1744
    d  badegg-1-py2.4.egg
1745
    ...
1746

1747 1748 1749 1750
    >>> ls('bin')
    -  bo
    -  buildout
    """
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763

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(
1764
    ...   'extdemo ==1.4', dest,
1765 1766 1767 1768 1769 1770
    ...   {'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']

    '''
1771

1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
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
1794
    >>> print_(system(buildout), end='')
1795 1796
    Installing x.
    Getting distribution for 'demo'.
Jim Fulton's avatar
Jim Fulton committed
1797
    Got demo 0.3.
1798
    Getting distribution for 'demoneeded'.
Jim Fulton's avatar
Jim Fulton committed
1799
    Got demoneeded 1.1.
1800
    Generated script '/sample-buildout/bin/demo'.
1801

Jim Fulton's avatar
Jim Fulton committed
1802
    >>> print_(system(join('bin', 'demo')), end='')
Jim Fulton's avatar
Jim Fulton committed
1803
    3 1
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814

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

Jim Fulton's avatar
Jim Fulton committed
1816
    >>> print_(system(buildout), end='')
1817 1818 1819 1820 1821
    Uninstalling x.
    Installing x.
    Getting distribution for 'demo==0.1'.
    Got demo 0.1.
    Generated script '/sample-buildout/bin/demo'.
1822

Jim Fulton's avatar
Jim Fulton committed
1823
    >>> print_(system(join('bin', 'demo')), end='')
Jim Fulton's avatar
Jim Fulton committed
1824
    1 1
1825 1826
    """

1827 1828 1829 1830 1831 1832 1833 1834
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()
1835 1836
        ...     print_(p.stdout.read().decode())
        ...     print_('Exit:', bool(p.wait()))
1837

1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
        >>> call(buildout)
        <BLANKLINE>
        Exit: False

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

1848
        >>> call(buildout) # doctest: +NORMALIZE_WHITESPACE
1849
        While:
1850 1851
          Installing.
          Getting section x.
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877
        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
        ... ''')

1878
        >>> call(buildout) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
1879
        Develop: '/sample-buildout/.'
1880
        While:
1881 1882 1883 1884
          Installing.
          Getting section x.
          Initializing section x.
          Loading zc.buildout recipe entry zc.buildout.testexit:default.
1885
        <BLANKLINE>
pombredanne's avatar
pombredanne committed
1886
        An internal error occurred due to a bug in either zc.buildout or in a
1887
        recipe being used:
1888 1889 1890 1891 1892
        Traceback (most recent call last):
        ...
             x y
               ^
         SyntaxError: invalid syntax
1893 1894 1895 1896
        <BLANKLINE>
        Exit: True
        """

1897
def bug_59270_recipes_always_start_in_buildout_dir():
1898
    r"""
1899 1900 1901 1902
    Recipes can rely on running from buildout directory

    >>> mkdir('bad_start')
    >>> write('bad_recipe.py',
1903 1904
    ... r'''
    ... import os, sys
Jim Fulton's avatar
Jim Fulton committed
1905
    ... def print_(*args):
Jim Fulton's avatar
Jim Fulton committed
1906
    ...     sys.stdout.write(' '.join(map(str, args)) + '\n')
1907 1908
    ... class Bad:
    ...     def __init__(self, *_):
Jim Fulton's avatar
Jim Fulton committed
1909
    ...         print_(os.getcwd())
1910
    ...     def install(self):
1911
    ...         sys.stdout.write(os.getcwd()+'\n')
1912
    ...         os.chdir('bad_start')
1913
    ...         sys.stdout.write(os.getcwd()+'\n')
1914 1915 1916 1917 1918 1919 1920 1921 1922
    ...         return ()
    ... ''')

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

1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = .
    ... parts = b1 b2
    ... [b1]
    ... recipe = bad.test
    ... [b2]
    ... recipe = bad.test
    ... ''')

    >>> os.chdir('bad_start')
1936
    >>> print_(system(join(sample_buildout, 'bin', 'buildout')
Jim Fulton's avatar
Jim Fulton committed
1937
    ...              +' -c '+join(sample_buildout, 'buildout.cfg')), end='')
Jim Fulton's avatar
Jim Fulton committed
1938
    Develop: '/sample-buildout/.'
1939 1940
    /sample-buildout
    /sample-buildout
1941
    Installing b1.
1942 1943
    /sample-buildout
    /sample-buildout/bad_start
1944
    Installing b2.
1945 1946 1947 1948
    /sample-buildout
    /sample-buildout/bad_start
    """

1949 1950
def bug_61890_file_urls_dont_seem_to_work_in_find_dash_links():
    """
1951

1952
    This bug arises from the fact that setuptools is overly restrictive
1953 1954 1955 1956 1957
    about file urls, requiring that file urls pointing at directories
    must end in a slash.

    >>> dest = tmpdir('sample-install')
    >>> import zc.buildout.easy_install
1958
    >>> sample_eggs = sample_eggs.replace(os.path.sep, '/')
1959 1960 1961 1962 1963 1964
    >>> ws = zc.buildout.easy_install.install(
    ...     ['demo==0.2'], dest,
    ...     links=['file://'+sample_eggs], index=link_server+'index/')


    >>> for dist in ws:
1965
    ...     print_(dist)
1966 1967 1968 1969
    demo 0.2
    demoneeded 1.1

    >>> ls(dest)
Jim Fulton's avatar
Jim Fulton committed
1970
    d  demo-0.2-py2.4.egg
Jim Fulton's avatar
Jim Fulton committed
1971
    d  demoneeded-1.1-py2.4.egg
1972

1973 1974 1975 1976 1977
    """

def bug_75607_buildout_should_not_run_if_it_creates_an_empty_buildout_cfg():
    """
    >>> remove('buildout.cfg')
Jim Fulton's avatar
Jim Fulton committed
1978
    >>> print_(system(buildout), end='')
1979
    While:
1980
      Initializing.
1981 1982 1983
    Error: Couldn't open /sample-buildout/buildout.cfg


1984

1985
    """
1986

1987 1988
def dealing_with_extremely_insane_dependencies():
    r"""
1989

1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
    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]
2011
    ... develop = pack0 pack1 pack2 pack3 pack4
2012 2013 2014 2015 2016 2017 2018
    ... parts = pack1
    ...
    ... [pack1]
    ... recipe = zc.recipe.egg:eggs
    ... eggs = pack0
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
2019
    >>> print_(system(buildout), end='') # doctest: +ELLIPSIS
2020 2021 2022 2023 2024 2025
    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
2026
    ...
2027 2028 2029 2030 2031 2032 2033
    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
2034
    >>> print_(system(buildout+' -v'), end='') # doctest: +ELLIPSIS
2035
    Installing 'zc.buildout', 'setuptools'.
2036
    We have a develop egg: zc.buildout 1.0.0
2037 2038
    We have the best distribution that satisfies 'setuptools'.
    Picked: setuptools = 0.7
2039 2040 2041 2042 2043
    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
2044
    ...Installing pack1.
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
    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
2068
    ...
2069 2070 2071 2072 2073 2074
    While:
      Installing pack1.
      Getting distribution for 'pack5'.
    Error: Couldn't find a distribution for 'pack5'.
    """

2075
def read_find_links_to_load_extensions():
Jim Fulton's avatar
Jim Fulton committed
2076 2077
    r"""
We'll create a wacky buildout extension that just announces itself when used:
2078 2079 2080 2081

    >>> src = tmpdir('src')
    >>> write(src, 'wacky_handler.py',
    ... '''
Jim Fulton's avatar
Jim Fulton committed
2082
    ... import sys
2083
    ... def install(buildout=None):
Jim Fulton's avatar
Jim Fulton committed
2084
    ...     sys.stdout.write("I am a wacky extension\\n")
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
    ... ''')
    >>> 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
2096
    >>> print_(system(buildout+' setup '+src+' bdist_egg'), end='')
2097 2098 2099 2100 2101 2102 2103
    ... # 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')
2104
    >>> dist = 'file://' + join(src, 'dist').replace(os.path.sep, '/')
2105 2106 2107
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
Jim Fulton's avatar
Jim Fulton committed
2108
    ... parts =
2109
    ... extensions = wackyextension
Jim Fulton's avatar
Jim Fulton committed
2110
    ... find-links = %(dist)s
2111 2112 2113 2114 2115
    ... ''' % 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
2116
    >>> print_(system(buildout), end='')
2117 2118
    Getting distribution for 'wackyextension'.
    Got wackyextension 1.
Jim Fulton's avatar
Jim Fulton committed
2119
    I am a wacky extension
2120

2121
    """
2122

Jim Fulton's avatar
Jim Fulton committed
2123 2124 2125
def distributions_from_local_find_links_make_it_to_download_cache():
    """

pombredanne's avatar
pombredanne committed
2126
If we specify a local directory in find links, distros found there
Jim Fulton's avatar
Jim Fulton committed
2127 2128 2129 2130 2131 2132 2133 2134 2135
need to make it to the download cache.

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

2136
    >>> print_(system(buildout+' setup test bdist_egg')) # doctest: +ELLIPSIS
Jim Fulton's avatar
Jim Fulton committed
2137 2138 2139 2140 2141 2142 2143 2144 2145
    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 ...
2146

Jim Fulton's avatar
Jim Fulton committed
2147 2148 2149 2150
    >>> ls('cache')
    -  foo-0.0.0-py2.4.egg

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

Jim Fulton's avatar
Jim Fulton committed
2152
    """
2153

2154 2155
def create_egg(name, version, dest, install_requires=None,
               dependency_links=None):
2156 2157 2158 2159 2160
    d = tempfile.mkdtemp()
    if dest=='available':
        extras = dict(x=['x'])
    else:
        extras = {}
2161 2162 2163 2164 2165 2166 2167 2168
    if dependency_links:
        links = 'dependency_links = %s, ' % dependency_links
    else:
        links = ''
    if install_requires:
        requires = 'install_requires = %s, ' % install_requires
    else:
        requires = ''
2169 2170 2171 2172
    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'
2173 2174
            '      %s %s py_modules=["setup"]\n)'
            % (name, str(version), extras, requires, links)
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
            )
        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
2192
        zc.buildout.easy_install.install(['spam'], 'existing', ['available'])
2193 2194 2195
        )

    if dist.extras:
2196
        print_('downloaded', dist.version)
2197
    else:
2198
        print_('had', dist.version)
2199 2200 2201 2202 2203 2204
    sys.path_importer_cache.clear()

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

2205
Using different version numbers to work around zip importer cache problems. :(
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 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293

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

2295 2296 2297 2298 2299
    """

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

Jim Fulton's avatar
Jim Fulton committed
2302
The default is prefer-final = true:
2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314

    >>> 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
2315
    >>> print_(system(buildout+' -v'), end='') # doctest: +ELLIPSIS
2316
    Installing 'zc.buildout', 'setuptools'.
2317
    ...
Jim Fulton's avatar
Jim Fulton committed
2318
    Picked: demo = 0.3
2319
    ...
Jim Fulton's avatar
Jim Fulton committed
2320
    Picked: demoneeded = 1.1
2321 2322

Here we see that the final versions of demo and demoneeded are used.
Jim Fulton's avatar
Jim Fulton committed
2323
We get the same behavior if we add prefer-final = true
2324 2325 2326 2327 2328 2329

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = eggs
    ... find-links = %(link_server)s
Jim Fulton's avatar
Jim Fulton committed
2330
    ... prefer-final = true
2331 2332 2333 2334 2335 2336
    ...
    ... [eggs]
    ... recipe = zc.recipe.egg:eggs
    ... eggs = demo
    ... ''' % globals())

Jim Fulton's avatar
Jim Fulton committed
2337
    >>> print_(system(buildout+' -v'), end='') # doctest: +ELLIPSIS
2338
    Installing 'zc.buildout', 'setuptools'.
2339
    ...
Jim Fulton's avatar
Jim Fulton committed
2340
    Picked: demo = 0.3
2341
    ...
Jim Fulton's avatar
Jim Fulton committed
2342
    Picked: demoneeded = 1.1
2343

Jim Fulton's avatar
Jim Fulton committed
2344
If we specify prefer-final = false, we'll get the newest
2345 2346 2347 2348 2349 2350 2351
distributions:

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts = eggs
    ... find-links = %(link_server)s
Jim Fulton's avatar
Jim Fulton committed
2352
    ... prefer-final = false
2353 2354 2355 2356 2357 2358
    ...
    ... [eggs]
    ... recipe = zc.recipe.egg:eggs
    ... eggs = demo
    ... ''' % globals())

Jim Fulton's avatar
Jim Fulton committed
2359
    >>> print_(system(buildout+' -v'), end='') # doctest: +ELLIPSIS
2360
    Installing 'zc.buildout', 'setuptools'.
2361
    ...
2362
    Picked: demo = 0.4rc1
2363
    ...
2364
    Picked: demoneeded = 1.2rc1
2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379

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
2380
    >>> print_(system(buildout+' -v'), end='') # doctest: +ELLIPSIS
2381 2382
    While:
      Initializing.
Jim Fulton's avatar
Jim Fulton committed
2383
    Error: Invalid value for 'prefer-final' option: 'no'
2384 2385 2386 2387 2388 2389
    """

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
2390 2391
    isn't specified using a versions entry, then buildout's version
    requirement gets set to >=CURRENT_VERSION.
2392 2393 2394 2395 2396 2397

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

    >>> [v] = [l.split('= >=', 1)[1].strip()
2400
    ...        for l in system(buildout+' -vv').split('\n')
2401 2402
    ...        if l.startswith('zc.buildout = >=')]
    >>> v == pkg_resources.working_set.find(
2403 2404 2405 2406 2407 2408 2409 2410
    ...         pkg_resources.Requirement.parse('zc.buildout')
    ...         ).version
    True

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts =
2411 2412
    ... [versions]
    ... zc.buildout = >.1
2413
    ... ''')
2414
    >>> [str(l.split('= >', 1)[1].strip())
2415
    ...        for l in system(buildout+' -vv').split('\n')
2416 2417
    ...        if l.startswith('zc.buildout =')]
    ['.1']
2418 2419 2420 2421 2422 2423 2424 2425 2426

    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts =
    ... versions = versions
    ... [versions]
    ... zc.buildout = 43
    ... ''')
2427 2428 2429
    >>> print_(system(buildout), end='') # doctest: +ELLIPSIS
    Getting distribution for 'zc.buildout==43'.
    ...
2430 2431 2432

    """

2433 2434 2435
def develop_with_modules():
    """
Distribution setup scripts can import modules in the distribution directory:
2436

2437 2438 2439 2440
    >>> mkdir('foo')
    >>> write('foo', 'bar.py',
    ... '''# empty
    ... ''')
2441

2442 2443 2444 2445 2446 2447
    >>> write('foo', 'setup.py',
    ... '''
    ... import bar
    ... from setuptools import setup
    ... setup(name="foo")
    ... ''')
2448

2449 2450 2451 2452
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... develop = foo
2453
    ... parts =
2454 2455
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
2456
    >>> print_(system(join('bin', 'buildout')), end='')
2457 2458 2459 2460 2461 2462 2463
    Develop: '/sample-buildout/foo'

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

    """
2464

2465
def dont_pick_setuptools_if_version_is_specified_when_required_by_src_dist():
Jim Fulton's avatar
Jim Fulton committed
2466
    """
2467
When installing a source distribution, we got setuptools without
Jim Fulton's avatar
Jim Fulton committed
2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487
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]
2488
    ... setuptools = %s
Jim Fulton's avatar
Jim Fulton committed
2489 2490 2491 2492 2493 2494
    ... foo = 1
    ...
    ... [foo]
    ... recipe = zc.recipe.egg
    ... eggs = foo
    ... ''' % pkg_resources.working_set.find(
2495
    ...    pkg_resources.Requirement.parse('setuptools')).version)
Jim Fulton's avatar
Jim Fulton committed
2496

Jim Fulton's avatar
Jim Fulton committed
2497
    >>> print_(system(buildout), end='')
Jim Fulton's avatar
Jim Fulton committed
2498 2499 2500
    Installing foo.
    Getting distribution for 'foo==1'.
    Got foo 1.
2501

Jim Fulton's avatar
Jim Fulton committed
2502
    """
2503

2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521
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',
2522 2523
    ... r'''
    ... import eggrecipedemo, eggrecipedemoneeded, sys
Jim Fulton's avatar
Jim Fulton committed
2524 2525 2526 2527 2528 2529
    ... 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')
2530 2531
    ... ''')

Jim Fulton's avatar
Jim Fulton committed
2532
    >>> print_(system(join('bin', 'py')+ ' t.py'), end='')
Jim Fulton's avatar
Jim Fulton committed
2533 2534
    /sample-buildout/eggs/demo-0.3-py2.4.egg/eggrecipedemo.py
    /sample-buildout/eggs/demoneeded-1.1-py2.4.egg/eggrecipedemoneeded.py
2535 2536
    """

2537 2538 2539 2540 2541 2542
def dont_mess_with_standard_dirs_with_variable_refs():
    """
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... eggs-directory = ${buildout:directory}/develop-eggs
2543
    ... parts =
2544
    ... ''' % globals())
Jim Fulton's avatar
Jim Fulton committed
2545
    >>> print_(system(buildout), end='')
2546

2547 2548
    """

2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575
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
2576
    >>> print_(system(buildout), end='')
2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607
    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
2608
    >>> print_(system(buildout), end='')
2609 2610 2611 2612 2613 2614
    Develop: '/sample-buildout/samplea'
    Couldn't develop '/sample-buildout/grumble*' (not found)
    Installing eggs.

    """

2615 2616 2617 2618
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
2619
    >>> _ = open('setup.py', 'w').write('''
2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637
    ... 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
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
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
2663
    >>> print_(system(buildout), end='')
Jim Fulton's avatar
Jim Fulton committed
2664 2665 2666 2667 2668 2669 2670 2671
    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
2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702
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
2703
    >>> print_(system(buildout), end='')
Jim Fulton's avatar
Jim Fulton committed
2704 2705 2706 2707 2708 2709 2710 2711 2712
    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
2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728
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
2729

Jim Fulton's avatar
Jim Fulton committed
2730
    >>> print_(system(buildout+' buildout:parts+=p2 p1:foo+=bar'), end='')
Jim Fulton's avatar
Jim Fulton committed
2731 2732 2733 2734 2735 2736 2737
    Installing p1.
      foo='1 a\nb\nbar'
      recipe='zc.buildout:debug'
    Installing p2.
      foo='1 a\nb\nbar'
      recipe='zc.buildout:debug'
    """
2738

2739 2740
def test_constrained_requirement():
    """
pombredanne's avatar
pombredanne committed
2741
    zc.buildout.easy_install._constrained_requirement(constraint, requirement)
2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752

    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'),
    ... ('x>1',      '2',        'x==2'),
    ... ('x>3',      '2',        IncompatibleConstraintError),
2753
    ... ('x>1',      '>2',       'x>1,>2'),
2754 2755 2756 2757 2758 2759 2760 2761 2762 2763
    ... ]
    >>> 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
2764
    ...     if str(g) != str(e):
2765 2766 2767
    ...         print_('failed', o, c, g, '!=', e)
    """

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 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838
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
    """


2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849
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
2850
    Getting distribution for 'zc.recipe.egg<2dev,>=2.0.0a3'.
2851 2852 2853 2854 2855
    While:
      Installing.
      Getting section egg.
      Initializing section egg.
      Installing recipe zc.recipe.egg <2dev.
2856 2857
      Getting distribution for 'zc.recipe.egg<2dev,>=2.0.0a3'.
    Error: Couldn't find a distribution for 'zc.recipe.egg<2dev,>=2.0.0a3'.
2858 2859
    """

2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892
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
    """

2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903
def bootstrap_honors_relative_paths():
    """
    >>> working = tmpdir('working')
    >>> cd(working)
    >>> write('buildout.cfg',
    ... '''
    ... [buildout]
    ... parts =
    ... relative-paths = true
    ... ''')
    >>> _ = system(buildout+' bootstrap')
2904 2905
    >>> cat('bin', 'buildout') # doctest: +ELLIPSIS
    #!/usr/local/bin/python2.7
2906 2907 2908 2909 2910 2911 2912 2913 2914
    <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] = [
2915
      join(base, 'eggs/setuptools-0.7-py2.7.egg'),
2916
      ...
2917 2918 2919 2920 2921 2922 2923
      ]
    <BLANKLINE>
    import zc.buildout.buildout
    <BLANKLINE>
    if __name__ == '__main__':
        sys.exit(zc.buildout.buildout.main())
    """
2924

2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937
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
2938
    Nothing is installed, even from cache, in offline
2939 2940 2941 2942
    mode, which might better be called 'no-install mode'.
    <BLANKLINE>
    """

2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967
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>
    """

2968 2969 2970 2971 2972 2973
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
2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987
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.
    """

2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007
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
3008 3009
if sys.platform == 'win32':
    del buildout_honors_umask # umask on dohs is academic
3010

Jim Fulton's avatar
Jim Fulton committed
3011
######################################################################
3012

3013
def create_sample_eggs(test, executable=sys.executable):
Jim Fulton's avatar
Jim Fulton committed
3014
    assert executable == sys.executable, (executable, sys.executable)
3015 3016 3017 3018 3019 3020
    write = test.globs['write']
    dest = test.globs['sample_eggs']
    tmp = tempfile.mkdtemp()
    try:
        write(tmp, 'README.txt', '')

3021
        for i in (0, 1, 2):
3022
            write(tmp, 'eggrecipedemoneeded.py', 'y=%s\ndef f():\n  pass' % i)
3023
            rc1 = i==2 and 'rc1' or ''
3024 3025 3026
            write(
                tmp, 'setup.py',
                "from setuptools import setup\n"
3027
                "setup(name='demoneeded', py_modules=['eggrecipedemoneeded'],"
3028
                " zip_safe=True, version='1.%s%s', author='bob', url='bob', "
3029
                "author_email='bob')\n"
3030
                % (i, rc1)
3031 3032
                )
            zc.buildout.testing.sdist(tmp, dest)
3033

3034 3035 3036
        write(
            tmp, 'distutilsscript',
            '#!/usr/bin/python\n'
Reinout van Rees's avatar
Reinout van Rees committed
3037
            '# -*- coding: utf-8 -*-\n'
3038 3039
            '"""Module docstring."""\n'
            'from __future__ import print_statement\n'
3040
            'import os\n'
3041 3042
            'import sys; sys.stdout.write("distutils!\\n")\n'
            )
3043 3044 3045
        write(
            tmp, 'setup.py',
            "from setuptools import setup\n"
Jim Fulton's avatar
Jim Fulton committed
3046
            "setup(name='other', zip_safe=False, version='1.0', "
3047
            "scripts=['distutilsscript'],"
3048
            "py_modules=['eggrecipedemoneeded'])\n"
3049
            )
Jim Fulton's avatar
Jim Fulton committed
3050
        zc.buildout.testing.bdist_egg(tmp, sys.executable, dest)
3051

3052 3053 3054 3055 3056 3057 3058 3059 3060
        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)

3061
        os.remove(os.path.join(tmp, 'distutilsscript'))
3062
        os.remove(os.path.join(tmp, 'eggrecipedemoneeded.py'))
3063

3064
        for i in (1, 2, 3, 4):
3065 3066
            write(
                tmp, 'eggrecipedemo.py',
3067 3068 3069
                'import eggrecipedemoneeded, sys\n'
                'def print_(*a):\n'
                '    sys.stdout.write(" ".join(map(str, a))+"\\n")\n'
3070
                'x=%s\n'
3071 3072
                'def main():\n'
                '   print_(x, eggrecipedemoneeded.y)\n'
3073
                % i)
3074
            rc1 = i==4 and 'rc1' or ''
3075 3076 3077 3078 3079 3080 3081
            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']},"
3082
                " zip_safe=True, version='0.%s%s')\n" % (i, rc1)
3083
                )
Jim Fulton's avatar
Jim Fulton committed
3084
            zc.buildout.testing.bdist_egg(tmp, dest)
3085 3086 3087 3088 3089 3090 3091 3092 3093 3094

        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
3095
        zc.buildout.testing.bdist_egg(tmp, sys.executable, dest)
3096

3097 3098 3099
    finally:
        shutil.rmtree(tmp)

3100
extdemo_c2 = """
3101 3102 3103 3104 3105 3106 3107 3108
#include <Python.h>
#include <extdemo.h>

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

PyMODINIT_FUNC
initextdemo(void)
{
3109 3110 3111 3112 3113 3114 3115
    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
3116 3117
}
"""
3118

3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151
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
3152
from distutils.core import setup, Extension
3153

3154
if os.environ.get('test-variable'):
3155
    print("Have environment test-variable: %%s" %% os.environ['test-variable'])
3156

3157
setup(name = "extdemo", version = "%s", url="http://www.zope.org",
3158 3159 3160 3161
      author="Demo", author_email="demo@demo.com",
      ext_modules = [Extension('extdemo', ['extdemo.c'])],
      )
"""
3162

3163 3164 3165 3166 3167
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']
3168 3169 3170
    write = test.globs['write']
    try:
        write(tmp, 'extdemo.c', extdemo_c);
3171
        write(tmp, 'setup.py', extdemo_setup_py % version);
3172 3173 3174 3175 3176
        write(tmp, 'README', "");
        write(tmp, 'MANIFEST.in', "include *.c\n");
        test.globs['sdist'](tmp, test.globs['sample_eggs'])
    except:
        shutil.rmtree(tmp)
3177

3178 3179 3180 3181 3182 3183 3184 3185 3186
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'])
3187
    test.globs['update_extdemo'] = lambda : add_source_dist(test, 1.5)
3188
    zc.buildout.testing.install_develop('zc.recipe.egg', test)
3189

3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212
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'),
        )

3213 3214
egg_parse = re.compile('([0-9a-zA-Z_.]+)-([0-9a-zA-Z_.]+)-py(\d[.]\d).egg$'
                       ).match
Gary Poster's avatar
Gary Poster committed
3215
def makeNewRelease(project, ws, dest, version='99.99'):
3216 3217 3218 3219
    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
3220
    dest = os.path.join(dest, "%s-%s-py%s.egg" % (eggname, version, pyver))
3221 3222 3223 3224 3225
    if os.path.isfile(dist.location):
        shutil.copy(dist.location, dest)
        zip = zipfile.ZipFile(dest, 'a')
        zip.writestr(
            'EGG-INFO/PKG-INFO',
3226
            ((zip.read('EGG-INFO/PKG-INFO').decode('ISO-8859-1')
Jim Fulton's avatar
Jim Fulton committed
3227 3228
              ).replace("Version: %s" % oldver,
                        "Version: %s" % version)
3229
             ).encode('ISO-8859-1')
3230 3231 3232
            )
        zip.close()
    else:
3233
        shutil.copytree(dist.location, dest)
3234
        info_path = os.path.join(dest, 'EGG-INFO', 'PKG-INFO')
3235
        info = open(info_path).read().replace("Version: %s" % oldver,
Gary Poster's avatar
Gary Poster committed
3236
                                              "Version: %s" % version)
3237 3238
        open(info_path, 'w').write(info)

Gary Poster's avatar
Gary Poster committed
3239
def getWorkingSetWithBuildoutEgg(test):
3240 3241 3242
    sample_buildout = test.globs['sample_buildout']
    eggs = os.path.join(sample_buildout, 'eggs')

Gary Poster's avatar
Gary Poster committed
3243
    # If the zc.buildout dist is a develop dist, convert it to a
3244 3245 3246 3247 3248 3249 3250
    # 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))
3251 3252 3253 3254 3255 3256
        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(
3257
                         pkg_resources.Requirement.parse('setuptools')
3258 3259 3260
                         ).location,
                     ),
            )
3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271
        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
3272
    return ws
3273

Gary Poster's avatar
Gary Poster committed
3274 3275 3276 3277 3278
def updateSetup(test):
    zc.buildout.testing.buildoutSetUp(test)
    new_releases = test.globs['tmpdir']('new_releases')
    test.globs['new_releases'] = new_releases
    ws = getWorkingSetWithBuildoutEgg(test)
3279 3280 3281
    # now let's make the new releases
    makeNewRelease('zc.buildout', ws, new_releases)
    os.mkdir(os.path.join(new_releases, 'zc.buildout'))
3282 3283
    makeNewRelease('setuptools', ws, new_releases)
    os.mkdir(os.path.join(new_releases, 'setuptools'))
3284

Jim Fulton's avatar
Jim Fulton committed
3285 3286 3287 3288 3289
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
3290
def bootstrapSetup(test):
Jim Fulton's avatar
Jim Fulton committed
3291 3292 3293
    buildout_txt_setup(test)
    test.globs['link_server'] = test.globs['start_server'](
        test.globs['sample_eggs'])
Gary Poster's avatar
Gary Poster committed
3294 3295
    sample_eggs = test.globs['sample_eggs']
    ws = getWorkingSetWithBuildoutEgg(test)
Jim Fulton's avatar
Jim Fulton committed
3296
    makeNewRelease('zc.buildout', ws, sample_eggs, '2.0.0')
Jim Fulton's avatar
Jim Fulton committed
3297
    makeNewRelease('zc.buildout', ws, sample_eggs, '22.0.0')
Gary Poster's avatar
Gary Poster committed
3298
    os.environ['bootstrap-testing-find-links'] = test.globs['link_server']
Jim Fulton's avatar
Jim Fulton committed
3299
    test.globs['bootstrap_py'] = bootstrap_py
's avatar
committed
3300

3301
normalize_bang = (
's avatar
committed
3302 3303
    re.compile(re.escape('#!'+
                         zc.buildout.easy_install._safe_arg(sys.executable))),
Jim Fulton's avatar
Jim Fulton committed
3304
    '#!/usr/local/bin/python2.7',
3305 3306
    )

Jim Fulton's avatar
Jim Fulton committed
3307 3308 3309 3310 3311
normalize_S = (
    re.compile(r'#!/usr/local/bin/python2.7 -S'),
    '#!/usr/local/bin/python2.7',
    )

3312
def test_suite():
3313
    test_suite = [
3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324
        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,
3325
                    zc.buildout.testing.adding_find_link,
3326
                    # (re.compile(r"Installing 'zc.buildout >=\S+"), ''),
3327 3328 3329 3330
                    (re.compile('__buildout_signature__ = recipes-\S+'),
                     '__buildout_signature__ = recipes-SSSSSSSSSSS'),
                    (re.compile('executable = [\S ]+python\S*', re.I),
                     'executable = python'),
3331
                    (re.compile('[-d]  (setuptools|setuptools)-\S+[.]egg'),
3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346
                     '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: '
                     ),
3347
                    (re.compile('setuptools'), 'setuptools'),
3348
                    (re.compile('Got zc.recipe.egg \S+'), 'Got zc.recipe.egg'),
3349 3350
                    (re.compile(r'zc\.(buildout|recipe\.egg)\s*= >=\S+'),
                     'zc.\1 = >=1.99'),
3351 3352
                    ])
                ) + manuel.capture.Manuel(),
Jim Fulton's avatar
Jim Fulton committed
3353
            'buildout.txt', 'meta-recipes.txt',
3354 3355 3356 3357 3358
            setUp=buildout_txt_setup,
            tearDown=zc.buildout.testing.buildoutTearDown,
            ),
        doctest.DocFileSuite(
            'runsetup.txt', 'repeatable.txt', 'setup.txt',
Jim Fulton's avatar
Jim Fulton committed
3359
            setUp=zc.buildout.testing.buildoutSetUp,
3360
            tearDown=zc.buildout.testing.buildoutTearDown,
Jim Fulton's avatar
Jim Fulton committed
3361
            checker=renormalizing.RENormalizing([
3362
               zc.buildout.testing.normalize_path,
3363
               zc.buildout.testing.normalize_endings,
3364 3365
               zc.buildout.testing.normalize_script,
               zc.buildout.testing.normalize_egg_py,
3366
               zc.buildout.testing.not_found,
3367
               zc.buildout.testing.adding_find_link,
3368 3369 3370
               # (re.compile(r"Installing 'zc.buildout >=\S+"), ''),
               # (re.compile(r"Getting distribution for 'zc.buildout >=\S+"),
               #  ''),
Jim Fulton's avatar
Jim Fulton committed
3371 3372
               (re.compile('__buildout_signature__ = recipes-\S+'),
                '__buildout_signature__ = recipes-SSSSSSSSSSS'),
3373
               (re.compile('[-d]  setuptools-\S+[.]egg'), 'setuptools.egg'),
Jim Fulton's avatar
Jim Fulton committed
3374 3375
               (re.compile('zc.buildout(-\S+)?[.]egg(-link)?'),
                'zc.buildout.egg'),
3376
               (re.compile('creating \S*setup.cfg'), 'creating setup.cfg'),
Jim Fulton's avatar
Jim Fulton committed
3377
               (re.compile('hello\%ssetup' % os.path.sep), 'hello/setup'),
3378 3379 3380 3381
               (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.'),
3382
               (re.compile(r'\\[\\]?'), '/'),
3383
               (re.compile('WindowsError'), 'OSError'),
3384
               (re.compile('setuptools = \S+'), 'setuptools = 0.7.99'),
3385 3386 3387 3388
               (re.compile(r'\[Error 17\] Cannot create a file '
                           r'when that file already exists: '),
                '[Errno 17] File exists: '
                ),
3389 3390
               (re.compile('executable = %s' % re.escape(sys.executable)),
                'executable = python'),
3391 3392 3393
               (re.compile(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{6}'),
                'YYYY-MM-DD hh:mm:ss.dddddd'),
               ]),
3394
            ),
3395 3396 3397 3398 3399
        doctest.DocFileSuite(
            'debugging.txt',
            setUp=zc.buildout.testing.buildoutSetUp,
            tearDown=zc.buildout.testing.buildoutTearDown,
            checker=renormalizing.RENormalizing([
Jim Fulton's avatar
Jim Fulton committed
3400 3401 3402
                zc.buildout.testing.normalize_path,
                zc.buildout.testing.normalize_endings,
                zc.buildout.testing.normalize_exception_type_for_python_2_and_3,
3403
                zc.buildout.testing.not_found,
3404
                zc.buildout.testing.adding_find_link,
Jim Fulton's avatar
Jim Fulton committed
3405 3406 3407 3408 3409 3410
                (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)'),
                ])
3411
            ),
3412 3413 3414 3415 3416 3417

        doctest.DocFileSuite(
            'update.txt',
            setUp=updateSetup,
            tearDown=zc.buildout.testing.buildoutTearDown,
            checker=renormalizing.RENormalizing([
3418
                (re.compile(r'(zc.buildout|setuptools)-\d+[.]\d+\S*'
Jim Fulton's avatar
Jim Fulton committed
3419 3420 3421 3422 3423 3424
                            '-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,
3425
                zc.buildout.testing.not_found,
3426
                zc.buildout.testing.adding_find_link,
Jim Fulton's avatar
Jim Fulton committed
3427 3428
                normalize_bang,
                normalize_S,
3429
                # (re.compile(r"Installing 'zc.buildout >=\S+"), ''),
3430 3431
                (re.compile(r"Getting distribution for 'zc.buildout>=\S+"),
                 ''),
Jim Fulton's avatar
Jim Fulton committed
3432 3433
                (re.compile('99[.]99'), 'NINETYNINE.NINETYNINE'),
                (re.compile(
3434
                    r'(zc.buildout|setuptools)( version)? \d+[.]\d+\S*'),
Jim Fulton's avatar
Jim Fulton committed
3435
                 '\\1 V.V'),
3436
                (re.compile('[-d]  setuptools'), '-  setuptools'),
3437
                (re.compile(re.escape(os.path.sep)+'+'), '/'),
3438 3439
               ])
            ),
3440

Jim Fulton's avatar
Jim Fulton committed
3441
        doctest.DocFileSuite(
3442
            'easy_install.txt', 'downloadcache.txt', 'dependencylinks.txt',
Jim Fulton's avatar
Jim Fulton committed
3443
            'allowhosts.txt',
3444 3445
            setUp=easy_install_SetUp,
            tearDown=zc.buildout.testing.buildoutTearDown,
3446
            checker=renormalizing.RENormalizing([
3447
                zc.buildout.testing.normalize_script,
Jim Fulton's avatar
Jim Fulton committed
3448 3449 3450
                zc.buildout.testing.normalize_path,
                zc.buildout.testing.normalize_endings,
                zc.buildout.testing.normalize_egg_py,
3451
                zc.buildout.testing.normalize_exception_type_for_python_2_and_3,
3452
                zc.buildout.testing.adding_find_link,
3453
                zc.buildout.testing.not_found,
Jim Fulton's avatar
Jim Fulton committed
3454 3455
                normalize_bang,
                normalize_S,
3456
                (re.compile('[-d]  setuptools-\S+[.]egg'), 'setuptools.egg'),
Jim Fulton's avatar
Jim Fulton committed
3457
                (re.compile(r'\\[\\]?'), '/'),
3458 3459
                (re.compile('(\n?)-  ([a-zA-Z_.-]+)\n-  \\2.exe\n'),
                 '\\1-  \\2\n'),
Jim Fulton's avatar
Jim Fulton committed
3460
                ]+(sys.version_info < (2, 5) and [
3461 3462 3463 3464
                  (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 [])),
3465 3466


Jim Fulton's avatar
Jim Fulton committed
3467
            ),
3468 3469 3470 3471 3472 3473 3474

        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([
3475 3476
              zc.buildout.testing.normalize_exception_type_for_python_2_and_3,
              zc.buildout.testing.not_found,
3477
              zc.buildout.testing.adding_find_link,
3478 3479 3480 3481 3482
              (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,
3483
              zc.buildout.testing.ignore_not_upgrading,
3484
              ]),
3485 3486
            ),

3487
        doctest.DocTestSuite(
3488
            setUp=easy_install_SetUp,
3489
            tearDown=zc.buildout.testing.buildoutTearDown,
3490
            checker=renormalizing.RENormalizing([
Jim Fulton's avatar
Jim Fulton committed
3491 3492 3493 3494 3495
                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__,
3496
                zc.buildout.testing.not_found,
3497
                zc.buildout.testing.normalize_exception_type_for_python_2_and_3,
3498
                zc.buildout.testing.adding_find_link,
3499
                normalize_bang,
Jim Fulton's avatar
Jim Fulton committed
3500 3501 3502
                (re.compile(r'^(\w+\.)*(Missing\w+: )'), '\2'),
                (re.compile("buildout: Running \S*setup.py"),
                 'buildout: Running setup.py'),
3503 3504
                (re.compile('setuptools-\S+-'),
                 'setuptools.egg'),
Jim Fulton's avatar
Jim Fulton committed
3505 3506
                (re.compile('zc.buildout-\S+-'),
                 'zc.buildout.egg'),
3507
                (re.compile('setuptools = \S+'), 'setuptools = 0.7.99'),
Jim Fulton's avatar
Jim Fulton committed
3508 3509 3510 3511
                (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'),
3512 3513
                (re.compile('Picked: setuptools = \S+'),
                 'Picked: setuptools = V'),
3514
                (re.compile('[-d]  setuptools'), '-  setuptools'),
Jim Fulton's avatar
Jim Fulton committed
3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525
                (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'."),
3526 3527
                # Python 3.4 changed the wording of NameErrors
                (re.compile('NameError: global name'), 'NameError: name'),
Jim Fulton's avatar
Jim Fulton committed
3528
                ]),
3529
            ),
3530
        zc.buildout.rmtree.test_suite(),
3531 3532 3533 3534 3535 3536
        doctest.DocFileSuite(
            'windows.txt',
            setUp=zc.buildout.testing.buildoutSetUp,
            tearDown=zc.buildout.testing.buildoutTearDown,
            checker=renormalizing.RENormalizing([
               zc.buildout.testing.normalize_path,
3537
               zc.buildout.testing.normalize_endings,
3538 3539
               zc.buildout.testing.normalize_script,
               zc.buildout.testing.normalize_egg_py,
3540
               zc.buildout.testing.not_found,
3541
               zc.buildout.testing.adding_find_link,
3542 3543
               (re.compile('__buildout_signature__ = recipes-\S+'),
                '__buildout_signature__ = recipes-SSSSSSSSSSS'),
3544
               (re.compile('[-d]  setuptools-\S+[.]egg'), 'setuptools.egg'),
3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559
               (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: '
                ),
               ])
3560 3561 3562
            ),
        doctest.DocFileSuite(
            'testing_bugfix.txt'),
3563 3564
    ]

3565
    # adding bootstrap.txt doctest to the suite
3566 3567 3568
    # only if bootstrap.py is present
    if os.path.exists(bootstrap_py):
        test_suite.append(doctest.DocFileSuite(
Jim Fulton's avatar
Jim Fulton committed
3569
            'bootstrap.txt', 'bootstrap_cl_settings.test',
Gary Poster's avatar
Gary Poster committed
3570
            setUp=bootstrapSetup,
3571 3572 3573
            tearDown=zc.buildout.testing.buildoutTearDown,
            checker=renormalizing.RENormalizing([
               zc.buildout.testing.normalize_path,
3574
               zc.buildout.testing.normalize_endings,
3575
               zc.buildout.testing.normalize_script,
3576
               zc.buildout.testing.not_found,
3577
               normalize_bang,
3578
               zc.buildout.testing.adding_find_link,
3579
               (re.compile('Downloading.*setuptools.*egg\n'), ''),
3580 3581 3582 3583
               ]),
            ))

    return unittest.TestSuite(test_suite)