brew/Library/Homebrew/test/lock_file_spec.rb

58 lines
1.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-05-22 03:23:50 +02:00
require "lock_file"
2017-02-20 22:43:09 +01:00
RSpec.describe LockFile do
subject(:lock_file) { described_class.new(:lock, Pathname("foo")) }
2017-02-20 22:43:09 +01:00
2024-11-23 23:45:17 +01:00
let(:lock_file_copy) { described_class.new(:lock, Pathname("foo")) }
2017-02-20 22:43:09 +01:00
describe "#lock" do
2024-11-23 23:45:17 +01:00
it "ensures the lock file is created" do
expect(lock_file.path).not_to exist
lock_file.lock
expect(lock_file.path).to exist
end
it "does not raise an error when the same instance is locked multiple times" do
lock_file.lock
2017-02-20 22:43:09 +01:00
expect { lock_file.lock }.not_to raise_error
2017-02-20 22:43:09 +01:00
end
2024-11-23 23:45:17 +01:00
it "raises an error if another instance is already locked" do
lock_file.lock
2017-02-20 22:43:09 +01:00
expect do
2024-11-23 23:45:17 +01:00
lock_file_copy.lock
end.to raise_error(OperationInProgressError)
2017-02-20 22:43:09 +01:00
end
end
describe "#unlock" do
it "does not raise an error when already unlocked" do
expect { lock_file.unlock }.not_to raise_error
2017-02-20 22:43:09 +01:00
end
2017-05-22 03:23:50 +02:00
it "unlocks when locked" do
lock_file.lock
lock_file.unlock
2017-02-20 22:43:09 +01:00
2024-11-23 23:45:17 +01:00
expect { lock_file_copy.lock }.not_to raise_error
end
it "allows deleting a lock file only by the instance that locked it" do
lock_file.lock
expect(lock_file.path).to exist
expect(lock_file_copy.path).to exist
lock_file_copy.unlock(unlink: true)
expect(lock_file_copy.path).to exist
expect(lock_file.path).to exist
lock_file.unlock(unlink: true)
expect(lock_file.path).not_to exist
2017-02-20 22:43:09 +01:00
end
end
end