From 060af0a26ab7219e46b500fd1c7f420b6cc74cbb Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 22 May 2017 03:23:50 +0200 Subject: Rename `FormulaLock` to `LockFile`. --- Library/Homebrew/formula.rb | 2 +- Library/Homebrew/formula_lock.rb | 37 ---------------------- Library/Homebrew/keg.rb | 2 +- Library/Homebrew/lock_file.rb | 49 ++++++++++++++++++++++++++++++ Library/Homebrew/migrator.rb | 2 +- Library/Homebrew/test/formula_lock_spec.rb | 34 --------------------- Library/Homebrew/test/lock_file_spec.rb | 34 +++++++++++++++++++++ 7 files changed, 86 insertions(+), 74 deletions(-) delete mode 100644 Library/Homebrew/formula_lock.rb create mode 100644 Library/Homebrew/lock_file.rb delete mode 100644 Library/Homebrew/test/formula_lock_spec.rb create mode 100644 Library/Homebrew/test/lock_file_spec.rb (limited to 'Library') diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 0b3e87bb1..d42332c64 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -1,5 +1,5 @@ require "formula_support" -require "formula_lock" +require "lock_file" require "formula_pin" require "hardware" require "utils/bottles" diff --git a/Library/Homebrew/formula_lock.rb b/Library/Homebrew/formula_lock.rb deleted file mode 100644 index bf747fea2..000000000 --- a/Library/Homebrew/formula_lock.rb +++ /dev/null @@ -1,37 +0,0 @@ -require "fcntl" - -class FormulaLock - def initialize(name) - @name = name - @path = HOMEBREW_LOCK_DIR/"#{@name}.brewing" - @lockfile = nil - end - - def lock - @path.parent.mkpath - create_lockfile - return if @lockfile.flock(File::LOCK_EX | File::LOCK_NB) - raise OperationInProgressError, @name - end - - def unlock - return if @lockfile.nil? || @lockfile.closed? - @lockfile.flock(File::LOCK_UN) - @lockfile.close - end - - def with_lock - lock - yield - ensure - unlock - end - - private - - def create_lockfile - return unless @lockfile.nil? || @lockfile.closed? - @lockfile = @path.open(File::RDWR | File::CREAT) - @lockfile.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) - end -end diff --git a/Library/Homebrew/keg.rb b/Library/Homebrew/keg.rb index 8733def27..07d4dd9cd 100644 --- a/Library/Homebrew/keg.rb +++ b/Library/Homebrew/keg.rb @@ -1,6 +1,6 @@ require "extend/pathname" require "keg_relocate" -require "formula_lock" +require "lock_file" require "ostruct" class Keg diff --git a/Library/Homebrew/lock_file.rb b/Library/Homebrew/lock_file.rb new file mode 100644 index 000000000..83743b744 --- /dev/null +++ b/Library/Homebrew/lock_file.rb @@ -0,0 +1,49 @@ +require "fcntl" + +class LockFile + def initialize(name) + @name = name + @path = HOMEBREW_LOCK_DIR/"#{@name}.lock" + @lockfile = nil + end + + def lock + @path.parent.mkpath + create_lockfile + return if @lockfile.flock(File::LOCK_EX | File::LOCK_NB) + raise OperationInProgressError, @name + end + + def unlock + return if @lockfile.nil? || @lockfile.closed? + @lockfile.flock(File::LOCK_UN) + @lockfile.close + end + + def with_lock + lock + yield + ensure + unlock + end + + private + + def create_lockfile + return unless @lockfile.nil? || @lockfile.closed? + @lockfile = @path.open(File::RDWR | File::CREAT) + @lockfile.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) + end +end + +class FormulaLock < LockFile + def initialize(name) + super("#{name}.formula") + end +end + +class CaskLock < LockFile + def initialize(name) + super("#{name}.cask") + end +end diff --git a/Library/Homebrew/migrator.rb b/Library/Homebrew/migrator.rb index 3cb6c5178..f975962cf 100644 --- a/Library/Homebrew/migrator.rb +++ b/Library/Homebrew/migrator.rb @@ -1,5 +1,5 @@ require "formula" -require "formula_lock" +require "lock_file" require "keg" require "tab" diff --git a/Library/Homebrew/test/formula_lock_spec.rb b/Library/Homebrew/test/formula_lock_spec.rb deleted file mode 100644 index 9b5ece813..000000000 --- a/Library/Homebrew/test/formula_lock_spec.rb +++ /dev/null @@ -1,34 +0,0 @@ -require "formula_lock" - -describe FormulaLock do - subject { described_class.new("foo") } - - describe "#lock" do - it "does not raise an error when already locked" do - subject.lock - - expect { subject.lock }.not_to raise_error - end - - it "raises an error if a lock already exists" do - subject.lock - - expect { - described_class.new("foo").lock - }.to raise_error(OperationInProgressError) - end - end - - describe "#unlock" do - it "does not raise an error when already unlocked" do - expect { subject.unlock }.not_to raise_error - end - - it "unlocks a locked Formula" do - subject.lock - subject.unlock - - expect { described_class.new("foo").lock }.not_to raise_error - end - end -end diff --git a/Library/Homebrew/test/lock_file_spec.rb b/Library/Homebrew/test/lock_file_spec.rb new file mode 100644 index 000000000..82c47a70a --- /dev/null +++ b/Library/Homebrew/test/lock_file_spec.rb @@ -0,0 +1,34 @@ +require "lock_file" + +describe LockFile do + subject { described_class.new("foo") } + + describe "#lock" do + it "does not raise an error when already locked" do + subject.lock + + expect { subject.lock }.not_to raise_error + end + + it "raises an error if a lock already exists" do + subject.lock + + expect { + described_class.new("foo").lock + }.to raise_error(OperationInProgressError) + end + end + + describe "#unlock" do + it "does not raise an error when already unlocked" do + expect { subject.unlock }.not_to raise_error + end + + it "unlocks when locked" do + subject.lock + subject.unlock + + expect { described_class.new("foo").lock }.not_to raise_error + end + end +end -- cgit v1.2.3