Completed
Pull Request — develop (#134)
by
unknown
14:34 queued 04:31
created

TestGit   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

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