| 
									
										
										
										
											2024-02-06 22:12:51 +01:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-18 15:11:11 -08:00
										 |  |  | RSpec.describe Cask::CaskLoader, :cask do | 
					
						
							| 
									
										
										
										
											2024-02-06 22:12:51 +01:00
										 |  |  |   describe "::for" do | 
					
						
							|  |  |  |     let(:tap) { CoreCaskTap.instance } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when a cask is renamed" do | 
					
						
							|  |  |  |       let(:old_token) { "version-newest" } | 
					
						
							|  |  |  |       let(:new_token) { "version-latest" } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       let(:api_casks) do | 
					
						
							|  |  |  |         [old_token, new_token].to_h do |token| | 
					
						
							|  |  |  |           hash = described_class.load(new_token).to_hash_with_variations | 
					
						
							|  |  |  |           json = JSON.pretty_generate(hash) | 
					
						
							|  |  |  |           cask_json = JSON.parse(json) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           [token, cask_json.except("token")] | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       let(:cask_renames) do | 
					
						
							|  |  |  |         { old_token => new_token } | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       before do | 
					
						
							|  |  |  |         allow(Homebrew::API::Cask) | 
					
						
							|  |  |  |           .to receive(:all_casks) | 
					
						
							|  |  |  |           .and_return(api_casks) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         allow(tap).to receive(:cask_renames) | 
					
						
							|  |  |  |           .and_return(cask_renames) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       context "when not using the API" do | 
					
						
							|  |  |  |         before do | 
					
						
							|  |  |  |           ENV["HOMEBREW_NO_INSTALL_FROM_API"] = "1" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it "warns when using the short token" do | 
					
						
							|  |  |  |           expect do | 
					
						
							| 
									
										
										
										
											2024-02-08 14:53:55 +01:00
										 |  |  |             expect(described_class.for("version-newest")).to be_a Cask::CaskLoader::FromPathLoader | 
					
						
							| 
									
										
										
										
											2024-02-06 22:12:51 +01:00
										 |  |  |           end.to output(/version-newest was renamed to version-latest/).to_stderr | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it "warns when using the full token" do | 
					
						
							|  |  |  |           expect do | 
					
						
							| 
									
										
										
										
											2024-02-08 14:53:55 +01:00
										 |  |  |             expect(described_class.for("homebrew/cask/version-newest")).to be_a Cask::CaskLoader::FromPathLoader | 
					
						
							| 
									
										
										
										
											2024-02-06 22:12:51 +01:00
										 |  |  |           end.to output(/version-newest was renamed to version-latest/).to_stderr | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       context "when using the API" do | 
					
						
							|  |  |  |         before do | 
					
						
							|  |  |  |           ENV.delete("HOMEBREW_NO_INSTALL_FROM_API") | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it "warns when using the short token" do | 
					
						
							|  |  |  |           expect do | 
					
						
							|  |  |  |             expect(described_class.for("version-newest")).to be_a Cask::CaskLoader::FromAPILoader | 
					
						
							|  |  |  |           end.to output(/version-newest was renamed to version-latest/).to_stderr | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it "warns when using the full token" do | 
					
						
							|  |  |  |           expect do | 
					
						
							|  |  |  |             expect(described_class.for("homebrew/cask/version-newest")).to be_a Cask::CaskLoader::FromAPILoader | 
					
						
							|  |  |  |           end.to output(/version-newest was renamed to version-latest/).to_stderr | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2024-02-13 21:12:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     context "when not using the API" do | 
					
						
							|  |  |  |       before do | 
					
						
							|  |  |  |         ENV["HOMEBREW_NO_INSTALL_FROM_API"] = "1" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       context "when a cask is migrated to the default tap" do | 
					
						
							|  |  |  |         let(:token) { "local-caffeine" } | 
					
						
							|  |  |  |         let(:tap_migrations) do | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             token => default_tap.name, | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         let(:old_tap) { CoreTap.instance } | 
					
						
							|  |  |  |         let(:default_tap) { CoreCaskTap.instance } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         before do | 
					
						
							|  |  |  |           (old_tap.path/"tap_migrations.json").write tap_migrations.to_json | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it "does not warn when loading the short token" do | 
					
						
							|  |  |  |           expect do | 
					
						
							|  |  |  |             described_class.for(token) | 
					
						
							|  |  |  |           end.not_to output.to_stderr | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it "does not warn when loading the full token in the default tap" do | 
					
						
							|  |  |  |           expect do | 
					
						
							|  |  |  |             described_class.for("#{default_tap}/#{token}") | 
					
						
							|  |  |  |           end.not_to output.to_stderr | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it "warns when loading the full token in the old tap" do | 
					
						
							|  |  |  |           expect do | 
					
						
							|  |  |  |             described_class.for("#{old_tap}/#{token}") | 
					
						
							|  |  |  |           end.to output(%r{Cask #{old_tap}/#{token} was renamed to #{token}\.}).to_stderr | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2024-02-16 10:22:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-16 10:42:46 +01:00
										 |  |  |         # FIXME | 
					
						
							|  |  |  |         # context "when there is an infinite tap migration loop" do | 
					
						
							|  |  |  |         #   before do | 
					
						
							|  |  |  |         #     (default_tap.path/"tap_migrations.json").write({ | 
					
						
							|  |  |  |         #       token => old_tap.name, | 
					
						
							|  |  |  |         #     }.to_json) | 
					
						
							|  |  |  |         #   end | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         #   it "stops recursing" do | 
					
						
							|  |  |  |         #     expect do | 
					
						
							|  |  |  |         #       described_class.for("#{default_tap}/#{token}") | 
					
						
							|  |  |  |         #     end.not_to output.to_stderr | 
					
						
							|  |  |  |         #   end | 
					
						
							|  |  |  |         # end | 
					
						
							| 
									
										
										
										
											2024-02-13 21:12:51 +01:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2024-02-06 22:12:51 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | end |