| 
									
										
										
										
											2017-02-27 17:01:22 +01:00
										 |  |  | require "test/support/fixtures/testball" | 
					
						
							|  |  |  | require "cleanup" | 
					
						
							|  |  |  | require "fileutils" | 
					
						
							|  |  |  | require "pathname" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe Homebrew::Cleanup do | 
					
						
							|  |  |  |   let(:ds_store) { Pathname.new("#{HOMEBREW_PREFIX}/Library/.DS_Store") } | 
					
						
							| 
									
										
										
										
											2017-08-26 02:01:45 +05:30
										 |  |  |   let(:sec_in_a_day) { 60 * 60 * 24 } | 
					
						
							| 
									
										
										
										
											2017-02-27 17:01:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   around(:each) do |example| | 
					
						
							|  |  |  |     begin | 
					
						
							|  |  |  |       FileUtils.touch ds_store | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       example.run | 
					
						
							|  |  |  |     ensure | 
					
						
							|  |  |  |       FileUtils.rm_f ds_store | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "::cleanup" do | 
					
						
							|  |  |  |     it "removes .DS_Store files" do | 
					
						
							| 
									
										
										
										
											2017-07-29 19:55:05 +02:00
										 |  |  |       described_class.cleanup | 
					
						
							| 
									
										
										
										
											2017-02-27 17:01:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(ds_store).not_to exist | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "doesn't remove anything if `--dry-run` is specified" do | 
					
						
							|  |  |  |       ARGV << "--dry-run" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-29 19:55:05 +02:00
										 |  |  |       described_class.cleanup | 
					
						
							| 
									
										
										
										
											2017-02-27 17:01:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(ds_store).to exist | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-03-21 04:13:13 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     context "when it can't remove a keg" do | 
					
						
							|  |  |  |       let(:f1) { Class.new(Testball) { version "0.1" }.new } | 
					
						
							|  |  |  |       let(:f2) { Class.new(Testball) { version "0.2" }.new } | 
					
						
							|  |  |  |       let(:unremovable_kegs) { [] } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       before(:each) do | 
					
						
							|  |  |  |         described_class.instance_variable_set(:@unremovable_kegs, []) | 
					
						
							| 
									
										
										
										
											2017-07-29 19:55:05 +02:00
										 |  |  |         [f1, f2].each do |f| | 
					
						
							|  |  |  |           f.brew do | 
					
						
							|  |  |  |             f.install | 
					
						
							| 
									
										
										
										
											2017-03-21 04:13:13 -05:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-07-29 19:55:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |           Tab.create(f, DevelopmentTools.default_compiler, :libcxx).write | 
					
						
							| 
									
										
										
										
											2017-03-21 04:13:13 -05:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         allow_any_instance_of(Keg) | 
					
						
							|  |  |  |           .to receive(:uninstall) | 
					
						
							|  |  |  |           .and_raise(Errno::EACCES) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "doesn't remove any kegs" do | 
					
						
							| 
									
										
										
										
											2017-07-29 19:55:05 +02:00
										 |  |  |         described_class.cleanup_formula f2 | 
					
						
							| 
									
										
										
										
											2017-03-21 04:13:13 -05:00
										 |  |  |         expect(f1.installed_kegs.size).to eq(2) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "lists the unremovable kegs" do | 
					
						
							| 
									
										
										
										
											2017-07-29 19:55:05 +02:00
										 |  |  |         described_class.cleanup_formula f2 | 
					
						
							| 
									
										
										
										
											2017-03-21 04:13:13 -05:00
										 |  |  |         expect(described_class.unremovable_kegs).to contain_exactly(f1.installed_kegs[0]) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-02-27 17:01:22 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "::cleanup_formula" do | 
					
						
							|  |  |  |     f1 = Class.new(Testball) do | 
					
						
							|  |  |  |       version "1.0" | 
					
						
							|  |  |  |     end.new | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     f2 = Class.new(Testball) do | 
					
						
							|  |  |  |       version "0.2" | 
					
						
							|  |  |  |       version_scheme 1
 | 
					
						
							|  |  |  |     end.new | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     f3 = Class.new(Testball) do | 
					
						
							|  |  |  |       version "0.3" | 
					
						
							|  |  |  |       version_scheme 1
 | 
					
						
							|  |  |  |     end.new | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     f4 = Class.new(Testball) do | 
					
						
							|  |  |  |       version "0.1" | 
					
						
							|  |  |  |       version_scheme 2
 | 
					
						
							|  |  |  |     end.new | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-29 19:55:05 +02:00
										 |  |  |     [f1, f2, f3, f4].each do |f| | 
					
						
							|  |  |  |       f.brew do | 
					
						
							|  |  |  |         f.install | 
					
						
							| 
									
										
										
										
											2017-02-27 17:01:22 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-07-29 19:55:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       Tab.create(f, DevelopmentTools.default_compiler, :libcxx).write | 
					
						
							| 
									
										
										
										
											2017-02-27 17:01:22 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(f1).to be_installed | 
					
						
							|  |  |  |     expect(f2).to be_installed | 
					
						
							|  |  |  |     expect(f3).to be_installed | 
					
						
							|  |  |  |     expect(f4).to be_installed | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-29 19:55:05 +02:00
										 |  |  |     described_class.cleanup_formula f3 | 
					
						
							| 
									
										
										
										
											2017-02-27 17:01:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(f1).not_to be_installed | 
					
						
							|  |  |  |     expect(f2).not_to be_installed | 
					
						
							|  |  |  |     expect(f3).to be_installed | 
					
						
							|  |  |  |     expect(f4).to be_installed | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-13 04:21:26 +05:30
										 |  |  |   describe "::cleanup_logs" do | 
					
						
							| 
									
										
										
										
											2017-08-19 17:17:38 +05:30
										 |  |  |     let(:path) { (HOMEBREW_LOGS/"delete_me") } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-13 04:21:26 +05:30
										 |  |  |     before do | 
					
						
							|  |  |  |       path.mkpath | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "cleans all logs if prune all" do | 
					
						
							|  |  |  |       ARGV << "--prune=all" | 
					
						
							|  |  |  |       described_class.cleanup_logs | 
					
						
							|  |  |  |       expect(path).not_to exist | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-02-27 17:01:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-13 04:21:26 +05:30
										 |  |  |     it "cleans up logs if older than 14 days" do | 
					
						
							| 
									
										
										
										
											2017-08-26 02:01:45 +05:30
										 |  |  |       allow_any_instance_of(Pathname).to receive(:mtime).and_return(Time.now - sec_in_a_day * 15) | 
					
						
							| 
									
										
										
										
											2017-08-13 04:21:26 +05:30
										 |  |  |       described_class.cleanup_logs | 
					
						
							|  |  |  |       expect(path).not_to exist | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-02-27 17:01:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-13 04:21:26 +05:30
										 |  |  |     it "does not clean up logs less than 14 days old" do | 
					
						
							| 
									
										
										
										
											2017-08-26 02:01:45 +05:30
										 |  |  |       allow_any_instance_of(Pathname).to receive(:mtime).and_return(Time.now - sec_in_a_day * 2) | 
					
						
							| 
									
										
										
										
											2017-08-13 04:21:26 +05:30
										 |  |  |       described_class.cleanup_logs | 
					
						
							|  |  |  |       expect(path).to exist | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-02-27 17:01:22 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "::cleanup_cache" do | 
					
						
							|  |  |  |     it "cleans up incomplete downloads" do | 
					
						
							|  |  |  |       incomplete = (HOMEBREW_CACHE/"something.incomplete") | 
					
						
							|  |  |  |       incomplete.mkpath | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-29 19:55:05 +02:00
										 |  |  |       described_class.cleanup_cache | 
					
						
							| 
									
										
										
										
											2017-02-27 17:01:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(incomplete).not_to exist | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-13 04:21:26 +05:30
										 |  |  |     it "cleans up 'glide_home'" do | 
					
						
							|  |  |  |       glide_home = (HOMEBREW_CACHE/"glide_home") | 
					
						
							|  |  |  |       glide_home.mkpath | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       described_class.cleanup_cache | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(glide_home).not_to exist | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-27 17:01:22 +01:00
										 |  |  |     it "cleans up 'java_cache'" do | 
					
						
							|  |  |  |       java_cache = (HOMEBREW_CACHE/"java_cache") | 
					
						
							|  |  |  |       java_cache.mkpath | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-29 19:55:05 +02:00
										 |  |  |       described_class.cleanup_cache | 
					
						
							| 
									
										
										
										
											2017-02-27 17:01:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(java_cache).not_to exist | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "cleans up 'npm_cache'" do | 
					
						
							|  |  |  |       npm_cache = (HOMEBREW_CACHE/"npm_cache") | 
					
						
							|  |  |  |       npm_cache.mkpath | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-29 19:55:05 +02:00
										 |  |  |       described_class.cleanup_cache | 
					
						
							| 
									
										
										
										
											2017-02-27 17:01:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(npm_cache).not_to exist | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-07-19 02:46:45 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-04 05:47:05 +05:30
										 |  |  |     it "cleans up all files and directories" do | 
					
						
							|  |  |  |       git = (HOMEBREW_CACHE/"gist--git") | 
					
						
							|  |  |  |       gist = (HOMEBREW_CACHE/"gist") | 
					
						
							|  |  |  |       svn = (HOMEBREW_CACHE/"gist--svn") | 
					
						
							| 
									
										
										
										
											2017-08-13 04:21:26 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-04 05:47:05 +05:30
										 |  |  |       git.mkpath | 
					
						
							|  |  |  |       gist.mkpath | 
					
						
							|  |  |  |       FileUtils.touch svn | 
					
						
							| 
									
										
										
										
											2017-08-13 04:21:26 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-19 02:46:45 +05:30
										 |  |  |       allow(ARGV).to receive(:value).with("prune").and_return("all") | 
					
						
							| 
									
										
										
										
											2017-08-13 04:21:26 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |       described_class.cleanup_cache | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(git).not_to exist | 
					
						
							|  |  |  |       expect(gist).to exist | 
					
						
							|  |  |  |       expect(svn).not_to exist | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "does not clean up directories that are not VCS checkouts" do | 
					
						
							|  |  |  |       git = (HOMEBREW_CACHE/"git") | 
					
						
							|  |  |  |       git.mkpath | 
					
						
							|  |  |  |       allow(ARGV).to receive(:value).with("prune").and_return("all") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       described_class.cleanup_cache | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(git).to exist | 
					
						
							| 
									
										
										
										
											2017-08-04 05:47:05 +05:30
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-05 19:36:35 +05:30
										 |  |  |     it "cleans up VCS checkout directories with modified time < prune time" do | 
					
						
							|  |  |  |       foo = (HOMEBREW_CACHE/"--foo") | 
					
						
							|  |  |  |       foo.mkpath | 
					
						
							|  |  |  |       allow(ARGV).to receive(:value).with("prune").and_return("1") | 
					
						
							| 
									
										
										
										
											2017-08-26 02:01:45 +05:30
										 |  |  |       allow_any_instance_of(Pathname).to receive(:mtime).and_return(Time.now - sec_in_a_day * 2) | 
					
						
							| 
									
										
										
										
											2017-08-13 04:21:26 +05:30
										 |  |  |       described_class.cleanup_cache | 
					
						
							|  |  |  |       expect(foo).not_to exist | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "does not clean up VCS checkout directories with modified time >= prune time" do | 
					
						
							|  |  |  |       foo = (HOMEBREW_CACHE/"--foo") | 
					
						
							|  |  |  |       foo.mkpath | 
					
						
							|  |  |  |       allow(ARGV).to receive(:value).with("prune").and_return("1") | 
					
						
							|  |  |  |       described_class.cleanup_cache | 
					
						
							|  |  |  |       expect(foo).to exist | 
					
						
							| 
									
										
										
										
											2017-08-05 19:36:35 +05:30
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "cleans old files in HOMEBREW_CACHE" do | 
					
						
							| 
									
										
										
										
											2017-08-08 00:25:01 +05:30
										 |  |  |       let(:bottle) { (HOMEBREW_CACHE/"testball-0.0.1.bottle.tar.gz") } | 
					
						
							|  |  |  |       let(:testball) { (HOMEBREW_CACHE/"testball-0.0.1") } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-05 06:06:33 +05:30
										 |  |  |       before(:each) do | 
					
						
							| 
									
										
										
										
											2017-08-08 00:25:01 +05:30
										 |  |  |         FileUtils.touch(bottle) | 
					
						
							|  |  |  |         FileUtils.touch(testball) | 
					
						
							| 
									
										
										
										
											2017-08-05 06:06:33 +05:30
										 |  |  |         (HOMEBREW_CELLAR/"testball"/"0.0.1").mkpath | 
					
						
							|  |  |  |         FileUtils.touch(CoreTap.instance.formula_dir/"testball.rb") | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "cleans up file if outdated" do | 
					
						
							|  |  |  |         allow(Utils::Bottles).to receive(:file_outdated?).with(any_args).and_return(true) | 
					
						
							|  |  |  |         described_class.cleanup_cache | 
					
						
							| 
									
										
										
										
											2017-08-08 00:25:01 +05:30
										 |  |  |         expect(bottle).not_to exist | 
					
						
							|  |  |  |         expect(testball).not_to exist | 
					
						
							| 
									
										
										
										
											2017-08-05 06:06:33 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "cleans up file if ARGV has -s and formula not installed" do | 
					
						
							|  |  |  |         ARGV << "-s" | 
					
						
							|  |  |  |         described_class.cleanup_cache | 
					
						
							| 
									
										
										
										
											2017-08-08 00:25:01 +05:30
										 |  |  |         expect(bottle).not_to exist | 
					
						
							|  |  |  |         expect(testball).not_to exist | 
					
						
							| 
									
										
										
										
											2017-08-05 06:06:33 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "cleans up file if stale" do | 
					
						
							| 
									
										
										
										
											2017-08-13 04:21:26 +05:30
										 |  |  |         described_class.cleanup_cache | 
					
						
							| 
									
										
										
										
											2017-08-08 00:25:01 +05:30
										 |  |  |         expect(bottle).not_to exist | 
					
						
							|  |  |  |         expect(testball).not_to exist | 
					
						
							| 
									
										
										
										
											2017-07-19 02:46:45 +05:30
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-02-27 17:01:22 +01:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2017-08-05 19:36:35 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-13 04:21:26 +05:30
										 |  |  |   describe "::prune?" do | 
					
						
							|  |  |  |     before do | 
					
						
							|  |  |  |       foo.mkpath | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-26 02:01:45 +05:30
										 |  |  |     let(:foo) { HOMEBREW_CACHE/"foo" } | 
					
						
							| 
									
										
										
										
											2017-08-13 04:21:26 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     it "returns true when path_modified_time < days_default" do | 
					
						
							| 
									
										
										
										
											2017-08-26 02:01:45 +05:30
										 |  |  |       allow_any_instance_of(Pathname).to receive(:mtime).and_return(Time.now - sec_in_a_day * 2) | 
					
						
							| 
									
										
										
										
											2017-08-05 19:36:35 +05:30
										 |  |  |       expect(described_class.prune?(foo, days_default: "1")).to be_truthy | 
					
						
							| 
									
										
										
										
											2017-08-13 04:21:26 +05:30
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-19 17:17:38 +05:30
										 |  |  |     it "returns false when path_modified_time >= days_default" do | 
					
						
							| 
									
										
										
										
											2017-08-13 04:21:26 +05:30
										 |  |  |       expect(described_class.prune?(foo, days_default: "2")).to be_falsey | 
					
						
							| 
									
										
										
										
											2017-08-05 19:36:35 +05:30
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2017-02-27 17:01:22 +01:00
										 |  |  | end |