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.answers_for_testing = ['', '']
    >>> 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:

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

Run the postrelease script.  Since the version number does not end
with a number, the script cannot make a suggestion:

    >>> utils.answers_for_testing = ['0.2', '']
    >>> from zest.releaser import postrelease
    >>> postrelease.main()
    Current version is '0.1b'
    Question: Enter new development version ('.dev0' will be appended):
    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.answers_for_testing = ['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.answers_for_testing = ['', '']
    >>> 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.answers_for_testing = ['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.answers_for_testing = ['', '', '0.3rc', '']
    >>> postrelease.main()
    Current version is '0.3beta'
    Question: Enter new development version ('.dev0' will be appended):
    Our reply: <ENTER>
    Question: Enter new development version ('.dev0' will be appended):
    Our reply: <ENTER>
    Question: Enter new development version ('.dev0' will be appended):
    Our reply: 0.3rc
    Checking data dict
    Question: OK to commit this (Y/n)?
    Our reply: <ENTER>

Numbers and characters can be combined:

    >>> utils.answers_for_testing = ['1.0a1', '']
    >>> prerelease.main()
    Question: Enter version [0.3rc]:
    Our reply: 1.0a1
    Checking data dict
    Question: OK to commit this (Y/n)?
    Our reply: <ENTER>
    >>> utils.answers_for_testing = ['', '']
    >>> 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.answers_for_testing = ['1.0', '']
    >>> prerelease.main()
    Question: ...
    >>> open('CHANGES.txt', 'wb').write('')
    >>> utils.answers_for_testing = ['', '']
    >>> 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.answers_for_testing = ['', '']
    >>> prerelease.main()
    Question: ...
    >>> os.remove('CHANGES.txt')
    >>> utils.answers_for_testing = ['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', 'wb').write('1.0 (1972-12-25)\n\n- hello\n')
    >>> utils.answers_for_testing = ['1.3', '']
    >>> prerelease.main()
    Question: ...
    >>> utils.answers_for_testing = ['', '']
    >>> 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
