This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
/
test_git.py
203 lines (184 loc) · 8.47 KB
/
test_git.py
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Copyright 2013-2014 Sebastian Kreft
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import subprocess
import unittest
import mock
import gitlint.git as git
# pylint: disable=too-many-public-methods
class GitTest(unittest.TestCase):
def test_repository_root_ok(self):
with mock.patch('subprocess.check_output',
return_value=b'/home/user/repo\n') as git_call:
self.assertEqual('/home/user/repo', git.repository_root())
git_call.asser_called_once_with(
['git', 'rev-parse', '--show-toplevel'])
def test_repository_root_error(self):
with mock.patch('subprocess.check_output',
side_effect=subprocess.CalledProcessError(1, '', '')):
self.assertEqual(None, git.repository_root())
def test_modified_files(self):
output = os.linesep.join(['A docs/file1.txt',
'M data/file2.json',
'D file3.py',
'?? file4.js',
'AM file5.txt',
'MM file6.txt']).encode('utf-8')
with mock.patch('subprocess.check_output',
return_value=output) as git_call:
self.assertEqual(
{
'/home/user/repo/docs/file1.txt': 'A ',
'/home/user/repo/data/file2.json': 'M ',
'/home/user/repo/file4.js': '??',
'/home/user/repo/file5.txt': 'AM',
'/home/user/repo/file6.txt': 'MM',
},
git.modified_files('/home/user/repo'))
git_call.assert_called_once_with([
'git', 'status', '--porcelain', '--untracked-files=all',
'--ignore-submodules=all'])
def test_modified_files_tracked_only(self):
output = os.linesep.join(['A docs/file1.txt',
'M data/file2.json',
'D file3.py',
'?? file4.js',
'AM file5.txt']).encode('utf-8')
with mock.patch('subprocess.check_output',
return_value=output) as git_call:
self.assertEqual(
{'/home/user/repo/docs/file1.txt': 'A ',
'/home/user/repo/data/file2.json': 'M ',
'/home/user/repo/file5.txt': 'AM'},
git.modified_files('/home/user/repo', tracked_only=True))
git_call.assert_called_once_with([
'git', 'status', '--porcelain', '--untracked-files=all',
'--ignore-submodules=all'])
def test_modified_files_with_spaces(self):
output = os.linesep.join(['A "docs/file 1.txt"',
'M "data/file 2.json"']).encode('utf-8')
with mock.patch('subprocess.check_output',
return_value=output) as git_call:
self.assertEqual(
{'/home/user/repo/docs/file 1.txt': 'A ',
'/home/user/repo/data/file 2.json': 'M '},
git.modified_files('/home/user/repo'))
git_call.assert_called_once_with([
'git', 'status', '--porcelain', '--untracked-files=all',
'--ignore-submodules=all'])
def test_modified_files_nothing_changed(self):
output = b''
with mock.patch('subprocess.check_output',
return_value=output) as git_call:
self.assertEqual({}, git.modified_files('/home/user/repo'))
git_call.assert_called_once_with([
'git', 'status', '--porcelain', '--untracked-files=all',
'--ignore-submodules=all'])
def test_modified_files_with_commit(self):
output = os.linesep.join([
'M\ttest/e2etest/data/bash/error.sh',
'D\ttest/e2etest/data/ruby/error.rb',
'A\ttest/e2etest/data/rubylint/error.rb',
'',
]).encode('utf-8')
commit = '0a' * 20
with mock.patch('subprocess.check_output',
return_value=output) as git_call:
self.assertEqual(
{
'/home/user/repo/test/e2etest/data/bash/error.sh': 'M ',
'/home/user/repo/test/e2etest/data/rubylint/error.rb': 'A ',
},
git.modified_files('/home/user/repo', commit=commit))
git_call.assert_called_once_with(
['git', 'diff-tree', '-r', '--root', '--no-commit-id',
'--name-status', commit])
def test_modified_files_non_absolute_root(self):
with self.assertRaises(AssertionError):
git.modified_files('foo/bar')
def test_modified_lines(self):
output = os.linesep.join([
'baz',
'0000000000000000000000000000000000000000 2 2 4',
'foo',
'0000000000000000000000000000000000000000 5 5',
'bar']).encode('utf-8')
with mock.patch('subprocess.check_output',
return_value=output) as check_output:
self.assertEqual(
[2, 5],
list(git.modified_lines('/home/user/repo/foo/bar.txt', ' M')))
self.assertEqual(
[2, 5],
list(git.modified_lines('/home/user/repo/foo/bar.txt', 'M ')))
self.assertEqual(
[2, 5],
list(git.modified_lines('/home/user/repo/foo/bar.txt', 'MM')))
expected_calls = [mock.call(
['git', 'blame', '--porcelain',
'/home/user/repo/foo/bar.txt'])] * 3
self.assertEqual(expected_calls, check_output.call_args_list)
def test_modified_lines_with_commit(self):
output = os.linesep.join([
'baz',
'0123456789abcdef31410123456789abcdef3141 2 2 4',
'foo',
'0123456789abcdef31410123456789abcdef3141 5 5',
'bar']).encode('utf-8')
with mock.patch('subprocess.check_output',
return_value=output) as check_output:
self.assertEqual(
[2, 5],
list(git.modified_lines(
'/home/user/repo/foo/bar.txt',
' M',
commit='0123456789abcdef31410123456789abcdef3141')))
self.assertEqual(
[2, 5],
list(git.modified_lines(
'/home/user/repo/foo/bar.txt',
'M ',
commit='0123456789abcdef31410123456789abcdef3141')))
self.assertEqual(
[2, 5],
list(git.modified_lines(
'/home/user/repo/foo/bar.txt',
'MM',
commit='0123456789abcdef31410123456789abcdef3141')))
expected_calls = [mock.call(
['git', 'blame', '--porcelain',
'/home/user/repo/foo/bar.txt'])] * 3
self.assertEqual(expected_calls, check_output.call_args_list)
def test_modified_lines_new_addition(self):
self.assertEqual(
None,
git.modified_lines('/home/user/repo/foo/bar.txt', 'A '))
def test_modified_lines_untracked(self):
self.assertEqual(
None,
git.modified_lines('/home/user/repo/foo/bar.txt', '??'))
def test_modified_lines_no_info(self):
self.assertEqual(
[],
list(git.modified_lines('/home/user/repo/foo/bar.txt', None)))
def test_last_commit(self):
with mock.patch('subprocess.check_output',
return_value=b'0a' * 20 + b'\n') as git_call:
self.assertEqual('0a' * 20, git.last_commit())
git_call.asser_called_once_with(
['git', 'rev-parse', 'HEAD'])
def test_last_commit_not_in_repo(self):
with mock.patch('subprocess.check_output',
side_effect=subprocess.CalledProcessError(255, '', '')):
self.assertEqual(None, git.last_commit())