Merge pull request #9478 from caskroom/rename-cleanup-cli-option

cli: [refactor] rename outdated option internally
This commit is contained in:
Roland Walker 2015-02-07 19:12:13 -05:00
commit add0279512
7 changed files with 117 additions and 58 deletions

View File

@ -194,7 +194,7 @@ class Hbc::CLI
Hbc.verbose = true
end
opts.on("--outdated") do |v|
Hbc.outdated = true
Hbc.cleanup_outdated = true
end
end
end

View File

@ -1,24 +1,41 @@
class Hbc::CLI::Cleanup < Hbc::CLI::Base
OUTDATED_DAYS = 10
OUTDATED_TIMESTAMP = Time.now - (60 * 60 * 24 * OUTDATED_DAYS)
def self.help
"cleans up cached downloads and tracker symlinks"
end
def self.run(*_ignored)
default.cleanup!
end
def self.default
new(HOMEBREW_CACHE_CASKS, Hbc.cleanup_outdated)
end
attr_reader :cache_location, :cleanup_outdated
def initialize(cache_location, cleanup_outdated)
@cache_location = Pathname(cache_location)
@cleanup_outdated = cleanup_outdated
end
def cleanup!
remove_dead_symlinks
remove_all_cache_files
end
def self.cache_symlinks
HOMEBREW_CACHE_CASKS.children.select(&:symlink?)
def cache_symlinks
cache_location.children.select(&:symlink?)
end
def self.dead_symlinks
def dead_symlinks
cache_symlinks.reject(&:exist?)
end
def self.cache_incompletes(outdated=nil)
def cache_incompletes(outdated=nil)
cache_symlinks.collect do |symlink|
incomplete_file = Dir.chdir HOMEBREW_CACHE_CASKS do
incomplete_file = Dir.chdir cache_location do
f = symlink.readlink
f = f.realpath if f.exist?
Pathname.new(f.to_s.concat('.incomplete'))
@ -29,9 +46,9 @@ class Hbc::CLI::Cleanup < Hbc::CLI::Base
end.compact
end
def self.cache_completes(outdated=nil)
def cache_completes(outdated=nil)
cache_symlinks.collect do |symlink|
file = Dir.chdir HOMEBREW_CACHE_CASKS do
file = Dir.chdir cache_location do
f = symlink.readlink
f.exist? ? f.realpath : f
end
@ -45,16 +62,16 @@ class Hbc::CLI::Cleanup < Hbc::CLI::Base
end
# will include dead symlinks if they aren't handled separately
def self.all_cache_files(outdated=nil)
def all_cache_files(outdated=nil)
cache_incompletes(outdated) + cache_completes(outdated)
end
def self.space_in_megs(files)
def space_in_megs(files)
bytes = files.map { |f| begin File.size(f); rescue; 0; end }.reduce(&:+) || 0
sprintf '%0.2f', bytes / (1024.0 * 1024.0)
end
def self.remove_dead_symlinks
def remove_dead_symlinks
ohai "Removing dead symlinks"
to_delete = dead_symlinks
puts "Nothing to do" unless to_delete.count > 0
@ -64,19 +81,15 @@ class Hbc::CLI::Cleanup < Hbc::CLI::Base
end
end
def self.remove_all_cache_files
def remove_all_cache_files
message = "Removing cached downloads"
message.concat " older than #{OUTDATED_DAYS} days old" if Hbc.outdated
message.concat " older than #{OUTDATED_DAYS} days old" if cleanup_outdated
ohai message
to_delete = all_cache_files(Hbc.outdated)
to_delete = all_cache_files(cleanup_outdated)
puts "Nothing to do" unless to_delete.count > 0
to_delete.each do |item|
puts item
item.unlink
end
end
def self.help
"cleans up cached downloads and tracker symlinks"
end
end

View File

@ -217,9 +217,9 @@ class Hbc::CLI::Doctor < Hbc::CLI::Base
end
def self.render_cached_downloads
files = Hbc::CLI::Cleanup.all_cache_files
files = Hbc::CLI::Cleanup.default.all_cache_files
count = files.count
space = Hbc::CLI::Cleanup.space_in_megs files
space = Hbc::CLI::Cleanup.default.space_in_megs files
[
HOMEBREW_CACHE,
HOMEBREW_CACHE_CASKS,

View File

@ -28,12 +28,12 @@ module Hbc::Options
@verbose = _verbose
end
def outdated
@outdated ||= false
def cleanup_outdated
@cleanup_outdated ||= false
end
def outdated=(_outdated)
@outdated = _outdated
def cleanup_outdated=(_cleanup_outdated)
@cleanup_outdated = _cleanup_outdated
end
end
end

View File

@ -0,0 +1,74 @@
require 'spec_helper'
describe Hbc::CLI::Cleanup do
let(:homebrew_cache_location) { Pathname(Dir.mktmpdir).realpath }
let(:cache_location) { homebrew_cache_location.join('Casks').tap(&:mkdir) }
let(:cleanup_outdated) { false }
subject { described_class.new(cache_location, cleanup_outdated) }
after { homebrew_cache_location.rmtree }
describe 'cleanup!' do
it 'removes dead symlinks' do
bad_symlink = cache_location.join('bad_symlink')
bad_symlink.make_symlink('../does_not_exist')
expect {
subject.cleanup!
}.to output(<<-OUTPUT.undent).to_stdout
==> Removing dead symlinks
#{bad_symlink}
==> Removing cached downloads
Nothing to do
OUTPUT
expect(bad_symlink.symlink?).to eq(false)
end
it 'removes cached downloads' do
cached_download = homebrew_cache_location.join('SomeDownload.dmg')
FileUtils.touch(cached_download)
cached_download_symlink = cache_location.join('SomeDownload.dmg')
cached_download_symlink.make_symlink(cached_download)
expect {
subject.cleanup!
}.to output(<<-OUTPUT.undent).to_stdout
==> Removing dead symlinks
Nothing to do
==> Removing cached downloads
#{cached_download}
#{cached_download_symlink}
OUTPUT
expect(cached_download.exist?).to eq(false)
expect(cached_download_symlink.symlink?).to eq(false)
end
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 = homebrew_cache_location.join('SomeNewDownload.dmg')
FileUtils.touch(cached_download)
cached_download_symlink = cache_location.join('SomeNewDownload.dmg')
cached_download_symlink.make_symlink(cached_download)
expect {
subject.cleanup!
}.to output(<<-OUTPUT.undent).to_stdout
==> Removing dead symlinks
Nothing to do
==> Removing cached downloads older than 10 days old
Nothing to do
OUTPUT
expect(cached_download.exist?).to eq(true)
expect(cached_download_symlink.symlink?).to eq(true)
end
end
end
end

View File

@ -1,18 +1,16 @@
require 'test_helper'
require 'spec_helper'
require 'hbc/version'
describe Hbc::CLI::Doctor do
it 'displays some nice info about the environment' do
out, err = capture_io do
expect {
Hbc::CLI::Doctor.run
end
# no point in trying to match more of this environment-specific info
out.must_match /\A==> OS X Release:/
}.to output(/\A==> OS X Release:/).to_stdout
end
it "raises an exception when arguments are given" do
lambda {
expect {
Hbc::CLI::Doctor.run('argument')
}.must_raise ArgumentError
}.to raise_error(ArgumentError)
end
end

View File

@ -1,26 +0,0 @@
require 'test_helper'
describe Hbc::CLI::Cleanup do
it 'does nothing with --outdated in the clean test environment' do
Hbc.outdated = true
out, err = capture_io do
Hbc::CLI::Cleanup.run
end
out.must_equal <<-OUTPUT.undent
==> Removing dead symlinks
Nothing to do
==> Removing cached downloads older than 10 days old
Nothing to do
OUTPUT
end
# note: this test will fail in isolation. It depends on other
# portions of the test suite leaving some files to do cleanup.
it 'cleans up new files in the test environment' do
Hbc.outdated = false
out, err = capture_io do
Hbc::CLI::Cleanup.run
end
out.must_match(/^==> Removing dead symlinks\nNothing to do\n==> Removing cached downloads\n/)
end
end