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