| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2020-04-28 22:35:47 +05:30
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "rubocops/keg_only" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe RuboCop::Cop::FormulaAudit::KegOnly do | 
					
						
							|  |  |  |   subject(:cop) { described_class.new } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "keg_only_needs_downcasing" do | 
					
						
							|  |  |  |     expect_offense(<<~RUBY) | 
					
						
							|  |  |  |       class Foo < Formula | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         url "https://brew.sh/foo-1.0.tgz" | 
					
						
							|  |  |  |         homepage "https://brew.sh" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         keg_only "Because why not" | 
					
						
							| 
									
										
										
										
											2020-07-03 14:35:32 -04:00
										 |  |  |                  ^^^^^^^^^^^^^^^^^ 'Because' from the `keg_only` reason should be 'because'. | 
					
						
							| 
									
										
										
										
											2020-04-28 22:35:47 +05:30
										 |  |  |       end | 
					
						
							|  |  |  |     RUBY | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "keg_only_redundant_period" do | 
					
						
							|  |  |  |     expect_offense(<<~RUBY) | 
					
						
							|  |  |  |       class Foo < Formula | 
					
						
							|  |  |  |         url "https://brew.sh/foo-1.0.tgz" | 
					
						
							|  |  |  |         homepage "https://brew.sh" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         keg_only "ending with a period." | 
					
						
							| 
									
										
										
										
											2020-07-03 14:35:32 -04:00
										 |  |  |                  ^^^^^^^^^^^^^^^^^^^^^^^ `keg_only` reason should not end with a period. | 
					
						
							| 
									
										
										
										
											2020-04-28 22:35:47 +05:30
										 |  |  |       end | 
					
						
							|  |  |  |     RUBY | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-03 14:35:32 -04:00
										 |  |  |   specify "keg_only_autocorrects_downcasing" do | 
					
						
							|  |  |  |     source = <<~RUBY | 
					
						
							|  |  |  |       class Foo < Formula | 
					
						
							|  |  |  |         url "https://brew.sh/foo-1.0.tgz" | 
					
						
							|  |  |  |         homepage "https://brew.sh" | 
					
						
							|  |  |  |         keg_only "Because why not" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     RUBY | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     corrected_source = <<~RUBY | 
					
						
							|  |  |  |       class Foo < Formula | 
					
						
							|  |  |  |         url "https://brew.sh/foo-1.0.tgz" | 
					
						
							|  |  |  |         homepage "https://brew.sh" | 
					
						
							|  |  |  |         keg_only "because why not" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     RUBY | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     new_source = autocorrect_source(source) | 
					
						
							|  |  |  |     expect(new_source).to eq(corrected_source) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "keg_only_autocorrects_redundant_period" do | 
					
						
							|  |  |  |     source = <<~RUBY | 
					
						
							|  |  |  |       class Foo < Formula | 
					
						
							|  |  |  |         url "https://brew.sh/foo-1.0.tgz" | 
					
						
							|  |  |  |         homepage "https://brew.sh" | 
					
						
							|  |  |  |         keg_only "ending with a period." | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     RUBY | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     corrected_source = <<~RUBY | 
					
						
							|  |  |  |       class Foo < Formula | 
					
						
							|  |  |  |         url "https://brew.sh/foo-1.0.tgz" | 
					
						
							|  |  |  |         homepage "https://brew.sh" | 
					
						
							|  |  |  |         keg_only "ending with a period" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     RUBY | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     new_source = autocorrect_source(source) | 
					
						
							|  |  |  |     expect(new_source).to eq(corrected_source) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-28 22:35:47 +05:30
										 |  |  |   specify "keg_only_handles_block_correctly" do | 
					
						
							|  |  |  |     expect_no_offenses(<<~RUBY) | 
					
						
							|  |  |  |       class Foo < Formula | 
					
						
							|  |  |  |         url "https://brew.sh/foo-1.0.tgz" | 
					
						
							|  |  |  |         homepage "https://brew.sh" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         keg_only <<~EOF | 
					
						
							|  |  |  |           this line starts with a lowercase word. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           This line does not but that shouldn't be a | 
					
						
							|  |  |  |           problem | 
					
						
							|  |  |  |         EOF | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     RUBY | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-06 19:12:12 +01:00
										 |  |  |   specify "keg_only_handles_allowlist_correctly" do | 
					
						
							| 
									
										
										
										
											2020-04-28 22:35:47 +05:30
										 |  |  |     expect_no_offenses(<<~RUBY) | 
					
						
							|  |  |  |       class Foo < Formula | 
					
						
							|  |  |  |         url "https://brew.sh/foo-1.0.tgz" | 
					
						
							|  |  |  |         homepage "https://brew.sh" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         keg_only "Apple ships foo in the CLT package" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     RUBY | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "keg_only does not need downcasing of formula name in reason" do | 
					
						
							|  |  |  |     filename = Formulary.core_path("foo") | 
					
						
							|  |  |  |     File.open(filename, "w") do |file| | 
					
						
							|  |  |  |       FileUtils.chmod "-rwx", filename | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect_no_offenses(<<~RUBY, file) | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           url "https://brew.sh/foo-1.0.tgz" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           keg_only "Foo is the formula name hence downcasing is not required" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |