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
|
# Copyright (C) 2011-2012 Codethink Limited
#
# 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; version 2 of the License.
#
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import os
import shutil
import tempfile
import unittest
import morphlib
class CacheDirTests(unittest.TestCase):
def cat(self, relative_name):
with open(os.path.join(self.cachedir.dirname, relative_name)) as f:
return f.read()
def setUp(self):
self.dirname = tempfile.mkdtemp()
self.cachedir = morphlib.cachedir.CacheDir(self.dirname)
def tearDown(self):
shutil.rmtree(self.dirname)
def test_sets_dirname_attribute(self):
self.assertEqual(self.cachedir.dirname, os.path.abspath(self.dirname))
def test_generates_string_key_for_arbitrary_dict_key(self):
key = self.cachedir.key({
'foo': 'bar',
'xyzzy': 'plugh',
})
self.assertEqual(type(key), str)
self.assertNotEqual(key, '')
def test_generates_same_string_key_twice(self):
dict_key = {
'foo': 'bar',
'xyzzy': 'plugh',
}
self.assertEqual(self.cachedir.key(dict_key),
self.cachedir.key(dict_key))
def test_generates_different_string_keys(self):
dict_key_1 = {
'foo': 'bar',
'xyzzy': 'plugh',
}
dict_key_2 = {
'foo': 'foobar',
'xyzzy': 'stevenage',
}
self.assertNotEqual(self.cachedir.key(dict_key_1),
self.cachedir.key(dict_key_2))
def test_returns_a_chunk_pathname_in_cache_directory(self):
dict_key = {
'kind': 'chunk',
'ref': 'DEADBEEF',
'repo': 'git://git.baserock.org/hello/',
'arch': 'armv7',
}
pathname = self.cachedir.name(dict_key)
self.assert_(pathname.startswith(self.cachedir.dirname + '/'))
self.assert_(pathname.endswith('.chunk'))
def test_returns_a_stratum_pathname_in_cache_directory(self):
dict_key = {
'kind': 'stratum',
'ref': 'DEADBEEF',
'repo': 'git://git.baserock.org/hello/',
'arch': 'armv7',
}
pathname = self.cachedir.name(dict_key)
self.assert_(pathname.startswith(self.cachedir.dirname + '/'))
self.assert_(pathname.endswith('.stratum'))
def test_returns_a_valid_pathname_in_cache_directory(self):
dict_key = {
'ref': 'DEADBEEF',
'repo': 'git://git.baserock.org/hello/',
'arch': 'armv7',
}
pathname = self.cachedir.name(dict_key)
self.assert_(pathname.startswith(self.cachedir.dirname + '/'))
def test_allows_file_to_be_written_via_basename(self):
f = self.cachedir.open('foo')
f.write('bar')
f.close()
self.assertEqual(self.cat('foo'), 'bar')
def test_allows_file_to_be_written_via_basename_and_suffix(self):
f = self.cachedir.open('foo', '.blip')
f.write('bar')
f.close()
self.assertEqual(self.cat('foo.blip'), 'bar')
def test_allows_file_to_be_written_via_dict_key(self):
dict_key = {
'kind': 'chunk',
'meh': 'moo',
}
name = self.cachedir.name(dict_key)
f = self.cachedir.open(dict_key)
f.write('bar')
f.close()
self.assertEqual(self.cat(name), 'bar')
def test_allows_file_to_be_aborted(self):
f = self.cachedir.open('foo')
f.write('bar')
f.abort()
pathname = os.path.join(self.cachedir.dirname, 'foo')
self.assertFalse(os.path.exists(pathname))
|