Skip to content

Commit ef475da

Browse files
authored
Merge pull request #56 from Shopify/ec-push
Rescue when a gem has already been pushed:
2 parents 070d51c + 33761df commit ef475da

2 files changed

Lines changed: 73 additions & 3 deletions

File tree

lib/cibuildgem/cli.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "thor"
44
require "rake/extensiontask"
55
require "prism"
6+
require "open3"
67

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

115-
Kernel.system("gem push #{file}", exception: true)
116+
out, status = Open3.capture2e("gem push #{file}")
117+
next if status.success?
118+
119+
if out =~ /Repushing of gem versions is not allowed/
120+
puts "Gem #{file} already exists on RubyGems.org, skipping..."
121+
122+
next
123+
end
124+
125+
raise out
116126
end
117127
end
118128

test/cli_test.rb

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "test_helper"
44
require "English"
5+
require "open3"
56

67
module Cibuildgem
78
class CLITest < Minitest::Test
@@ -129,14 +130,20 @@ def test_ci_template_when_passed_a_test_command_and_workdir
129130
FileUtils.rm_rf("test/fixtures/dummy_gem/.github")
130131
end
131132

132-
def test_release
133+
def test_release_succeeds
133134
FileUtils.touch("tmp/foo.gem")
134135
FileUtils.touch("tmp/bar.gem")
135136
FileUtils.touch("tmp/some_file")
136137

138+
status = Struct.new(:success?)
137139
gem_pushed = []
140+
callable = proc do |gem_name|
141+
gem_pushed << gem_name
138142

139-
Kernel.stub(:system, ->(gem, _) { gem_pushed << gem }) do
143+
["", status.new(true)]
144+
end
145+
146+
Open3.stub(:capture2e, callable) do
140147
CLI.start(["release", "--glob", "tmp/*"])
141148
end
142149

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

157+
def test_release_when_gem_was_already_pushed
158+
FileUtils.touch("tmp/foo.gem")
159+
FileUtils.touch("tmp/bar.gem")
160+
FileUtils.touch("tmp/some_file")
161+
162+
status = Struct.new(:success?)
163+
gem_pushed = []
164+
callable = proc do |gem_name|
165+
gem_pushed << gem_name
166+
167+
if gem_name == "gem push tmp/bar.gem"
168+
["Repushing of gem versions is not allowed", status.new(false)]
169+
else
170+
["", status.new(true)]
171+
end
172+
end
173+
174+
Open3.stub(:capture2e, callable) do
175+
out, _ = capture_subprocess_io do
176+
CLI.start(["release", "--glob", "tmp/*"])
177+
end
178+
179+
assert_equal("Gem tmp/bar.gem already exists on RubyGems.org, skipping...\n", out)
180+
end
181+
182+
assert_equal(["gem push tmp/bar.gem", "gem push tmp/foo.gem"], gem_pushed.sort)
183+
ensure
184+
FileUtils.rm_rf("tmp/foo.gem")
185+
FileUtils.rm_rf("tmp/bar.gem")
186+
FileUtils.rm_rf("tmp/some_file")
187+
end
188+
189+
def test_release_fails
190+
FileUtils.touch("tmp/foo.gem")
191+
FileUtils.touch("tmp/bar.gem")
192+
FileUtils.touch("tmp/some_file")
193+
194+
status = Struct.new(:success?)
195+
callable = proc do
196+
["Something went wrong", status.new(false)]
197+
end
198+
199+
Open3.stub(:capture2e, callable) do
200+
assert_raises(RuntimeError) do
201+
CLI.start(["release", "--glob", "tmp/*"])
202+
end
203+
end
204+
ensure
205+
FileUtils.rm_rf("tmp/foo.gem")
206+
FileUtils.rm_rf("tmp/bar.gem")
207+
FileUtils.rm_rf("tmp/some_file")
208+
end
209+
150210
def test_print_ruby_cc_version
151211
out, _ = capture_subprocess_io do
152212
Dir.chdir("test/fixtures/dummy_gem") do

0 commit comments

Comments
 (0)