1
# Copyright (C) 2005, 2007 Canonical Ltd
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.
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.
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
17
"""Test Escaped Stores."""
19
from cStringIO import StringIO
23
from bzrlib import osutils
24
from bzrlib.errors import BzrError, UnlistableStore, NoSuchFile
25
from bzrlib.store.text import TextStore
26
from bzrlib.tests import TestCaseWithTransport
27
import bzrlib.transport
30
class TestEscaped(TestCaseWithTransport):
31
"""Mixin template class that provides some common tests for stores"""
33
def get_store(self, prefixed=False, escaped=True):
34
t = bzrlib.transport.get_transport(self.get_url())
35
return TextStore(t, prefixed=prefixed, escaped=escaped)
37
def test_prefixed(self):
38
# Prefix should be determined by unescaped string
39
text_store = self.get_store(prefixed=True)
41
# hash_prefix() is not defined for unicode characters
42
# it is only defined for byte streams.
43
# so hash_prefix() needs to operate on *at most* utf-8
44
# encoded. However urlutils.escape() does both encoding to utf-8
45
# and urllib quoting, so we will use the escaped form
46
# as the path passed to hash_prefix
48
self.assertEqual('62/a', text_store._relpath('a'))
49
self.assertEqual('88/%2520', text_store._relpath(' '))
50
self.assertEqual('72/%40%253a%253c%253e',
51
text_store._relpath('@:<>'))
54
text_store = self.get_store(prefixed=True)
56
text_store.add(StringIO('a'), 'a')
57
self.failUnlessExists('62/a')
59
text_store.add(StringIO('space'), ' ')
60
self.failUnlessExists('88/%20')
61
self.assertEquals('space', text_store.get(' ').read())
63
text_store.add(StringIO('surprise'), '@:<>')
64
self.failUnlessExists('72/@%3a%3c%3e')
65
self.assertEquals('surprise', text_store.get('@:<>').read())
67
text_store.add(StringIO('utf8'), '\xc2\xb5')
68
self.failUnlessExists('77/%c2%b5')
69
self.assertEquals('utf8', text_store.get('\xc2\xb5').read())
72
from bzrlib.store.versioned import WeaveStore
73
from bzrlib.transactions import PassThroughTransaction
75
trans = PassThroughTransaction()
77
t = bzrlib.transport.get_transport(self.get_url())
78
weave_store = WeaveStore(t, prefixed=True, escaped=True)
79
def add_text(file_id, rev_id, contents, parents, transaction):
80
vfile = weave_store.get_weave_or_empty(file_id, transaction)
81
vfile.add_lines(rev_id, parents, contents)
83
def check_text(file_id, revision_id, contents):
84
vfile = weave_store.get_weave(file_id, trans)
85
self.assertEqual(contents, vfile.get_lines(revision_id))
87
add_text('a', 'r', ['a'], [], trans)
88
self.failUnlessExists('62/a.weave')
89
check_text('a', 'r', ['a'])
91
add_text(' ', 'r', ['space'], [], trans)
92
self.failIfExists('21/ .weave')
93
self.failUnlessExists('88/%20.weave')
94
check_text(' ', 'r', ['space'])
96
add_text('@:<>', 'r', ['surprise'], [], trans)
97
self.failUnlessExists('72/@%3a%3c%3e.weave')
98
check_text('@:<>', 'r', ['surprise'])
100
add_text('\xc2\xb5', 'r', ['utf8'], [], trans)
101
self.failUnlessExists('77/%c2%b5.weave')
102
check_text('\xc2\xb5', 'r', ['utf8'])