import os, sys

import glob
import doctest
import unittest
from Globals import package_home
from Products.PloneTestCase import PloneTestCase
from Testing.ZopeTestCase import FunctionalDocFileSuite as Suite

PloneTestCase.installProduct('PleiadesGeocoder')
PloneTestCase.setupPloneSite(products=['PleiadesGeocoder'])

TEST_HOME = os.path.abspath(os.path.dirname(__file__))

REQUIRE_TESTBROWSER = ['FolderGeoViews.txt', 'Georeferencing.txt']

OPTIONFLAGS = (doctest.REPORT_ONLY_FIRST_FAILURE |
               doctest.ELLIPSIS |
               doctest.NORMALIZE_WHITESPACE)

def list_doctests():
    return [filename for filename in
            glob.glob(os.path.sep.join([TEST_HOME, '*.txt']))]

def list_nontestbrowser_tests():
    return [filename for filename in list_doctests()
            if os.path.basename(filename) not in REQUIRE_TESTBROWSER]

def test_suite():

    # BBB: We can obviously remove this when testbrowser is Plone
    #      mainstream, read: with Five 1.4.
    try:
        import Products.Five.testbrowser
    except ImportError:
        print >> sys.stderr, ("WARNING: testbrowser not found - you probably"
                              "need to add Five 1.4 to the Products folder. "
                              "testbrowser tests skipped")
        filenames = list_nontestbrowser_tests()
    else:
        filenames = list_doctests()

    return unittest.TestSuite(
        [Suite(os.path.basename(filename),
               optionflags=OPTIONFLAGS,
               package='Products.PleiadesGeocoder.tests',
               test_class=PloneTestCase.FunctionalTestCase)
         for filename in filenames]
        )

