Skip to content

Commit

Permalink
Provide a better error message if a podspec is found but cannot be pa…
Browse files Browse the repository at this point in the history
…rsed
  • Loading branch information
dnkoutso committed Mar 15, 2017
1 parent f5d7b5b commit 3a8cc9c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,10 @@ To install release candidates run `[sudo] gem install cocoapods --pre`

##### Bug Fixes

* Provide a better error message if a podspec is found but cannot be parsed.
[Dimitris Koutsogiorgas](https://github.com/dnkoutso)
[#6457](https://github.com/CocoaPods/CocoaPods/issues/6457)

* Only share pod target xcscheme if present during validation.
[Dimitris Koutsogiorgas](https://github.com/dnkoutso)
[#6558](https://github.com/CocoaPods/CocoaPods/pull/6558)
Expand Down
36 changes: 22 additions & 14 deletions lib/cocoapods/external_sources/abstract_external_source.rb
Expand Up @@ -111,11 +111,15 @@ def pre_download(sandbox)
title = "Pre-downloading: `#{name}` #{description}"
UI.titled_section(title, :verbose_prefix => '-> ') do
target = sandbox.pod_dir(name)
download_result = Downloader.download(download_request, target, :can_cache => can_cache)
begin
download_result = Downloader.download(download_request, target, :can_cache => can_cache)
rescue Pod::DSLError => e
raise Informative, "Failed to load '#{name}' podspec: #{e.message}"
rescue => _
raise Informative, "Unable to find a specification for '#{name}'."
end
spec = download_result.spec

raise Informative, "Unable to find a specification for '#{name}'." unless spec

store_podspec(sandbox, spec)
sandbox.store_pre_downloaded_pod(name)
sandbox.store_checkout_source(name, download_result.checkout_options)
Expand Down Expand Up @@ -147,17 +151,21 @@ def download_request
# @return [void]
#
def store_podspec(sandbox, spec, json = false)
spec = case spec
when Pathname
Specification.from_file(spec)
when String
path = "#{name}.podspec"
path << '.json' if json
Specification.from_string(spec, path)
when Specification
spec.dup
else
raise "Unknown spec type: #{spec}"
begin
spec = case spec
when Pathname
Specification.from_file(spec)
when String
path = "#{name}.podspec"
path << '.json' if json
Specification.from_string(spec, path)
when Specification
spec.dup
else
raise "Unknown spec type: #{spec}"
end
rescue Pod::DSLError => e
raise Informative, "Failed to load '#{name}' podspec: #{e.message}"
end
spec.defined_in_file = nil
validate_podspec(spec)
Expand Down
10 changes: 3 additions & 7 deletions lib/cocoapods/sandbox/podspec_finder.rb
Expand Up @@ -12,13 +12,9 @@ def podspecs
@specs_by_name = {}
spec_files = Pathname.glob(root + '{,*}.podspec{,.json}')
spec_files.sort_by { |p| -p.to_path.split(File::SEPARATOR).size }.each do |file|
begin
spec = Specification.from_file(file)
spec.validate_cocoapods_version
@specs_by_name[spec.name] = spec
rescue => e
UI.warn "Unable to load a podspec from `#{file.basename}`, skipping:\n\n#{e}"
end
spec = Specification.from_file(file)
spec.validate_cocoapods_version
@specs_by_name[spec.name] = spec
end
@specs_by_name
end
Expand Down
30 changes: 30 additions & 0 deletions spec/unit/external_sources/abstract_external_source_spec.rb
Expand Up @@ -26,6 +26,36 @@ module Pod
@subject.fetch(config.sandbox)
config.sandbox.specification('Reachability').name.should == 'Reachability'
end

it 'raises appropriate error if a DSLError was raised' do
Downloader.stubs(:download).raises(Pod::DSLError.new('Invalid `Reachability.podspec` file:', 'some/path/to/podspec', Exception.new('Error Message')))
should.raise(Informative) do
e = @subject.send(:pre_download, config.sandbox)
e.message.should.include "Failed to load 'Reachability' podspec:"
e.message.should.include 'Invalid `Reachability.podspec` file:'
end
end

it 'raises appropriate error if a DSLError when storing a podspec from string' do
podspec = 'Pod::Spec.new do |s|; error; end'
should.raise(Informative) { @subject.send(:store_podspec, config.sandbox, podspec) }.
message.should.include "Invalid `Reachability.podspec` file: undefined local variable or method `error'"
end

it 'raises appropriate error if a DSLError when storing a podspec from file' do
podspec = 'Pod::Spec.new do |s|; error; end'
path = SpecHelper.temporary_directory + 'BananaLib.podspec'
File.open(path, 'w') { |f| f.write(podspec) }
should.raise(Informative) { @subject.send(:store_podspec, config.sandbox, path) }.
message.should.include "Invalid `BananaLib.podspec` file: undefined local variable or method `error'"
end

it 'raises a generic error if a podspec was not found' do
Downloader.stubs(:download).raises(Pod::Downloader::DownloaderError.new('Some generic exception'))
should.raise(Informative) do
@subject.send(:pre_download, config.sandbox).message.should == "Unable to find a specification for 'Reachability'."
end
end
end

#--------------------------------------#
Expand Down
6 changes: 0 additions & 6 deletions spec/unit/sandbox/podspec_finder_spec.rb
Expand Up @@ -15,12 +15,6 @@ module Pod
@finder.podspecs.should.be.empty
end

it "warns when a found podspec can't be parsed" do
@root.+('RestKit.podspec.json').open('w') { |f| f << '{]' }
@finder.podspecs.should.be.empty
UI.warnings.should.include "Unable to load a podspec from `RestKit.podspec.json`, skipping:\n\n"
end

it 'ignores podspecs not in the root' do
path = @root + 'Dir/RestKit.podspec.json'
path.parent.mkpath
Expand Down

0 comments on commit 3a8cc9c

Please sign in to comment.