Commit 2659252f authored by Kyle Fuller's avatar Kyle Fuller

[linter] Refactor URL validation tests

parent dfbacc75
require File.expand_path('../../spec_helper', __FILE__) require File.expand_path('../../spec_helper', __FILE__)
require 'webmock'
module Bacon module Bacon
class Context class Context
...@@ -86,8 +87,7 @@ module Pod ...@@ -86,8 +87,7 @@ module Pod
file = write_podspec(stub_podspec) file = write_podspec(stub_podspec)
validator = Validator.new(file) validator = Validator.new(file)
validator.quick = true validator.quick = true
validator.stubs(:validate_homepage) validator.stubs(:validate_url)
validator.stubs(:validate_screenshots)
validator.validate validator.validate
validator.validation_dir.should.be == Pathname.new("/private/tmp/CocoaPods/Lint") validator.validation_dir.should.be == Pathname.new("/private/tmp/CocoaPods/Lint")
end end
...@@ -97,9 +97,7 @@ module Pod ...@@ -97,9 +97,7 @@ module Pod
describe "Extensive analysis" do describe "Extensive analysis" do
describe "Homepage validation" do describe "URL validation" do
require 'webmock'
before do before do
@sut = Validator.new(podspec_path) @sut = Validator.new(podspec_path)
@sut.stubs(:install_pod) @sut.stubs(:install_pod)
...@@ -108,101 +106,95 @@ module Pod ...@@ -108,101 +106,95 @@ module Pod
@sut.stubs(:tear_down_validation_environment) @sut.stubs(:tear_down_validation_environment)
end end
it "checks if the homepage is valid" do describe "Homepage validation" do
WebMock::API.stub_request(:head, /not-found/).to_return(:status => 404) it "checks if the homepage is valid" do
WebMock::API.stub_request(:get, /not-found/).to_return(:status => 404) WebMock::API.stub_request(:head, /not-found/).to_return(:status => 404)
Specification.any_instance.stubs(:homepage).returns('http://banana-corp.local/not-found/') WebMock::API.stub_request(:get, /not-found/).to_return(:status => 404)
@sut.validate Specification.any_instance.stubs(:homepage).returns('http://banana-corp.local/not-found/')
@sut.results.map(&:to_s).first.should.match /The URL (.*) is not reachable/ @sut.validate
end @sut.results.map(&:to_s).first.should.match /The URL (.*) is not reachable/
end
it "indicates if it was not able to validate the homepage" do
WebMock::API.stub_request(:head, 'banana-corp.local').to_raise(SocketError) it "indicates if it was not able to validate the homepage" do
Specification.any_instance.stubs(:homepage).returns('http://banana-corp.local/') WebMock::API.stub_request(:head, 'banana-corp.local').to_raise(SocketError)
@sut.validate Specification.any_instance.stubs(:homepage).returns('http://banana-corp.local/')
@sut.results.map(&:to_s).first.should.match /There was a problem validating the URL/ @sut.validate
end @sut.results.map(&:to_s).first.should.match /There was a problem validating the URL/
end
it "does not fail if the homepage redirects" do
WebMock::API.stub_request(:head, /redirect/).to_return( it "does not fail if the homepage redirects" do
:status => 301, :headers => { 'Location' => 'http://banana-corp.local/found/' } ) WebMock::API.stub_request(:head, /redirect/).to_return(
WebMock::API.stub_request(:head, /found/).to_return( :status => 200 ) :status => 301, :headers => { 'Location' => 'http://banana-corp.local/found/' } )
Specification.any_instance.stubs(:homepage).returns('http://banana-corp.local/redirect/') WebMock::API.stub_request(:head, /found/).to_return( :status => 200 )
@sut.validate Specification.any_instance.stubs(:homepage).returns('http://banana-corp.local/redirect/')
@sut.results.length.should.equal 0 @sut.validate
end @sut.results.length.should.equal 0
end
it "does not fail if the homepage does not support HEAD" do
WebMock::API.stub_request(:head, /page/).to_return( :status => 405 ) it "does not fail if the homepage does not support HEAD" do
WebMock::API.stub_request(:get, /page/).to_return( :status => 200 ) WebMock::API.stub_request(:head, /page/).to_return( :status => 405 )
Specification.any_instance.stubs(:homepage).returns('http://banana-corp.local/page/') WebMock::API.stub_request(:get, /page/).to_return( :status => 200 )
@sut.validate Specification.any_instance.stubs(:homepage).returns('http://banana-corp.local/page/')
@sut.results.length.should.equal 0 @sut.validate
end @sut.results.length.should.equal 0
end
it "does not fail if the homepage errors on HEAD" do
WebMock::API.stub_request(:head, /page/).to_return( :status => 500 ) it "does not fail if the homepage errors on HEAD" do
WebMock::API.stub_request(:get, /page/).to_return( :status => 200 ) WebMock::API.stub_request(:head, /page/).to_return( :status => 500 )
Specification.any_instance.stubs(:homepage).returns('http://banana-corp.local/page/') WebMock::API.stub_request(:get, /page/).to_return( :status => 200 )
@sut.validate Specification.any_instance.stubs(:homepage).returns('http://banana-corp.local/page/')
@sut.results.length.should.equal 0 @sut.validate
end @sut.results.length.should.equal 0
end
it "does not follow redirects infinitely" do
WebMock::API.stub_request(:head, /redirect/).to_return( it "does not follow redirects infinitely" do
:status => 301, WebMock::API.stub_request(:head, /redirect/).to_return(
:headers => { 'Location' => 'http://banana-corp.local/redirect/' } ) :status => 301,
Specification.any_instance.stubs(:homepage).returns( :headers => { 'Location' => 'http://banana-corp.local/redirect/' } )
'http://banana-corp.local/redirect/') Specification.any_instance.stubs(:homepage).returns(
@sut.validate 'http://banana-corp.local/redirect/')
@sut.results.map(&:to_s).first.should.match /The URL \(.*\) is not reachable/ @sut.validate
end @sut.results.map(&:to_s).first.should.match /The URL \(.*\) is not reachable/
end
it "supports relative redirects" do
WebMock::API.stub_request(:head, /redirect/).to_return( it "supports relative redirects" do
:status => 302, WebMock::API.stub_request(:head, /redirect/).to_return(
:headers => { 'Location' => '/foo' }) :status => 302,
WebMock::API.stub_request(:head, /foo/).to_return( :headers => { 'Location' => '/foo' })
WebMock::API.stub_request(:head, /foo/).to_return(
:status => 200 ) :status => 200 )
Specification.any_instance.stubs(:homepage).returns( Specification.any_instance.stubs(:homepage).returns(
'http://banana-corp.local/redirect') 'http://banana-corp.local/redirect')
@sut.validate @sut.validate
@sut.results.length.should.equal 0 @sut.results.length.should.equal 0
end
end end
end
describe "Screenshot validation" do describe "Screenshot validation" do
require 'webmock' before do
@sut.stubs(:validate_homepage)
before do WebMock::API.stub_request(:head, 'banana-corp.local/valid-image.png').to_return(:status => 200, :headers => { 'Content-Type' => 'image/png' })
@sut = Validator.new(podspec_path) end
@sut.stubs(:install_pod)
@sut.stubs(:build_pod) it "checks if the screenshots are valid" do
@sut.stubs(:check_file_patterns) Specification.any_instance.stubs(:screenshots).returns(['http://banana-corp.local/valid-image.png'])
@sut.stubs(:tear_down_validation_environment) @sut.validate
@sut.stubs(:validate_homepage) @sut.results.should.be.empty?
WebMock::API.stub_request(:head, 'banana-corp.local/valid-image.png').to_return(:status => 200, :headers => { 'Content-Type' => 'image/png' }) end
end
it "should fail if any of the screenshots URLS do not return an image" do
it "checks if the screenshots are valid" do WebMock::API.stub_request(:head, 'banana-corp.local/').to_return(:status => 200)
Specification.any_instance.stubs(:screenshots).returns(['http://banana-corp.local/valid-image.png']) Specification.any_instance.stubs(:screenshots).returns(['http://banana-corp.local/valid-image.png', 'http://banana-corp.local/'])
@sut.validate @sut.validate
@sut.results.should.be.empty? @sut.results.map(&:to_s).first.should.match /The screenshot .* is not a valid image/
end end
it "should fail if any of the screenshots URLS do not return an image" do
WebMock::API.stub_request(:head, 'banana-corp.local/').to_return(:status => 200)
Specification.any_instance.stubs(:screenshots).returns(['http://banana-corp.local/valid-image.png', 'http://banana-corp.local/'])
@sut.validate
@sut.results.map(&:to_s).first.should.match /The screenshot .* is not a valid image/
end end
end end
it "respects the no clean option" do it "respects the no clean option" do
file = write_podspec(stub_podspec) file = write_podspec(stub_podspec)
sut = Validator.new(file) sut = Validator.new(file)
sut.stubs(:validate_homepage) sut.stubs(:validate_url)
sut.stubs(:validate_screenshots)
sut.no_clean = true sut.no_clean = true
sut.validate sut.validate
sut.validation_dir.should.exist sut.validation_dir.should.exist
...@@ -211,8 +203,7 @@ module Pod ...@@ -211,8 +203,7 @@ module Pod
it "builds the pod per platform" do it "builds the pod per platform" do
file = write_podspec(stub_podspec) file = write_podspec(stub_podspec)
sut = Validator.new(file) sut = Validator.new(file)
sut.stubs(:validate_homepage) sut.stubs(:validate_url)
sut.stubs(:validate_screenshots)
sut.expects(:install_pod).twice sut.expects(:install_pod).twice
sut.expects(:build_pod).twice sut.expects(:build_pod).twice
sut.expects(:check_file_patterns).twice sut.expects(:check_file_patterns).twice
...@@ -221,7 +212,7 @@ module Pod ...@@ -221,7 +212,7 @@ module Pod
it "uses the deployment target of the specification" do it "uses the deployment target of the specification" do
sut = Validator.new(podspec_path) sut = Validator.new(podspec_path)
sut.stubs(:validate_homepage) sut.stubs(:validate_url)
sut.stubs(:validate_screenshots) sut.stubs(:validate_screenshots)
podfile = sut.send(:podfile_from_spec, :ios, '5.0') podfile = sut.send(:podfile_from_spec, :ios, '5.0')
dependency = podfile.target_definitions['Pods'].dependencies.first dependency = podfile.target_definitions['Pods'].dependencies.first
...@@ -230,8 +221,7 @@ module Pod ...@@ -230,8 +221,7 @@ module Pod
it "respects the local option" do it "respects the local option" do
sut = Validator.new(podspec_path) sut = Validator.new(podspec_path)
sut.stubs(:validate_homepage) sut.stubs(:validate_url)
sut.stubs(:validate_screenshots)
podfile = sut.send(:podfile_from_spec, :ios, '5.0') podfile = sut.send(:podfile_from_spec, :ios, '5.0')
deployment_target = podfile.target_definitions['Pods'].platform.deployment_target deployment_target = podfile.target_definitions['Pods'].platform.deployment_target
deployment_target.to_s.should == "5.0" deployment_target.to_s.should == "5.0"
...@@ -241,8 +231,7 @@ module Pod ...@@ -241,8 +231,7 @@ module Pod
sut = Validator.new(podspec_path) sut = Validator.new(podspec_path)
sut.stubs(:check_file_patterns) sut.stubs(:check_file_patterns)
sut.stubs(:xcodebuild).returns("file.m:1:1: warning: direct access to objective-c's isa is deprecated") sut.stubs(:xcodebuild).returns("file.m:1:1: warning: direct access to objective-c's isa is deprecated")
sut.stubs(:validate_homepage) sut.stubs(:validate_url)
sut.stubs(:validate_screenshots)
sut.validate sut.validate
first = sut.results.map(&:to_s).first first = sut.results.map(&:to_s).first
first.should.include "[xcodebuild]" first.should.include "[xcodebuild]"
...@@ -253,8 +242,7 @@ module Pod ...@@ -253,8 +242,7 @@ module Pod
file = write_podspec(stub_podspec(/s\.source_files = 'JSONKit\.\*'/, "s.source_files = 'wrong_paht.*'")) file = write_podspec(stub_podspec(/s\.source_files = 'JSONKit\.\*'/, "s.source_files = 'wrong_paht.*'"))
sut = Validator.new(file) sut = Validator.new(file)
sut.stubs(:build_pod) sut.stubs(:build_pod)
sut.stubs(:validate_homepage) sut.stubs(:validate_url)
sut.stubs(:validate_screenshots)
sut.validate sut.validate
sut.results.map(&:to_s).first.should.match /source_files.*did not match/ sut.results.map(&:to_s).first.should.match /source_files.*did not match/
sut.result_type.should == :error sut.result_type.should == :error
...@@ -268,8 +256,7 @@ module Pod ...@@ -268,8 +256,7 @@ module Pod
spec = Specification.from_file(file) spec = Specification.from_file(file)
sut = Validator.new(spec) sut = Validator.new(spec)
sut.stubs(:validate_homepage) sut.stubs(:validate_url)
sut.stubs(:validate_screenshots)
sut.stubs(:build_pod) sut.stubs(:build_pod)
sut.validate sut.validate
sut.validated?.should.be.true sut.validated?.should.be.true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment