| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:41:14 +01:00
										 |  |  | require "rubocops/homepage" | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | describe RuboCop::Cop::FormulaAudit::Homepage do | 
					
						
							|  |  |  |   subject(:cop) { described_class.new } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   context "When auditing homepage" do | 
					
						
							|  |  |  |     it "When there is no homepage" do | 
					
						
							| 
									
										
										
										
											2018-07-11 15:17:40 +02:00
										 |  |  |       source = <<~RUBY | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |         class Foo < Formula | 
					
						
							| 
									
										
										
										
											2018-11-28 20:51:55 +01:00
										 |  |  |           url 'https://brew.sh/foo-1.0.tgz' | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-07-11 15:17:40 +02:00
										 |  |  |       RUBY | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-02 17:18:07 +00:00
										 |  |  |       expected_offenses = [{  message:  "Formula should have a homepage.", | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |                               severity: :convention, | 
					
						
							| 
									
										
										
										
											2018-11-02 17:18:07 +00:00
										 |  |  |                               line:     1, | 
					
						
							|  |  |  |                               column:   0, | 
					
						
							|  |  |  |                               source:   source }] | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 22:31:23 +02:00
										 |  |  |       inspect_source(source) | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |       expected_offenses.zip(cop.offenses).each do |expected, actual| | 
					
						
							|  |  |  |         expect_offense(expected, actual) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "Homepage with ftp" do | 
					
						
							| 
									
										
										
										
											2018-07-11 15:17:40 +02:00
										 |  |  |       source = <<~RUBY | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |         class Foo < Formula | 
					
						
							| 
									
										
										
										
											2018-11-28 20:51:55 +01:00
										 |  |  |           homepage "ftp://brew.sh/foo" | 
					
						
							|  |  |  |           url "https://brew.sh/foo-1.0.tgz" | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-07-11 15:17:40 +02:00
										 |  |  |       RUBY | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-02 17:27:24 +00:00
										 |  |  |       expected_offenses = [{ message:  "The homepage should start with http or " \ | 
					
						
							| 
									
										
										
										
											2018-11-28 20:51:55 +01:00
										 |  |  |                                         "https (URL is ftp://brew.sh/foo).", | 
					
						
							| 
									
										
										
										
											2018-11-02 17:27:24 +00:00
										 |  |  |                              severity: :convention, | 
					
						
							|  |  |  |                              line:     2, | 
					
						
							|  |  |  |                              column:   2, | 
					
						
							|  |  |  |                              source:   source }] | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 22:31:23 +02:00
										 |  |  |       inspect_source(source) | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |       expected_offenses.zip(cop.offenses).each do |expected, actual| | 
					
						
							|  |  |  |         expect_offense(expected, actual) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "Homepage URLs" do | 
					
						
							|  |  |  |       formula_homepages = { | 
					
						
							| 
									
										
										
										
											2018-11-02 17:18:07 +00:00
										 |  |  |         "bar"    => "http://www.freedesktop.org/wiki/bar", | 
					
						
							|  |  |  |         "baz"    => "http://www.freedesktop.org/wiki/Software/baz", | 
					
						
							|  |  |  |         "qux"    => "https://code.google.com/p/qux", | 
					
						
							|  |  |  |         "quux"   => "http://github.com/quux", | 
					
						
							|  |  |  |         "corge"  => "http://savannah.nongnu.org/corge", | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |         "grault" => "http://grault.github.io/", | 
					
						
							|  |  |  |         "garply" => "http://www.gnome.org/garply", | 
					
						
							| 
									
										
										
										
											2018-11-02 17:18:07 +00:00
										 |  |  |         "sf1"    => "http://foo.sourceforge.net/", | 
					
						
							|  |  |  |         "sf2"    => "http://foo.sourceforge.net", | 
					
						
							|  |  |  |         "sf3"    => "http://foo.sf.net/", | 
					
						
							|  |  |  |         "sf4"    => "http://foo.sourceforge.io/", | 
					
						
							|  |  |  |         "waldo"  => "http://www.gnu.org/waldo", | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       formula_homepages.each do |name, homepage| | 
					
						
							| 
									
										
										
										
											2018-07-11 15:17:40 +02:00
										 |  |  |         source = <<~RUBY | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |           class #{name.capitalize} < Formula | 
					
						
							|  |  |  |             homepage "#{homepage}" | 
					
						
							| 
									
										
										
										
											2018-11-28 20:51:55 +01:00
										 |  |  |             url "https://brew.sh/#{name}-1.0.tgz" | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2018-07-11 15:17:40 +02:00
										 |  |  |         RUBY | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 22:31:23 +02:00
										 |  |  |         inspect_source(source) | 
					
						
							| 
									
										
										
										
											2020-06-02 09:49:23 +01:00
										 |  |  |         if homepage.match?(%r{http://www\.freedesktop\.org}) | 
					
						
							| 
									
										
										
										
											2019-10-13 19:26:39 +01:00
										 |  |  |           if homepage.match?(/Software/) | 
					
						
							| 
									
										
										
										
											2018-11-02 17:18:07 +00:00
										 |  |  |             expected_offenses = [{  message:  "#{homepage} should be styled " \ | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |                                              "`https://wiki.freedesktop.org/www/Software/project_name`", | 
					
						
							|  |  |  |                                     severity: :convention, | 
					
						
							| 
									
										
										
										
											2018-11-02 17:18:07 +00:00
										 |  |  |                                     line:     2, | 
					
						
							|  |  |  |                                     column:   2, | 
					
						
							|  |  |  |                                     source:   source }] | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |           else | 
					
						
							|  |  |  |             expected_offenses = [{  message:  "#{homepage} should be styled " \ | 
					
						
							|  |  |  |                                               "`https://wiki.freedesktop.org/project_name`", | 
					
						
							|  |  |  |                                     severity: :convention, | 
					
						
							| 
									
										
										
										
											2018-11-02 17:18:07 +00:00
										 |  |  |                                     line:     2, | 
					
						
							|  |  |  |                                     column:   2, | 
					
						
							|  |  |  |                                     source:   source }] | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2020-06-02 09:49:23 +01:00
										 |  |  |         elsif homepage.match?(%r{https://code\.google\.com}) | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |           expected_offenses = [{  message:  "#{homepage} should end with a slash", | 
					
						
							|  |  |  |                                   severity: :convention, | 
					
						
							| 
									
										
										
										
											2018-11-02 17:18:07 +00:00
										 |  |  |                                   line:     2, | 
					
						
							|  |  |  |                                   column:   2, | 
					
						
							|  |  |  |                                   source:   source }] | 
					
						
							| 
									
										
										
										
											2019-10-13 19:26:39 +01:00
										 |  |  |         elsif homepage.match?(/foo\.(sf|sourceforge)\.net/) | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |           expected_offenses = [{  message:  "#{homepage} should be `https://foo.sourceforge.io/`", | 
					
						
							|  |  |  |                                   severity: :convention, | 
					
						
							| 
									
										
										
										
											2018-11-02 17:18:07 +00:00
										 |  |  |                                   line:     2, | 
					
						
							|  |  |  |                                   column:   2, | 
					
						
							|  |  |  |                                   source:   source }] | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |         else | 
					
						
							|  |  |  |           expected_offenses = [{  message:  "Please use https:// for #{homepage}", | 
					
						
							|  |  |  |                                   severity: :convention, | 
					
						
							| 
									
										
										
										
											2018-11-02 17:18:07 +00:00
										 |  |  |                                   line:     2, | 
					
						
							|  |  |  |                                   column:   2, | 
					
						
							|  |  |  |                                   source:   source }] | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |         end | 
					
						
							|  |  |  |         expected_offenses.zip([cop.offenses.last]).each do |expected, actual| | 
					
						
							|  |  |  |           expect_offense(expected, actual) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def expect_offense(expected, actual) | 
					
						
							|  |  |  |       expect(actual.message).to eq(expected[:message]) | 
					
						
							|  |  |  |       expect(actual.severity).to eq(expected[:severity]) | 
					
						
							|  |  |  |       expect(actual.line).to eq(expected[:line]) | 
					
						
							|  |  |  |       expect(actual.column).to eq(expected[:column]) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |