Skip to content

Commit

Permalink
Rawler returns number of errors as exit code
Browse files Browse the repository at this point in the history
With a proper exit code, rawler can now more easily be set up in autmated
environments like continues integration systems and ensure the link
integrity of static pages.
  • Loading branch information
enricogenauck committed Feb 1, 2015
1 parent 943b283 commit 4f2f385
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
7 changes: 5 additions & 2 deletions bin/rawler
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

require_relative '../lib/rawler'
require File.join(File.dirname(__FILE__), '..', '/vendor/lib-trollop.rb')
require_relative '../vendor/lib-trollop'

opts = Trollop::options do
version "rawler #{Rawler::VERSION} (c) 2011 Oscar Del Ben"
Expand Down Expand Up @@ -39,4 +39,7 @@ else
end
end

Rawler::Base.new(domain, $stdout, opts).validate
rawler = Rawler::Base.new(domain, $stdout, opts)
rawler.validate

exit(rawler.errors.count)
6 changes: 6 additions & 0 deletions lib/rawler/base.rb
Expand Up @@ -37,6 +37,12 @@ def validate
@logfile.close if Rawler.log
end

def errors
@responses.reject{ |link, response|
(100..399).include?(response[:status].to_i)
}
end

private

def validate_links_in_page(page)
Expand Down
28 changes: 27 additions & 1 deletion spec/lib/rawler_spec.rb
Expand Up @@ -282,9 +282,35 @@
output.should_receive(:error).with("Unknown code #{code} - #{link} - Called from: #{from}")
rawler.send(:record_response, code, link, from)
end
end
end

describe "error introspection" do
context "no error codes occured" do
before do
rawler.responses["http://example.com/"] = { :status => 100 }
rawler.responses["http://example.com/foo"] = { :status => 200 }
rawler.responses["http://example.com/bar"] = { :status => 300 }
end

it "should give all occurring errors" do
rawler.errors.should be_empty
end
end

context "error codes occured" do
before do
rawler.responses["http://example.com/"] = { :status => 100 }
rawler.responses["http://example.com/foo"] = { :status => 400 }
rawler.responses["http://example.com/bar"] = { :status => 500 }
end

it "should give all occurring errors" do
rawler.errors.count.should eq 2
end
end

end



private
Expand Down

0 comments on commit 4f2f385

Please sign in to comment.