link-checker 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
 - data/bin/check-links +1 -0
 - data/lib/link_checker.rb +23 -12
 - data/link-checker.gemspec +1 -1
 - data/spec/link-checker_spec.rb +33 -15
 - metadata +2 -2
 
    
        data/VERSION
    CHANGED
    
    | 
         @@ -1 +1 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            0. 
     | 
| 
      
 1 
     | 
    
         
            +
            0.5.0
         
     | 
    
        data/bin/check-links
    CHANGED
    
    | 
         @@ -5,6 +5,7 @@ require 'trollop' 
     | 
|
| 
       5 
5 
     | 
    
         | 
| 
       6 
6 
     | 
    
         
             
            options = Trollop::options do
         
     | 
| 
       7 
7 
     | 
    
         
             
            	opt :no_warnings, "Don't warn about redirects to valid links"
         
     | 
| 
      
 8 
     | 
    
         
            +
            	opt :warnings_are_errors, "Treat any warning as an error and produce an error return code"
         
     | 
| 
       8 
9 
     | 
    
         
             
            end
         
     | 
| 
       9 
10 
     | 
    
         | 
| 
       10 
11 
     | 
    
         
             
            exit LinkChecker.new(:options => options, :target => ARGV[0]).check_uris
         
     | 
    
        data/lib/link_checker.rb
    CHANGED
    
    | 
         @@ -43,7 +43,6 @@ class LinkChecker 
     | 
|
| 
       43 
43 
     | 
    
         
             
                    when Net::HTTPRedirection then
         
     | 
| 
       44 
44 
     | 
    
         
             
                      return self.check_uri(URI(response['location']), true)
         
     | 
| 
       45 
45 
     | 
    
         
             
                    else
         
     | 
| 
       46 
     | 
    
         
            -
                      @return_code = 1
         
     | 
| 
       47 
46 
     | 
    
         
             
                      return Error.new(:uri_string => uri.to_s, :response => response)
         
     | 
| 
       48 
47 
     | 
    
         
             
                    end
         
     | 
| 
       49 
48 
     | 
    
         
             
                  end
         
     | 
| 
         @@ -89,9 +88,10 @@ class LinkChecker 
     | 
|
| 
       89 
88 
     | 
    
         
             
                    begin
         
     | 
| 
       90 
89 
     | 
    
         
             
                      uri = URI(uri_string)
         
     | 
| 
       91 
90 
     | 
    
         
             
                      response = self.class.check_uri(uri)
         
     | 
| 
       92 
     | 
    
         
            -
                       
     | 
| 
      
 91 
     | 
    
         
            +
                      response.uri_string = uri_string
         
     | 
| 
      
 92 
     | 
    
         
            +
                      response
         
     | 
| 
       93 
93 
     | 
    
         
             
                    rescue => error
         
     | 
| 
       94 
     | 
    
         
            -
                       
     | 
| 
      
 94 
     | 
    
         
            +
                      Error.new(:error => error.to_s, :uri_string => uri_string)
         
     | 
| 
       95 
95 
     | 
    
         
             
                    end
         
     | 
| 
       96 
96 
     | 
    
         
             
                  end
         
     | 
| 
       97 
97 
     | 
    
         
             
                  report_results(source_name, results)
         
     | 
| 
         @@ -99,10 +99,16 @@ class LinkChecker 
     | 
|
| 
       99 
99 
     | 
    
         
             
              end
         
     | 
| 
       100 
100 
     | 
    
         | 
| 
       101 
101 
     | 
    
         
             
              def report_results(file, results)
         
     | 
| 
       102 
     | 
    
         
            -
                 
     | 
| 
       103 
     | 
    
         
            -
                warnings = results.select{|result| result 
     | 
| 
      
 102 
     | 
    
         
            +
                errors = results.select{|result| result.class.eql? Error}
         
     | 
| 
      
 103 
     | 
    
         
            +
                warnings = results.select{|result| result.class.eql? Redirect}
         
     | 
| 
      
 104 
     | 
    
         
            +
                @return_code = 1 unless errors.empty?
         
     | 
| 
      
 105 
     | 
    
         
            +
                if @options[:warnings_are_errors]
         
     | 
| 
      
 106 
     | 
    
         
            +
                  @return_code = 1 unless warnings.empty?
         
     | 
| 
      
 107 
     | 
    
         
            +
                  errors = errors + warnings
         
     | 
| 
      
 108 
     | 
    
         
            +
                  warnings = []
         
     | 
| 
      
 109 
     | 
    
         
            +
                end
         
     | 
| 
       104 
110 
     | 
    
         
             
                Thread.exclusive do
         
     | 
| 
       105 
     | 
    
         
            -
                  if  
     | 
| 
      
 111 
     | 
    
         
            +
                  if errors.empty?
         
     | 
| 
       106 
112 
     | 
    
         
             
                    message = "Checked: #{file}"
         
     | 
| 
       107 
113 
     | 
    
         
             
                    if warnings.empty? || @options[:no_warnings]
         
     | 
| 
       108 
114 
     | 
    
         
             
                      puts message.green
         
     | 
| 
         @@ -111,22 +117,27 @@ class LinkChecker 
     | 
|
| 
       111 
117 
     | 
    
         
             
                    end
         
     | 
| 
       112 
118 
     | 
    
         
             
                    unless @options[:no_warnings]
         
     | 
| 
       113 
119 
     | 
    
         
             
                      warnings.each do |warning|
         
     | 
| 
       114 
     | 
    
         
            -
                        puts "   Warning: #{warning 
     | 
| 
       115 
     | 
    
         
            -
                        puts "     Redirected to: #{warning 
     | 
| 
      
 120 
     | 
    
         
            +
                        puts "   Warning: #{warning.uri_string}".yellow
         
     | 
| 
      
 121 
     | 
    
         
            +
                        puts "     Redirected to: #{warning.final_destination_uri_string}".yellow
         
     | 
| 
       116 
122 
     | 
    
         
             
                      end
         
     | 
| 
       117 
123 
     | 
    
         
             
                    end
         
     | 
| 
       118 
124 
     | 
    
         
             
                  else
         
     | 
| 
       119 
125 
     | 
    
         
             
                    puts "Problem: #{file}".red
         
     | 
| 
       120 
     | 
    
         
            -
                     
     | 
| 
       121 
     | 
    
         
            -
                      puts "   Link: #{ 
     | 
| 
       122 
     | 
    
         
            -
                       
     | 
| 
      
 126 
     | 
    
         
            +
                    errors.each do |error|
         
     | 
| 
      
 127 
     | 
    
         
            +
                      puts "   Link: #{error.uri_string}".red
         
     | 
| 
      
 128 
     | 
    
         
            +
                      case error
         
     | 
| 
      
 129 
     | 
    
         
            +
                      when Redirect
         
     | 
| 
      
 130 
     | 
    
         
            +
                        puts "     Redirected to: #{error.final_destination_uri_string}".red
         
     | 
| 
      
 131 
     | 
    
         
            +
                      when Error
         
     | 
| 
      
 132 
     | 
    
         
            +
                        puts "     Response: #{error.error.to_s}".red
         
     | 
| 
      
 133 
     | 
    
         
            +
                      end
         
     | 
| 
       123 
134 
     | 
    
         
             
                    end
         
     | 
| 
       124 
135 
     | 
    
         
             
                  end
         
     | 
| 
       125 
136 
     | 
    
         
             
                end
         
     | 
| 
       126 
137 
     | 
    
         
             
              end
         
     | 
| 
       127 
138 
     | 
    
         | 
| 
       128 
139 
     | 
    
         
             
              class Result
         
     | 
| 
       129 
     | 
    
         
            -
                 
     | 
| 
      
 140 
     | 
    
         
            +
                attr_accessor :uri_string
         
     | 
| 
       130 
141 
     | 
    
         
             
                def initialize(params)
         
     | 
| 
       131 
142 
     | 
    
         
             
                  @uri_string = params[:uri_string]
         
     | 
| 
       132 
143 
     | 
    
         
             
                end
         
     | 
    
        data/link-checker.gemspec
    CHANGED
    
    
    
        data/spec/link-checker_spec.rb
    CHANGED
    
    | 
         @@ -75,7 +75,8 @@ describe LinkChecker do 
     | 
|
| 
       75 
75 
     | 
    
         
             
                before(:each) do
         
     | 
| 
       76 
76 
     | 
    
         
             
                  LinkChecker.any_instance.stub(:html_file_paths) {
         
     | 
| 
       77 
77 
     | 
    
         
             
                    ['spec/test-site/public/blog/2012/10/02/a-list-of-links/index.html'] }
         
     | 
| 
       78 
     | 
    
         
            -
                  LinkChecker.stub(:external_link_uri_strings).and_return( 
     | 
| 
      
 78 
     | 
    
         
            +
                  LinkChecker.stub(:external_link_uri_strings).and_return(
         
     | 
| 
      
 79 
     | 
    
         
            +
                    ['http://something.com', 'http://something-else.com'])
         
     | 
| 
       79 
80 
     | 
    
         
             
                end
         
     | 
| 
       80 
81 
     | 
    
         | 
| 
       81 
82 
     | 
    
         
             
                it "prints green when the links are good." do
         
     | 
| 
         @@ -83,7 +84,7 @@ describe LinkChecker do 
     | 
|
| 
       83 
84 
     | 
    
         
             
                    LinkChecker::Good.new(:uri_string => 'http://something.com')
         
     | 
| 
       84 
85 
     | 
    
         
             
                  end
         
     | 
| 
       85 
86 
     | 
    
         
             
                  $stdout.should_receive(:puts).with(/Checked/i).once
         
     | 
| 
       86 
     | 
    
         
            -
                  LinkChecker.new(:target => @site_path).check_uris
         
     | 
| 
      
 87 
     | 
    
         
            +
                  LinkChecker.new(:target => @site_path).check_uris.should == 0 # Return value: good
         
     | 
| 
       87 
88 
     | 
    
         
             
                end
         
     | 
| 
       88 
89 
     | 
    
         | 
| 
       89 
90 
     | 
    
         
             
                it "prints red when the links are bad." do
         
     | 
| 
         @@ -94,9 +95,9 @@ describe LinkChecker do 
     | 
|
| 
       94 
95 
     | 
    
         
             
                    )
         
     | 
| 
       95 
96 
     | 
    
         
             
                  end
         
     | 
| 
       96 
97 
     | 
    
         
             
                  $stdout.should_receive(:puts).with(/Problem/i).once
         
     | 
| 
       97 
     | 
    
         
            -
                  $stdout.should_receive(:puts).with(/Link/i). 
     | 
| 
       98 
     | 
    
         
            -
                  $stdout.should_receive(:puts).with(/Response/i). 
     | 
| 
       99 
     | 
    
         
            -
                  LinkChecker.new(:target => @site_path).check_uris
         
     | 
| 
      
 98 
     | 
    
         
            +
                  $stdout.should_receive(:puts).with(/Link/i).twice
         
     | 
| 
      
 99 
     | 
    
         
            +
                  $stdout.should_receive(:puts).with(/Response/i).twice
         
     | 
| 
      
 100 
     | 
    
         
            +
                  LinkChecker.new(:target => @site_path).check_uris.should == 1 # Return value: error
         
     | 
| 
       100 
101 
     | 
    
         
             
                end
         
     | 
| 
       101 
102 
     | 
    
         | 
| 
       102 
103 
     | 
    
         
             
                it "prints yellow warnings when the links redirect." do
         
     | 
| 
         @@ -107,22 +108,38 @@ describe LinkChecker do 
     | 
|
| 
       107 
108 
     | 
    
         
             
                    )
         
     | 
| 
       108 
109 
     | 
    
         
             
                  end
         
     | 
| 
       109 
110 
     | 
    
         
             
                  $stdout.should_receive(:puts).with(/Checked/i).once
         
     | 
| 
       110 
     | 
    
         
            -
                  $stdout.should_receive(:puts).with(/Warning/i). 
     | 
| 
       111 
     | 
    
         
            -
                  $stdout.should_receive(:puts).with(/Redirected/i). 
     | 
| 
       112 
     | 
    
         
            -
                  LinkChecker.new(:target => @site_path).check_uris
         
     | 
| 
      
 111 
     | 
    
         
            +
                  $stdout.should_receive(:puts).with(/Warning/i).twice
         
     | 
| 
      
 112 
     | 
    
         
            +
                  $stdout.should_receive(:puts).with(/Redirected/i).twice
         
     | 
| 
      
 113 
     | 
    
         
            +
                  LinkChecker.new(:target => @site_path).check_uris.should == 0 # Return value: good
         
     | 
| 
      
 114 
     | 
    
         
            +
                end
         
     | 
| 
      
 115 
     | 
    
         
            +
             
     | 
| 
      
 116 
     | 
    
         
            +
                it "prints errors when there are warnings with the --warnings_are_errors option." do
         
     | 
| 
      
 117 
     | 
    
         
            +
                  LinkChecker.stub(:check_uri) do
         
     | 
| 
      
 118 
     | 
    
         
            +
                    LinkChecker::Redirect.new(
         
     | 
| 
      
 119 
     | 
    
         
            +
                      :uri_string => 'http://something.com',
         
     | 
| 
      
 120 
     | 
    
         
            +
                      :final_destination_uri_string => 'http://something-else.com'
         
     | 
| 
      
 121 
     | 
    
         
            +
                    )
         
     | 
| 
      
 122 
     | 
    
         
            +
                  end
         
     | 
| 
      
 123 
     | 
    
         
            +
                  $stdout.should_receive(:puts).with(/Problem/i).once
         
     | 
| 
      
 124 
     | 
    
         
            +
                  $stdout.should_receive(:puts).with(/Link/i).twice
         
     | 
| 
      
 125 
     | 
    
         
            +
                  $stdout.should_receive(:puts).with(/Redirected/i).twice
         
     | 
| 
      
 126 
     | 
    
         
            +
                  LinkChecker.new(
         
     | 
| 
      
 127 
     | 
    
         
            +
                    :target => @site_path,
         
     | 
| 
      
 128 
     | 
    
         
            +
                    :options => { :warnings_are_errors => true }
         
     | 
| 
      
 129 
     | 
    
         
            +
                  ).check_uris.should == 1 # Return value: error
         
     | 
| 
       113 
130 
     | 
    
         
             
                end
         
     | 
| 
       114 
131 
     | 
    
         | 
| 
       115 
132 
     | 
    
         
             
                it "does not print warnings when the links redirect with the --no-warnings option." do
         
     | 
| 
       116 
133 
     | 
    
         
             
                  LinkChecker.stub(:check_uri) do
         
     | 
| 
       117 
134 
     | 
    
         
             
                    LinkChecker::Redirect.new(
         
     | 
| 
       118 
135 
     | 
    
         
             
                      :uri_string => 'http://something.com',
         
     | 
| 
       119 
     | 
    
         
            -
                      : 
     | 
| 
      
 136 
     | 
    
         
            +
                      :final_destination_uri_string => 'http://something-else.com'
         
     | 
| 
       120 
137 
     | 
    
         
             
                    )
         
     | 
| 
       121 
138 
     | 
    
         
             
                  end
         
     | 
| 
       122 
139 
     | 
    
         
             
                  $stdout.should_receive(:puts).with(/Checked/i).once
         
     | 
| 
       123 
     | 
    
         
            -
                  $stdout.should_receive(:puts).with(/Warning/i). 
     | 
| 
       124 
     | 
    
         
            -
                  $stdout.should_receive(:puts).with(/Redirected/i). 
     | 
| 
       125 
     | 
    
         
            -
                  LinkChecker.new(:target => @site_path).check_uris
         
     | 
| 
      
 140 
     | 
    
         
            +
                  $stdout.should_receive(:puts).with(/Warning/i).twice
         
     | 
| 
      
 141 
     | 
    
         
            +
                  $stdout.should_receive(:puts).with(/Redirected/i).twice
         
     | 
| 
      
 142 
     | 
    
         
            +
                  LinkChecker.new(:target => @site_path).check_uris.should == 0 # Return value: good
         
     | 
| 
       126 
143 
     | 
    
         
             
                end
         
     | 
| 
       127 
144 
     | 
    
         | 
| 
       128 
145 
     | 
    
         
             
              end
         
     | 
| 
         @@ -130,10 +147,11 @@ describe LinkChecker do 
     | 
|
| 
       130 
147 
     | 
    
         
             
              describe "prints output for invalid links and" do
         
     | 
| 
       131 
148 
     | 
    
         | 
| 
       132 
149 
     | 
    
         
             
                it "declares them to be bad." do
         
     | 
| 
       133 
     | 
    
         
            -
                  LinkChecker.stub(:external_link_uri_strings).and_return( 
     | 
| 
      
 150 
     | 
    
         
            +
                  LinkChecker.stub(:external_link_uri_strings).and_return(
         
     | 
| 
      
 151 
     | 
    
         
            +
                    ['hQQp://!!!.com', 'hOOp://???.com'])
         
     | 
| 
       134 
152 
     | 
    
         
             
                  $stdout.should_receive(:puts).with(/Problem/i).once
         
     | 
| 
       135 
     | 
    
         
            -
                  $stdout.should_receive(:puts).with(/Link/i). 
     | 
| 
       136 
     | 
    
         
            -
                  $stdout.should_receive(:puts).with(/Response/i). 
     | 
| 
      
 153 
     | 
    
         
            +
                  $stdout.should_receive(:puts).with(/Link/i).twice
         
     | 
| 
      
 154 
     | 
    
         
            +
                  $stdout.should_receive(:puts).with(/Response/i).twice
         
     | 
| 
       137 
155 
     | 
    
         
             
                  thread = LinkChecker.new(:target => @site_path).start_link_check_thread('<html></html>', 'source.html')
         
     | 
| 
       138 
156 
     | 
    
         
             
                  thread.join
         
     | 
| 
       139 
157 
     | 
    
         
             
                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.5.0
         
     | 
| 
       5 
5 
     | 
    
         
             
              prerelease: 
         
     | 
| 
       6 
6 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       7 
7 
     | 
    
         
             
            authors:
         
     | 
| 
         @@ -558,7 +558,7 @@ required_ruby_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       558 
558 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       559 
559 
     | 
    
         
             
                  segments:
         
     | 
| 
       560 
560 
     | 
    
         
             
                  - 0
         
     | 
| 
       561 
     | 
    
         
            -
                  hash:  
     | 
| 
      
 561 
     | 
    
         
            +
                  hash: -2838258292777551333
         
     | 
| 
       562 
562 
     | 
    
         
             
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
       563 
563 
     | 
    
         
             
              none: false
         
     | 
| 
       564 
564 
     | 
    
         
             
              requirements:
         
     |