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
|
# Copyright (C) 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 unittest
from morphlib.morph2 import Morphology
from morphlib.morphologyfactory import (MorphologyFactory,
AutodetectError,
NotcachedError)
from morphlib.remoterepocache import CatFileError
class FakeRemoteRepoCache(object):
def cat_file(self, reponame, sha1, filename):
if filename.endswith('.morph'):
return '''{
"name": "remote-foo",
"kind": "chunk",
"build-system": "bar"
}'''
return 'text'
class FakeLocalRepo(object):
def cat(self, sha1, filename):
if filename.endswith('.morph'):
return '''{
"name": "local-foo",
"kind": "chunk",
"build-system": "bar"
}'''
return 'text'
class FakeLocalRepoCache(object):
def __init__(self, lr):
self.lr = lr
def has_repo(self, reponame):
return True
def get_repo(self, reponame):
return self.lr
class MorphologyFactoryTests(unittest.TestCase):
def setUp(self):
self.lr = FakeLocalRepo()
self.lrc = FakeLocalRepoCache(self.lr)
self.rrc = FakeRemoteRepoCache()
self.mf = MorphologyFactory(self.lrc, self.rrc)
self.lmf = MorphologyFactory(self.lrc, None)
def nolocalfile(self, *args):
raise IOError('File not found')
def noremotefile(self, *args):
raise CatFileError('reponame', 'ref', 'filename')
def nolocalmorph(self, *args):
if args[-1].endswith('.morph'):
raise IOError('File not found')
return 'text'
def noremotemorph(self, *args):
if args[-1].endswith('.morph'):
raise CatFileError('reponame', 'ref', 'filename')
return 'text'
def doesnothaverepo(self, reponame):
return False
def test_gets_morph_from_local_repo(self):
morph = self.mf.get_morphology('reponame', 'sha1',
'foo.morph')
self.assertEqual('local-foo', morph['name'])
def test_gets_morph_from_remote_repo(self):
self.lrc.has_repo = self.doesnothaverepo
morph = self.mf.get_morphology('reponame', 'sha1',
'foo.morph')
self.assertEqual('remote-foo', morph['name'])
def test_autodetects_local_morphology(self):
self.lr.cat = self.nolocalmorph
morph = self.mf.get_morphology('reponame', 'sha1',
'assumed-local.morph')
self.assertEqual('assumed-local', morph['name'])
def test_autodetects_remote_morphology(self):
self.lrc.has_repo = self.doesnothaverepo
self.rrc.cat_file = self.noremotemorph
morph = self.mf.get_morphology('reponame', 'sha1',
'assumed-remote.morph')
self.assertEqual('assumed-remote', morph['name'])
def test_raises_error_when_fails_detect_locally(self):
self.lr.cat = self.nolocalfile
self.assertRaises(AutodetectError, self.mf.get_morphology,
'reponame', 'sha1', 'unreached.morph')
def test_raises_error_when_fails_detect_remotely(self):
self.lrc.has_repo = self.doesnothaverepo
self.rrc.cat_file = self.noremotefile
# self.mf.get_morphology('reponame', 'sha1', 'unreached.morph')
self.assertRaises(AutodetectError, self.mf.get_morphology,
'reponame', 'sha1', 'unreached.morph')
def test_looks_locally_with_no_remote(self):
morph = self.lmf.get_morphology('reponame', 'sha1',
'foo.morph')
self.assertEqual('local-foo', morph['name'])
def test_autodetects_locally_with_no_remote(self):
self.lr.cat = self.nolocalmorph
morph = self.lmf.get_morphology('reponame', 'sha1',
'assumed-local.morph')
self.assertEqual('assumed-local', morph['name'])
def test_fails_when_local_not_cached_and_no_remote(self):
self.lrc.has_repo = self.doesnothaverepo
self.assertRaises(NotcachedError, self.lmf.get_morphology,
'reponame', 'sha1', 'unreached.morph')
|