forked from sds/overcommit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotected_branches_spec.rb
273 lines (220 loc) · 7.12 KB
/
protected_branches_spec.rb
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
require 'spec_helper'
describe Overcommit::Hook::PrePush::ProtectedBranches,
if: Overcommit::GIT_VERSION >= '2.0' do
let(:flags) { '' }
let(:pushed_ref) { remote_ref }
subject do
shell("git push #{flags} origin #{pushed_ref}:#{remote_ref}".split)
end
let(:config) { <<-YML }
CommitMsg:
ALL:
enabled: false
PreCommit:
ALL:
enabled: false
PrePush:
ALL:
enabled: false
ProtectedBranches:
enabled: true
branches:
- protected
YML
around do |example|
remote_repo = repo do
`git checkout -b protected > #{File::NULL} 2>&1`
`git commit --allow-empty -m "Remote commit"`
`git checkout -b unprotected > #{File::NULL} 2>&1`
`git checkout -b dummy > #{File::NULL} 2>&1`
end
repo do
File.open('.overcommit.yml', 'w') { |f| f.write(config) }
`git remote add origin file://#{remote_repo}`
`git checkout -b #{remote_ref} > #{File::NULL} 2>&1`
`git commit --allow-empty -m "Local commit"`
example.run
end
end
shared_context 'deleting' do
let(:pushed_ref) { '' }
end
shared_context 'force-pushing' do
let(:flags) { '--force' }
end
shared_context 'remote exists locally' do
before { `git fetch origin #{remote_ref} > #{File::NULL} 2>&1` }
end
shared_context 'local branch up-to-date' do
before { `git rebase --keep-empty origin/#{remote_ref} > #{File::NULL} 2>&1` }
end
shared_context 'ProtectedBranches enabled' do
before { `overcommit --install > #{File::NULL}` }
end
shared_examples 'push succeeds' do
it 'exits successfully' do
subject.status.should == 0
end
end
shared_examples 'push fails' do
it 'exits with a non-zero status' do
subject.status.should_not == 0
end
end
shared_examples 'push succeeds when remote exists locally' do
context 'when remote exists locally' do
include_context 'remote exists locally'
context 'when up-to-date with remote' do
include_context 'local branch up-to-date'
include_examples 'push succeeds'
end
context 'when not up-to-date with remote' do
include_examples 'push succeeds'
end
end
context 'when remote does not exist locally' do
include_examples 'push fails'
end
end
shared_examples 'push succeeds when up-to-date with remote' do
context 'when remote exists locally' do
include_context 'remote exists locally'
context 'when up-to-date with remote' do
include_context 'local branch up-to-date'
include_examples 'push succeeds'
end
context 'when not up-to-date with remote' do
include_examples 'push fails'
end
end
context 'when remote does not exist locally' do
include_examples 'push fails'
end
end
shared_examples 'push always fails' do
context 'when remote exists locally' do
include_context 'remote exists locally'
context 'when up-to-date with remote' do
include_context 'local branch up-to-date'
include_examples 'push fails'
end
context 'when not up-to-date with remote' do
include_examples 'push fails'
end
end
context 'when remote does not exist locally' do
include_examples 'push fails'
end
end
shared_examples 'push always succeeds' do
context 'when remote exists locally' do
include_context 'remote exists locally'
context 'when up-to-date with remote' do
include_context 'local branch up-to-date'
include_examples 'push succeeds'
end
context 'when not up-to-date with remote' do
include_examples 'push succeeds'
end
end
context 'when remote does not exist locally' do
include_examples 'push succeeds'
end
end
context 'when pushing to a protected branch' do
let(:remote_ref) { 'protected' }
context 'when force-pushing' do
include_context 'force-pushing'
context 'with ProtectedBranches enabled' do
include_context 'ProtectedBranches enabled'
include_examples 'push succeeds when up-to-date with remote'
end
context 'with ProtectedBranches disabled' do
include_examples 'push always succeeds'
end
end
context 'when deleting' do
include_context 'deleting'
context 'with ProtectedBranches enabled' do
include_context 'ProtectedBranches enabled'
include_examples 'push always fails'
end
context 'with ProtectedBranches disabled' do
include_examples 'push always succeeds'
end
end
context 'when not deleting or force-pushing' do
context 'with ProtectedBranches enabled' do
include_context 'ProtectedBranches enabled'
include_examples 'push succeeds when up-to-date with remote'
end
context 'with ProtectedBranches disabled' do
include_examples 'push succeeds when up-to-date with remote'
end
end
end
context 'when pushing to an unprotected branch' do
let(:remote_ref) { 'unprotected' }
context 'when force-pushing' do
include_context 'force-pushing'
context 'with ProtectedBranches enabled' do
include_context 'ProtectedBranches enabled'
include_examples 'push always succeeds'
end
context 'with ProtectedBranches disabled' do
include_examples 'push always succeeds'
end
end
context 'when deleting' do
include_context 'deleting'
context 'with ProtectedBranches enabled' do
include_context 'ProtectedBranches enabled'
include_examples 'push always succeeds'
end
context 'with ProtectedBranches disabled' do
include_examples 'push always succeeds'
end
end
context 'when not deleting or force-pushing' do
context 'with ProtectedBranches enabled' do
include_context 'ProtectedBranches enabled'
include_examples 'push succeeds when up-to-date with remote'
end
context 'with ProtectedBranches disabled' do
include_examples 'push succeeds when up-to-date with remote'
end
end
end
context 'when pushing to a nonexistent branch' do
let(:remote_ref) { 'new-branch' }
context 'when force-pushing' do
include_context 'force-pushing'
context 'with ProtectedBranches enabled' do
include_context 'ProtectedBranches enabled'
include_examples 'push succeeds'
end
context 'with ProtectedBranches disabled' do
include_examples 'push succeeds'
end
end
context 'when deleting' do
include_context 'deleting'
context 'with ProtectedBranches enabled' do
include_context 'ProtectedBranches enabled'
include_examples 'push fails'
end
context 'with ProtectedBranches disabled' do
include_examples 'push fails'
end
end
context 'when not deleting or force-pushing' do
context 'with ProtectedBranches enabled' do
include_context 'ProtectedBranches enabled'
include_examples 'push succeeds'
end
context 'with ProtectedBranches disabled' do
include_examples 'push succeeds'
end
end
end
end