Skip to content

Commit

Permalink
forbid running fswatch in non-MacOS environment
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatiusreza committed Feb 7, 2019
1 parent 4e9ae76 commit 5e0d0af
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/docker-sync/dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ def self.ensure_all_for_mac!(config)
Fswatch.ensure! if config.fswatch_required?
end

def self.ensure_all_for_linux!(_config)
def self.ensure_all_for_linux!(config)
Docker.ensure!
Fswatch.forbid! if config.fswatch_required?
end

def self.ensure_all_for_freebsd!(config)
Docker.ensure!
Unison.ensure! if config.unison_required?
Rsync.ensure! if config.rsync_required?
Fswatch.forbid! if config.fswatch_required?
end
end
end
10 changes: 8 additions & 2 deletions lib/docker-sync/dependencies/fswatch.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module DockerSync
module Dependencies
module Fswatch
UNSUPPORTED = 'Fswatch is not expected to run on platforms other then MacOS'

def self.available?
raise 'Fswatch cannot be available for other platforms then MacOS' unless Environment.mac?
forbid! unless Environment.mac?
return @available if defined? @available
@available = find_executable0('fswatch')
end
Expand All @@ -13,7 +15,11 @@ def self.ensure!
PackageManager.install_package('fswatch')
puts "please restart docker sync so the installation of fswatch takes effect"
exit(1)
end
end

def self.forbid!
raise UNSUPPORTED
end
end
end
end
53 changes: 50 additions & 3 deletions spec/lib/docker-sync/dependencies_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

RSpec.describe DockerSync::Dependencies do
let(:config) { double(:config, unison_required?: false, rsync_required?: false, fswatch_required?: false) }
let(:linux?) { false }
let(:mac?) { false }
let(:linux?) { false }
let(:freebsd?) { false }
let(:mac?) { false }

before do
allow(DockerSync::Environment).to receive(:linux?).and_return(linux?)
allow(DockerSync::Environment).to receive(:freebsd?).and_return(freebsd?)
allow(DockerSync::Environment).to receive(:mac?).and_return(mac?)
end

describe '.ensure_all!(config)' do
before do
allow(described_class).to receive(:ensure_all_for_mac!)
allow(described_class).to receive(:ensure_all_for_freebsd!)
allow(described_class).to receive(:ensure_all_for_linux!)
end

Expand All @@ -30,12 +33,21 @@
context 'when running on Linux' do
let(:linux?) { true }

it 'delegates to `ensure_all_for_mac!` with given config' do
it 'delegates to `ensure_all_for_linux!` with given config' do
subject
expect(described_class).to have_received(:ensure_all_for_linux!).with(config)
end
end

context 'when running on FreeBSD' do
let(:freebsd?) { true }

it 'delegates to `ensure_all_for_freebsd!` with given config' do
subject
expect(described_class).to have_received(:ensure_all_for_freebsd!).with(config)
end
end

context 'when running on another OS' do
it 'raises an error' do
expect { subject }.to raise_error(RuntimeError)
Expand All @@ -56,6 +68,41 @@
subject
expect(described_class::Docker).to have_received(:ensure!)
end

context "when FSWatch is required by given `config`" do
before do
allow(config).to receive(:fswatch_required?).and_return(true)
end

it 'is forbidden' do
expect { subject }.to raise_error(DockerSync::Dependencies::Fswatch::UNSUPPORTED)
end
end
end

describe '.ensure_all_for_freebsd!(_config)' do
let(:freebsd?) { true }

before do
allow(described_class::Docker).to receive(:ensure!)
end

subject { described_class.ensure_all_for_freebsd!(config) }

it 'ensures that Docker is available' do
subject
expect(described_class::Docker).to have_received(:ensure!)
end

context "when FSWatch is required by given `config`" do
before do
allow(config).to receive(:fswatch_required?).and_return(true)
end

it 'is forbidden' do
expect { subject }.to raise_error(DockerSync::Dependencies::Fswatch::UNSUPPORTED)
end
end
end

describe '.ensure_all_for_mac!(config)' do
Expand Down

0 comments on commit 5e0d0af

Please sign in to comment.