diff --git a/Library/Homebrew/cleanup.rb b/Library/Homebrew/cleanup.rb index 960bcb8b7b..0b4d97d59e 100644 --- a/Library/Homebrew/cleanup.rb +++ b/Library/Homebrew/cleanup.rb @@ -122,7 +122,7 @@ module Homebrew lockfiles = candidates.select(&:file?) lockfiles.each do |file| next unless file.readable? - file.open.flock(File::LOCK_EX | File::LOCK_NB) && file.unlink + file.open(File::RDWR).flock(File::LOCK_EX | File::LOCK_NB) && file.unlink end end diff --git a/Library/Homebrew/test/cleanup_spec.rb b/Library/Homebrew/test/cleanup_spec.rb index da262c5ca0..f7b79a0b43 100644 --- a/Library/Homebrew/test/cleanup_spec.rb +++ b/Library/Homebrew/test/cleanup_spec.rb @@ -5,23 +5,27 @@ require "pathname" describe Homebrew::Cleanup do let(:ds_store) { Pathname.new("#{HOMEBREW_PREFIX}/Library/.DS_Store") } + let(:lock_file) { Pathname.new("#{HOMEBREW_LOCK_DIR}/foo") } let(:sec_in_a_day) { 60 * 60 * 24 } around(:each) do |example| begin FileUtils.touch ds_store + FileUtils.touch lock_file example.run ensure FileUtils.rm_f ds_store + FileUtils.rm_f lock_file end end describe "::cleanup" do - it "removes .DS_Store files" do + it "removes .DS_Store and lock files" do described_class.cleanup expect(ds_store).not_to exist + expect(lock_file).not_to exist end it "doesn't remove anything if `--dry-run` is specified" do @@ -30,6 +34,15 @@ describe Homebrew::Cleanup do described_class.cleanup expect(ds_store).to exist + expect(lock_file).to exist + end + + it "doesn't remove the lock file if it is locked" do + lock_file.open(File::RDWR | File::CREAT).flock(File::LOCK_EX | File::LOCK_NB) + + described_class.cleanup + + expect(lock_file).to exist end context "when it can't remove a keg" do