~bzr-pqm/bzr/bzr.dev

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Copyright (C) 2005 by Canonical Ltd

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


from unittest import TestResult, TestCase


class TestBase(TestCase):
    """Base class for bzr test cases.

    Just defines some useful helper functions; doesn't actually test
    anything.
    """
    # TODO: Special methods to invoke bzr
    
    def runcmd(self, cmd, expected=0):
        self.log('$ ' + ' '.join(cmd))
        from os import spawnvp, P_WAIT
        rc = spawnvp(P_WAIT, cmd[0], cmd)
        if rc != expected:
            self.fail("command %r returned status %d" % (cmd, rc))


    def backtick(self, cmd):
        """Run a command and return its output"""
        from os import popen
        self.log('$ ' + ' '.join(cmd))
        pipe = popen(cmd)
        out = ''
        while True:
            buf = pipe.read()
            if buf:
                out += buf
            else:
                break
        rc = pipe.close()
        if rc:
            self.fail("command %r returned status %d" % (cmd, rc))
        else:
            return out
            

    def log(self, msg):
        """Log a message to a progress file"""
        print >>TEST_LOG, msg
               



class _MyResult(TestResult):
    """
    Custom TestResult.

    No special behaviour for now.
    """
#     def startTest(self, test):
#         print str(test).ljust(50),
#         TestResult.startTest(self, test)

#     def stopTest(self, test):
#         print
#         TestResult.stopTest(self, test)


    pass




def selftest():
    from unittest import TestLoader, TestSuite
    import bzrlib
    import bzrlib.selftest.whitebox
    import bzrlib.selftest.blackbox
    from doctest import DocTestSuite
    import os
    import shutil
    import time

    _setup_test_log()
    _setup_test_dir()

    suite = TestSuite()
    tl = TestLoader()

    for m in bzrlib.selftest.whitebox, bzrlib.selftest.blackbox:
        suite.addTest(tl.loadTestsFromModule(m))

    for m in bzrlib.store, bzrlib.inventory, bzrlib.branch, bzrlib.osutils, \
            bzrlib.commands:
        suite.addTest(DocTestSuite(m))

    result = _MyResult()
    suite.run(result)

    _show_results(result)

    return result.wasSuccessful()


def _setup_test_log():
    import time
    import os
    
    global TEST_LOG
    log_filename = os.path.abspath('testbzr.log')
    TEST_LOG = open(log_filename, 'wt', buffering=1) # line buffered

    print >>TEST_LOG, "bzr tests run at " + time.ctime()
    print '%-30s %s' % ('test log', log_filename)


def _setup_test_dir():
    import os
    import shutil
    
    global ORIG_DIR, TEST_DIR
    ORIG_DIR = os.getcwdu()
    TEST_DIR = os.path.abspath("testbzr.tmp")

    print '%-30s %s' % ('running tests in', TEST_DIR)

    if os.path.exists(TEST_DIR):
        shutil.rmtree(TEST_DIR)
    os.mkdir(TEST_DIR)
    os.chdir(TEST_DIR)    

    

def _show_results(result):
     for case, tb in result.errors:
         _show_test_failure('ERROR', case, tb)

     for case, tb in result.failures:
         _show_test_failure('FAILURE', case, tb)
         
     print
     print '%4d tests run' % result.testsRun
     print '%4d errors' % len(result.errors)
     print '%4d failures' % len(result.failures)



def _show_test_failure(kind, case, tb):
     print (kind + '! ').ljust(60, '-')
     print case
     print tb
     print ''.ljust(60, '-')