##############################################################################
#
# Copyright (c) 2009 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Unit Testing Feature

$Id: unittest.py 98421 2009-03-27 12:42:04Z pcardune $
"""
import os
import zope.interface
from zope.schema.fieldproperty import FieldProperty

from z3c.builder.core import buildout, python
from z3c.builder.core.base import SimpleFileBuilder
from z3c.feature.core import base, interfaces

TESTING_DOCUMENTATION = """
The Automated Testing feature generates boiler plate code for running
unittests, specifically in the form of DocTests.  Here are the various
pieces generated by the Automated Testing Feature:

  1. **new buildout.cfg sections**

       Three new sections are added to the buildout.cfg file. Each
       correspond to executable scripts in the bin/ directory.  There is
       a test runner which you can run with this command::

         $ ./bin/test

       There are many command line options that can be used by the test
       runner.  See them all by running::

         $ ./bin/test --help

       You can also generate coverage reports for the tests with the command::

         $ ./bin/coverage-test

       This runs the same test runner as ``./bin/test`` buts adds on the
       options for generating coverage reports which are always a little
       painful to remember.  By default, this will put all the reports in
       a ``coverage/`` directory. After you have generated the coverage
       reports, you render them into nice color-coded html pages with
       this command::

         $ ./bin/coverage-report

  2. **new setup.py extras_require section**

       In order to support the test runner, a new extras_require section
       called ``test`` is added to the ``setup.py`` file.  This pulls in
       z3c.coverage and zope.testing.

  3. **``tests`` module**

       The test runner will search through your project src for modules
       starting with the name ``test``.  The Automated Testing feature
       adds a sub package named ``tests`` to the project source, and adds
       a python module called ``test_doc.py`` into the tests module.  The
       test_doc module hooks up the test suite that references the doc
       tests.
"""

class TestingFeature(base.BaseFeature):
    zope.interface.implements(interfaces.ITestingFeature)

    iface = interfaces.ITestingFeature

    coverageDirectory = FieldProperty(
        interfaces.ITestingFeature['coverageDirectory'])
    coverageReportDirectory = FieldProperty(
        interfaces.ITestingFeature['coverageReportDirectory'])

    featureTitle = u'Automated Tests'
    featureDocumentation = TESTING_DOCUMENTATION

    def _applyTo(self, project):
        testPartBuilder = buildout.PartBuilder(
            u'test',
            values=[('recipe','zc.recipe.testrunner'),
                    ('eggs',project.projectName+' [test]')])

        project.buildout.add(testPartBuilder)

        coverageTestPart = buildout.PartBuilder(
            u'coverage-test',
            values=[('recipe','zc.recipe.testrunner'),
                    ('eggs',project.projectName+' [test]'),
                    ('defaults',"['--coverage', '%s']" % self.coverageDirectory)])

        project.buildout.add(coverageTestPart)

        coverageReportPart = buildout.PartBuilder(
            u'coverage-report',
            values=[('recipe','zc.recipe.egg'),
                    ('eggs','z3c.coverage'),
                    ('scripts','coverage=coverage-report'),
                    ('arguments',"('coverage', '%s')" % self.coverageReportDirectory)])

        project.buildout.add(coverageReportPart)

        project.setup.addExtrasRequires('test',('zope.testing',
                                                'z3c.coverage'))

        project.package.add(python.PackageBuilder(u'tests'))
        project.package['tests'].add(
            SimpleFileBuilder(u'test_doc.py',
                              template=os.path.join(
                                  os.path.dirname(__file__),
                                  'file-templates',
                                  'test_doc.py')
                              ))

        project.package.add(
            SimpleFileBuilder(u'README.txt',
                              template=os.path.join(
                                  os.path.dirname(__file__),
                                  'file-templates',
                                  'README.txt')
                              ))
