| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "rubocops/text" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-18 15:11:11 -08:00
										 |  |  | RSpec.describe RuboCop::Cop::FormulaAuditStrict::Text do | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |   subject(:cop) { described_class.new } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   context "when auditing formula text in homebrew/core" do | 
					
						
							|  |  |  |     it "reports an offense if `env :userpaths` is present" do | 
					
						
							|  |  |  |       expect_offense(<<~RUBY) | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           url "https://brew.sh/foo-1.0.tgz" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           env :userpaths | 
					
						
							| 
									
										
										
										
											2023-04-07 17:16:48 +01:00
										 |  |  |           ^^^^^^^^^^^^^^ FormulaAuditStrict/Text: `env :userpaths` in homebrew/core formulae is deprecated | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "reports an offense if `env :std` is present in homebrew/core" do | 
					
						
							|  |  |  |       expect_offense(<<~RUBY, "/homebrew-core/") | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           url "https://brew.sh/foo-1.0.tgz" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           env :std | 
					
						
							| 
									
										
										
										
											2023-04-07 17:16:48 +01:00
										 |  |  |           ^^^^^^^^ FormulaAuditStrict/Text: `env :std` in homebrew/core formulae is deprecated | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it %Q(reports an offense if "\#{share}/<formula name>" is present) do | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |       expect_offense(<<~'RUBY', "/homebrew-core/Formula/foo.rb") | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           def install | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |             ohai "#{share}/foo" | 
					
						
							|  |  |  |                  ^^^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `#{pkgshare}` instead of `#{share}/foo` | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |       expect_offense(<<~'RUBY', "/homebrew-core/Formula/foo.rb") | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           def install | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |             ohai "#{share}/foo/bar" | 
					
						
							|  |  |  |                  ^^^^^^^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `#{pkgshare}` instead of `#{share}/foo` | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |       expect_offense(<<~'RUBY', "/homebrew-core/Formula/foolibc++.rb") | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |         class Foolibcxx < Formula | 
					
						
							|  |  |  |           def install | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |             ohai "#{share}/foolibc++" | 
					
						
							|  |  |  |                  ^^^^^^^^^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `#{pkgshare}` instead of `#{share}/foolibc++` | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'reports an offense if `share/"<formula name>"` is present' do | 
					
						
							|  |  |  |       expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb") | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           def install | 
					
						
							|  |  |  |             ohai share/"foo" | 
					
						
							| 
									
										
										
										
											2023-04-07 17:16:48 +01:00
										 |  |  |                  ^^^^^^^^^^^ FormulaAuditStrict/Text: Use `pkgshare` instead of `share/"foo"` | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb") | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           def install | 
					
						
							|  |  |  |             ohai share/"foo/bar" | 
					
						
							| 
									
										
										
										
											2023-04-07 17:16:48 +01:00
										 |  |  |                  ^^^^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `pkgshare` instead of `share/"foo"` | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect_offense(<<~RUBY, "/homebrew-core/Formula/foolibc++.rb") | 
					
						
							|  |  |  |         class Foolibcxx < Formula | 
					
						
							|  |  |  |           def install | 
					
						
							|  |  |  |             ohai share/"foolibc++" | 
					
						
							| 
									
										
										
										
											2023-04-07 17:16:48 +01:00
										 |  |  |                  ^^^^^^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `pkgshare` instead of `share/"foolibc++"` | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it %Q(reports no offenses if "\#{share}/<directory name>" doesn't match formula name) do | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |       expect_no_offenses(<<~'RUBY', "/homebrew-core/Formula/foo.rb") | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           def install | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |             ohai "#{share}/foo-bar" | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'reports no offenses if `share/"<formula name>"` is not present' do | 
					
						
							|  |  |  |       expect_no_offenses(<<~RUBY, "/homebrew-core/Formula/foo.rb") | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           def install | 
					
						
							|  |  |  |             ohai share/"foo-bar" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect_no_offenses(<<~RUBY, "/homebrew-core/Formula/foo.rb") | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           def install | 
					
						
							|  |  |  |             ohai share/"bar" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect_no_offenses(<<~RUBY, "/homebrew-core/Formula/foo.rb") | 
					
						
							|  |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           def install | 
					
						
							|  |  |  |             ohai share/"bar/foo" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-17 12:30:12 -07:00
										 |  |  |     it %Q(reports no offenses if formula name appears after "\#{share}/<directory name>") do | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |       expect_no_offenses(<<~'RUBY', "/homebrew-core/Formula/foo.rb") | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           def install | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |             ohai "#{share}/bar/foo" | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2024-07-22 23:06:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-04 23:38:11 +01:00
										 |  |  |     context "for interpolated bin paths" do | 
					
						
							|  |  |  |       it 'reports an offense & autocorrects if "\#{bin}/<formula_name>" or other dashed binaries too are present' do | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |         expect_offense(<<~'RUBY', "/homebrew-core/Formula/foo.rb") | 
					
						
							| 
									
										
										
										
											2024-08-04 23:38:11 +01:00
										 |  |  |           class Foo < Formula | 
					
						
							|  |  |  |             test do | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |               system "#{bin}/foo", "-v" | 
					
						
							|  |  |  |                      ^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo"` instead of `"#{bin}/foo"` | 
					
						
							|  |  |  |               system "#{bin}/foo-bar", "-v" | 
					
						
							|  |  |  |                      ^^^^^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo-bar"` instead of `"#{bin}/foo-bar"` | 
					
						
							| 
									
										
										
										
											2024-08-04 23:38:11 +01:00
										 |  |  |             end | 
					
						
							| 
									
										
										
										
											2024-08-01 22:28:21 +01:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2024-08-04 23:38:11 +01:00
										 |  |  |         RUBY | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect_correction(<<~RUBY) | 
					
						
							|  |  |  |           class Foo < Formula | 
					
						
							|  |  |  |             test do | 
					
						
							|  |  |  |               system bin/"foo", "-v" | 
					
						
							|  |  |  |               system bin/"foo-bar", "-v" | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2024-07-22 23:06:53 +01:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2024-08-04 23:38:11 +01:00
										 |  |  |         RUBY | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it 'does not report an offense if \#{bin}/foo and then a space and more text' do | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |         expect_no_offenses(<<~'RUBY', "/homebrew-core/Formula/foo.rb") | 
					
						
							| 
									
										
										
										
											2024-08-04 23:38:11 +01:00
										 |  |  |           class Foo < Formula | 
					
						
							|  |  |  |             test do | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |               shell_output("#{bin}/foo --version") | 
					
						
							|  |  |  |               assert_match "help", shell_output("#{bin}/foo-something --help 2>&1") | 
					
						
							|  |  |  |               assert_match "OK", shell_output("#{bin}/foo-something_else --check 2>&1") | 
					
						
							| 
									
										
										
										
											2024-08-04 23:38:11 +01:00
										 |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         RUBY | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2024-07-22 23:06:53 +01:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2024-07-25 10:41:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-04 23:38:11 +01:00
										 |  |  |     it 'does not report an offense if "\#{bin}/foo" is in a word array' do | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |       expect_no_offenses(<<~'RUBY', "/homebrew-core/Formula/foo.rb") | 
					
						
							| 
									
										
										
										
											2024-07-25 10:41:13 +01:00
										 |  |  |         class Foo < Formula | 
					
						
							|  |  |  |           test do | 
					
						
							| 
									
										
										
										
											2024-08-04 23:38:11 +01:00
										 |  |  |             cmd = %W[
 | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |               #{bin}/foo | 
					
						
							| 
									
										
										
										
											2024-08-04 23:38:11 +01:00
										 |  |  |               version | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |             assert_match version.to_s, shell_output(cmd) | 
					
						
							| 
									
										
										
										
											2024-07-25 10:41:13 +01:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | end |