summaryrefslogtreecommitdiff
path: root/icklib/git_tests.py
blob: 432d621b4e686d53eb78b820d96f23a79c53e97d (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
# Copyright 2015  Lars Wirzenius
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-3+ =*=


import os
import shutil
import tempfile
import unittest

import cliapp

import icklib


class GitCloneTests(unittest.TestCase):

    def setUp(self):
        self.tempdir = tempfile.mkdtemp()
        self.origin = os.path.join(self.tempdir, 'origin.git')
        cliapp.runcmd(['git', 'init', self.origin])
        with open(os.path.join(self.origin, 'README'), 'w') as f:
            f.write('This is a README\n')
        cliapp.runcmd(['git', 'add', '.'], cwd=self.origin)
        cliapp.runcmd(['git', 'commit', '-mfoo'], cwd=self.origin)

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

    def test_sets_dirname(self):
        clone = icklib.GitClone()
        self.assertEqual(clone.dirname, None)
        clone.set_dirname('foo')
        self.assertEqual(clone.dirname, 'foo')

    def test_sets_url(self):
        clone = icklib.GitClone()
        self.assertEqual(clone.url, None)
        clone.set_url('foo')
        self.assertEqual(clone.url, 'foo')

    def test_sets_subdir(self):
        clone = icklib.GitClone()
        self.assertEqual(clone.subdir, None)
        clone.set_subdir('foo')
        self.assertEqual(clone.subdir, 'foo')

    def test_clones_from_origin(self):
        clonedir = os.path.join(self.tempdir, 'clone')
        clone = icklib.GitClone()
        clone.set_dirname(clonedir)
        clone.set_url(self.origin)
        clone.clone_from_url()
        self.assertTrue(os.path.exists(os.path.join(clonedir, '.git')))

    def test_checks_out_branch(self):
        clonedir = os.path.join(self.tempdir, 'clone')
        clone = icklib.GitClone()
        clone.set_dirname(clonedir)
        clone.set_url(self.origin)
        clone.clone_from_url()
        clone.checkout('master')
        output = cliapp.runcmd(
            ['git', 'status', '--porcelain', '--branch'],
            cwd=clonedir)
        self.assertTrue(output.startswith('## master'))

    def test_updates_from_origin(self):
        clonedir = os.path.join(self.tempdir, 'clone')
        clone = icklib.GitClone()
        clone.set_dirname(clonedir)
        clone.set_url(self.origin)
        clone.clone_from_url()

        with open(os.path.join(self.origin, 'README'), 'w') as f:
            f.write('This is a NEW README\n')
        cliapp.runcmd(['git', 'add', '.'], cwd=self.origin)
        cliapp.runcmd(['git', 'commit', '-mfoo'], cwd=self.origin)

        clone.update_from_url()
        clone.checkout('master')

        with open(os.path.join(clonedir, 'README')) as f:
            text = f.read()
        self.assertEqual(text, 'This is a NEW README\n')

    def test_syncs_from_uri(self):
        clonedir = os.path.join(self.tempdir, 'clone')
        clone = icklib.GitClone()
        clone.set_dirname(clonedir)
        clone.set_url(self.origin)
        clone.sync_from_url()

        with open(os.path.join(self.origin, 'README'), 'w') as f:
            f.write('This is a NEW README\n')
        cliapp.runcmd(['git', 'add', '.'], cwd=self.origin)
        cliapp.runcmd(['git', 'commit', '-mfoo'], cwd=self.origin)

        clone.sync_from_url()
        clone.checkout('master')

        with open(os.path.join(clonedir, 'README')) as f:
            text = f.read()
        self.assertEqual(text, 'This is a NEW README\n')

    def test_returns_some_commit_id_for_HEAD(self):
        clonedir = os.path.join(self.tempdir, 'clone')
        clone = icklib.GitClone()
        clone.set_dirname(clonedir)
        clone.set_url(self.origin)
        clone.clone_from_url()
        self.assertEqual(type(clone.get_HEAD_commit()), str)