Completed
Pull Request — develop (#134)
by
unknown
21:16 queued 11:22
created

TestGit.test_update()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
b 0
f 0
dl 0
loc 8
rs 9.4285
1
# pylint: disable=no-self-use
2
3
from unittest.mock import patch, Mock
4
import os
5
from gitman import git
6
from gitman.exceptions import ShellError
7
8
from . import assert_calls
9
10
11
@patch('gitman.git.call')
12
class TestGit:
13
    """Tests for calls to Git."""
14
15
    @patch('os.path.isdir', Mock(return_value=False))
16
    def test_clone(self, mock_call):
17
        """Verify the commands to set up a new reference repository."""
18
        git.clone('mock.git', 'mock/path', cache='cache')
19
        assert_calls(mock_call, [
20
            "git clone --mirror mock.git " +
21
            os.path.normpath("cache/mock.reference"),
22
            "git clone --reference " +
23
            os.path.normpath("cache/mock.reference")
24
            " mock.git " +
0 ignored issues
show
introduced by
invalid syntax
Loading history...
25
            os.path.normpath("mock/path")
26
        ])
27
28
    @patch('os.path.isdir', Mock(return_value=True))
29
    def test_clone_from_reference(self, mock_call):
30
        """Verify the commands to clone a Git repository from a reference."""
31
        git.clone('mock.git', 'mock/path', cache='cache')
32
        assert_calls(mock_call, [
33
            "git clone --reference " +
34
            os.path.normpath("cache/mock.reference") +
35
            " mock.git " +
36
            os.path.normpath("mock/path")
37
        ])
38
39
    def test_fetch(self, mock_call):
40
        """Verify the commands to fetch from a Git repository."""
41
        git.fetch('mock.git')
42
        assert_calls(mock_call, [
43
            "git remote set-url origin mock.git",
44
            "git fetch --tags --force --prune origin",
45
        ])
46
47
    def test_fetch_rev(self, mock_call):
48
        """Verify the commands to fetch from a Git repository w/ rev."""
49
        git.fetch('mock.git', 'mock-rev')
50
        assert_calls(mock_call, [
51
            "git remote set-url origin mock.git",
52
            "git fetch --tags --force --prune origin mock-rev",
53
        ])
54
55
    def test_fetch_rev_sha(self, mock_call):
56
        """Verify the commands to fetch from a Git repository w/ SHA."""
57
        git.fetch('mock.git', 'abcdef1234' * 4)
58
        assert_calls(mock_call, [
59
            "git remote set-url origin mock.git",
60
            "git fetch --tags --force --prune origin",
61
        ])
62
63
    def test_fetch_rev_revparse(self, mock_call):
64
        """Verify the commands to fetch from a Git repository w/ rev-parse."""
65
        git.fetch('mock.git', 'master@{2015-02-12 18:30:00}')
66
        assert_calls(mock_call, [
67
            "git remote set-url origin mock.git",
68
            "git fetch --tags --force --prune origin",
69
        ])
70
71
    def test_changes(self, mock_call):
72
        """Verify the commands to check for uncommitted changes."""
73
        git.changes(include_untracked=True)
74
        assert_calls(mock_call, [
75
            # based on: http://stackoverflow.com/questions/3878624
76
            "git update-index -q --refresh",
77
            "git diff-index --quiet HEAD",
78
            "git ls-files --others --exclude-standard",
79
            "git status",  # used for displaying the overall status
80
        ])
81
82
    def test_changes_false(self, _):
83
        """Verify the absence of changes can be detected."""
84
        with patch('gitman.git.call', Mock(return_value="")):
85
            assert False is git.changes()
86
87
    def test_changes_false_with_untracked(self, _):
88
        """Verify untracked files can be detected."""
89
        with patch('gitman.git.call', Mock(return_value="file_1")):
90
            assert False is git.changes()
91
92
    def test_changes_true_when_untracked_included(self, _):
93
        """Verify untracked files can be detected."""
94
        with patch('gitman.git.call', Mock(return_value="file_1")):
95
            assert True is git.changes(include_untracked=True)
96
97
    def test_changes_true_when_uncommitted(self, _):
98
        """Verify uncommitted changes can be detected."""
99
        with patch('gitman.git.call', Mock(side_effect=ShellError)):
100
            assert True is git.changes(display_status=False)
101
102
    def test_update(self, mock_call):
103
        """Verify the commands to update a working tree to a revision."""
104
        git.update('mock_rev')
105
        assert_calls(mock_call, [
106
            "git stash",
107
            "git clean --force -d -x",
108
            "git checkout --force mock_rev",
109
            "git branch --set-upstream-to origin/mock_rev",
110
        ])
111
112
    def test_update_branch(self, mock_call):
113
        """Verify the commands to update a working tree to a branch."""
114
        git.update('mock_branch', fetch=True)
115
        assert_calls(mock_call, [
116
            "git stash",
117
            "git clean --force -d -x",
118
            "git checkout --force mock_branch",
119
            "git branch --set-upstream-to origin/mock_branch",
120
            "git pull --ff-only --no-rebase",
121
        ])
122
123
    def test_update_no_clean(self, mock_call):
124
        git.update('mock_rev', clean=False)
125
        assert_calls(mock_call, [
126
            "git stash",
127
            "git checkout --force mock_rev",
128
            "git branch --set-upstream-to origin/mock_rev",
129
        ])
130
131
    def test_update_revparse(self, mock_call):
132
        """Verify the commands to update a working tree to a rev-parse."""
133
        mock_call.return_value = "abc123"
134
        git.update('mock_branch@{2015-02-12 18:30:00}')
135
        assert_calls(mock_call, [
136
            "git stash",
137
            "git clean --force -d -x",
138
            "git checkout --force mock_branch",
139
            "git rev-list -n 1 --before='2015-02-12 18:30:00' mock_branch",
140
            "git checkout --force abc123",
141
            "git branch --set-upstream-to origin/abc123",
142
        ])
143
144
    def test_get_url(self, mock_call):
145
        """Verify the commands to get the current repository's URL."""
146
        git.get_url()
147
        assert_calls(mock_call, ["git config --get remote.origin.url"])
148
149
    def test_get_hash(self, mock_call):
150
        """Verify the commands to get the working tree's hash."""
151
        git.get_hash()
152
        assert_calls(mock_call, ["git rev-parse HEAD"])
153
154
    def test_get_tag(self, mock_call):
155
        """Verify the commands to get the working tree's tag."""
156
        git.get_tag()
157
        assert_calls(mock_call, ["git describe --tags --exact-match"])
158
159
    def test_get_branch(self, mock_call):
160
        """Verify the commands to get the working tree's branch."""
161
        git.get_branch()
162
        assert_calls(mock_call, ["git rev-parse --abbrev-ref HEAD"])
163