/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-21 06:10:54 UTC
  • Revision ID: mbp@sourcefrog.net-20050621061054-8fc3f7478e841993
- todo: bzr mkdir

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
 
 
21
class TestBase(TestCase):
 
22
    """Base class for bzr test cases.
 
23
 
 
24
    Just defines some useful helper functions; doesn't actually test
 
25
    anything.
 
26
    """
 
27
    # TODO: Special methods to invoke bzr
 
28
    
 
29
    def runcmd(self, cmd, expected=0):
 
30
        self.log('$ ' + ' '.join(cmd))
 
31
        from os import spawnvp, P_WAIT
 
32
        rc = spawnvp(P_WAIT, cmd[0], cmd)
 
33
        if rc != expected:
 
34
            self.fail("command %r returned status %d" % (cmd, rc))
 
35
 
 
36
 
 
37
    def backtick(self, cmd):
 
38
        """Run a command and return its output"""
 
39
        from os import popen
 
40
        self.log('$ ' + ' '.join(cmd))
 
41
        pipe = popen(cmd)
 
42
        out = ''
 
43
        while True:
 
44
            buf = pipe.read()
 
45
            if buf:
 
46
                out += buf
 
47
            else:
 
48
                break
 
49
        rc = pipe.close()
 
50
        if rc:
 
51
            self.fail("command %r returned status %d" % (cmd, rc))
 
52
        else:
 
53
            return out
 
54
            
 
55
 
 
56
    def log(self, msg):
 
57
        """Log a message to a progress file"""
 
58
        print >>TEST_LOG, msg
 
59
               
 
60
 
 
61
 
 
62
 
 
63
class _MyResult(TestResult):
 
64
    """
 
65
    Custom TestResult.
 
66
 
 
67
    No special behaviour for now.
 
68
    """
 
69
#     def startTest(self, test):
 
70
#         print str(test).ljust(50),
 
71
#         TestResult.startTest(self, test)
 
72
 
 
73
#     def stopTest(self, test):
 
74
#         print
 
75
#         TestResult.stopTest(self, test)
 
76
 
 
77
 
 
78
    pass
 
79
 
 
80
 
 
81
 
 
82
 
 
83
def selftest():
 
84
    from unittest import TestLoader, TestSuite
 
85
    import bzrlib
 
86
    import bzrlib.selftest.whitebox
 
87
    import bzrlib.selftest.blackbox
 
88
    from doctest import DocTestSuite
 
89
    import os
 
90
    import shutil
 
91
    import time
 
92
 
 
93
    _setup_test_log()
 
94
    _setup_test_dir()
 
95
 
 
96
    suite = TestSuite()
 
97
    tl = TestLoader()
 
98
 
 
99
    for m in bzrlib.selftest.whitebox, bzrlib.selftest.blackbox:
 
100
        suite.addTest(tl.loadTestsFromModule(m))
 
101
 
 
102
    for m in bzrlib.store, bzrlib.inventory, bzrlib.branch, bzrlib.osutils, \
 
103
            bzrlib.commands:
 
104
        suite.addTest(DocTestSuite(m))
 
105
 
 
106
    result = _MyResult()
 
107
    suite.run(result)
 
108
 
 
109
    _show_results(result)
 
110
 
 
111
    return result.wasSuccessful()
 
112
 
 
113
 
 
114
def _setup_test_log():
 
115
    import time
 
116
    import os
 
117
    
 
118
    global TEST_LOG
 
119
    log_filename = os.path.abspath('testbzr.log')
 
120
    TEST_LOG = open(log_filename, 'wt', buffering=1) # line buffered
 
121
 
 
122
    print >>TEST_LOG, "bzr tests run at " + time.ctime()
 
123
    print '%-30s %s' % ('test log', log_filename)
 
124
 
 
125
 
 
126
def _setup_test_dir():
 
127
    import os
 
128
    import shutil
 
129
    
 
130
    global ORIG_DIR, TEST_DIR
 
131
    ORIG_DIR = os.getcwdu()
 
132
    TEST_DIR = os.path.abspath("testbzr.tmp")
 
133
 
 
134
    print '%-30s %s' % ('running tests in', TEST_DIR)
 
135
 
 
136
    if os.path.exists(TEST_DIR):
 
137
        shutil.rmtree(TEST_DIR)
 
138
    os.mkdir(TEST_DIR)
 
139
    os.chdir(TEST_DIR)    
 
140
 
 
141
    
 
142
 
 
143
def _show_results(result):
 
144
     for case, tb in result.errors:
 
145
         _show_test_failure('ERROR', case, tb)
 
146
 
 
147
     for case, tb in result.failures:
 
148
         _show_test_failure('FAILURE', case, tb)
 
149
         
 
150
     print
 
151
     print '%4d tests run' % result.testsRun
 
152
     print '%4d errors' % len(result.errors)
 
153
     print '%4d failures' % len(result.failures)
 
154
 
 
155
 
 
156
 
 
157
def _show_test_failure(kind, case, tb):
 
158
     print (kind + '! ').ljust(60, '-')
 
159
     print case
 
160
     print tb
 
161
     print ''.ljust(60, '-')
 
162