summaryrefslogtreecommitdiff
path: root/morphlib/sourcemanager_tests.py
blob: eb18dcc346d6df5715454413b9a1c332effa18e1 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# 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 glob
import unittest
import tempfile
import shutil
import os
import subprocess
import urlparse

import morphlib


class DummyApp(object):

    def __init__(self):
        self.settings = {
            'git-base-url': ['.',],
            'bundle-server': None,
            'cachedir': '/foo/bar/baz',
            'ignore-submodules': False
        }
        self.msg = lambda msg: None


class SourceManagerTests(unittest.TestCase):

    def setUp(self):
        self.temprepodir = tempfile.mkdtemp()
        env = os.environ
        env["DATADIR"]=self.temprepodir
        subprocess.call("./tests/show-dependencies.setup", shell=True, env=env)
        self.temprepo = self.temprepodir + '/test-repo/'
        bundle_name = morphlib.sourcemanager.quote_url(self.temprepo) + '.bndl'
        with open('/dev/null', 'w') as f:
            subprocess.check_call("git bundle create %s/%s master" %
                                    (self.temprepodir, bundle_name),
                                  stderr=f, shell=True, cwd=self.temprepo)

    def tearDown(self):
        shutil.rmtree(self.temprepodir)

    def test_uses_provided_cache_dir(self):
        app = DummyApp()
        
        tempdir = '/bla/bla/bla'
        s = morphlib.sourcemanager.SourceManager(app, tempdir)
        self.assertEqual(s.cache_dir, tempdir)

    def test_uses_cachedir_gits_if_no_cache_dir_provided(self):
        app = DummyApp()

        s = morphlib.sourcemanager.SourceManager(app)
        self.assertEqual(s.cache_dir,
                         os.path.join(app.settings['cachedir'], 'gits'))

    def test_resolves_sha1_treeish_for_test_repo_correctly(self):
        tempdir = tempfile.mkdtemp()

        s = morphlib.sourcemanager.SourceManager(DummyApp(), tempdir)
        t = s.get_treeish(self.temprepo,
                          'e28a23812eadf2fce6583b8819b9c5dbd36b9fb9')
        self.assertEquals(t.sha1, 'e28a23812eadf2fce6583b8819b9c5dbd36b9fb9')

        shutil.rmtree(tempdir)

    def test_resolves_sha1_treeish_for_test_repo_correctly_twice(self):
        tempdir = tempfile.mkdtemp()

        # try two times with different source manager objects
        s = morphlib.sourcemanager.SourceManager(DummyApp(), tempdir)
        t = s.get_treeish(self.temprepo,
                          'e28a23812eadf2fce6583b8819b9c5dbd36b9fb9')
        self.assertEquals(t.sha1, 'e28a23812eadf2fce6583b8819b9c5dbd36b9fb9')
        s = morphlib.sourcemanager.SourceManager(DummyApp(), tempdir)
        t = s.get_treeish(self.temprepo,
                          'e28a23812eadf2fce6583b8819b9c5dbd36b9fb9')
        self.assertEquals(t.sha1, 'e28a23812eadf2fce6583b8819b9c5dbd36b9fb9')

        # try two times with the same source manager object
        s = morphlib.sourcemanager.SourceManager(DummyApp(), tempdir)
        t = s.get_treeish(self.temprepo,
                          'e28a23812eadf2fce6583b8819b9c5dbd36b9fb9')
        self.assertEquals(t.sha1, 'e28a23812eadf2fce6583b8819b9c5dbd36b9fb9')
        t = s.get_treeish(self.temprepo,
                          'e28a23812eadf2fce6583b8819b9c5dbd36b9fb9')
        self.assertEquals(t.sha1, 'e28a23812eadf2fce6583b8819b9c5dbd36b9fb9')

        shutil.rmtree(tempdir)

    def test_resolves_ref_treeish_for_test_repo_correctly(self):
        tempdir = tempfile.mkdtemp()

        s = morphlib.sourcemanager.SourceManager(DummyApp(), tempdir)
        t = s.get_treeish(self.temprepo, 'master')
        self.assertEquals(t.ref, 'refs/remotes/origin/master')

        shutil.rmtree(tempdir)

    def test_resolves_ref_treeish_for_test_repo_without_submodules(self):
        tempdir = tempfile.mkdtemp()

        s = morphlib.sourcemanager.SourceManager(DummyApp(), tempdir)
        t = s.get_treeish(self.temprepo, 'master')
        self.assertEquals(len(t.submodules), 0)

        shutil.rmtree(tempdir)

    def test_resolves_sha1_treeish_for_test_repo_correctly_from_bundle(self):
        tempdir = tempfile.mkdtemp()
        bundle_server_loc = self.temprepodir

        app = DummyApp()
        app.settings['bundle-server'] = 'file://' + bundle_server_loc

        s = morphlib.sourcemanager.SourceManager(app, tempdir)

        def wget(url, filename):
            bundle_file = os.path.join(self.temprepodir,
                                       os.path.basename(filename))
            shutil.copy(bundle_file, s.cache_dir)

        s._wget = wget

        t = s.get_treeish(self.temprepo,
                          'e28a23812eadf2fce6583b8819b9c5dbd36b9fb9')
        self.assertEquals(t.sha1, 'e28a23812eadf2fce6583b8819b9c5dbd36b9fb9')

        shutil.rmtree(tempdir)

    def test_fails_to_resolves_sha1_treeish_for_non_existent_repo(self):
        tempdir = tempfile.mkdtemp()
        app = DummyApp()

        s = morphlib.sourcemanager.SourceManager(app, tempdir)

        def wget(url, filename):
            bundle_file = os.path.join(self.temprepodir,
                                       os.path.basename(filename))
            shutil.copy(bundle_file, s.cache_dir)

        s._wget = wget
        self.assertRaises(morphlib.sourcemanager.RepositoryFetchError,
                          s.get_treeish, 'asdf',
                          'e28a23812eadf2fce6583b8819b9c5dbd36b9fb9')

        shutil.rmtree(tempdir)

    def test_fails_to_resolves_sha1_treeish_for_non_existent_repo_bundle(self):
        tempdir = tempfile.mkdtemp()
        app = DummyApp()
        app.settings['bundle-server'] = 'file://' + self.temprepodir

        s = morphlib.sourcemanager.SourceManager(app, tempdir)

        def wget(url, filename):
            bundle_file = os.path.join(self.temprepodir,
                                       os.path.basename(filename))
            shutil.copy(bundle_file, s.cache_dir)

        s._wget = wget
        self.assertRaises(morphlib.sourcemanager.RepositoryFetchError,
                          s.get_treeish, 'asdf',
                          'e28a23812eadf2fce6583b8819b9c5dbd36b9fb9')

        shutil.rmtree(tempdir)

    def test_get_sha1_treeish_for_self_multiple_base(self):

        tempdir = tempfile.mkdtemp()
        app = DummyApp()
        app.settings['git-base-url'] = ['.', '/somewhere/else']

        s = morphlib.sourcemanager.SourceManager(app, tempdir)
        t = s.get_treeish(self.temprepo,
                          'e28a23812eadf2fce6583b8819b9c5dbd36b9fb9')
        self.assertEquals(t.sha1, 'e28a23812eadf2fce6583b8819b9c5dbd36b9fb9')

        shutil.rmtree(tempdir)