Skip to content
This repository was archived by the owner on Apr 14, 2021. It is now read-only.

Commit 6705960

Browse files
bundlerbotdeivid-rodriguez
authored andcommitted
Merge #7199
7199: Allow `rake release` to ask for input (3rd take) r=colby-swandale a=deivid-rodriguez This PR supersedes #7108 and #7005. It fixes #6854. ### What was the end-user problem that led to this PR? The problem was that if users has 2FA authentication on their rubygems account, `rake release` doesn't really work at the moment, since it hangs asking for the OTP code, but without letting the user know. ### What was your diagnosis of the problem? My diagnosis was that we need to allow the `rake release` helper that shells out to `gem push` to read `gem push` output and show it to the user, so that she can introduce the requested information. ### What is your fix for the problem, implemented in this PR? My fix is inspired by @segiddins's comment in #6854 (comment). `Kernel#system` works like we would expect in this situation. ### Why did you choose this fix out of the possible options? I chose this fix because #7108 had a few problems: * It would update the `sh` helper, which is used in many different places. This was unnecessary since most of the times we shell out to the `gem` CLI we don't need to ask for input, and it also produced a very verbose output in those cases, since everything the `gem` CLI prints to the screen would be printed by the bundler helpers too. This PR does not change the current output, other than for `rake release`. * It would print _duplicate_ output. This is a `rake release` test using #7108: ``` $ RUBYOPT="-I../bundler/lib" ../bundler/exe/bundle exec rake release Successfully built RubyGem Name: rake_release_tester Version: 0.1.0 File: rake_release_tester-0.1.0.gem rake_release_tester 0.1.0 built to pkg/rake_release_tester-0.1.0.gem. v0.1.0 Tag v0.1.0 has already been created. Pushing gem to https://rubygems.org... You have enabled multi-factor authentication. Please enter OTP code. Code: asd Your OTP code is incorrect. Please check it and retry. rake aborted! Pushing gem to https://rubygems.org... You have enabled multi-factor authentication. Please enter OTP code. Code: Your OTP code is incorrect. Please check it and retry. /home/deivid/Code/bundler/lib/bundler/gem_helper.rb:187:in `sh' /home/deivid/Code/bundler/lib/bundler/gem_helper.rb:109:in `rubygem_push' /home/deivid/Code/bundler/lib/bundler/gem_helper.rb:70:in `block in install' /home/deivid/Code/bundler/lib/bundler/cli/exec.rb:69:in `load' /home/deivid/Code/bundler/lib/bundler/cli/exec.rb:69:in `kernel_load' /home/deivid/Code/bundler/lib/bundler/cli/exec.rb:28:in `run' /home/deivid/Code/bundler/lib/bundler/cli.rb:468:in `exec' /home/deivid/Code/bundler/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run' /home/deivid/Code/bundler/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command' /home/deivid/Code/bundler/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch' /home/deivid/Code/bundler/lib/bundler/cli.rb:26:in `dispatch' /home/deivid/Code/bundler/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start' /home/deivid/Code/bundler/lib/bundler/cli.rb:17:in `start' ../bundler/exe/bundle:30:in `block in <main>' /home/deivid/Code/bundler/lib/bundler/friendly_errors.rb:123:in `with_friendly_errors' ../bundler/exe/bundle:22:in `<main>' Tasks: TOP => release => release:rubygem_push (See full trace by running task with --trace) ``` This is the same test using this PR: ``` $ RUBYOPT="-I../bundler/lib" ../bundler/exe/bundle exec rake release rake_release_tester 0.1.0 built to pkg/rake_release_tester-0.1.0.gem. Tag v0.1.0 has already been created. Pushing gem to https://rubygems.org... You have enabled multi-factor authentication. Please enter OTP code. Code: asdfasdf Your OTP code is incorrect. Please check it and retry. ``` * Previous approach was hard to test. The test included here might not be great but it's something... Co-authored-by: David Rodríguez <deivid.rodriguez@riseup.net> (cherry picked from commit cd05f13)
1 parent 293d743 commit 6705960

File tree

4 files changed

+45
-22
lines changed

4 files changed

+45
-22
lines changed

lib/bundler/gem_helper.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def rubygem_push(path)
106106
unless allowed_push_host || Bundler.user_home.join(".gem/credentials").file?
107107
raise "Your rubygems.org credentials aren't set. Run `gem push` to set them."
108108
end
109-
sh(gem_command)
109+
sh_with_input(gem_command)
110110
Bundler.ui.confirm "Pushed #{name} #{version} to #{gem_push_host}"
111111
end
112112

@@ -180,6 +180,13 @@ def name
180180
gemspec.name
181181
end
182182

183+
def sh_with_input(cmd)
184+
Bundler.ui.debug(cmd)
185+
SharedHelpers.chdir(base) do
186+
abort unless Kernel.system(*cmd)
187+
end
188+
end
189+
183190
def sh(cmd, &block)
184191
out, status = sh_with_status(cmd, &block)
185192
unless status.success?

spec/bundler/gem_helper_spec.rb

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
before(:each) do
1212
global_config "BUNDLE_GEM__MIT" => "false", "BUNDLE_GEM__TEST" => "false", "BUNDLE_GEM__COC" => "false"
1313
bundle "gem #{app_name}"
14+
prepare_gemspec(app_gemspec_path)
1415
end
1516

1617
context "determining gemspec" do
@@ -218,15 +219,26 @@ def mock_build_message(name, version)
218219
end
219220
end
220221

221-
it "on releasing" do
222-
mock_build_message app_name, app_version
223-
mock_confirm_message "Tagged v#{app_version}."
224-
mock_confirm_message "Pushed git commits and tags."
225-
expect(subject).to receive(:rubygem_push).with(app_gem_path.to_s)
222+
context "on releasing" do
223+
before do
224+
mock_build_message app_name, app_version
225+
mock_confirm_message "Tagged v#{app_version}."
226+
mock_confirm_message "Pushed git commits and tags."
226227

227-
Dir.chdir(app_path) { sys_exec("git push -u origin master") }
228+
Dir.chdir(app_path) { sys_exec("git push -u origin master") }
229+
end
228230

229-
Rake.application["release"].invoke
231+
it "calls rubygem_push with proper arguments" do
232+
expect(subject).to receive(:rubygem_push).with(app_gem_path.to_s)
233+
234+
Rake.application["release"].invoke
235+
end
236+
237+
it "uses Kernel.system" do
238+
expect(Kernel).to receive(:system).with("gem", "push", app_gem_path.to_s, "--host", "http://example.org").and_return(true)
239+
240+
Rake.application["release"].invoke
241+
end
230242
end
231243

232244
it "even if tag already exists" do
@@ -249,7 +261,7 @@ def mock_build_message(name, version)
249261
before(:each) do
250262
Rake.application = Rake::Application.new
251263
subject.install
252-
allow(subject).to receive(:sh)
264+
allow(subject).to receive(:sh_with_input)
253265
end
254266

255267
after(:each) do

spec/commands/newgem_spec.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,7 @@ def gem_skeleton_assertions(gem_name)
182182
in_app_root
183183
bundle! "gem newgem --bin"
184184

185-
process_file(bundled_app("newgem", "newgem.gemspec")) do |line|
186-
# Simulate replacing TODOs with real values
187-
case line
188-
when /spec\.metadata\["(?:allowed_push_host|homepage_uri|source_code_uri|changelog_uri)"\]/, /spec\.homepage/
189-
line.gsub(/\=.*$/, "= 'http://example.org'")
190-
when /spec\.summary/
191-
line.gsub(/\=.*$/, "= %q{A short summary of my new gem.}")
192-
when /spec\.description/
193-
line.gsub(/\=.*$/, "= %q{A longer description of my new gem.}")
194-
else
195-
line
196-
end
197-
end
185+
prepare_gemspec(bundled_app("newgem", "newgem.gemspec"))
198186

199187
Dir.chdir(bundled_app("newgem")) do
200188
gems = ["rake-10.0.2", :bundler]

spec/support/helpers.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,22 @@ def with_read_only(pattern)
543543
Dir[pattern].each(&chmod[0o755, 0o644])
544544
end
545545

546+
# Simulate replacing TODOs with real values
547+
def prepare_gemspec(pathname)
548+
process_file(pathname) do |line|
549+
case line
550+
when /spec\.metadata\["(?:allowed_push_host|homepage_uri|source_code_uri|changelog_uri)"\]/, /spec\.homepage/
551+
line.gsub(/\=.*$/, "= 'http://example.org'")
552+
when /spec\.summary/
553+
line.gsub(/\=.*$/, "= %q{A short summary of my new gem.}")
554+
when /spec\.description/
555+
line.gsub(/\=.*$/, "= %q{A longer description of my new gem.}")
556+
else
557+
line
558+
end
559+
end
560+
end
561+
546562
def process_file(pathname)
547563
changed_lines = pathname.readlines.map do |line|
548564
yield line

0 commit comments

Comments
 (0)