summaryrefslogtreecommitdiff
path: root/morphlib/sourcemanager_tests.py
blob: 87d29f7b96f91c5e610737311a7f0742867c9798 (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
# 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
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',
        }
        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'
        subprocess.call("git bundle create %s/%s master" %
                        (self.temprepodir, bundle_name),
        shell=True, cwd=self.temprepo)

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

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

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

    def test_get_sha1_treeish_for_self(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_get_sha1_treeish_for_self_twice(self):
        tempdir = tempfile.mkdtemp()

        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')

        shutil.rmtree(tempdir)

    def test_get_ref_treeish_for_self(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_get_sha1_treeish_for_self_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):
            path=urlparse.urlparse(url).path
            shutil.copy(path, s.cache_dir)

        s._wget = wget

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

        shutil.rmtree(tempdir)

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

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

        def wget(url):
            path=urlparse.urlparse(url).path
            shutil.copy(path, s.cache_dir)

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

        shutil.rmtree(tempdir)

    def test_get_sha1_treeish_for_self_multple_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)