Skip to content

Commit

Permalink
validate gemspec specifications (with a test)
Browse files Browse the repository at this point in the history
  • Loading branch information
indirect committed May 13, 2015
1 parent ac66cd2 commit 654e44a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
23 changes: 20 additions & 3 deletions lib/bundler/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ def gemspec(opts = nil)

case gemspecs.size
when 1
spec = Bundler.load_gemspec(gemspecs.first)
raise InvalidOption, "There was an error loading the gemspec at #{gemspecs.first}." unless spec
spec = load_valid_gemspec(gemspecs.first)

gem spec.name, :path => path, :glob => glob

group(development_group) do
spec.development_dependencies.each do |dep|
gem dep.name, *(dep.requirement.as_list + [:type => :development])
Expand All @@ -59,7 +60,8 @@ def gemspec(opts = nil)
when 0
raise InvalidOption, "There are no gemspecs at #{expanded_path}."
else
raise InvalidOption, "There are multiple gemspecs at #{expanded_path}. Please use the :name option to specify which one."
raise InvalidOption, "There are multiple gemspecs at #{expanded_path}. " \
"Please use the :name option to specify which one should be used."
end
end

Expand Down Expand Up @@ -358,6 +360,21 @@ def check_primary_source_safety(source)
end
end

def load_valid_gemspec(path)
spec = Bundler.load_gemspec(path)

unless spec
raise InvalidOption, "There was an error loading the gemspec at " \
"#{path}. Make sure you can build the gem, then try again."
end

spec.validate
spec
rescue Gem::InvalidSpecificationException => e
raise InvalidOption, "The gemspec at #{path} is not valid. " \
"The validation error was '#{e.message}'"
end

class DSLError < GemfileError
# @return [String] the description that should be presented to the user.
#
Expand Down
14 changes: 14 additions & 0 deletions spec/bundler/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,18 @@
to raise_error(Bundler::GemfileError, /There was an error parsing `Gemfile`: can't modify frozen String. Bundler cannot continue./)
end
end

describe "#gemspec" do
it "errors on invalid specs" do
File.open(bundled_app("foo.gemspec"), "w") do |f|
f.write <<-G
Gem::Specification.new do |s|
s.name = "foo"
end
G
end
expect(Bundler).to receive(:default_gemfile).and_return(bundled_app("Gemfile"))
expect { subject.gemspec }.to raise_error(Bundler::InvalidOption).with_message(/missing value for attribute version/)
end
end
end

0 comments on commit 654e44a

Please sign in to comment.