link-checker 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -0
- data/Gemfile.lock +2 -0
- data/VERSION +1 -1
- data/bin/check-links +5 -3
- data/lib/link_checker.rb +21 -10
- data/link-checker.gemspec +5 -2
- data/spec/link-checker_spec.rb +38 -8
- metadata +19 -3
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -41,6 +41,7 @@ GEM
|
|
41
41
|
multi_json (~> 1.0)
|
42
42
|
simplecov-html (~> 0.5.3)
|
43
43
|
simplecov-html (0.5.3)
|
44
|
+
trollop (2.0)
|
44
45
|
yard (0.8.2.1)
|
45
46
|
|
46
47
|
PLATFORMS
|
@@ -56,4 +57,5 @@ DEPENDENCIES
|
|
56
57
|
redcarpet (~> 2.1.1)
|
57
58
|
rspec (~> 2.11.0)
|
58
59
|
simplecov (~> 0.6.4)
|
60
|
+
trollop (~> 2.0)
|
59
61
|
yard (~> 0.8.2.1)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/bin/check-links
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require 'link_checker'
|
4
|
+
require 'trollop'
|
4
5
|
|
5
|
-
|
6
|
-
|
6
|
+
options = Trollop::options do
|
7
|
+
opt :no_warnings, "Don't warn about redirects to valid links"
|
8
|
+
end
|
7
9
|
|
8
|
-
LinkChecker.new(target).check_uris
|
10
|
+
exit LinkChecker.new(:options => options, :target => ARGV[0]).check_uris
|
data/lib/link_checker.rb
CHANGED
@@ -8,8 +8,10 @@ require 'anemone'
|
|
8
8
|
|
9
9
|
class LinkChecker
|
10
10
|
|
11
|
-
def initialize(
|
12
|
-
@
|
11
|
+
def initialize(params)
|
12
|
+
@options = params[:options] || {}
|
13
|
+
@target = params[:target] || './'
|
14
|
+
@return_code = 0
|
13
15
|
end
|
14
16
|
|
15
17
|
def html_file_paths
|
@@ -41,6 +43,7 @@ class LinkChecker
|
|
41
43
|
when Net::HTTPRedirection then
|
42
44
|
return self.check_uri(URI(response['location']), true)
|
43
45
|
else
|
46
|
+
@return_code = 1
|
44
47
|
return Error.new(:uri_string => uri.to_s, :response => response)
|
45
48
|
end
|
46
49
|
end
|
@@ -48,11 +51,16 @@ class LinkChecker
|
|
48
51
|
end
|
49
52
|
|
50
53
|
def check_uris
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
54
|
+
begin
|
55
|
+
if @target =~ /^https?\:\/\//
|
56
|
+
check_uris_by_crawling
|
57
|
+
else
|
58
|
+
check_uris_in_files
|
59
|
+
end
|
60
|
+
rescue => error
|
61
|
+
puts "Error: #{error.to_s}".red
|
55
62
|
end
|
63
|
+
@return_code
|
56
64
|
end
|
57
65
|
|
58
66
|
def check_uris_by_crawling
|
@@ -60,6 +68,7 @@ class LinkChecker
|
|
60
68
|
Anemone.crawl(@target) do |anemone|
|
61
69
|
anemone.storage = Anemone::Storage.PStore('link-checker-crawled-pages.pstore')
|
62
70
|
anemone.on_every_page do |crawled_page|
|
71
|
+
raise StandardError.new(crawled_page.error) if crawled_page.error
|
63
72
|
threads << start_link_check_thread(crawled_page.body, crawled_page.url.to_s)
|
64
73
|
end
|
65
74
|
end
|
@@ -95,14 +104,16 @@ class LinkChecker
|
|
95
104
|
Thread.exclusive do
|
96
105
|
if bad_checks.empty?
|
97
106
|
message = "Checked: #{file}"
|
98
|
-
if warnings.empty?
|
107
|
+
if warnings.empty? || @options[:no_warnings]
|
99
108
|
puts message.green
|
100
109
|
else
|
101
110
|
puts message.yellow
|
102
111
|
end
|
103
|
-
|
104
|
-
|
105
|
-
|
112
|
+
unless @options[:no_warnings]
|
113
|
+
warnings.each do |warning|
|
114
|
+
puts " Warning: #{warning[:uri_string]}".yellow
|
115
|
+
puts " Redirected to: #{warning[:response].final_destination_uri_string}".yellow
|
116
|
+
end
|
106
117
|
end
|
107
118
|
else
|
108
119
|
puts "Problem: #{file}".red
|
data/link-checker.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "link-checker"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Ryan Alyn Porter"]
|
12
|
-
s.date = "2012-10-
|
12
|
+
s.date = "2012-10-06"
|
13
13
|
s.description = "Check the links in a web site before deploying, using Nokogiri."
|
14
14
|
s.executables = ["check-links"]
|
15
15
|
s.extra_rdoc_files = [
|
@@ -378,6 +378,7 @@ Gem::Specification.new do |s|
|
|
378
378
|
s.add_runtime_dependency(%q<nokogiri>, ["~> 1.5.5"])
|
379
379
|
s.add_runtime_dependency(%q<colorize>, ["~> 0.5.8"])
|
380
380
|
s.add_runtime_dependency(%q<anemone>, ["~> 0.7.2"])
|
381
|
+
s.add_runtime_dependency(%q<trollop>, ["~> 2.0"])
|
381
382
|
s.add_development_dependency(%q<rspec>, ["~> 2.11.0"])
|
382
383
|
s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
|
383
384
|
s.add_development_dependency(%q<simplecov>, ["~> 0.6.4"])
|
@@ -389,6 +390,7 @@ Gem::Specification.new do |s|
|
|
389
390
|
s.add_dependency(%q<nokogiri>, ["~> 1.5.5"])
|
390
391
|
s.add_dependency(%q<colorize>, ["~> 0.5.8"])
|
391
392
|
s.add_dependency(%q<anemone>, ["~> 0.7.2"])
|
393
|
+
s.add_dependency(%q<trollop>, ["~> 2.0"])
|
392
394
|
s.add_dependency(%q<rspec>, ["~> 2.11.0"])
|
393
395
|
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
394
396
|
s.add_dependency(%q<simplecov>, ["~> 0.6.4"])
|
@@ -401,6 +403,7 @@ Gem::Specification.new do |s|
|
|
401
403
|
s.add_dependency(%q<nokogiri>, ["~> 1.5.5"])
|
402
404
|
s.add_dependency(%q<colorize>, ["~> 0.5.8"])
|
403
405
|
s.add_dependency(%q<anemone>, ["~> 0.7.2"])
|
406
|
+
s.add_dependency(%q<trollop>, ["~> 2.0"])
|
404
407
|
s.add_dependency(%q<rspec>, ["~> 2.11.0"])
|
405
408
|
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
406
409
|
s.add_dependency(%q<simplecov>, ["~> 0.6.4"])
|
data/spec/link-checker_spec.rb
CHANGED
@@ -10,7 +10,7 @@ describe LinkChecker do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
it "finds all of the HTML files in the target path." do
|
13
|
-
files = LinkChecker.new(@site_path).html_file_paths
|
13
|
+
files = LinkChecker.new(:target => @site_path).html_file_paths
|
14
14
|
files.size.should == 3
|
15
15
|
end
|
16
16
|
|
@@ -60,8 +60,8 @@ describe LinkChecker do
|
|
60
60
|
end
|
61
61
|
|
62
62
|
it "declares bad redirect targets to be bad." do
|
63
|
-
|
64
|
-
|
63
|
+
FakeWeb.register_uri(:get, @redirect_uri.to_s,
|
64
|
+
:location => @bad_uri.to_s, :status => ["302", "Moved"])
|
65
65
|
result = LinkChecker.check_uri(@redirect_uri)
|
66
66
|
result.class.should be LinkChecker::Error
|
67
67
|
end
|
@@ -83,7 +83,7 @@ describe LinkChecker do
|
|
83
83
|
LinkChecker::Good.new(:uri_string => 'http://something.com')
|
84
84
|
end
|
85
85
|
$stdout.should_receive(:puts).with(/Checked/i).once
|
86
|
-
LinkChecker.new(@site_path).check_uris
|
86
|
+
LinkChecker.new(:target => @site_path).check_uris
|
87
87
|
end
|
88
88
|
|
89
89
|
it "prints red when the links are bad." do
|
@@ -96,7 +96,7 @@ describe LinkChecker do
|
|
96
96
|
$stdout.should_receive(:puts).with(/Problem/i).once
|
97
97
|
$stdout.should_receive(:puts).with(/Link/i).once
|
98
98
|
$stdout.should_receive(:puts).with(/Response/i).once
|
99
|
-
LinkChecker.new(@site_path).check_uris
|
99
|
+
LinkChecker.new(:target => @site_path).check_uris
|
100
100
|
end
|
101
101
|
|
102
102
|
it "prints yellow warnings when the links redirect." do
|
@@ -109,7 +109,20 @@ describe LinkChecker do
|
|
109
109
|
$stdout.should_receive(:puts).with(/Checked/i).once
|
110
110
|
$stdout.should_receive(:puts).with(/Warning/i).once
|
111
111
|
$stdout.should_receive(:puts).with(/Redirected/i).once
|
112
|
-
LinkChecker.new(@site_path).check_uris
|
112
|
+
LinkChecker.new(:target => @site_path).check_uris
|
113
|
+
end
|
114
|
+
|
115
|
+
it "does not print warnings when the links redirect with the --no-warnings option." do
|
116
|
+
LinkChecker.stub(:check_uri) do
|
117
|
+
LinkChecker::Redirect.new(
|
118
|
+
:uri_string => 'http://something.com',
|
119
|
+
:final_desination => 'http://something-else.com'
|
120
|
+
)
|
121
|
+
end
|
122
|
+
$stdout.should_receive(:puts).with(/Checked/i).once
|
123
|
+
$stdout.should_receive(:puts).with(/Warning/i).once
|
124
|
+
$stdout.should_receive(:puts).with(/Redirected/i).once
|
125
|
+
LinkChecker.new(:target => @site_path).check_uris
|
113
126
|
end
|
114
127
|
|
115
128
|
end
|
@@ -121,19 +134,36 @@ describe LinkChecker do
|
|
121
134
|
$stdout.should_receive(:puts).with(/Problem/i).once
|
122
135
|
$stdout.should_receive(:puts).with(/Link/i).once
|
123
136
|
$stdout.should_receive(:puts).with(/Response/i).once
|
124
|
-
thread = LinkChecker.new(@site_path).start_link_check_thread('<html></html>', 'source.html')
|
137
|
+
thread = LinkChecker.new(:target => @site_path).start_link_check_thread('<html></html>', 'source.html')
|
125
138
|
thread.join
|
126
139
|
end
|
127
140
|
|
128
141
|
end
|
129
142
|
|
130
143
|
it "crawls a web site." do
|
144
|
+
FakeWeb.register_uri(:any, 'http://some-target.com', :body => "Yay it worked.")
|
131
145
|
LinkChecker.stub(:external_link_uri_strings).and_return(['http://something.com'])
|
132
146
|
LinkChecker.stub(:check_uri) do
|
133
147
|
LinkChecker::Good.new(:uri_string => 'http://something.com')
|
134
148
|
end
|
135
149
|
$stdout.should_receive(:puts).with(/Checked/i).once
|
136
|
-
LinkChecker.new('http://
|
150
|
+
LinkChecker.new(:target => 'http://some-target.com').check_uris
|
151
|
+
end
|
152
|
+
|
153
|
+
describe "produces useful return codes when" do
|
154
|
+
|
155
|
+
it "the target file does not exist." do
|
156
|
+
Find.stub(:find).and_raise(Errno::ENOENT.new('test'))
|
157
|
+
$stdout.should_receive(:puts).with(/Error/i).once
|
158
|
+
LinkChecker.new(:target => 'does-not-exist').check_uris
|
159
|
+
end
|
160
|
+
|
161
|
+
it "the target file does not exist." do
|
162
|
+
FakeWeb.allow_net_connect = false
|
163
|
+
$stdout.should_receive(:puts).with(/Error/i).once
|
164
|
+
LinkChecker.new(:target => 'http://does-not-exist.com').check_uris
|
165
|
+
end
|
166
|
+
|
137
167
|
end
|
138
168
|
|
139
169
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: link-checker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
@@ -59,6 +59,22 @@ dependencies:
|
|
59
59
|
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: 0.7.2
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: trollop
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '2.0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '2.0'
|
62
78
|
- !ruby/object:Gem::Dependency
|
63
79
|
name: rspec
|
64
80
|
requirement: !ruby/object:Gem::Requirement
|
@@ -542,7 +558,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
542
558
|
version: '0'
|
543
559
|
segments:
|
544
560
|
- 0
|
545
|
-
hash:
|
561
|
+
hash: 1966410350842699633
|
546
562
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
547
563
|
none: false
|
548
564
|
requirements:
|