~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/__init__.py

- add TestCase.assertEqualDiffs helper

  much nicer when comparing large values

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
 
18
18
from cStringIO import StringIO
 
19
import difflib
 
20
import errno
19
21
import logging
20
 
import unittest
21
 
import tempfile
22
22
import os
 
23
import re
 
24
import shutil
 
25
import subprocess
23
26
import sys
24
 
import errno
25
 
import subprocess
26
 
import shutil
27
 
import re
 
27
import tempfile
 
28
import unittest
28
29
 
29
30
import bzrlib.commands
30
31
import bzrlib.trace
155
156
        bzrlib.trace.disable_default_logging()
156
157
        self._enable_file_logging()
157
158
 
 
159
    def _ndiff_strings(self, a, b):
 
160
        """Return ndiff between two strings containing lines."""
 
161
        return ''.join(difflib.ndiff(a.splitlines(True), b.splitlines(True)))
158
162
 
 
163
    def assertEqualDiff(self, a, b):
 
164
        """Assert two texts are equal, if not raise an exception.
 
165
        
 
166
        This is intended for use with multi-line strings where it can 
 
167
        be hard to find the differences by eye.
 
168
        """
 
169
        # TODO: perhaps override assertEquals to call this for strings?
 
170
        if a == b:
 
171
            return
 
172
        raise AssertionError("texts not equal:\n" + 
 
173
                             self._ndiff_strings(a, b))      
 
174
        
159
175
    def _enable_file_logging(self):
160
176
        fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
161
177