summaryrefslogtreecommitdiff
path: root/morphlib/gitindex_tests.py
blob: 7a8953f2e11adeebc656d53de00b41c70bf1ca59 (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
# Copyright (C) 2013  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.
#
# =*= License: GPL-2 =*=


import os
import shutil
import tempfile
import unittest

import morphlib


class GitIndexTests(unittest.TestCase):

    def setUp(self):
        self.tempdir = tempfile.mkdtemp()
        self.dirname = os.path.join(self.tempdir, 'foo')
        os.mkdir(self.dirname)
        gd = morphlib.gitdir.init(self.dirname)
        with open(os.path.join(self.dirname, 'foo'), 'w') as f:
            f.write('dummy text\n')
        gd._runcmd(['git', 'add', '.'])
        gd._runcmd(['git', 'commit', '-m', 'Initial commit'])
        self.mirror = os.path.join(self.tempdir, 'mirror')
        gd._runcmd(['git', 'clone', '--mirror', self.dirname, self.mirror])

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

    def test_uncommitted_changes(self):
        idx = morphlib.gitdir.GitDirectory(self.dirname).get_index()
        self.assertEqual(list(idx.get_uncommitted_changes()), [])
        os.unlink(os.path.join(self.dirname, 'foo'))
        self.assertEqual(sorted(idx.get_uncommitted_changes()),
                         [(' D', 'foo', None)])

    def test_uncommitted_alt_index(self):
        gd = morphlib.gitdir.GitDirectory(self.dirname)
        idx = gd.get_index(os.path.join(self.tempdir, 'index'))
        self.assertEqual(sorted(idx.get_uncommitted_changes()),
                         [('D ', 'foo', None)])
        # 'D ' means not in the index, but in the working tree

    def test_set_to_tree_alt_index(self):
        gd = morphlib.gitdir.GitDirectory(self.dirname)
        idx = gd.get_index(os.path.join(self.tempdir, 'index'))
        # Read the HEAD commit into the index, which is the same as the
        # working tree, so there are no uncommitted changes reported
        # by status
        idx.set_to_tree(gd.HEAD)
        self.assertEqual(list(idx.get_uncommitted_changes()),[])

    def test_add_files_from_index_info(self):
        gd = morphlib.gitdir.GitDirectory(self.dirname)
        idx = gd.get_index(os.path.join(self.tempdir, 'index'))
        filepath = os.path.join(gd.dirname, 'foo')
        with open(filepath, 'r') as f:
            sha1 = gd.store_blob(f)
            idx.add_files_from_index_info(
                [(os.stat(filepath).st_mode, sha1, 'foo')])
        self.assertEqual(list(idx.get_uncommitted_changes()),[])

    def test_add_files_from_working_tree(self):
        gd = morphlib.gitdir.GitDirectory(self.dirname)
        idx = gd.get_index()
        idx.add_files_from_working_tree(['foo'])
        self.assertEqual(list(idx.get_uncommitted_changes()),[])

    def test_add_files_from_working_tree_fails_in_bare(self):
        gd = morphlib.gitdir.GitDirectory(self.mirror)
        idx = gd.get_index()
        self.assertRaises(morphlib.gitdir.NoWorkingTreeError,
                          idx.add_files_from_working_tree, ['foo'])

    def test_write_tree(self):
        gd = morphlib.gitdir.GitDirectory(self.dirname)
        idx = gd.get_index()
        self.assertEqual(idx.write_tree(), gd.resolve_ref_to_tree(gd.HEAD))