Permalink
Browse files

smoothed out size interfaces in Pusher and Rubygem

  • Loading branch information...
gnarg committed Nov 9, 2012
1 parent f905d4c commit ebbc2b629fa66ecee35a8a48a19a194d7059959b
Showing with 48 additions and 27 deletions.
  1. +4 −11 app/models/pusher.rb
  2. +0 −6 app/models/rubygem.rb
  3. +43 −8 test/unit/pusher_test.rb
  4. +1 −2 test/unit/rubygem_test.rb
View
@@ -9,7 +9,7 @@ def initialize(user, body, host_with_port=nil)
end
def process
pull_spec && size_gem && find && authorize && save
pull_spec && find && authorize && save
end
def authorize
@@ -28,15 +28,6 @@ def save
end
end
def size_gem
if body
@size = body.size
true
else
false
end
end
def pull_spec
Gem::Package.open body, "r", nil do |pkg|
@spec = pkg.metadata
@@ -55,7 +46,8 @@ def pull_spec
def find
@rubygem = Rubygem.find_or_initialize_by_name(spec.name)
@version = @rubygem.find_or_initialize_version_from_spec_and_size(spec, size)
@version = @rubygem.find_or_initialize_version_from_spec(spec)
@version.size ||= size
if @version.new_record?
true
@@ -88,6 +80,7 @@ def notify(message, code)
def update
rubygem.update_attributes_from_gem_specification!(version, spec)
rubygem.create_ownership(user) unless version.new_record?
@size = body.size if body
true
rescue ActiveRecord::RecordInvalid, ActiveRecord::Rollback
false
View
@@ -230,12 +230,6 @@ def yank!(version)
end
end
def find_or_initialize_version_from_spec_and_size(spec, size)
version = find_or_initialize_version_from_spec(spec)
version.size = size
version
end
def find_or_initialize_version_from_spec(spec)
version = self.versions.find_or_initialize_by_number_and_platform(spec.version.to_s, spec.original_platform.to_s)
version.rubygem = self
View
@@ -38,7 +38,6 @@ class PusherTest < ActiveSupport::TestCase
context "processing incoming gems" do
should "work normally when things go well" do
mock(@cutter).pull_spec { true }
mock(@cutter).size_gem { true }
mock(@cutter).find { true }
stub(@cutter).authorize { true }
mock(@cutter).save
@@ -48,7 +47,6 @@ class PusherTest < ActiveSupport::TestCase
should "not attempt to find rubygem if spec can't be pulled" do
mock(@cutter).pull_spec { false }
mock(@cutter).size_gem.never
mock(@cutter).find.never
mock(@cutter).authorize.never
mock(@cutter).save.never
@@ -57,7 +55,6 @@ class PusherTest < ActiveSupport::TestCase
should "not attempt to authorize if not found" do
mock(@cutter).pull_spec { true }
mock(@cutter).size_gem { true }
mock(@cutter).find { nil }
mock(@cutter).authorize.never
mock(@cutter).save.never
@@ -67,7 +64,6 @@ class PusherTest < ActiveSupport::TestCase
should "not attempt to save if not authorized" do
mock(@cutter).pull_spec { true }
mock(@cutter).size_gem { true }
mock(@cutter).find { true }
mock(@cutter).authorize { false }
mock(@cutter).save.never
@@ -84,19 +80,31 @@ class PusherTest < ActiveSupport::TestCase
assert_equal @cutter.code, 422
end
context "finding rubygem" do
should "initialize new gem if one does not exist" do
context "initialize new gem with find if one does not exist" do
setup do
spec = "spec"
stub(spec).name { "some name" }
stub(spec).version { "1.3.3.7" }
stub(spec).original_platform { "ruby" }
stub(@cutter).spec { spec }
stub(@cutter).size { 5 }
@cutter.find
end
assert_not_nil @cutter.rubygem
assert_not_nil @cutter.version
should "set rubygem" do
assert_equal 'some name', @cutter.rubygem.name
end
should "set version" do
assert_equal '1.3.3.7', @cutter.version.number
end
should "set gem version size" do
assert_equal 5, @cutter.version.size
end
end
context "finding an existing gem" do
should "bring up existing gem with matching spec" do
@rubygem = create(:rubygem)
spec = "spec"
@@ -145,5 +153,32 @@ class PusherTest < ActiveSupport::TestCase
end
end
end
context "successfully saving a gemcutter" do
setup do
@rubygem = create(:rubygem)
stub(@cutter).rubygem { @rubygem }
create(:version, :rubygem => @rubygem, :number => '0.1.1')
stub(@cutter).version { @rubygem.versions[0] }
stub(@rubygem).update_attributes_from_gem_specification!
any_instance_of(Indexer) {|i| stub(i).write_gem }
@cutter.save
end
should "update rubygem attributes" do
assert_received(@rubygem) do |rubygem|
rubygem.update_attributes_from_gem_specification!(@cutter.version,
@cutter.spec)
end
end
should "set gem file size" do
assert_equal @gem.size, @cutter.size
end
should "set success code" do
assert_equal 200, @cutter.code
end
end
end
end
@@ -548,7 +548,7 @@ class RubygemTest < ActiveSupport::TestCase
Rubygem.create(:name => "rake")
@rubygem = Rubygem.new(:name => @specification.name)
@version = @rubygem.find_or_initialize_version_from_spec_and_size(@specification, 5)
@version = @rubygem.find_or_initialize_version_from_spec(@specification)
end
should "save the gem" do
@@ -561,7 +561,6 @@ class RubygemTest < ActiveSupport::TestCase
assert_equal 1, @rubygem.versions.count
assert_equal 1, @rubygem.versions_count
assert_equal 2, @version.dependencies.count
assert_equal 5, @version.size
assert Rubygem.exists?(:name => 'thoughtbot-shoulda')
assert Rubygem.exists?(:name => 'rake')
end

0 comments on commit ebbc2b6

Please sign in to comment.