bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
608
by Martin Pool
- Split selftests out into a new module and start changing them |
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 |
||
|
609
by Martin Pool
- cleanup test code |
17 |
|
|
720
by Martin Pool
- start moving external tests into the testsuite framework |
18 |
from unittest import TestResult, TestCase |
|
719
by Martin Pool
- reorganize selftest code |
19 |
|
|
721
by Martin Pool
- framework for running external commands from unittest suite |
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 |
||
|
720
by Martin Pool
- start moving external tests into the testsuite framework |
63 |
class _MyResult(TestResult): |
|
721
by Martin Pool
- framework for running external commands from unittest suite |
64 |
""" |
65 |
Custom TestResult.
|
|
66 |
||
67 |
No special behaviour for now.
|
|
68 |
"""
|
|
|
719
by Martin Pool
- reorganize selftest code |
69 |
# def startTest(self, test):
|
70 |
# print str(test).ljust(50),
|
|
|
720
by Martin Pool
- start moving external tests into the testsuite framework |
71 |
# TestResult.startTest(self, test)
|
|
719
by Martin Pool
- reorganize selftest code |
72 |
|
73 |
# def stopTest(self, test):
|
|
74 |
# print
|
|
|
720
by Martin Pool
- start moving external tests into the testsuite framework |
75 |
# TestResult.stopTest(self, test)
|
|
719
by Martin Pool
- reorganize selftest code |
76 |
|
77 |
||
78 |
pass
|
|
79 |
||
80 |
||
81 |
||
|
720
by Martin Pool
- start moving external tests into the testsuite framework |
82 |
|
|
608
by Martin Pool
- Split selftests out into a new module and start changing them |
83 |
def selftest(): |
|
721
by Martin Pool
- framework for running external commands from unittest suite |
84 |
from unittest import TestLoader, TestSuite |
85 |
import bzrlib |
|
86 |
import bzrlib.whitebox |
|
87 |
import bzrlib.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.whitebox, bzrlib.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
|
|
|
719
by Martin Pool
- reorganize selftest code |
151 |
print '%4d tests run' % result.testsRun |
152 |
print '%4d errors' % len(result.errors) |
|
153 |
print '%4d failures' % len(result.failures) |
|
154 |
||
|
721
by Martin Pool
- framework for running external commands from unittest suite |
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 |