summaryrefslogtreecommitdiff
path: root/morphlib/sourcemanager_tests.py
blob: f769e60e36e5cb966ebdbdc3184410a105d7e2b3 (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
# 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
from urlparse import urlparse

import morphlib


class DummyApp(object):
   def __init__(self):
        self.settings = { 'git-base-url': ['.',] }
        self.msg = lambda msg: None


class SourceManagerTests(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

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

        s = morphlib.sourcemanager.SourceManager(tempdir, DummyApp())
        t = s.get_treeish(os.getcwd(),
                          '41ee528492db9bd41604311b100da5a871098b3a')
        assert(t.sha1 == '41ee528492db9bd41604311b100da5a871098b3a')

        shutil.rmtree(tempdir)

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

        s = morphlib.sourcemanager.SourceManager(tempdir, DummyApp())
        t = s.get_treeish(os.getcwd(),
                          '41ee528492db9bd41604311b100da5a871098b3a')
        assert(t.sha1 == '41ee528492db9bd41604311b100da5a871098b3a')

        s = morphlib.sourcemanager.SourceManager(tempdir, DummyApp())
        t = s.get_treeish(os.getcwd(),
                          '41ee528492db9bd41604311b100da5a871098b3a')
        assert(t.sha1 == '41ee528492db9bd41604311b100da5a871098b3a')

        shutil.rmtree(tempdir)

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

        s = morphlib.sourcemanager.SourceManager(tempdir, DummyApp())
        t = s.get_treeish(os.getcwd(), 'master')
        assert(t.ref == 'refs/heads/master')

        shutil.rmtree(tempdir)

    def test_get_sha1_treeish_for_self_bundle(self):
        tempdir = tempfile.mkdtemp()
        bundle_server_loc = tempdir+'/bundle_server'
        os.mkdir(bundle_server_loc)
        bundle_name = morphlib.sourcemanager.quote_url(os.getcwd()) + '.bndl'
        shutil.copy(os.getcwd() +'/testdata/morph.bndl',
                    bundle_server_loc + '/' +bundle_name)

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

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

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

        s._wget = wget

        t = s.get_treeish(os.getcwd(),
                          '41ee528492db9bd41604311b100da5a871098b3a')
        assert(t.sha1 == '41ee528492db9bd41604311b100da5a871098b3a')

        shutil.rmtree(tempdir)


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

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

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

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

        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(tempdir, app) 
        t = s.get_treeish(os.getcwd(),
                          '41ee528492db9bd41604311b100da5a871098b3a')
        assert(t.sha1 == '41ee528492db9bd41604311b100da5a871098b3a')

        shutil.rmtree(tempdir)