brew/Library/Homebrew/test/cask/cli/cleanup_spec.rb

80 lines
2.3 KiB
Ruby
Raw Normal View History

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 }
2017-05-19 20:27:25 +02:00
let(:outdated_only) { false }
2016-08-18 22:11:42 +03:00
2017-05-21 02:32:46 +02:00
subject { described_class.new(*cask_tokens, cache_location: cache_location) }
before(:each) do
allow_any_instance_of(described_class).to receive(:outdated_only?).and_return(outdated_only)
end
2016-08-18 22:11:42 +03:00
after do
cache_location.rmtree
end
2016-08-23 06:13:03 +02:00
describe "cleanup" do
2017-05-19 20:27:25 +02:00
let(:cask_token) { "caffeine" }
let(:cask_tokens) { [cask_token] }
2016-08-23 06:13:03 +02:00
2017-05-19 20:27:25 +02:00
it "removes cached downloads of given casks" do
2016-08-23 06:13:03 +02:00
cached_downloads = [
2017-05-19 20:27:25 +02:00
cache_location.join("#{cask_token}--latest.zip"),
2016-10-14 20:33:16 +02:00
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 {
2017-05-19 20:27:25 +02:00
subject.run
2016-08-23 06:13:03 +02:00
}.to output(<<-EOS.undent).to_stdout
2017-05-19 20:27:25 +02:00
==> Removing cached downloads for #{cask_token}
2016-08-23 06:13:03 +02:00
#{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
2016-08-18 22:11:42 +03:00
2017-05-19 20:27:25 +02:00
context "when no argument is given" do
let(:cask_tokens) { [] }
2016-08-18 22:11:42 +03:00
2017-05-19 20:27:25 +02:00
it "removes all cached downloads" do
cached_download = cache_location.join("SomeDownload.dmg")
2016-08-18 22:11:42 +03:00
FileUtils.touch(cached_download)
2017-05-19 20:27:25 +02:00
cleanup_size = subject.disk_cleanup_size
2016-08-18 22:11:42 +03:00
expect {
2017-05-19 20:27:25 +02:00
subject.run
2016-08-23 06:13:03 +02:00
}.to output(<<-EOS.undent).to_stdout
2017-05-19 20:27:25 +02: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
2017-05-19 20:27:25 +02:00
expect(cached_download.exist?).to eq(false)
end
context "and :outdated_only is specified" do
let(:outdated_only) { 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.run
}.to output(<<-EOS.undent).to_stdout
==> Removing cached downloads older than 10 days old
Nothing to do
EOS
expect(cached_download.exist?).to eq(true)
end
2016-08-18 22:11:42 +03:00
end
end
end
end