Skip to content

Commit

Permalink
extracted checking access to bin
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Jan 16, 2013
1 parent 2f87430 commit de7698b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 7 additions & 2 deletions lib/image_optim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']
Expand Down
8 changes: 4 additions & 4 deletions spec/image_optim_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit de7698b

Please sign in to comment.