Skip to content

Commit

Permalink
encapsulate system call inside a helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatiusreza committed Feb 7, 2019
1 parent c5eec22 commit c93505d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 46 deletions.
4 changes: 2 additions & 2 deletions lib/docker-sync/dependencies/docker_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ module Driver
def self.docker_for_mac?
return false unless Environment.mac?
return @docker_for_mac if defined? @docker_for_mac
@docker_for_mac = system('pgrep -q com.docker.hyperkit')
@docker_for_mac = Environment.system('pgrep -q com.docker.hyperkit')
end

def self.docker_toolbox?
return false unless Environment.mac? || Environment.freebsd?
return false unless find_executable0('docker-machine')
return @docker_toolbox if defined? @docker_toolbox
@docker_toolbox = system('docker info | grep -q "Operating System: Boot2Docker"')
@docker_toolbox = Environment.system('docker info | grep -q "Operating System: Boot2Docker"')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/docker-sync/dependencies/package_managers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def ask_user_confirmation
end

def perform_installation
defined?(Bundler) ? Bundler.clean_system(install_cmd) : system(install_cmd)
Environment.system(install_cmd)
end

def install_cmd
Expand Down
4 changes: 2 additions & 2 deletions lib/docker-sync/dependencies/unox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.available?

return @available if defined? @available
cmd = 'brew list unox 2>&1 > /dev/null'
@available = defined?(Bundler) ? Bundler.clean_system(cmd) : system(cmd)
@available = Environment.system(cmd)
@available
end

Expand All @@ -35,7 +35,7 @@ def self.cleanup_non_brew_version!
say_status 'warning', LEGACY_UNOX_WARNING, :yellow
raise(FAILED_TO_REMOVE_LEGACY_UNOX) unless yes?('Uninstall legacy unison-fsmonitor (unox)? (y/N)')
say_status 'command', uninstall_cmd, :white
system(uninstall_cmd)
Environment.system(uninstall_cmd)
end

def self.non_brew_version_installed?
Expand Down
4 changes: 4 additions & 0 deletions lib/docker-sync/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ def self.mac?
def self.freebsd?
@freebsd ||= OS.freebsd?
end

def self.system(cmd)
defined?(Bundler) ? Bundler.clean_system(cmd) : Kernel.system(cmd)
end
end
end
17 changes: 10 additions & 7 deletions spec/lib/docker-sync/dependencies/docker_driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
let(:mac?) { true }

before do
allow(DockerSync::Dependencies::Docker::Driver).to receive(:system).with('pgrep -q com.docker.hyperkit').and_return(true)
allow(DockerSync::Environment).to receive(:system).with('pgrep -q com.docker.hyperkit').and_return(true)
allow(DockerSync::Environment).to receive(:mac?).and_return(mac?)
allow(described_class).to receive(:system).and_return(true)

described_class.remove_instance_variable(:@docker_for_mac) if described_class.instance_variable_defined? :@docker_for_mac
end

Expand All @@ -21,12 +21,14 @@

it 'checks if Docker is running in Hyperkit' do
subject
expect(described_class).to have_received(:system).with('pgrep -q com.docker.hyperkit')

expect(DockerSync::Environment).to have_received(:system).with('pgrep -q com.docker.hyperkit')
end

it 'is memoized' do
expect { 1.times { described_class.docker_for_mac? } }.to change { described_class.instance_variable_defined?(:@docker_for_mac) }
expect(described_class).to have_received(:system).exactly(:once)

expect(DockerSync::Environment).to have_received(:system).exactly(:once)
end
end

Expand All @@ -36,8 +38,9 @@

before do
allow(DockerSync::Environment).to receive(:mac?).and_return(mac?)
allow(DockerSync::Environment).to receive(:system).and_return(true)
allow(described_class).to receive(:find_executable0).with('docker-machine').and_return(docker_machine_available?)
allow(described_class).to receive(:system).and_return(true)

described_class.remove_instance_variable(:@docker_toolbox) if described_class.instance_variable_defined? :@docker_toolbox
end

Expand All @@ -57,12 +60,12 @@

it 'checks if Docker is running in Boot2Docker' do
subject
expect(described_class).to have_received(:system).with('docker info | grep -q "Operating System: Boot2Docker"')
expect(DockerSync::Environment).to have_received(:system).with('docker info | grep -q "Operating System: Boot2Docker"')
end

it 'is memoized' do
expect { 2.times { described_class.docker_toolbox? } }.to change { described_class.instance_variable_defined?(:@docker_toolbox) }
expect(described_class).to have_received(:system).exactly(:once)
expect(DockerSync::Environment).to have_received(:system).exactly(:once)
end
end
end
51 changes: 19 additions & 32 deletions spec/lib/docker-sync/dependencies/unox_spec.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,33 @@
require 'spec_helper'

# unox is only available/allowed for mac
RSpec.describe DockerSync::Dependencies::Unox do
let(:available) { true }
let(:mac?) { true }

before do
allow(described_class).to receive(:system).with(/^brew list unox/)
allow(Bundler).to receive(:clean_system).with(/^brew list unox/)
allow(DockerSync::Environment).to receive(:system)
allow(DockerSync::Environment).to receive(:system).with(/^brew list unox/).and_return(available)
allow(DockerSync::Environment).to receive(:mac?).and_return(mac?)

described_class.remove_instance_variable(:@available) if described_class.instance_variable_defined? :@available
end

it_behaves_like 'a dependency'

describe '.available?' do
let(:mac?) { true }

before do
# unox is only available/allowed for mac
allow(DockerSync::Environment).to receive(:mac?).and_return(:mac?)
described_class.remove_instance_variable(:@available) if described_class.instance_variable_defined? :@available
end

subject { described_class.available? }

context 'when Bundler is not used' do
before { hide_const("Bundler") if defined? Bundler }
context "when Unox was installed using brew" do
let(:available) { true }

context "when Unox was installed using brew" do
before { allow(described_class).to receive(:system).with(/^brew list unox/).and_return(true) }
it { is_expected.to be true }
end

context "when Unox was not installed using brew" do
before { allow(described_class).to receive(:system).with(/^brew list unox/).and_return(false) }
it { is_expected.to be false }
end
it { is_expected.to be true }
end

context 'when Bundler is used' do
before { stub_const("Bundler") unless defined? Bundler }
context "when Unox was not installed using brew" do
let(:available) { false }

it 'performs brew search in clean env (outside of Bundler env)' do
subject
expect(Bundler).to have_received(:clean_system).with(/^brew list unox/)
end
it { is_expected.to be false }
end

context 'when its not a mac' do
Expand All @@ -55,13 +43,13 @@
subject { described_class.ensure! }

context 'when it is already available' do
before { allow(described_class).to receive(:available?).and_return(true) }
it { is_expected_to_not_raise_error }
end

context 'when it is not available' do
let(:available) { false }

before do
allow(described_class).to receive(:available?).and_return(false)
allow(DockerSync::Dependencies::PackageManager).to receive(:install_package)
end

Expand All @@ -88,7 +76,6 @@
describe 'cleanup_non_brew_version!' do
before do
allow_any_instance_of(Thor::Shell::Color).to receive(:say_status)
allow(described_class).to receive(:system)
end

subject { described_class.cleanup_non_brew_version! }
Expand All @@ -98,7 +85,7 @@

it 'does nothing' do
subject
expect(described_class).to_not have_received(:system)
expect(DockerSync::Environment).to_not have_received(:system)
end
end

Expand All @@ -110,7 +97,7 @@

it 'deletes the binary' do
subject
expect(described_class).to have_received(:system).with('sudo rm -f /usr/local/bin/unison-fsmonitor')
expect(DockerSync::Environment).to have_received(:system).with('sudo rm -f /usr/local/bin/unison-fsmonitor')
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/shared_examples/package_managers_shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
let(:package_name) { 'some-package' }

before do
allow(DockerSync::Environment).to receive(:system).and_return(true)
allow(described_class).to receive(:ensure!)
allow(Kernel).to receive(:system).and_return(true)
allow_any_instance_of(Thor::Shell::Color).to receive_messages(
say_status: nil,
yes?: user_confirmed?
Expand All @@ -34,7 +34,7 @@

it 'executes the package installation command' do
subject
expect(Kernel).to have_received(:system).with(install_command)
expect(DockerSync::Environment).to have_received(:system).with(install_command)
end
end

Expand Down

0 comments on commit c93505d

Please sign in to comment.