Detailed tests of postrelease.py
================================

.. :doctest:
.. :setup: zest.releaser.tests.functional.setup
.. :teardown: zest.releaser.tests.functional.teardown

Several items are prepared for us.

An svn repository:

    >>> repo_url
    'file://TESTREPO'

An svn checkout of a project:

    >>> svnsourcedir
    'TESTTEMP/tha.example-svn'
    >>> import os
    >>> os.chdir(svnsourcedir)
    >>> from zest.releaser.utils import system
    >>> print system("svn info %s" % svnsourcedir)
    Path: ...
    URL: file://TESTREPO/tha.example/trunk
    ...

The version is at 0.1.dev0:

    >>> svnhead('setup.py')
    from setuptools import setup, find_packages
    import os.path
    <BLANKLINE>
    version = '0.1.dev0'

Asking input on the prompt is not unittestable unless we use the prepared
testing hack in utils.py:

    >>> from zest.releaser import utils
    >>> utils.TESTMODE = True

Run the postrelease script:

    >>> utils.test_answer_book.set_answers(['', ''])
    >>> from zest.releaser import postrelease
    >>> postrelease.main()
    Current version is '0.1'
    Question: Enter new development version ('.dev0' will be appended) [0.2]:
    Our reply: <ENTER>
    Checking data dict
    Question: OK to commit this (Y/n)?
    Our reply: <ENTER>

The changelog and setup.py are at 0.2 and indicate dev mode:

    >>> svnhead('CHANGES.txt')
    Changelog of tha.example
    =====================
    <BLANKLINE>
    0.2 (unreleased)
    ----------------
    >>> svnhead('setup.py')
    from setuptools import setup, find_packages
    import os.path
    <BLANKLINE>
    version = '0.2.dev0'

Now we set the version to something that does not end in a number and
is not recognized as development version (with setuptools 8 or higher
the version is actually reported with a zero at the end):

    >>> from zest.releaser.svn import Subversion
    >>> vcs = Subversion()
    >>> vcs.version
    '0.2.dev0'
    >>> vcs.version = '0.1b'
    >>> vcs.version
    '0.1b0'

Run the postrelease script.  Since the version number does not end
with a number, the script cannot make a suggestion, except when the
number is normalized by setuptools already:

    >>> utils.test_answer_book.set_answers(['0.2', ''])
    >>> from zest.releaser import postrelease
    >>> postrelease.main()
    Current version is '0.1b0'
    Question: Enter new development version ('.dev0' will be appended) [0.1b1]:
    Our reply: 0.2
    Checking data dict
    Question: OK to commit this (Y/n)?
    Our reply: <ENTER>

The changelog and setup.py are at 0.2 and indicate dev mode:

    >>> svnhead('CHANGES.txt')
    Changelog of tha.example
    =====================
    <BLANKLINE>
    0.2 (unreleased)
    ----------------
    >>> svnhead('setup.py')
    from setuptools import setup, find_packages
    import os.path
    <BLANKLINE>
    version = '0.2.dev0'

To check some corner cases we switch back and forth between prerelease
and postrelease.  The next version after 0.2.19 should not be 0.2.110
but 0.2.20:

    >>> from zest.releaser import prerelease
    >>> utils.test_answer_book.set_answers(['0.2.19', ''])
    >>> prerelease.main()
    Question: Enter version [0.2]:
    Our reply: 0.2.19
    Checking data dict
    Question: OK to commit this (Y/n)?
    Our reply: <ENTER>
    >>> utils.test_answer_book.set_answers(['', ''])
    >>> postrelease.main()
    Current version is '0.2.19'
    Question: Enter new development version ('.dev0' will be appended) [0.2.20]:
    Our reply: <ENTER>
    Checking data dict
    Question: OK to commit this (Y/n)?
    Our reply: <ENTER>

Releases without numbers at the end should not fluster us even when we
cannot suggest a reasonable number. We'll ask for a version until we get one:

    >>> utils.test_answer_book.set_answers(['0.3beta', ''])
    >>> prerelease.main()
    Question: Enter version [0.2.20]:
    Our reply: 0.3beta
    Checking data dict
    Question: OK to commit this (Y/n)?
    Our reply: <ENTER>
    >>> utils.test_answer_book.set_answers(['0.3rc', ''])
    >>> postrelease.main()
    Current version is '0.3b0'
    Question: Enter new development version ('.dev0' will be appended) [0.3b1]:
    Our reply: 0.3rc
    Checking data dict
    Question: OK to commit this (Y/n)?
    Our reply: <ENTER>

Numbers and characters can be combined:

    >>> utils.test_answer_book.set_answers(['1.0a1', ''])
    >>> prerelease.main()
    Question: Enter version [0.3rc0]:
    Our reply: 1.0a1
    Checking data dict
    Question: OK to commit this (Y/n)?
    Our reply: <ENTER>
    >>> utils.test_answer_book.set_answers(['', ''])
    >>> postrelease.main()
    Current version is '1.0a1'
    Question: Enter new development version ('.dev0' will be appended) [1.0a2]:
    Our reply: <ENTER>
    Checking data dict
    Question: OK to commit this (Y/n)?
    Our reply: <ENTER>

If there's an empty history file, it gets a fresh header.

    >>> utils.test_answer_book.set_answers(['1.0', ''])
    >>> prerelease.main()
    Question: ...
    >>> open('CHANGES.txt', 'w').write('')
    >>> utils.test_answer_book.set_answers(['', ''])
    >>> postrelease.main()
    Current ...
    >>> print open('CHANGES.txt', 'r').read()
    1.1 (unreleased)
    ----------------
    <BLANKLINE>
    - Nothing changed yet.

If there is no history file, we get no errors and a new history file is not
created:

    >>> utils.test_answer_book.set_answers(['', ''])
    >>> prerelease.main()
    Question: ...
    >>> os.remove('CHANGES.txt')
    >>> utils.test_answer_book.set_answers(['8.2', ''])
    >>> postrelease.main() # The setup.py complains and quits. Our test setup catches this.
    Traceback (most recent call last):
    ...
    RuntimeError: SYSTEM EXIT (code=1)
    >>> open('CHANGES.txt') # Nope, doesn't exist.
    Traceback (most recent call last):
    ...
    IOError: [Errno 2] No such file or directory: 'CHANGES.txt'

Re-instate the history file again, but omit the restructuredtext header line:

    >>> open('CHANGES.txt', 'w').write('1.0 (1972-12-25)\n\n- hello\n')
    >>> utils.test_answer_book.set_answers(['1.3', ''])
    >>> prerelease.main()
    Question: ...
    >>> utils.test_answer_book.set_answers(['', ''])
    >>> postrelease.main()
    Current ...

No errors are raised and an ``----`` underline is assumed for the new header.
The old one is left untouched:

    >>> print open('CHANGES.txt').read()
    1.4 (unreleased)
    ----------------
    <BLANKLINE>
    - Nothing changed yet.
    <BLANKLINE>
    <BLANKLINE>
    1.3 (2009-11-26)
    <BLANKLINE>
    - hello
