~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_knit.py

  • Committer: Gary van der Merwe
  • Date: 2010-08-02 19:56:52 UTC
  • mfrom: (5050.3.18 2.2)
  • mto: (5050.3.19 2.2)
  • mto: This revision was merged to the branch mainline in revision 5371.
  • Revision ID: garyvdm@gmail.com-20100802195652-o1ppjemhwrr98i61
MergeĀ lp:bzr/2.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2006-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
17
17
"""Tests for Knit data structure"""
18
18
 
19
19
from cStringIO import StringIO
20
 
import difflib
21
 
import gzip
22
20
import sys
23
21
 
24
22
from bzrlib import (
25
23
    errors,
26
 
    generate_ids,
27
24
    knit,
28
25
    multiparent,
29
26
    osutils,
30
27
    pack,
 
28
    tests,
 
29
    transport,
 
30
    tuned_gzip,
31
31
    )
32
32
from bzrlib.errors import (
33
 
    RevisionAlreadyPresent,
34
33
    KnitHeaderError,
35
 
    RevisionNotPresent,
36
34
    NoSuchFile,
37
35
    )
38
36
from bzrlib.index import *
39
37
from bzrlib.knit import (
40
38
    AnnotatedKnitContent,
41
39
    KnitContent,
42
 
    KnitSequenceMatcher,
43
40
    KnitVersionedFiles,
44
41
    PlainKnitContent,
45
42
    _VFContentMapGenerator,
49
46
    _KnitKeyAccess,
50
47
    make_file_factory,
51
48
    )
 
49
from bzrlib.patiencediff import PatienceSequenceMatcher
52
50
from bzrlib.repofmt import pack_repo
53
51
from bzrlib.tests import (
54
 
    Feature,
55
 
    KnownFailure,
56
52
    TestCase,
57
53
    TestCaseWithMemoryTransport,
58
54
    TestCaseWithTransport,
59
55
    TestNotApplicable,
60
56
    )
61
 
from bzrlib.transport import get_transport
62
 
from bzrlib.transport.memory import MemoryTransport
63
 
from bzrlib.tuned_gzip import GzipFile
64
57
from bzrlib.versionedfile import (
65
58
    AbsentContentFactory,
66
59
    ConstantMapper,
69
62
    )
70
63
 
71
64
 
72
 
class _CompiledKnitFeature(Feature):
73
 
 
74
 
    def _probe(self):
75
 
        try:
76
 
            import bzrlib._knit_load_data_pyx
77
 
        except ImportError:
78
 
            return False
79
 
        return True
80
 
 
81
 
    def feature_name(self):
82
 
        return 'bzrlib._knit_load_data_pyx'
83
 
 
84
 
CompiledKnitFeature = _CompiledKnitFeature()
 
65
compiled_knit_feature = tests.ModuleAvailableFeature(
 
66
                            'bzrlib._knit_load_data_pyx')
85
67
 
86
68
 
87
69
class KnitContentTestsMixin(object):
116
98
        line_delta = source_content.line_delta(target_content)
117
99
        delta_blocks = list(KnitContent.get_line_delta_blocks(line_delta,
118
100
            source_lines, target_lines))
119
 
        matcher = KnitSequenceMatcher(None, source_lines, target_lines)
120
 
        matcher_blocks = list(list(matcher.get_matching_blocks()))
 
101
        matcher = PatienceSequenceMatcher(None, source_lines, target_lines)
 
102
        matcher_blocks = list(matcher.get_matching_blocks())
121
103
        self.assertEqual(matcher_blocks, delta_blocks)
122
104
 
123
105
    def test_get_line_delta_blocks(self):
366
348
        :return: (versioned_file, reload_counter)
367
349
            versioned_file  a KnitVersionedFiles using the packs for access
368
350
        """
369
 
        builder = self.make_branch_builder('.')
 
351
        builder = self.make_branch_builder('.', format="1.9")
370
352
        builder.start_series()
371
353
        builder.build_snapshot('rev-1', None, [
372
354
            ('add', ('', 'root-id', 'directory', None)),
710
692
 
711
693
    def create_gz_content(self, text):
712
694
        sio = StringIO()
713
 
        gz_file = gzip.GzipFile(mode='wb', fileobj=sio)
 
695
        gz_file = tuned_gzip.GzipFile(mode='wb', fileobj=sio)
714
696
        gz_file.write(text)
715
697
        gz_file.close()
716
698
        return sio.getvalue()
872
854
 
873
855
    def get_knit_index(self, transport, name, mode):
874
856
        mapper = ConstantMapper(name)
875
 
        orig = knit._load_data
876
 
        def reset():
877
 
            knit._load_data = orig
878
 
        self.addCleanup(reset)
879
857
        from bzrlib._knit_load_data_py import _load_data_py
880
 
        knit._load_data = _load_data_py
 
858
        self.overrideAttr(knit, '_load_data', _load_data_py)
881
859
        allow_writes = lambda: 'w' in mode
882
860
        return _KndxIndex(transport, mapper, lambda:None, allow_writes, lambda:True)
883
861
 
1308
1286
 
1309
1287
class LowLevelKnitIndexTests_c(LowLevelKnitIndexTests):
1310
1288
 
1311
 
    _test_needs_features = [CompiledKnitFeature]
 
1289
    _test_needs_features = [compiled_knit_feature]
1312
1290
 
1313
1291
    def get_knit_index(self, transport, name, mode):
1314
1292
        mapper = ConstantMapper(name)
1315
 
        orig = knit._load_data
1316
 
        def reset():
1317
 
            knit._load_data = orig
1318
 
        self.addCleanup(reset)
1319
1293
        from bzrlib._knit_load_data_pyx import _load_data_c
1320
 
        knit._load_data = _load_data_c
 
1294
        self.overrideAttr(knit, '_load_data', _load_data_c)
1321
1295
        allow_writes = lambda: mode == 'w'
1322
 
        return _KndxIndex(transport, mapper, lambda:None, allow_writes, lambda:True)
 
1296
        return _KndxIndex(transport, mapper, lambda:None,
 
1297
                          allow_writes, lambda:True)
1323
1298
 
1324
1299
 
1325
1300
class Test_KnitAnnotator(TestCaseWithMemoryTransport):
1596
1571
        # could leave an empty .kndx file, which bzr would later claim was a
1597
1572
        # corrupted file since the header was not present. In reality, the file
1598
1573
        # just wasn't created, so it should be ignored.
1599
 
        t = get_transport('.')
 
1574
        t = transport.get_transport('.')
1600
1575
        t.put_bytes('test.kndx', '')
1601
1576
 
1602
1577
        knit = self.make_test_knit()
1603
1578
 
1604
1579
    def test_knit_index_checks_header(self):
1605
 
        t = get_transport('.')
 
1580
        t = transport.get_transport('.')
1606
1581
        t.put_bytes('test.kndx', '# not really a knit header\n\n')
1607
1582
        k = self.make_test_knit()
1608
1583
        self.assertRaises(KnitHeaderError, k.keys)