| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "rubocops/lines" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-18 15:11:11 -08:00
										 |  |  | RSpec.describe RuboCop::Cop::FormulaAudit::OptionDeclarations do | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |   subject(:cop) { described_class.new } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   context "when auditing options" do | 
					
						
							|  |  |  |     it "reports an offense when `build.without?` is used in homebrew/core" do | 
					
						
							|  |  |  |       expect_offense(<<~RUBY, "/homebrew-core/") | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           desc "foo" | 
					
						
							|  |  |  |           url 'https://brew.sh/foo-1.0.tgz' | 
					
						
							|  |  |  |           def install | 
					
						
							|  |  |  |             build.without? "bar" | 
					
						
							| 
									
										
										
										
											2023-04-07 17:16:48 +01:00
										 |  |  |             ^^^^^^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Formulae in homebrew/core should not use `build.without?`. | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "reports an offense when `build.with?` is used in homebrew/core" do | 
					
						
							|  |  |  |       expect_offense(<<~RUBY, "/homebrew-core/") | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           desc "foo" | 
					
						
							|  |  |  |           url 'https://brew.sh/foo-1.0.tgz' | 
					
						
							|  |  |  |           def install | 
					
						
							|  |  |  |             build.with? "bar" | 
					
						
							| 
									
										
										
										
											2023-04-07 17:16:48 +01:00
										 |  |  |             ^^^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Formulae in homebrew/core should not use `build.with?`. | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "reports an offense when `build.without?` is used for a conditional dependency" do | 
					
						
							|  |  |  |       expect_offense(<<~RUBY) | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           depends_on "bar" if build.without?("baz") | 
					
						
							| 
									
										
										
										
											2023-04-07 17:16:48 +01:00
										 |  |  |                               ^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Use `:optional` or `:recommended` instead of `if build.without?("baz")` | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 23:54:07 +00:00
										 |  |  |     it "reports an offense when `build.with?` is used for a conditional dependency" do | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |       expect_offense(<<~RUBY) | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           depends_on "bar" if build.with?("baz") | 
					
						
							| 
									
										
										
										
											2023-04-07 17:16:48 +01:00
										 |  |  |                               ^^^^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Use `:optional` or `:recommended` instead of `if build.with?("baz")` | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "reports an offense when `build.without?` is used with `unless`" do | 
					
						
							|  |  |  |       expect_offense(<<~RUBY) | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           desc "foo" | 
					
						
							|  |  |  |           url 'https://brew.sh/foo-1.0.tgz' | 
					
						
							|  |  |  |           def post_install | 
					
						
							|  |  |  |             return unless build.without? "bar" | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |                           ^^^^^^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Use `if build.with? "bar"` instead of `unless build.without? "bar"` | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "reports an offense when `build.with?` is used with `unless`" do | 
					
						
							|  |  |  |       expect_offense(<<~RUBY) | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           desc "foo" | 
					
						
							|  |  |  |           url 'https://brew.sh/foo-1.0.tgz' | 
					
						
							|  |  |  |           def post_install | 
					
						
							|  |  |  |             return unless build.with? "bar" | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |                           ^^^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Use `if build.without? "bar"` instead of `unless build.with? "bar"` | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "reports an offense when `build.with?` is negated" do | 
					
						
							|  |  |  |       expect_offense(<<~RUBY) | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           desc "foo" | 
					
						
							|  |  |  |           url 'https://brew.sh/foo-1.0.tgz' | 
					
						
							|  |  |  |           def post_install | 
					
						
							|  |  |  |             return if !build.with? "bar" | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |                       ^^^^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Instead of negating `build.with?`, use `build.without?` | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "reports an offense when `build.without?` is negated" do | 
					
						
							|  |  |  |       expect_offense(<<~RUBY) | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           desc "foo" | 
					
						
							|  |  |  |           url 'https://brew.sh/foo-1.0.tgz' | 
					
						
							|  |  |  |           def post_install | 
					
						
							|  |  |  |             return if !build.without? "bar" | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |                       ^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Instead of negating `build.without?`, use `build.with?` | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "reports an offense when a `build.without?` conditional is unnecessary" do | 
					
						
							|  |  |  |       expect_offense(<<~RUBY) | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           desc "foo" | 
					
						
							|  |  |  |           url 'https://brew.sh/foo-1.0.tgz' | 
					
						
							|  |  |  |           def post_install | 
					
						
							|  |  |  |             return if build.without? "--without-bar" | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |                                      ^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Instead of duplicating `without`, use `build.without? "bar"` to check for "--without-bar" | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "reports an offense when a `build.with?` conditional is unnecessary" do | 
					
						
							|  |  |  |       expect_offense(<<~RUBY) | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           desc "foo" | 
					
						
							|  |  |  |           url 'https://brew.sh/foo-1.0.tgz' | 
					
						
							|  |  |  |           def post_install | 
					
						
							|  |  |  |             return if build.with? "--with-bar" | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |                                   ^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Instead of duplicating `with`, use `build.with? "bar"` to check for '--with-bar' | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "reports an offense when `build.include?` is used" do | 
					
						
							|  |  |  |       expect_offense(<<~RUBY) | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           desc "foo" | 
					
						
							|  |  |  |           url 'https://brew.sh/foo-1.0.tgz' | 
					
						
							|  |  |  |           def post_install | 
					
						
							|  |  |  |             return if build.include? "foo" | 
					
						
							| 
									
										
										
										
											2023-04-07 17:16:48 +01:00
										 |  |  |                       ^^^^^^^^^^^^^^^^^^^^ FormulaAudit/OptionDeclarations: `build.include?` is deprecated | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "reports an offense when `def option` is used" do | 
					
						
							|  |  |  |       expect_offense(<<~RUBY) | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           desc "foo" | 
					
						
							|  |  |  |           url 'https://brew.sh/foo-1.0.tgz' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           def options | 
					
						
							| 
									
										
										
										
											2023-04-07 17:16:48 +01:00
										 |  |  |           ^^^^^^^^^^^ FormulaAudit/OptionDeclarations: Use new-style option definitions | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |             [["--bar", "desc"]] | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |