| 
									
										
										
										
											2020-03-27 19:18:05 +00:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "language/perl" | 
					
						
							|  |  |  | require "utils/shebang" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe Language::Perl::Shebang do | 
					
						
							|  |  |  |   let(:file) { Tempfile.new("perl-shebang") } | 
					
						
							| 
									
										
										
										
											2023-08-14 20:26:09 -04:00
										 |  |  |   let(:f) do | 
					
						
							|  |  |  |     f = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     f[:perl] = formula "perl" do | 
					
						
							| 
									
										
										
										
											2020-03-27 19:18:05 +00:00
										 |  |  |       url "https://brew.sh/perl-1.0.tgz" | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2023-08-14 20:26:09 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     f[:depends_on] = formula "foo" do | 
					
						
							|  |  |  |       url "https://brew.sh/foo-1.0.tgz" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       depends_on "perl" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     f[:uses_from_macos] = formula "foo" do | 
					
						
							| 
									
										
										
										
											2020-03-27 19:18:05 +00:00
										 |  |  |       url "https://brew.sh/foo-1.0.tgz" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       uses_from_macos "perl" | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2023-08-14 20:26:09 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     f[:no_deps] = formula "foo" do | 
					
						
							|  |  |  |       url "https://brew.sh/foo-1.0.tgz" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     f | 
					
						
							| 
									
										
										
										
											2020-03-27 19:18:05 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   before do | 
					
						
							|  |  |  |     file.write <<~EOS | 
					
						
							|  |  |  |       #!/usr/bin/env perl | 
					
						
							|  |  |  |       a | 
					
						
							|  |  |  |       b | 
					
						
							|  |  |  |       c | 
					
						
							|  |  |  |     EOS | 
					
						
							|  |  |  |     file.flush | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   after { file.unlink } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#detected_perl_shebang" do | 
					
						
							| 
									
										
										
										
											2023-08-14 20:26:09 -04:00
										 |  |  |     it "can be used to replace Perl shebangs when depends_on \"perl\" is used" do | 
					
						
							|  |  |  |       allow(Formulary).to receive(:factory) | 
					
						
							|  |  |  |       allow(Formulary).to receive(:factory).with(f[:perl].name).and_return(f[:perl]) | 
					
						
							|  |  |  |       Utils::Shebang.rewrite_shebang described_class.detected_perl_shebang(f[:depends_on]), file.path | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(File.read(file)).to eq <<~EOS | 
					
						
							|  |  |  |         #!#{HOMEBREW_PREFIX}/opt/perl/bin/perl | 
					
						
							|  |  |  |         a | 
					
						
							|  |  |  |         b | 
					
						
							|  |  |  |         c | 
					
						
							|  |  |  |       EOS | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "can be used to replace Perl shebangs when uses_from_macos \"perl\" is used" do | 
					
						
							| 
									
										
										
										
											2022-11-09 14:45:43 +00:00
										 |  |  |       allow(Formulary).to receive(:factory) | 
					
						
							| 
									
										
										
										
											2023-08-14 20:26:09 -04:00
										 |  |  |       allow(Formulary).to receive(:factory).with(f[:perl].name).and_return(f[:perl]) | 
					
						
							|  |  |  |       Utils::Shebang.rewrite_shebang described_class.detected_perl_shebang(f[:uses_from_macos]), file.path | 
					
						
							| 
									
										
										
										
											2020-03-27 19:18:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expected_shebang = if OS.mac? | 
					
						
							| 
									
										
										
										
											2021-04-29 17:49:01 +01:00
										 |  |  |         "/usr/bin/perl#{MacOS.preferred_perl_version}" | 
					
						
							| 
									
										
										
										
											2020-03-27 19:18:05 +00:00
										 |  |  |       else | 
					
						
							|  |  |  |         HOMEBREW_PREFIX/"opt/perl/bin/perl" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(File.read(file)).to eq <<~EOS | 
					
						
							|  |  |  |         #!#{expected_shebang} | 
					
						
							|  |  |  |         a | 
					
						
							|  |  |  |         b | 
					
						
							|  |  |  |         c | 
					
						
							|  |  |  |       EOS | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2023-08-14 20:26:09 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it "errors if formula doesn't depend on perl" do | 
					
						
							|  |  |  |       expect { Utils::Shebang.rewrite_shebang described_class.detected_perl_shebang(f[:no_deps]), file.path } | 
					
						
							|  |  |  |         .to raise_error(ShebangDetectionError, "Cannot detect Perl shebang: formula does not depend on Perl.") | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2020-03-27 19:18:05 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | end |