From de7698b8180a69ee92575f324fd35acb8964432b Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Wed, 16 Jan 2013 23:50:32 +0100 Subject: [PATCH] extracted checking access to bin --- lib/image_optim.rb | 9 +++++++-- spec/image_optim_spec.rb | 8 ++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/image_optim.rb b/lib/image_optim.rb index fdee90ff..ca16470f 100644 --- a/lib/image_optim.rb +++ b/lib/image_optim.rb @@ -175,9 +175,9 @@ def resolve_bin!(bin) symlink.make_symlink(File.expand_path(path)) at_exit{ symlink.unlink } - @resolved_bins[bin] = system(*%W[which -s #{symlink}]) + @resolved_bins[bin] = bin_accessible?(symlink) else - @resolved_bins[bin] = system(*%W[which -s #{bin}]) + @resolved_bins[bin] = bin_accessible?(bin) end end end @@ -208,6 +208,11 @@ def apply_threading(array) end end + # Check if bin can be accessed + def bin_accessible?(bin) + system(*%W[which -s #{bin}]) + end + # http://stackoverflow.com/questions/891537/ruby-detect-number-of-cpus-installed def processor_count @processor_count ||= case host_os = RbConfig::CONFIG['host_os'] diff --git a/spec/image_optim_spec.rb b/spec/image_optim_spec.rb index 363e5910..afbdd52b 100644 --- a/spec/image_optim_spec.rb +++ b/spec/image_optim_spec.rb @@ -196,7 +196,7 @@ def with_env(key, value) it "should resolve bin in path" do with_env 'LS_BIN', nil do image_optim = ImageOptim.new - image_optim.should_receive(:system).with(*%w[which -s ls]).once.and_return(true) + image_optim.should_receive(:bin_accessible?).with(:ls).once.and_return(true) FSPath.should_not_receive(:temp_dir) 5.times do @@ -212,7 +212,7 @@ def with_env(key, value) symlink = stub(:symlink) image_optim = ImageOptim.new - image_optim.should_receive(:system).with(*%W[which -s #{symlink}]).once.and_return(true) + image_optim.should_receive(:bin_accessible?).with(symlink).once.and_return(true) FSPath.should_receive(:temp_dir).once.and_return(tmpdir) tmpdir.should_receive(:/).with(:image_optim).once.and_return(symlink) symlink.should_receive(:make_symlink).with(File.expand_path(path)).once @@ -235,7 +235,7 @@ def with_env(key, value) it "should raise on failure to resolve bin" do with_env 'SHOULD_NOT_EXIST_BIN', nil do image_optim = ImageOptim.new - image_optim.should_receive(:system).with(*%w[which -s should_not_exist]).once.and_return(false) + image_optim.should_receive(:bin_accessible?).with(:should_not_exist).once.and_return(false) FSPath.should_not_receive(:temp_dir) 5.times do @@ -253,7 +253,7 @@ def with_env(key, value) symlink = stub(:symlink) image_optim = ImageOptim.new - image_optim.should_receive(:system).with(*%W[which -s #{symlink}]).once.and_return(false) + image_optim.should_receive(:bin_accessible?).with(symlink).once.and_return(false) FSPath.should_receive(:temp_dir).once.and_return(tmpdir) tmpdir.should_receive(:/).with(:should_not_exist).once.and_return(symlink) symlink.should_receive(:make_symlink).with(File.expand_path(path)).once