Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for disk_cleanup and unremovable kegs #2706

Merged
merged 11 commits into from
Aug 28, 2017
8 changes: 4 additions & 4 deletions Library/Homebrew/cleanup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ module Homebrew
module Cleanup
@disk_cleanup_size = 0

class << self
attr_reader :disk_cleanup_size
end

module_function

def cleanup
Expand All @@ -21,10 +25,6 @@ def update_disk_cleanup_size(path_size)
@disk_cleanup_size += path_size
end

def disk_cleanup_size
@disk_cleanup_size
end

def unremovable_kegs
@unremovable_kegs ||= []
end
Expand Down
132 changes: 126 additions & 6 deletions Library/Homebrew/test/cleanup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

describe Homebrew::Cleanup do
let(:ds_store) { Pathname.new("#{HOMEBREW_PREFIX}/Library/.DS_Store") }
let(:sec_in_a_day) { 60 * 60 * 24 }

around(:each) do |example|
begin
Expand Down Expand Up @@ -104,14 +105,30 @@
expect(f4).to be_installed
end

specify "::cleanup_logs" do
path = (HOMEBREW_LOGS/"delete_me")
path.mkpath
ARGV << "--prune=all"
describe "::cleanup_logs" do
let(:path) { (HOMEBREW_LOGS/"delete_me") }

described_class.cleanup_logs
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

expect(path).not_to exist
it "cleans up logs if older than 14 days" do
allow_any_instance_of(Pathname).to receive(:mtime).and_return(Time.now - sec_in_a_day * 15)
described_class.cleanup_logs
expect(path).not_to exist
end

it "does not clean up logs less than 14 days old" do
allow_any_instance_of(Pathname).to receive(:mtime).and_return(Time.now - sec_in_a_day * 2)
described_class.cleanup_logs
expect(path).to exist
end
end

describe "::cleanup_cache" do
Expand All @@ -124,6 +141,15 @@
expect(incomplete).not_to exist
end

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

it "cleans up 'java_cache'" do
java_cache = (HOMEBREW_CACHE/"java_cache")
java_cache.mkpath
Expand All @@ -141,5 +167,99 @@

expect(npm_cache).not_to exist
end

it "cleans up all files and directories" do
git = (HOMEBREW_CACHE/"gist--git")
gist = (HOMEBREW_CACHE/"gist")
svn = (HOMEBREW_CACHE/"gist--svn")

git.mkpath
gist.mkpath
FileUtils.touch svn

allow(ARGV).to receive(:value).with("prune").and_return("all")

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
end

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")
allow_any_instance_of(Pathname).to receive(:mtime).and_return(Time.now - sec_in_a_day * 2)
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
end

context "cleans old files in HOMEBREW_CACHE" do
let(:bottle) { (HOMEBREW_CACHE/"testball-0.0.1.bottle.tar.gz") }
let(:testball) { (HOMEBREW_CACHE/"testball-0.0.1") }

before(:each) do
FileUtils.touch(bottle)
FileUtils.touch(testball)
(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
expect(bottle).not_to exist
expect(testball).not_to exist
end

it "cleans up file if ARGV has -s and formula not installed" do
ARGV << "-s"
described_class.cleanup_cache
expect(bottle).not_to exist
expect(testball).not_to exist
end

it "cleans up file if stale" do
described_class.cleanup_cache
expect(bottle).not_to exist
expect(testball).not_to exist
end
end
end

describe "::prune?" do
before do
foo.mkpath
end

let(:foo) { HOMEBREW_CACHE/"foo" }

it "returns true when path_modified_time < days_default" do
allow_any_instance_of(Pathname).to receive(:mtime).and_return(Time.now - sec_in_a_day * 2)
expect(described_class.prune?(foo, days_default: "1")).to be_truthy
end

it "returns false when path_modified_time >= days_default" do
expect(described_class.prune?(foo, days_default: "2")).to be_falsey
end
end
end