# Copyright (c) 2002, 2003 by Intevation GmbH
# Authors:
# Bernhard Herzog <bh@intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with Thuban for details.

"""
Main entry point for the Thuban test suite.

Just run this file as a python script to execute all tests
"""

__version__ = "$Revision: 1.13 $"
# $Source: /thubanrepository/thuban/test/runtests.py,v $
# $Id: runtests.py,v 1.13 2003/11/19 15:46:22 bh Exp $

import os

# It should be possible to run the Thuban testsuite without an X
# connection, so we remove the DISPLAY environment variable which should
# lead to an error if the wxGTK module is imported accidentally. The
# DISPLAY variable is not always set so we catch and ignore the KeyError
try:
    del os.environ["DISPLAY"]
except KeyError:
    pass

import sys
import warnings
import unittest
import getopt

import support
support.initthuban()
import Thuban.Lib.connector

def main():
    """Run all the tests in the Thuban test suite"""

    # Turn Thuban's deprecation warnings into errors so they're cought
    # by the tests
    #
    # Maintenance: Keep a warning filter until the backwards
    # compatibility code is removed at which time using the old
    # interfaces should lead to other errors anyway.

    # The layer attributes table, shapetable, shapefile and filename are
    # deprecated.
    warnings.filterwarnings("error", "The Layer attribute.*is deprecated",
                            DeprecationWarning)

    verbosity = 1

    opts, args = getopt.getopt(sys.argv[1:], 'v', ['verbose'])
    for optchar, value in opts:
        if optchar in ("-v", "--verbose"):
            verbosity = 2
        else:
            print>>sys.stderr, "Unknown option", optchar

    # All Python files starting with test in the current directory
    # contain test cases.
    # FIXME: It should be possible to run runtests.py even when not in
    # the test directory
    files = os.listdir(os.curdir)
    if args:
        names = args
    else:
        names = []
        for file in files:
            if file[:4] == "test" and file[-3:] == ".py":
                names.append(file[:-3])

    suite = unittest.defaultTestLoader.loadTestsFromNames(names)
    runner = support.ThubanTestRunner(verbosity = verbosity)
    result = support.execute_as_testsuite(runner.run, suite)

    sys.exit(not result.wasSuccessful())


if __name__ == "__main__":
    main()
