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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
# frozen_string_literal: true
require "rake"
require "bundler/gem_helper"
RSpec.describe Bundler::GemHelper do
let(:app_name) { "lorem__ipsum" }
let(:app_path) { bundled_app app_name }
let(:app_gemspec_path) { app_path.join("#{app_name}.gemspec") }
before(:each) do
global_config "BUNDLE_GEM__MIT" => "false", "BUNDLE_GEM__TEST" => "false", "BUNDLE_GEM__COC" => "false"
bundle "gem #{app_name}"
prepare_gemspec(app_gemspec_path)
end
context "determining gemspec" do
subject { Bundler::GemHelper.new(app_path) }
context "fails" do
it "when there is no gemspec" do
FileUtils.rm app_gemspec_path
expect { subject }.to raise_error(/Unable to determine name/)
end
it "when there are two gemspecs and the name isn't specified" do
FileUtils.touch app_path.join("#{app_name}-2.gemspec")
expect { subject }.to raise_error(/Unable to determine name/)
end
end
context "interpolates the name" do
it "when there is only one gemspec" do
expect(subject.gemspec.name).to eq(app_name)
end
it "for a hidden gemspec" do
FileUtils.mv app_gemspec_path, app_path.join(".gemspec")
expect(subject.gemspec.name).to eq(app_name)
end
end
it "handles namespaces and converts them to CamelCase" do
bundle "gem #{app_name}-foo_bar"
underscore_path = bundled_app "#{app_name}-foo_bar"
lib = underscore_path.join("lib/#{app_name}/foo_bar.rb").read
expect(lib).to include("module LoremIpsum")
expect(lib).to include("module FooBar")
end
end
context "gem management", :ruby_repo do
def mock_confirm_message(message)
expect(Bundler.ui).to receive(:confirm).with(message)
end
def mock_build_message(name, version)
message = "#{name} #{version} built to pkg/#{name}-#{version}.gem."
mock_confirm_message message
end
subject! { Bundler::GemHelper.new(app_path) }
let(:app_version) { "0.1.0" }
let(:app_gem_dir) { app_path.join("pkg") }
let(:app_gem_path) { app_gem_dir.join("#{app_name}-#{app_version}.gem") }
let(:app_gemspec_content) { File.read(app_gemspec_path) }
before(:each) do
content = app_gemspec_content.gsub("TODO: ", "")
content.sub!(/homepage\s+= ".*"/, 'homepage = ""')
content.gsub!(/spec\.metadata.+\n/, "")
File.open(app_gemspec_path, "w") {|file| file << content }
end
it "uses a shell UI for output" do
expect(Bundler.ui).to be_a(Bundler::UI::Shell)
end
describe "#install" do
let!(:rake_application) { Rake.application }
before(:each) do
Rake.application = Rake::Application.new
end
after(:each) do
Rake.application = rake_application
end
context "defines Rake tasks" do
let(:task_names) do
%w[build install release release:guard_clean
release:source_control_push release:rubygem_push]
end
context "before installation" do
it "raises an error with appropriate message" do
task_names.each do |name|
expect { Rake.application[name] }.
to raise_error(/^Don't know how to build task '#{name}'/)
end
end
end
context "after installation" do
before do
subject.install
end
it "adds Rake tasks successfully" do
task_names.each do |name|
expect { Rake.application[name] }.not_to raise_error
expect(Rake.application[name]).to be_instance_of Rake::Task
end
end
it "provides a way to access the gemspec object" do
expect(subject.gemspec.name).to eq(app_name)
end
end
end
end
describe "#build_gem" do
context "when build failed" do
it "raises an error with appropriate message" do
# break the gemspec by adding back the TODOs
File.open(app_gemspec_path, "w") {|file| file << app_gemspec_content }
expect { subject.build_gem }.to raise_error(/TODO/)
end
end
context "when build was successful" do
it "creates .gem file" do
mock_build_message app_name, app_version
subject.build_gem
expect(app_gem_path).to exist
end
end
end
describe "#install_gem" do
context "when installation was successful" do
it "gem is installed" do
mock_build_message app_name, app_version
mock_confirm_message "#{app_name} (#{app_version}) installed."
subject.install_gem(nil, :local)
expect(app_gem_path).to exist
gem_command! :list
expect(out).to include("#{app_name} (#{app_version})")
end
end
context "when installation fails" do
it "raises an error with appropriate message" do
# create empty gem file in order to simulate install failure
allow(subject).to receive(:build_gem) do
FileUtils.mkdir_p(app_gem_dir)
FileUtils.touch app_gem_path
app_gem_path
end
expect { subject.install_gem }.to raise_error(/Couldn't install gem/)
end
end
end
describe "rake release" do
let!(:rake_application) { Rake.application }
before(:each) do
Rake.application = Rake::Application.new
subject.install
end
after(:each) do
Rake.application = rake_application
end
before do
Dir.chdir(app_path) do
`git init`
`git config user.email "you@example.com"`
`git config user.name "name"`
`git config commit.gpgsign false`
`git config push.default simple`
end
# silence messages
allow(Bundler.ui).to receive(:confirm)
allow(Bundler.ui).to receive(:error)
end
context "fails" do
it "when there are unstaged files" do
expect { Rake.application["release"].invoke }.
to raise_error("There are files that need to be committed first.")
end
it "when there are uncommitted files" do
Dir.chdir(app_path) { `git add .` }
expect { Rake.application["release"].invoke }.
to raise_error("There are files that need to be committed first.")
end
it "when there is no git remote" do
Dir.chdir(app_path) { `git commit -a -m "initial commit"` }
expect { Rake.application["release"].invoke }.to raise_error(RuntimeError)
end
end
context "succeeds" do
let(:repo) { build_git("foo", :bare => true) }
before do
Dir.chdir(app_path) do
sys_exec("git remote add origin file://#{repo.path}")
sys_exec('git commit -a -m "initial commit"')
end
end
context "on releasing" do
before do
mock_build_message app_name, app_version
mock_confirm_message "Tagged v#{app_version}."
mock_confirm_message "Pushed git commits and tags."
Dir.chdir(app_path) { sys_exec("git push -u origin master") }
end
it "calls rubygem_push with proper arguments" do
expect(subject).to receive(:rubygem_push).with(app_gem_path.to_s)
Rake.application["release"].invoke
end
it "uses Kernel.system" do
expect(Kernel).to receive(:system).with("gem", "push", app_gem_path.to_s, "--host", "http://example.org").and_return(true)
Rake.application["release"].invoke
end
end
it "even if tag already exists" do
mock_build_message app_name, app_version
mock_confirm_message "Tag v#{app_version} has already been created."
expect(subject).to receive(:rubygem_push).with(app_gem_path.to_s)
Dir.chdir(app_path) do
`git tag -a -m \"Version #{app_version}\" v#{app_version}`
end
Rake.application["release"].invoke
end
end
end
describe "release:rubygem_push" do
let!(:rake_application) { Rake.application }
before(:each) do
Rake.application = Rake::Application.new
subject.install
allow(subject).to receive(:sh_with_input)
end
after(:each) do
Rake.application = rake_application
end
before do
Dir.chdir(app_path) do
`git init`
`git config user.email "you@example.com"`
`git config user.name "name"`
`git config push.default simple`
end
# silence messages
allow(Bundler.ui).to receive(:confirm)
allow(Bundler.ui).to receive(:error)
credentials = double("credentials", "file?" => true)
allow(Bundler.user_home).to receive(:join).
with(".gem/credentials").and_return(credentials)
end
describe "success messaging" do
context "No allowed_push_host set" do
before do
allow(subject).to receive(:allowed_push_host).and_return(nil)
end
around do |example|
orig_host = ENV["RUBYGEMS_HOST"]
ENV["RUBYGEMS_HOST"] = rubygems_host_env
example.run
ENV["RUBYGEMS_HOST"] = orig_host
end
context "RUBYGEMS_HOST env var is set" do
let(:rubygems_host_env) { "https://custom.env.gemhost.com" }
it "should report successful push to the host from the environment" do
mock_confirm_message "Pushed #{app_name} #{app_version} to #{rubygems_host_env}"
Rake.application["release:rubygem_push"].invoke
end
end
context "RUBYGEMS_HOST env var is not set" do
let(:rubygems_host_env) { nil }
it "should report successful push to rubygems.org" do
mock_confirm_message "Pushed #{app_name} #{app_version} to rubygems.org"
Rake.application["release:rubygem_push"].invoke
end
end
context "RUBYGEMS_HOST env var is an empty string" do
let(:rubygems_host_env) { "" }
it "should report successful push to rubygems.org" do
mock_confirm_message "Pushed #{app_name} #{app_version} to rubygems.org"
Rake.application["release:rubygem_push"].invoke
end
end
end
context "allowed_push_host set in gemspec" do
before do
allow(subject).to receive(:allowed_push_host).and_return("https://my.gemhost.com")
end
it "should report successful push to the allowed gem host" do
mock_confirm_message "Pushed #{app_name} #{app_version} to https://my.gemhost.com"
Rake.application["release:rubygem_push"].invoke
end
end
end
end
end
end
|