/
base_spec.rb
293 lines (259 loc) · 12 KB
/
base_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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
require 'spec_helper'
describe AutoTagger::Base do
describe "#repo" do
it "returns a repo with the correct options" do
base = AutoTagger::Base.new :path => "/foo",
:dry_run => true,
:verbose => true,
:executable => "/usr/bin/git"
AutoTagger::Git::Repo.should_receive(:new).with "/foo",
:execute_commands => false,
:verbose => true,
:executable => "/usr/bin/git"
base.repo
end
end
describe "#last_ref_from_previous_stage" do
it "returns nil if there is no previous stage" do
refs = "0f7324495f06e2b refs/tags/ci/2001"
base = AutoTagger::Base.new :stages => ["ci", "demo", "production"], :stage => "ci"
base.repo.stub(:read).and_return(refs)
base.last_ref_from_previous_stage.should be_nil
end
it "returns nil if there are no matching refs" do
refs = "0f7324495f06e2b refs/tags-ci/2001"
base = AutoTagger::Base.new :stages => ["ci", "demo", "production"], :stage => "ci"
base.repo.stub(:read).and_return(refs)
base.last_ref_from_previous_stage.should be_nil
end
it "should return the last ref from the previous stage" do
refs = %Q{
41dee06050450ac refs/tags/ci/2003
41dee06050450a5 refs/tags/ci/2003
61c6627d766c1be refs/tags/demo/2001
}
base = AutoTagger::Base.new :stages => ["ci", "demo", "production"], :stage => "demo"
base.repo.stub(:read).and_return(refs)
ref = AutoTagger::Git::Ref.new(base.repo, "41dee06050450ac", "refs/tags/ci/2003")
base.last_ref_from_previous_stage.name.should == "refs/tags/ci/2003"
end
it "should return the last ref with correct order (git show-ref is not ordered)" do
refs = %Q{
a80af49962c95a92df59a527a3ce60e22da290fc refs/tags/ci/1002
0e892ad1b308dd86c40f5fd60b3cddd58022d93e refs/tags/ci/997
b8d7ce86f1c6440080e0c315c7cc1c0fe702127f refs/tags/ci/999
}
base = AutoTagger::Base.new :stages => ["ci", "demo", "production"], :stage => "demo"
base.repo.stub(:read).and_return(refs)
ref = AutoTagger::Git::Ref.new(base.repo, "a80af49962c95a92df59a527a3ce60e22da290fc", "refs/tags/ci/1002")
base.last_ref_from_previous_stage.name.should == "refs/tags/ci/1002"
end
it "should return the last ref with correct order using other date separator(git show-ref is not ordered)" do
refs = %Q{
a80af49962c95a92df59a527a3ce60e22da290fc refs/tags/ci/2011-09-09-18-17-43
0e892ad1b308dd86c40f5fd60b3cddd58022d93e refs/tags/ci/2011-09-09-19-17-43
b8d7ce86f1c6440080e0c315c7cc1c0fe702127f refs/tags/ci/2011-09-08-18-17-43
}
base = AutoTagger::Base.new :stages => ["ci", "demo", "production"], :stage => "demo", :date_separator => "-"
base.repo.stub(:read).and_return(refs)
ref = AutoTagger::Git::Ref.new(base.repo, "a80af49962c95a92df59a527a3ce60e22da290fc", "refs/tags/ci/2011-09-09-19-17-43")
base.last_ref_from_previous_stage.name.should == "refs/tags/ci/2011-09-09-19-17-43"
end
end
describe "#create_ref" do
it "creates a ref with the given sha and returns the ref" do
base = AutoTagger::Base.new :stage => "demo"
base.stub(:timestamp).and_return("20081010")
base.repo.stub(:exec) { true }
base.repo.should_receive(:exec).with("update-ref refs/tags/demo/20081010 abc123")
ref = base.create_ref "abc123"
ref.name.should == "refs/tags/demo/20081010"
ref.sha.should == "abc123"
end
it "defaults to the latest commit sha" do
base = AutoTagger::Base.new :stage => "demo"
base.stub(:timestamp).and_return("20081010")
base.repo.should_receive(:latest_commit_sha).and_return("abc123")
base.repo.stub(:exec) { true }
base.repo.should_receive(:exec).with("update-ref refs/tags/demo/20081010 abc123")
ref = base.create_ref
ref.name.should == "refs/tags/demo/20081010"
ref.sha.should == "abc123"
end
it "respects the passed in date separator" do
time = Time.now.utc
timestamp = time.strftime("%Y-%m-%d-%H-%M-%S")
base = AutoTagger::Base.new :stage => "ci", :date_separator => "-"
base.repo.stub(:exec) { true }
base.repo.should_receive(:exec).with("update-ref refs/tags/ci/#{timestamp} abc123")
base.create_ref "abc123"
end
it "raises an error if the stage is not set" do
proc do
AutoTagger::Base.new({}).create_ref
end.should raise_error(AutoTagger::Base::StageCannotBeBlankError)
end
it "fetches tags before creating tags" do
base = AutoTagger::Base.new :stage => "ci"
base.repo.stub(:exec) { true }
base.repo.should_receive(:exec).with("fetch origin refs/tags/*:refs/tags/*")
base.create_ref "abc123"
end
it "does not fetch tags before creating tags if fetch tags is false" do
base = AutoTagger::Base.new :stage => "ci", :fetch_tags => false
base.repo.stub(:exec) { true }
base.repo.should_receive(:exec).with("push origin refs/tags/*:refs/tags/*")
base.create_ref "abc123"
end
it "pushes tags before creating tags" do
base = AutoTagger::Base.new :stage => "ci"
base.repo.stub(:exec) { true }
base.repo.should_receive(:exec).with("push origin refs/tags/*:refs/tags/*")
base.create_ref "abc123"
end
it "does not push tags before creating tags if push tags is false" do
base = AutoTagger::Base.new :stage => "ci", :push_tags => false
base.repo.stub(:exec) { true }
base.repo.should_receive(:exec).with("push origin refs/tags/*:refs/tags/*")
base.create_ref "abc123"
end
end
describe "#delete_locally" do
it "blows up if you don't enter a stage" do
base = AutoTagger::Base.new({})
proc do
base.delete_locally
end.should raise_error(AutoTagger::Base::StageCannotBeBlankError)
end
it "executes the local delete commands for all the refs that match" do
base = AutoTagger::Base.new :stage => "ci"
base.repo.stub(:exec) { true }
base.stub(:refs_for_stage) do
[
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/ci/2008"),
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/ci/2009"),
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/ci/2010")
]
end
base.repo.should_receive(:exec).with("update-ref -d refs/tags/ci/2008")
base.repo.should_receive(:exec).with("update-ref -d refs/tags/ci/2009")
base.repo.should_not_receive(:exec).with("update-ref -d refs/tags/ci/2010")
base.delete_locally
end
end
describe "#delete_on_remote" do
it "blows up if you don't enter a stage" do
base = AutoTagger::Base.new({})
proc do
base.delete_on_remote
end.should raise_error(AutoTagger::Base::StageCannotBeBlankError)
end
it "does not push if there are no tags" do
base = AutoTagger::Base.new :stage => "ci"
base.stub(:refs_for_stage).with("ci") { [] }
base.repo.should_not_receive(:exec)
base.delete_on_remote
end
it "executes the remote delete commands in a batch" do
base = AutoTagger::Base.new :stage => "ci"
base.repo.stub(:exec) { true }
base.stub(:refs_for_stage).with("ci") do
[
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/ci/2008"),
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/ci/2009"),
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/ci/2010")
]
end
base.repo.should_receive(:exec).with("push origin :refs/tags/ci/2008 :refs/tags/ci/2009")
base.delete_on_remote
end
end
describe "#cleanup" do
it "blows up if you don't enter a stage" do
base = AutoTagger::Base.new({})
proc do
base.cleanup
end.should raise_error(AutoTagger::Base::StageCannotBeBlankError)
end
it "executes delete locally" do
base = AutoTagger::Base.new :stage => "ci"
base.repo.stub(:exec) { true }
base.stub(:refs_for_stage).with("ci") { [AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/ci/2008")] }
base.should_receive(:delete_local_refs)
base.cleanup
end
it "executes delete on remote if push refs is true" do
base = AutoTagger::Base.new :stage => "ci"
base.repo.stub(:exec) { true }
base.stub(:refs_for_stage).with("ci") { [AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/ci/2008")] }
base.should_receive(:delete_remote_refs)
base.cleanup
end
it "does not execute delete on remote if push refs is false" do
base = AutoTagger::Base.new :stage => "ci", :offline => true
base.repo.stub(:exec) { true }
base.stub(:refs_for_stage).with("ci") { [AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/ci/2008")] }
base.should_not_receive(:delete_remote_refs)
base.cleanup
end
end
describe ".items_to_remove" do
it "returns the items that can be removed from an array, based on the keep value passed in" do
AutoTagger::Base.items_to_remove(["2008"], 0).should == ["2008"]
AutoTagger::Base.items_to_remove(["2008"], 1).should == []
AutoTagger::Base.items_to_remove(["2008", "2009"], 0).should == ["2008", "2009"]
AutoTagger::Base.items_to_remove(["2008", "2009"], 1).should == ["2008"]
AutoTagger::Base.items_to_remove(["2008", "2009"], 2).should == []
AutoTagger::Base.items_to_remove(["2008", "2009"], 3).should == []
AutoTagger::Base.items_to_remove(["2008", "2009", "2010"], 0).should == ["2008", "2009", "2010"]
AutoTagger::Base.items_to_remove(["2008", "2009", "2010"], 1).should == ["2008", "2009"]
AutoTagger::Base.items_to_remove(["2008", "2009", "2010"], 2).should == ["2008"]
AutoTagger::Base.items_to_remove(["2008", "2009", "2010"], 3).should == []
end
end
describe "#refs_for_stage" do
it "returns refs that match the given stage" do
base = AutoTagger::Base.new :stage => "ci"
base.repo.stub(:exec) { true }
refs = [
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/auto_tags/ci/2008"),
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/ci/2009"),
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/2009"),
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags-ci/2009"),
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/heads/master")
]
base.repo.stub(:refs) { mock("RefSet", :all => refs) }
base.refs_for_stage("ci").should == [
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/ci/2009")
]
end
end
describe "#list" do
it "return a list of refs for the given stage" do
base = AutoTagger::Base.new :stage => "ci"
base.should_receive(:fetch)
base.should_receive(:refs_for_stage).with("ci")
base.list
end
end
describe "#release_tag_entries" do
it "lists the last ref from each stage" do
base = AutoTagger::Base.new :stage => "ci", :stages => ["ci", "demo", "production"]
base.repo.stub(:exec) { true }
refs = [
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/ci/2008"),
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/ci/2009"),
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/demo/2008"),
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/demo/2009"),
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/production/2008"),
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/production/2009")
]
base.repo.stub(:refs) { mock("RefSet", :all => refs) }
base.release_tag_entries.should == [
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/ci/2009"),
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/demo/2009"),
AutoTagger::Git::Ref.new(base.repo, "abc123", "refs/tags/production/2009")
]
end
end
end