/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/selftest/__init__.py

  • Committer: Martin Pool
  • Date: 2005-06-22 03:09:57 UTC
  • Revision ID: mbp@sourcefrog.net-20050622030957-692ff24093ce2a86
- add help, try, apply options to pwk script

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 by Canonical Ltd
 
2
 
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
 
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
 
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
 
 
18
from unittest import TestResult, TestCase
 
19
 
 
20
try:
 
21
    import shutil
 
22
    from subprocess import call, Popen, PIPE
 
23
except ImportError, e:
 
24
    sys.stderr.write("testbzr: sorry, this test suite requires the subprocess module\n"
 
25
                     "this is shipped with python2.4 and available separately for 2.3\n")
 
26
    raise
 
27
 
 
28
 
 
29
class CommandFailed(Exception):
 
30
    pass
 
31
 
 
32
 
 
33
class TestBase(TestCase):
 
34
    """Base class for bzr test cases.
 
35
 
 
36
    Just defines some useful helper functions; doesn't actually test
 
37
    anything.
 
38
    """
 
39
    
 
40
    # TODO: Special methods to invoke bzr, so that we can run it
 
41
    # through a specified Python intepreter
 
42
 
 
43
    OVERRIDE_PYTHON = None # to run with alternative python 'python'
 
44
    BZRPATH = 'bzr'
 
45
    
 
46
 
 
47
    def formcmd(self, cmd):
 
48
        if isinstance(cmd, basestring):
 
49
            cmd = cmd.split()
 
50
 
 
51
        if cmd[0] == 'bzr':
 
52
            cmd[0] = self.BZRPATH
 
53
            if self.OVERRIDE_PYTHON:
 
54
                cmd.insert(0, self.OVERRIDE_PYTHON)
 
55
 
 
56
        self.log('$ %r' % cmd)
 
57
 
 
58
        return cmd
 
59
 
 
60
 
 
61
    def runcmd(self, cmd, retcode=0):
 
62
        """Run one command and check the return code.
 
63
 
 
64
        Returns a tuple of (stdout,stderr) strings.
 
65
 
 
66
        If a single string is based, it is split into words.
 
67
        For commands that are not simple space-separated words, please
 
68
        pass a list instead."""
 
69
        cmd = self.formcmd(cmd)
 
70
 
 
71
        self.log('$ ' + ' '.join(cmd))
 
72
        actual_retcode = call(cmd, stdout=self.TEST_LOG, stderr=self.TEST_LOG)
 
73
 
 
74
        if retcode != actual_retcode:
 
75
            raise CommandFailed("test failed: %r returned %d, expected %d"
 
76
                                % (cmd, actual_retcode, retcode))
 
77
 
 
78
 
 
79
 
 
80
    def log(self, msg):
 
81
        """Log a message to a progress file"""
 
82
        print >>self.TEST_LOG, msg
 
83
               
 
84
 
 
85
 
 
86
 
 
87
class _MyResult(TestResult):
 
88
    """
 
89
    Custom TestResult.
 
90
 
 
91
    No special behaviour for now.
 
92
    """
 
93
#     def startTest(self, test):
 
94
#         print str(test).ljust(50),
 
95
#         TestResult.startTest(self, test)
 
96
 
 
97
#     def stopTest(self, test):
 
98
#         print
 
99
#         TestResult.stopTest(self, test)
 
100
 
 
101
 
 
102
    pass
 
103
 
 
104
 
 
105
 
 
106
 
 
107
def selftest():
 
108
    from unittest import TestLoader, TestSuite
 
109
    import bzrlib
 
110
    import bzrlib.selftest.whitebox
 
111
    import bzrlib.selftest.blackbox
 
112
    from doctest import DocTestSuite
 
113
    import os
 
114
    import shutil
 
115
    import time
 
116
 
 
117
    _setup_test_log()
 
118
    _setup_test_dir()
 
119
 
 
120
    suite = TestSuite()
 
121
    tl = TestLoader()
 
122
 
 
123
    for m in bzrlib.selftest.whitebox, :
 
124
        suite.addTest(tl.loadTestsFromModule(m))
 
125
 
 
126
    suite.addTest(bzrlib.selftest.blackbox.suite())
 
127
 
 
128
    for m in bzrlib.store, bzrlib.inventory, bzrlib.branch, bzrlib.osutils, \
 
129
            bzrlib.commands:
 
130
        suite.addTest(DocTestSuite(m))
 
131
 
 
132
    result = _MyResult()
 
133
    suite.run(result)
 
134
 
 
135
    _show_results(result)
 
136
 
 
137
    return result.wasSuccessful()
 
138
 
 
139
 
 
140
def _setup_test_log():
 
141
    import time
 
142
    import os
 
143
    
 
144
    log_filename = os.path.abspath('testbzr.log')
 
145
    TestBase.TEST_LOG = open(log_filename, 'wt', buffering=1) # line buffered
 
146
 
 
147
    print >>TestBase.TEST_LOG, "bzr tests run at " + time.ctime()
 
148
    print '%-30s %s' % ('test log', log_filename)
 
149
 
 
150
 
 
151
def _setup_test_dir():
 
152
    import os
 
153
    import shutil
 
154
    
 
155
    TestBase.ORIG_DIR = os.getcwdu()
 
156
    TestBase.TEST_DIR = os.path.abspath("testbzr.tmp")
 
157
 
 
158
    print '%-30s %s' % ('running tests in', TestBase.TEST_DIR)
 
159
 
 
160
    if os.path.exists(TestBase.TEST_DIR):
 
161
        shutil.rmtree(TestBase.TEST_DIR)
 
162
    os.mkdir(TestBase.TEST_DIR)
 
163
    os.chdir(TestBase.TEST_DIR)
 
164
 
 
165
    # make a fake bzr directory there to prevent any tests propagating
 
166
    # up onto the source directory's real branch
 
167
    os.mkdir(os.path.join(TestBase.TEST_DIR, '.bzr'))
 
168
 
 
169
    
 
170
 
 
171
def _show_results(result):
 
172
     for case, tb in result.errors:
 
173
         _show_test_failure('ERROR', case, tb)
 
174
 
 
175
     for case, tb in result.failures:
 
176
         _show_test_failure('FAILURE', case, tb)
 
177
         
 
178
     print
 
179
     print '%4d tests run' % result.testsRun
 
180
     print '%4d errors' % len(result.errors)
 
181
     print '%4d failures' % len(result.failures)
 
182
 
 
183
 
 
184
 
 
185
def _show_test_failure(kind, case, tb):
 
186
     print (kind + '! ').ljust(60, '-')
 
187
     print case
 
188
     print tb
 
189
     print ''.ljust(60, '-')
 
190