Refactor CLI::Cleanup.
This commit is contained in:
parent
58db95c1d2
commit
b7347dcc44
@ -13,29 +13,19 @@ module Hbc
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.run(*args)
|
def self.run(*args)
|
||||||
if args.empty?
|
new(*args).run
|
||||||
default.cleanup!
|
|
||||||
else
|
|
||||||
default.cleanup(args)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.default
|
|
||||||
@default ||= new(Hbc.cache, CLI.outdated?)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :cache_location, :outdated_only
|
attr_reader :cache_location, :outdated_only
|
||||||
def initialize(cache_location, outdated_only)
|
|
||||||
|
def initialize(*args, cache_location: Hbc.cache, outdated_only: CLI.outdated?)
|
||||||
|
@args = args
|
||||||
@cache_location = Pathname.new(cache_location)
|
@cache_location = Pathname.new(cache_location)
|
||||||
@outdated_only = outdated_only
|
@outdated_only = outdated_only
|
||||||
end
|
end
|
||||||
|
|
||||||
def cleanup!
|
def run
|
||||||
remove_cache_files
|
remove_cache_files(*@args)
|
||||||
end
|
|
||||||
|
|
||||||
def cleanup(tokens)
|
|
||||||
remove_cache_files(*tokens)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def cache_files
|
def cache_files
|
||||||
|
|||||||
@ -107,7 +107,7 @@ module Hbc
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.render_cached_downloads
|
def self.render_cached_downloads
|
||||||
cleanup = CLI::Cleanup.default
|
cleanup = CLI::Cleanup.new
|
||||||
count = cleanup.cache_files.count
|
count = cleanup.cache_files.count
|
||||||
size = cleanup.disk_cleanup_size
|
size = cleanup.disk_cleanup_size
|
||||||
msg = user_tilde(Hbc.cache.to_s)
|
msg = user_tilde(Hbc.cache.to_s)
|
||||||
|
|||||||
@ -1,19 +1,20 @@
|
|||||||
describe Hbc::CLI::Cleanup, :cask do
|
describe Hbc::CLI::Cleanup, :cask do
|
||||||
let(:cache_location) { Pathname.new(Dir.mktmpdir).realpath }
|
let(:cache_location) { Pathname.new(Dir.mktmpdir).realpath }
|
||||||
let(:cleanup_outdated) { false }
|
let(:outdated_only) { false }
|
||||||
|
|
||||||
subject { described_class.new(cache_location, cleanup_outdated) }
|
subject { described_class.new(*cask_tokens, cache_location: cache_location, outdated_only: outdated_only) }
|
||||||
|
|
||||||
after do
|
after do
|
||||||
cache_location.rmtree
|
cache_location.rmtree
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "cleanup" do
|
describe "cleanup" do
|
||||||
it "removes cached downloads of given casks" do
|
let(:cask_token) { "caffeine" }
|
||||||
cleaned_up_cached_download = "caffeine"
|
let(:cask_tokens) { [cask_token] }
|
||||||
|
|
||||||
|
it "removes cached downloads of given casks" do
|
||||||
cached_downloads = [
|
cached_downloads = [
|
||||||
cache_location.join("#{cleaned_up_cached_download}--latest.zip"),
|
cache_location.join("#{cask_token}--latest.zip"),
|
||||||
cache_location.join("transmission--2.61.dmg"),
|
cache_location.join("transmission--2.61.dmg"),
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -22,9 +23,9 @@ describe Hbc::CLI::Cleanup, :cask do
|
|||||||
cleanup_size = cached_downloads[0].disk_usage
|
cleanup_size = cached_downloads[0].disk_usage
|
||||||
|
|
||||||
expect {
|
expect {
|
||||||
subject.cleanup(cleaned_up_cached_download)
|
subject.run
|
||||||
}.to output(<<-EOS.undent).to_stdout
|
}.to output(<<-EOS.undent).to_stdout
|
||||||
==> Removing cached downloads for #{cleaned_up_cached_download}
|
==> Removing cached downloads for #{cask_token}
|
||||||
#{cached_downloads[0]}
|
#{cached_downloads[0]}
|
||||||
==> This operation has freed approximately #{disk_usage_readable(cleanup_size)} of disk space.
|
==> This operation has freed approximately #{disk_usage_readable(cleanup_size)} of disk space.
|
||||||
EOS
|
EOS
|
||||||
@ -32,16 +33,17 @@ describe Hbc::CLI::Cleanup, :cask do
|
|||||||
expect(cached_downloads[0].exist?).to eq(false)
|
expect(cached_downloads[0].exist?).to eq(false)
|
||||||
expect(cached_downloads[1].exist?).to eq(true)
|
expect(cached_downloads[1].exist?).to eq(true)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
describe "cleanup!" do
|
context "when no argument is given" do
|
||||||
it "removes cached downloads" do
|
let(:cask_tokens) { [] }
|
||||||
|
|
||||||
|
it "removes all cached downloads" do
|
||||||
cached_download = cache_location.join("SomeDownload.dmg")
|
cached_download = cache_location.join("SomeDownload.dmg")
|
||||||
FileUtils.touch(cached_download)
|
FileUtils.touch(cached_download)
|
||||||
cleanup_size = subject.disk_cleanup_size
|
cleanup_size = subject.disk_cleanup_size
|
||||||
|
|
||||||
expect {
|
expect {
|
||||||
subject.cleanup!
|
subject.run
|
||||||
}.to output(<<-EOS.undent).to_stdout
|
}.to output(<<-EOS.undent).to_stdout
|
||||||
==> Removing cached downloads
|
==> Removing cached downloads
|
||||||
#{cached_download}
|
#{cached_download}
|
||||||
@ -51,36 +53,15 @@ describe Hbc::CLI::Cleanup, :cask do
|
|||||||
expect(cached_download.exist?).to eq(false)
|
expect(cached_download.exist?).to eq(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: uncomment when unflaky.
|
context "and :outdated_only is specified" do
|
||||||
# it "does not removed locked files" do
|
let(:outdated_only) { true }
|
||||||
# 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
|
|
||||||
|
|
||||||
context "when cleanup_outdated is specified" do
|
|
||||||
let(:cleanup_outdated) { true }
|
|
||||||
|
|
||||||
it "does not remove cache files newer than 10 days old" do
|
it "does not remove cache files newer than 10 days old" do
|
||||||
cached_download = cache_location.join("SomeNewDownload.dmg")
|
cached_download = cache_location.join("SomeNewDownload.dmg")
|
||||||
FileUtils.touch(cached_download)
|
FileUtils.touch(cached_download)
|
||||||
|
|
||||||
expect {
|
expect {
|
||||||
subject.cleanup!
|
subject.run
|
||||||
}.to output(<<-EOS.undent).to_stdout
|
}.to output(<<-EOS.undent).to_stdout
|
||||||
==> Removing cached downloads older than 10 days old
|
==> Removing cached downloads older than 10 days old
|
||||||
Nothing to do
|
Nothing to do
|
||||||
@ -91,3 +72,4 @@ describe Hbc::CLI::Cleanup, :cask do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user