summaryrefslogtreecommitdiff
path: root/src/zope/tal/tests/test_files.py
blob: 8d453d59c7ac1585347d6f46cc3b90fbf5c5b8b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
"""Tests that run driver.py over input files comparing to output files.
"""

import glob
import os
import sys
import unittest

try:
    # Python 2.x
    from cStringIO import StringIO
except ImportError:
    # Python 3.x
    from io import StringIO

import zope.tal.runtest

from zope.tal.tests import utils


class FileTestCase(unittest.TestCase):

    def __init__(self, file, dir):
        self.__file = file
        self.__dir = dir
        unittest.TestCase.__init__(self)

    # For unittest.
    def shortDescription(self):
        path = os.path.basename(self.__file)
        return '%s (%s)' % (path, self.__class__)

    def id(self):
        return os.path.relpath(self.__file, os.path.dirname(__file__))

    __str__ = id

    def runTest(self):
        basename = os.path.basename(self.__file)
        if basename.startswith('test_sa'):
            sys.argv = ["runtest.py", "-Q", "-a", self.__file]
        elif basename.startswith('test_metal'):
            sys.argv = ["runtest.py", "-Q", "-m", self.__file]
        else:
            sys.argv = ["runtest.py", "-Q", self.__file]
        pwd = os.getcwd()
        try:
            os.chdir(self.__dir)
            old_stdout = sys.stdout
            sys.stdout = StringIO()
            failed = zope.tal.runtest.main()
        finally:
            captured_stdout = sys.stdout.getvalue()
            sys.stdout = old_stdout
            os.chdir(pwd)
        if failed:
            self.fail("output for %s didn't match:\n%s"
                      % (self.__file, captured_stdout))

try:
    script = __file__
except NameError:
    script = sys.argv[0]

def test_suite():
    suite = unittest.TestSuite()
    dir = os.path.dirname(script)
    dir = os.path.abspath(dir)
    parentdir = os.path.dirname(dir)
    prefix = os.path.join(dir, "input", "test*.")
    if utils.skipxml:
        xmlargs = []
    else:
        xmlargs = glob.glob(prefix + "xml")
        xmlargs.sort()
    htmlargs = glob.glob(prefix + "html")
    htmlargs.sort()
    args = xmlargs + htmlargs
    if not args:
        sys.stderr.write("Warning: no test input files found!!!\n")
    for arg in args:
        case = FileTestCase(arg, parentdir)
        suite.addTest(case)
    return suite