| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  | require "migrator" | 
					
						
							|  |  |  | require "test/support/fixtures/testball" | 
					
						
							|  |  |  | require "tab" | 
					
						
							|  |  |  | require "keg" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe Migrator do | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |   subject(:migrator) { described_class.new(new_formula) } | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   let(:new_formula) { Testball.new("newname") } | 
					
						
							|  |  |  |   let(:old_formula) { Testball.new("oldname") } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let(:new_keg_record) { HOMEBREW_CELLAR/"newname/0.1" } | 
					
						
							|  |  |  |   let(:old_keg_record) { HOMEBREW_CELLAR/"oldname/0.1" } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let(:old_tab) { Tab.empty } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let(:keg) { Keg.new(old_keg_record) } | 
					
						
							|  |  |  |   let(:old_pin) { HOMEBREW_PINNED_KEGS/"oldname" } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  |   before do |example| | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  |     allow(new_formula).to receive(:oldname).and_return("oldname") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # do not create directories for error tests | 
					
						
							|  |  |  |     next if example.metadata[:description].start_with?("raises an error") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     (old_keg_record/"bin").mkpath | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     %w[inside bindir].each do |file| | 
					
						
							|  |  |  |       FileUtils.touch old_keg_record/"bin/#{file}" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     old_tab.tabfile = HOMEBREW_CELLAR/"oldname/0.1/INSTALL_RECEIPT.json" | 
					
						
							|  |  |  |     old_tab.source["path"] = "/oldname" | 
					
						
							|  |  |  |     old_tab.write | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     keg.link | 
					
						
							|  |  |  |     keg.optlink | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     old_pin.make_relative_symlink old_keg_record | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |     migrator # needs to be evaluated eagerly | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     (HOMEBREW_PREFIX/"bin").mkpath | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  |   after do | 
					
						
							| 
									
										
										
										
											2017-05-29 18:24:52 +01:00
										 |  |  |     keg.unlink if !old_keg_record.parent.symlink? && old_keg_record.directory? | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if new_keg_record.directory? | 
					
						
							|  |  |  |       new_keg = Keg.new(new_keg_record) | 
					
						
							|  |  |  |       new_keg.unlink | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "::new" do | 
					
						
							|  |  |  |     it "raises an error if there is no old name" do | 
					
						
							|  |  |  |       expect { | 
					
						
							|  |  |  |         described_class.new(old_formula) | 
					
						
							|  |  |  |       }.to raise_error(Migrator::MigratorNoOldnameError) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "raises an error if there is no old path" do | 
					
						
							|  |  |  |       expect { | 
					
						
							|  |  |  |         described_class.new(new_formula) | 
					
						
							|  |  |  |       }.to raise_error(Migrator::MigratorNoOldpathError) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "raises an error if the Taps differ" do | 
					
						
							|  |  |  |       keg = HOMEBREW_CELLAR/"oldname/0.1" | 
					
						
							|  |  |  |       keg.mkpath | 
					
						
							|  |  |  |       tab = Tab.empty | 
					
						
							|  |  |  |       tab.tabfile = HOMEBREW_CELLAR/"oldname/0.1/INSTALL_RECEIPT.json" | 
					
						
							|  |  |  |       tab.source["tap"] = "homebrew/core" | 
					
						
							|  |  |  |       tab.write | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect { | 
					
						
							|  |  |  |         described_class.new(new_formula) | 
					
						
							|  |  |  |       }.to raise_error(Migrator::MigratorDifferentTapsError) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "#move_to_new_directory" do | 
					
						
							|  |  |  |     keg.unlink | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |     migrator.move_to_new_directory | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(new_keg_record).to be_a_directory | 
					
						
							|  |  |  |     expect(new_keg_record/"bin").to be_a_directory | 
					
						
							|  |  |  |     expect(new_keg_record/"bin/inside").to be_a_file | 
					
						
							|  |  |  |     expect(new_keg_record/"bin/bindir").to be_a_file | 
					
						
							|  |  |  |     expect(old_keg_record).not_to be_a_directory | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "#backup_oldname_cellar" do | 
					
						
							|  |  |  |     old_keg_record.parent.rmtree | 
					
						
							|  |  |  |     (new_keg_record/"bin").mkpath | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |     migrator.backup_oldname_cellar | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(old_keg_record/"bin").to be_a_directory | 
					
						
							|  |  |  |     expect(old_keg_record/"bin").to be_a_directory | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "#repin" do | 
					
						
							|  |  |  |     (new_keg_record/"bin").mkpath | 
					
						
							|  |  |  |     expected_relative = new_keg_record.relative_path_from HOMEBREW_PINNED_KEGS | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |     migrator.repin | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |     expect(migrator.new_pin_record).to be_a_symlink | 
					
						
							|  |  |  |     expect(migrator.new_pin_record.readlink).to eq(expected_relative) | 
					
						
							|  |  |  |     expect(migrator.old_pin_record).not_to exist | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "#unlink_oldname" do | 
					
						
							|  |  |  |     expect(HOMEBREW_LINKED_KEGS.children.count).to eq(1) | 
					
						
							|  |  |  |     expect((HOMEBREW_PREFIX/"opt").children.count).to eq(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |     migrator.unlink_oldname | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(HOMEBREW_LINKED_KEGS).not_to exist | 
					
						
							|  |  |  |     expect(HOMEBREW_LIBRARY/"bin").not_to exist | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "#link_newname" do | 
					
						
							|  |  |  |     keg.unlink | 
					
						
							|  |  |  |     keg.uninstall | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     (new_keg_record/"bin").mkpath | 
					
						
							|  |  |  |     %w[inside bindir].each do |file| | 
					
						
							|  |  |  |       FileUtils.touch new_keg_record/"bin"/file | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |     migrator.link_newname | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(HOMEBREW_LINKED_KEGS.children.count).to eq(1) | 
					
						
							|  |  |  |     expect((HOMEBREW_PREFIX/"opt").children.count).to eq(1) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "#link_oldname_opt" do | 
					
						
							|  |  |  |     new_keg_record.mkpath | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |     migrator.link_oldname_opt | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  |     expect((HOMEBREW_PREFIX/"opt/oldname").realpath).to eq(new_keg_record.realpath) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "#link_oldname_cellar" do | 
					
						
							|  |  |  |     (new_keg_record/"bin").mkpath | 
					
						
							|  |  |  |     keg.unlink | 
					
						
							|  |  |  |     keg.uninstall | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |     migrator.link_oldname_cellar | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  |     expect((HOMEBREW_CELLAR/"oldname").realpath).to eq(new_keg_record.parent.realpath) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "#update_tabs" do | 
					
						
							|  |  |  |     (new_keg_record/"bin").mkpath | 
					
						
							|  |  |  |     tab = Tab.empty | 
					
						
							|  |  |  |     tab.tabfile = HOMEBREW_CELLAR/"newname/0.1/INSTALL_RECEIPT.json" | 
					
						
							|  |  |  |     tab.source["path"] = "/path/that/must/be/changed/by/update_tabs" | 
					
						
							|  |  |  |     tab.write | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |     migrator.update_tabs | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  |     expect(Tab.for_keg(new_keg_record).source["path"]).to eq(new_formula.path.to_s) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "#migrate" do | 
					
						
							|  |  |  |     tab = Tab.empty | 
					
						
							|  |  |  |     tab.tabfile = HOMEBREW_CELLAR/"oldname/0.1/INSTALL_RECEIPT.json" | 
					
						
							|  |  |  |     tab.source["path"] = old_formula.path.to_s | 
					
						
							|  |  |  |     tab.write | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |     migrator.migrate | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(new_keg_record).to exist | 
					
						
							|  |  |  |     expect(old_keg_record.parent).to be_a_symlink | 
					
						
							|  |  |  |     expect(HOMEBREW_LINKED_KEGS/"oldname").not_to exist | 
					
						
							|  |  |  |     expect((HOMEBREW_LINKED_KEGS/"newname").realpath).to eq(new_keg_record.realpath) | 
					
						
							|  |  |  |     expect(old_keg_record.realpath).to eq(new_keg_record.realpath) | 
					
						
							|  |  |  |     expect((HOMEBREW_PREFIX/"opt/oldname").realpath).to eq(new_keg_record.realpath) | 
					
						
							|  |  |  |     expect((HOMEBREW_CELLAR/"oldname").realpath).to eq(new_keg_record.parent.realpath) | 
					
						
							|  |  |  |     expect((HOMEBREW_PINNED_KEGS/"newname").realpath).to eq(new_keg_record.realpath) | 
					
						
							|  |  |  |     expect(Tab.for_keg(new_keg_record).source["path"]).to eq(new_formula.path.to_s) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "#unlinik_oldname_opt" do | 
					
						
							|  |  |  |     new_keg_record.mkpath | 
					
						
							|  |  |  |     old_opt_record = HOMEBREW_PREFIX/"opt/oldname" | 
					
						
							|  |  |  |     old_opt_record.unlink if old_opt_record.symlink? | 
					
						
							|  |  |  |     old_opt_record.make_relative_symlink(new_keg_record) | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |     migrator.unlink_oldname_opt | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  |     expect(old_opt_record).not_to be_a_symlink | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "#unlink_oldname_cellar" do | 
					
						
							|  |  |  |     new_keg_record.mkpath | 
					
						
							|  |  |  |     keg.unlink | 
					
						
							|  |  |  |     keg.uninstall | 
					
						
							|  |  |  |     old_keg_record.parent.make_relative_symlink(new_keg_record.parent) | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |     migrator.unlink_oldname_cellar | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  |     expect(old_keg_record.parent).not_to be_a_symlink | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-28 19:16:56 +00:00
										 |  |  |   specify "#backup_oldname_cellar after uninstall" do | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  |     (new_keg_record/"bin").mkpath | 
					
						
							|  |  |  |     keg.unlink | 
					
						
							|  |  |  |     keg.uninstall | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |     migrator.backup_oldname_cellar | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  |     expect(old_keg_record.subdirs).not_to be_empty | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "#backup_old_tabs" do | 
					
						
							|  |  |  |     tab = Tab.empty | 
					
						
							|  |  |  |     tab.tabfile = HOMEBREW_CELLAR/"oldname/0.1/INSTALL_RECEIPT.json" | 
					
						
							|  |  |  |     tab.source["path"] = "/should/be/the/same" | 
					
						
							|  |  |  |     tab.write | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  |     migrator = described_class.new(new_formula) | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  |     tab.tabfile.delete | 
					
						
							|  |  |  |     migrator.backup_old_tabs | 
					
						
							|  |  |  |     expect(Tab.for_keg(old_keg_record).source["path"]).to eq("/should/be/the/same") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#backup_oldname" do | 
					
						
							|  |  |  |     context "when cellar exists" do | 
					
						
							|  |  |  |       it "backs up the old name" do | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |         migrator.backup_oldname | 
					
						
							| 
									
										
										
										
											2019-03-28 19:16:56 +00:00
										 |  |  |         expect(old_keg_record.parent).to be_a_directory | 
					
						
							|  |  |  |         expect(old_keg_record.parent.subdirs).not_to be_empty | 
					
						
							|  |  |  |         expect(HOMEBREW_LINKED_KEGS/"oldname").to exist | 
					
						
							|  |  |  |         expect(HOMEBREW_PREFIX/"opt/oldname").to exist | 
					
						
							|  |  |  |         expect(HOMEBREW_PINNED_KEGS/"oldname").to be_a_symlink | 
					
						
							|  |  |  |         expect(keg).to be_linked | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when cellar is removed" do | 
					
						
							|  |  |  |       it "backs up the old name" do | 
					
						
							|  |  |  |         (new_keg_record/"bin").mkpath | 
					
						
							|  |  |  |         keg.unlink | 
					
						
							|  |  |  |         keg.uninstall | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |         migrator.backup_oldname | 
					
						
							| 
									
										
										
										
											2019-03-28 19:16:56 +00:00
										 |  |  |         expect(old_keg_record.parent).to be_a_directory | 
					
						
							|  |  |  |         expect(old_keg_record.parent.subdirs).not_to be_empty | 
					
						
							|  |  |  |         expect(HOMEBREW_LINKED_KEGS/"oldname").to exist | 
					
						
							|  |  |  |         expect(HOMEBREW_PREFIX/"opt/oldname").to exist | 
					
						
							|  |  |  |         expect(HOMEBREW_PINNED_KEGS/"oldname").to be_a_symlink | 
					
						
							|  |  |  |         expect(keg).to be_linked | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when cellar is linked" do | 
					
						
							|  |  |  |       it "backs up the old name" do | 
					
						
							|  |  |  |         (new_keg_record/"bin").mkpath | 
					
						
							|  |  |  |         keg.unlink | 
					
						
							|  |  |  |         keg.uninstall | 
					
						
							|  |  |  |         old_keg_record.parent.make_relative_symlink(new_keg_record.parent) | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |         migrator.backup_oldname | 
					
						
							| 
									
										
										
										
											2019-03-28 19:16:56 +00:00
										 |  |  |         expect(old_keg_record.parent).to be_a_directory | 
					
						
							|  |  |  |         expect(old_keg_record.parent.subdirs).not_to be_empty | 
					
						
							|  |  |  |         expect(HOMEBREW_LINKED_KEGS/"oldname").to exist | 
					
						
							|  |  |  |         expect(HOMEBREW_PREFIX/"opt/oldname").to exist | 
					
						
							|  |  |  |         expect(HOMEBREW_PINNED_KEGS/"oldname").to be_a_symlink | 
					
						
							|  |  |  |         expect(keg).to be_linked | 
					
						
							| 
									
										
										
										
											2017-02-15 23:46:01 +01:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |