Skip to content

Commit

Permalink
Merge branch 'hotfix/0.2.2-windows-vs-absolute-paths'
Browse files Browse the repository at this point in the history
Fixes #6
  • Loading branch information
yaauie committed Sep 18, 2013
2 parents d930838 + 63a3043 commit 45495a9
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/cliver/dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def find_executables

detected_exes = []
cmds.product(paths, exts).map do |cmd, path, ext|
exe = File.expand_path("#{cmd}#{ext}", path)
exe = File.absolute_path?(cmd) ? cmd : File.expand_path("#{cmd}#{ext}", path)

next unless File.executable?(exe)
next if detected_exes.include?(exe) # don't yield the same exe path 2x
Expand Down
2 changes: 1 addition & 1 deletion lib/cliver/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module Cliver
# Cliver follows {http://semver.org SemVer}
VERSION = '0.2.1'
VERSION = '0.2.2'
end
19 changes: 18 additions & 1 deletion lib/core_ext/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ class File
# @param [String] - a pathname
# @return [Boolean]
def self.absolute_path?(path)
false | File.dirname(path)[/\A([A-Z]:)?#{Regexp.escape(File::SEPARATOR)}/i]
false | path[ABSOLUTE_PATH_PATTERN]
end

unless defined?(POSIX_ABSOLUTE_PATH_PATTERN)
POSIX_ABSOLUTE_PATH_PATTERN = /\A\//.freeze
end

unless defined?(WINDOWS_ABSOLUTE_PATH_PATTERN)
WINDOWS_ABSOLUTE_PATH_PATTERN = Regexp.union(
POSIX_ABSOLUTE_PATH_PATTERN,
/\A([A-Z]:)?(\\|\/)/i
).freeze
end

ABSOLUTE_PATH_PATTERN = begin
File::ALT_SEPARATOR ?
WINDOWS_ABSOLUTE_PATH_PATTERN :
POSIX_ABSOLUTE_PATH_PATTERN
end unless defined?(ABSOLUTE_PATH_PATTERN)
end
28 changes: 28 additions & 0 deletions spec/cliver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,34 @@
expect { action }.to raise_exception Cliver::Dependency::NotFound
end
end
context '(windows path)' do
before(:each) do
stub_const('File::ABSOLUTE_PATH_PATTERN', File::WINDOWS_ABSOLUTE_PATH_PATTERN)
end
let(:version_map) do
{'C:/baz/bingo/doodle.exe' => '0.2.1',
'C:/baz/fiddle/doodle.exe' => '1.1.4'}
end
context 'and executable at that path is sufficient' do
let(:executable) { 'C:/baz/fiddle/doodle.exe' }
it 'should not raise' do
expect { action }.to_not raise_exception
end
end
context 'and the executable at that path is not sufficent' do
let(:executable) { 'C:/baz/bingo/doodle.exe' }
it 'should raise' do
expect { action }.to raise_exception Cliver::Dependency::VersionMismatch
end
end
context 'and no executable exists at that path' do
let(:version_map) { Hash.new }
let(:executable) { 'C:/baz/fiddle/doodle.exe' }
it 'should raise' do
expect { action }.to raise_exception Cliver::Dependency::NotFound
end
end
end
context 'and the executable at that path is sufficent' do
let(:executable) { '/baz/fiddle/doodle' }
it 'should not raise' do
Expand Down
82 changes: 82 additions & 0 deletions spec/core_ext/file_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# encoding: utf-8
require 'core_ext/file'

describe 'File::absolute_path?' do
context 'posix' do
before(:each) do
stub_const("File::ALT_SEPARATOR", nil)
stub_const("File::ABSOLUTE_PATH_PATTERN", File::POSIX_ABSOLUTE_PATH_PATTERN)
end
context 'when given an absolute path' do
%w(
/foo/bar
/C/Windows/system32/
).each do |path|
context "(#{path})" do
context 'the return value' do
subject { File::absolute_path?(path) }
it { should be_true }
end
end
end
end
context 'when given a relative path' do
%w(
C:/foo/bar
\\foo\\bar
C:\\foo\\bar
foo/bar
foo
./foo/bar
../foo/bar
C:foo/bar
).each do |path|
context "(#{path})" do
context 'the return value' do
subject { File::absolute_path?(path) }
it { should be_false }
end
end
end
end
end

context 'windows' do
before(:each) do
stub_const("File::ALT_SEPARATOR", '\\')
stub_const("File::ABSOLUTE_PATH_PATTERN", File::WINDOWS_ABSOLUTE_PATH_PATTERN)
end
context 'when given an absolute path' do
%w(
/foo/bar
C:/foo/bar
\\foo\\bar
C:\\foo\\bar
/C/Windows/system32/
).each do |path|
context "(#{path})" do
context 'the return value' do
subject { File::absolute_path?(path) }
it { should be_true }
end
end
end
end
context 'when given a relative path' do
%w(
foo/bar
foo
./foo/bar
../foo/bar
C:foo/bar
).each do |path|
context "(#{path})" do
context 'the return value' do
subject { File::absolute_path?(path) }
it { should be_false }
end
end
end
end
end
end

0 comments on commit 45495a9

Please sign in to comment.