Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/cibuildgem/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "thor"
require "rake/extensiontask"
require "prism"
require "open3"

module Cibuildgem
class CLI < Thor
Expand Down Expand Up @@ -112,7 +113,16 @@ def release
pathname = Pathname(file)
next if pathname.directory? || pathname.extname != ".gem"

Kernel.system("gem push #{file}", exception: true)
out, status = Open3.capture2e("gem push #{file}")
next if status.success?

if out =~ /Repushing of gem versions is not allowed/
puts "Gem #{file} already exists on RubyGems.org, skipping..."

next
end

raise out
end
end

Expand Down
64 changes: 62 additions & 2 deletions test/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "test_helper"
require "English"
require "open3"

module Cibuildgem
class CLITest < Minitest::Test
Expand Down Expand Up @@ -129,14 +130,20 @@ def test_ci_template_when_passed_a_test_command_and_workdir
FileUtils.rm_rf("test/fixtures/dummy_gem/.github")
end

def test_release
def test_release_succeeds
FileUtils.touch("tmp/foo.gem")
FileUtils.touch("tmp/bar.gem")
FileUtils.touch("tmp/some_file")

status = Struct.new(:success?)
gem_pushed = []
callable = proc do |gem_name|
gem_pushed << gem_name

Kernel.stub(:system, ->(gem, _) { gem_pushed << gem }) do
["", status.new(true)]
end

Open3.stub(:capture2e, callable) do
CLI.start(["release", "--glob", "tmp/*"])
end

Expand All @@ -147,6 +154,59 @@ def test_release
FileUtils.rm_rf("tmp/some_file")
end

def test_release_when_gem_was_already_pushed
FileUtils.touch("tmp/foo.gem")
FileUtils.touch("tmp/bar.gem")
FileUtils.touch("tmp/some_file")

status = Struct.new(:success?)
gem_pushed = []
callable = proc do |gem_name|
gem_pushed << gem_name

if gem_name == "gem push tmp/bar.gem"
["Repushing of gem versions is not allowed", status.new(false)]
else
["", status.new(true)]
end
end

Open3.stub(:capture2e, callable) do
out, _ = capture_subprocess_io do
CLI.start(["release", "--glob", "tmp/*"])
end

assert_equal("Gem tmp/bar.gem already exists on RubyGems.org, skipping...\n", out)
end

assert_equal(["gem push tmp/bar.gem", "gem push tmp/foo.gem"], gem_pushed.sort)
ensure
FileUtils.rm_rf("tmp/foo.gem")
FileUtils.rm_rf("tmp/bar.gem")
FileUtils.rm_rf("tmp/some_file")
end

def test_release_fails
FileUtils.touch("tmp/foo.gem")
FileUtils.touch("tmp/bar.gem")
FileUtils.touch("tmp/some_file")

status = Struct.new(:success?)
callable = proc do
["Something went wrong", status.new(false)]
end

Open3.stub(:capture2e, callable) do
assert_raises(RuntimeError) do
CLI.start(["release", "--glob", "tmp/*"])
end
end
ensure
FileUtils.rm_rf("tmp/foo.gem")
FileUtils.rm_rf("tmp/bar.gem")
FileUtils.rm_rf("tmp/some_file")
end

def test_print_ruby_cc_version
out, _ = capture_subprocess_io do
Dir.chdir("test/fixtures/dummy_gem") do
Expand Down