| 
									
										
										
										
											2017-03-05 19:26:56 +01:00
										 |  |  | describe Hbc::CLI::Cleanup, :cask do | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |   let(:cache_location) { Pathname.new(Dir.mktmpdir).realpath } | 
					
						
							|  |  |  |   let(:cleanup_outdated) { false } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   subject { described_class.new(cache_location, cleanup_outdated) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   after do | 
					
						
							|  |  |  |     cache_location.rmtree | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-23 06:13:03 +02:00
										 |  |  |   describe "cleanup" do | 
					
						
							|  |  |  |     it "removes cached downloads of given casks" do | 
					
						
							| 
									
										
										
										
											2016-09-20 15:11:33 +02:00
										 |  |  |       cleaned_up_cached_download = "caffeine" | 
					
						
							| 
									
										
										
										
											2016-08-23 06:13:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       cached_downloads = [ | 
					
						
							| 
									
										
										
										
											2016-10-14 20:33:16 +02:00
										 |  |  |         cache_location.join("#{cleaned_up_cached_download}--latest.zip"), | 
					
						
							|  |  |  |         cache_location.join("transmission--2.61.dmg"), | 
					
						
							|  |  |  |       ] | 
					
						
							| 
									
										
										
										
											2016-08-23 06:13:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       cached_downloads.each(&FileUtils.method(:touch)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-07 18:02:31 +01:00
										 |  |  |       cleanup_size = cached_downloads[0].disk_usage | 
					
						
							| 
									
										
										
										
											2016-08-23 06:13:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect { | 
					
						
							|  |  |  |         subject.cleanup(cleaned_up_cached_download) | 
					
						
							|  |  |  |       }.to output(<<-EOS.undent).to_stdout
 | 
					
						
							|  |  |  |         ==> Removing cached downloads for #{cleaned_up_cached_download} | 
					
						
							|  |  |  |         #{cached_downloads[0]} | 
					
						
							|  |  |  |         ==> This operation has freed approximately #{disk_usage_readable(cleanup_size)} of disk space. | 
					
						
							|  |  |  |       EOS | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(cached_downloads[0].exist?).to eq(false) | 
					
						
							|  |  |  |       expect(cached_downloads[1].exist?).to eq(true) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |   describe "cleanup!" do | 
					
						
							|  |  |  |     it "removes cached downloads" do | 
					
						
							|  |  |  |       cached_download = cache_location.join("SomeDownload.dmg") | 
					
						
							|  |  |  |       FileUtils.touch(cached_download) | 
					
						
							|  |  |  |       cleanup_size = subject.disk_cleanup_size | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect { | 
					
						
							|  |  |  |         subject.cleanup! | 
					
						
							| 
									
										
										
										
											2016-08-23 06:13:03 +02:00
										 |  |  |       }.to output(<<-EOS.undent).to_stdout
 | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |         ==> Removing cached downloads | 
					
						
							|  |  |  |         #{cached_download} | 
					
						
							|  |  |  |         ==> This operation has freed approximately #{disk_usage_readable(cleanup_size)} of disk space. | 
					
						
							| 
									
										
										
										
											2016-08-23 06:13:03 +02:00
										 |  |  |       EOS | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(cached_download.exist?).to eq(false) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-11 14:32:32 +01:00
										 |  |  |     # TODO: uncomment when unflaky. | 
					
						
							|  |  |  |     # it "does not removed locked files" do | 
					
						
							|  |  |  |     #   cached_download = cache_location.join("SomeDownload.dmg") | 
					
						
							|  |  |  |     #   FileUtils.touch(cached_download) | 
					
						
							|  |  |  |     #   cleanup_size = subject.disk_cleanup_size | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     #   File.new(cached_download).flock(File::LOCK_EX) | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     #   expect(Hbc::Utils).to be_file_locked(cached_download) | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     #   expect { | 
					
						
							|  |  |  |     #     subject.cleanup! | 
					
						
							|  |  |  |     #   }.to output(<<-EOS.undent).to_stdout | 
					
						
							|  |  |  |     #     ==> Removing cached downloads | 
					
						
							|  |  |  |     #     skipping: #{cached_download} is locked | 
					
						
							|  |  |  |     #     ==> This operation has freed approximately #{disk_usage_readable(cleanup_size)} of disk space. | 
					
						
							|  |  |  |     #   EOS | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     #   expect(cached_download.exist?).to eq(true) | 
					
						
							|  |  |  |     # end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     context "when cleanup_outdated is specified" do | 
					
						
							|  |  |  |       let(:cleanup_outdated) { true } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "does not remove cache files newer than 10 days old" do | 
					
						
							|  |  |  |         cached_download = cache_location.join("SomeNewDownload.dmg") | 
					
						
							|  |  |  |         FileUtils.touch(cached_download) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect { | 
					
						
							|  |  |  |           subject.cleanup! | 
					
						
							| 
									
										
										
										
											2016-08-23 06:13:03 +02:00
										 |  |  |         }.to output(<<-EOS.undent).to_stdout
 | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |           ==> Removing cached downloads older than 10 days old | 
					
						
							|  |  |  |           Nothing to do | 
					
						
							| 
									
										
										
										
											2016-08-23 06:13:03 +02:00
										 |  |  |         EOS | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(cached_download.exist?).to eq(true) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |