which_works 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +34 -2
- data/VERSION +1 -1
- data/lib/which_works.rb +54 -9
- data/spec/all_spec.rb +20 -0
- data/spec/array_spec.rb +24 -0
- data/spec/basic_spec.rb +42 -0
- data/spec/helper.rb +30 -0
- data/which_works.gemspec +5 -3
- metadata +24 -22
- data/spec/which_spec.rb +0 -35
data/README.md
CHANGED
@@ -1,6 +1,38 @@
|
|
1
|
-
#
|
1
|
+
# which\_works
|
2
2
|
|
3
|
-
**Ruby UNIX-like which.**
|
3
|
+
**Ruby UNIX-like which. Locates a program file in the user's path.**
|
4
|
+
|
5
|
+
The `which` method takes a list of command names and searches the path
|
6
|
+
for each executable file that would be run had these commands actually
|
7
|
+
been invoked.
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
Which.which('ls') #=> "/bin/ls"
|
11
|
+
Which.which('ls', 'screen') #=> [ "/bin/ls", "/usr/bin/screen" ]
|
12
|
+
Which.which('unknown') #=> nil
|
13
|
+
|
14
|
+
# you can also check an absolute path
|
15
|
+
Which.which('/usr/bin/svn') #=> "/usr/bin/svn"
|
16
|
+
Which.which('/usr/bin/foo') #=> nil
|
17
|
+
|
18
|
+
# the :all option finds all executable files,
|
19
|
+
# not just the first one found
|
20
|
+
Which.which('svn', :all => true) #=> [ "/opt/local/bin/svn", "/usr/bin/svn" ]
|
21
|
+
|
22
|
+
# the :array option always returns an array
|
23
|
+
Which.which('unknown', :array => true) #=> []
|
24
|
+
Which.which('ls', :array => true) #=> [ "/bin/ls" ]
|
25
|
+
Which.which('ls', 'screen', :array => true) #=> [ "/bin/ls", "/usr/bin/screen" ]
|
26
|
+
|
27
|
+
# combined options
|
28
|
+
Which.which('ls', 'svn', :all => true, :array => true)
|
29
|
+
#=> [ "/bin/ls", "/opt/local/bin/svn", "/usr/bin/svn" ]
|
30
|
+
```
|
31
|
+
|
32
|
+
Tested with <a href="https://www.relishapp.com/rspec">RSpec</a>, <a href="https://github.com/thoughtbot/shoulda">shoulda</a> and <a href="http://travis-ci.org/#!/AlphaHydrae/which_works">Travis CI</a>.
|
33
|
+
|
34
|
+
* master [](http://travis-ci.org/AlphaHydrae/which\_works)
|
35
|
+
* develop [](http://travis-ci.org/AlphaHydrae/which\_works)
|
4
36
|
|
5
37
|
## License (MIT)
|
6
38
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/which_works.rb
CHANGED
@@ -1,20 +1,65 @@
|
|
1
1
|
|
2
|
-
|
2
|
+
# The {Which} module can locate program files in the user's path.
|
3
|
+
# See {Which.which}.
|
4
|
+
module Which
|
3
5
|
|
4
|
-
|
6
|
+
# Locates a program file in the user's path.
|
7
|
+
#
|
8
|
+
# The which method takes a list of command names and searches the path
|
9
|
+
# for each executable file that would be run had these commands actually
|
10
|
+
# been invoked.
|
11
|
+
#
|
12
|
+
# By default, which will return nil if no executable was found, one
|
13
|
+
# string if one executable file was found, or an array if several were
|
14
|
+
# found. You can customize this behavior by passing the <tt>:array</tt>
|
15
|
+
# option.
|
16
|
+
#
|
17
|
+
# == Options
|
18
|
+
# * <tt>:all => boolean</tt> - List all instances of executables found
|
19
|
+
# (instead of just the first one of each). False by default.
|
20
|
+
# * <tt>:array => boolean</tt> - Always return an array. False by default.
|
21
|
+
#
|
22
|
+
# == Examples
|
23
|
+
# Which.which('ls') #=> "/bin/ls"
|
24
|
+
# Which.which('unknown') #=> nil
|
25
|
+
# Which.which('ls', 'screen') #=> [ "/bin/ls", "/usr/bin/screen" ]
|
26
|
+
# Which.which('svn', :all => true) #=> [ "/opt/local/bin/svn", "/usr/bin/svn" ]
|
27
|
+
# Which.which('ls', :array => true) #=> [ "/bin/ls" ]
|
28
|
+
# Which.which('/usr/bin/screen') #=> "/usr/bin/screen"
|
29
|
+
def self.which *programs
|
30
|
+
|
31
|
+
found = []
|
32
|
+
options = programs.last.kind_of?(Hash) ? programs.pop : {}
|
5
33
|
|
6
|
-
|
7
|
-
extensions = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [ '' ]
|
34
|
+
programs.each do |program|
|
8
35
|
|
9
|
-
|
36
|
+
program_found = false
|
10
37
|
|
11
|
-
extensions
|
38
|
+
# valid file extensions (cross-platform)
|
39
|
+
extensions = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [ '' ]
|
12
40
|
|
13
|
-
|
14
|
-
|
41
|
+
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
|
42
|
+
|
43
|
+
extensions.each do |ext|
|
44
|
+
|
45
|
+
# using expand_path makes it work with absolute program paths
|
46
|
+
absolute_path = File.expand_path "#{program}#{ext}", path
|
47
|
+
|
48
|
+
if File.executable? absolute_path
|
49
|
+
program_found = true
|
50
|
+
found << absolute_path
|
51
|
+
break unless options[:all]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
break if program_found && !options[:all]
|
15
56
|
end
|
16
57
|
end
|
17
58
|
|
18
|
-
|
59
|
+
if found.length <= 1
|
60
|
+
options[:array] ? found : found.first
|
61
|
+
else
|
62
|
+
found
|
63
|
+
end
|
19
64
|
end
|
20
65
|
end
|
data/spec/all_spec.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe ':all option' do
|
4
|
+
|
5
|
+
before :each do
|
6
|
+
WhichSpecHelper.stub_unix!
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should find all executable files" do
|
10
|
+
Which.which('svn', :all => true).should == [ '/opt/local/bin/svn', '/usr/bin/svn' ]
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should find all executable files and other files" do
|
14
|
+
Which.which('ls', 'svn', 'screen', :all => true).should == [ '/bin/ls', '/opt/local/bin/svn', '/usr/bin/svn', '/usr/bin/screen' ]
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should not find anything when given no command to find" do
|
18
|
+
Which.which({ :all => true }).should be_nil
|
19
|
+
end
|
20
|
+
end
|
data/spec/array_spec.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe ':array option' do
|
4
|
+
|
5
|
+
before :each do
|
6
|
+
WhichSpecHelper.stub_unix!
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should return an array when given nothing to find" do
|
10
|
+
Which.which({ :array => true }).should == []
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should return an array if the given command does not exist" do
|
14
|
+
Which.which('unknown', :array => true).should == []
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return an array when finding a single command" do
|
18
|
+
Which.which('ls', :array => true).should == [ '/bin/ls' ]
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should return an array when finding several commands" do
|
22
|
+
Which.which('ls', 'svn', :array => true).should == [ '/bin/ls', '/opt/local/bin/svn' ]
|
23
|
+
end
|
24
|
+
end
|
data/spec/basic_spec.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe Which do
|
4
|
+
|
5
|
+
before :each do
|
6
|
+
WhichSpecHelper.stub_unix!
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should find a single command" do
|
10
|
+
Which.which('ls').should == '/bin/ls'
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should find a single command with an absolute path" do
|
14
|
+
Which.which('/bin/ls').should == '/bin/ls'
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should not find commands that do not exist" do
|
18
|
+
[ 'foo', 'bar', 'wrong', 'cp', '/usr/bin/du' ].each do |unknown|
|
19
|
+
Which.which(unknown).should be_nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should find nothing when given nothing" do
|
24
|
+
Which.which.should be_nil
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should find several commands" do
|
28
|
+
Which.which('ls', 'screen').should == [ '/bin/ls', '/usr/bin/screen' ]
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should find only existing commands" do
|
32
|
+
Which.which('ls', 'cp', 'screen', '/bin/foo').should == [ '/bin/ls', '/usr/bin/screen' ]
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should not find a command that is not in the path" do
|
36
|
+
Which.which('custom').should be_nil
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should find the first executable file in the path" do
|
40
|
+
Which.which('svn').should == '/opt/local/bin/svn'
|
41
|
+
end
|
42
|
+
end
|
data/spec/helper.rb
CHANGED
@@ -20,3 +20,33 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
20
20
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
21
21
|
require 'which_works'
|
22
22
|
|
23
|
+
module WhichSpecHelper
|
24
|
+
UNIX_COMMANDS = %w( /bin/ls /usr/bin/screen /home/johndoe/bin/custom /opt/local/bin/svn /usr/bin/svn )
|
25
|
+
UNIX_PATH = "/bin:/opt/local/bin:/usr/bin"
|
26
|
+
|
27
|
+
def self.stub_unix!
|
28
|
+
|
29
|
+
v = $VERBOSE
|
30
|
+
$VERBOSE = nil
|
31
|
+
File.const_set :SEPARATOR, '/'
|
32
|
+
File.const_set :PATH_SEPARATOR, ':'
|
33
|
+
$VERBOSE = v
|
34
|
+
|
35
|
+
UNIX_COMMANDS.each{ |com| com.gsub!(/\//, File::SEPARATOR) }
|
36
|
+
UNIX_PATH.gsub!(/\:/, File::PATH_SEPARATOR).gsub!(/\//, File::SEPARATOR)
|
37
|
+
|
38
|
+
ENV.stub!(:[]) do |name|
|
39
|
+
case name
|
40
|
+
when 'PATH'
|
41
|
+
UNIX_PATH
|
42
|
+
when 'PATHEXT'
|
43
|
+
nil
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
File.stub!(:executable?) do |name|
|
48
|
+
UNIX_COMMANDS.include? name.to_s
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
data/which_works.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "which_works"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["AlphaHydrae"]
|
12
|
-
s.date = "2012-03-
|
12
|
+
s.date = "2012-03-10"
|
13
13
|
s.description = "Ruby UNIX-like which."
|
14
14
|
s.email = "hydrae.alpha@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -28,8 +28,10 @@ Gem::Specification.new do |s|
|
|
28
28
|
"Rakefile",
|
29
29
|
"VERSION",
|
30
30
|
"lib/which_works.rb",
|
31
|
+
"spec/all_spec.rb",
|
32
|
+
"spec/array_spec.rb",
|
33
|
+
"spec/basic_spec.rb",
|
31
34
|
"spec/helper.rb",
|
32
|
-
"spec/which_spec.rb",
|
33
35
|
"which_works.gemspec"
|
34
36
|
]
|
35
37
|
s.homepage = "http://github.com/AlphaHydrae/which_works"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: which_works
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-03-
|
12
|
+
date: 2012-03-10 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70349900379000 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 0.9.2
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70349900379000
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70349900378040 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 2.8.0
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70349900378040
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: shoulda
|
38
|
-
requirement: &
|
38
|
+
requirement: &70349900375720 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 2.11.3
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70349900375720
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: bundler
|
49
|
-
requirement: &
|
49
|
+
requirement: &70349900374340 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70349900374340
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: jeweler
|
60
|
-
requirement: &
|
60
|
+
requirement: &70349900372640 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: 1.8.3
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70349900372640
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: simplecov
|
71
|
-
requirement: &
|
71
|
+
requirement: &70349900370600 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: 0.5.4
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70349900370600
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: yard
|
82
|
-
requirement: &
|
82
|
+
requirement: &70349900367500 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ~>
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: 0.7.5
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70349900367500
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: rdiscount
|
93
|
-
requirement: &
|
93
|
+
requirement: &70349900364380 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ~>
|
@@ -98,10 +98,10 @@ dependencies:
|
|
98
98
|
version: 1.6.8
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *70349900364380
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: travis-lint
|
104
|
-
requirement: &
|
104
|
+
requirement: &70349900362880 !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
107
107
|
- - ~>
|
@@ -109,7 +109,7 @@ dependencies:
|
|
109
109
|
version: 1.3.0
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
|
-
version_requirements: *
|
112
|
+
version_requirements: *70349900362880
|
113
113
|
description: Ruby UNIX-like which.
|
114
114
|
email: hydrae.alpha@gmail.com
|
115
115
|
executables: []
|
@@ -129,8 +129,10 @@ files:
|
|
129
129
|
- Rakefile
|
130
130
|
- VERSION
|
131
131
|
- lib/which_works.rb
|
132
|
+
- spec/all_spec.rb
|
133
|
+
- spec/array_spec.rb
|
134
|
+
- spec/basic_spec.rb
|
132
135
|
- spec/helper.rb
|
133
|
-
- spec/which_spec.rb
|
134
136
|
- which_works.gemspec
|
135
137
|
homepage: http://github.com/AlphaHydrae/which_works
|
136
138
|
licenses:
|
@@ -147,7 +149,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
147
149
|
version: '0'
|
148
150
|
segments:
|
149
151
|
- 0
|
150
|
-
hash:
|
152
|
+
hash: 4071309635441737713
|
151
153
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
154
|
none: false
|
153
155
|
requirements:
|
data/spec/which_spec.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
describe Which do
|
4
|
-
COMMANDS = %w( /bin/ls /usr/bin/screen /home/johndoe/bin/custom )
|
5
|
-
|
6
|
-
before :each do
|
7
|
-
|
8
|
-
ENV.stub!(:[]) do |name|
|
9
|
-
case name
|
10
|
-
when 'PATH'
|
11
|
-
"/bin#{File::PATH_SEPARATOR}/usr/bin"
|
12
|
-
when 'PATHEXT'
|
13
|
-
nil
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
File.stub!(:executable?) do |name|
|
18
|
-
COMMANDS.include? name.to_s
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should find commands" do
|
23
|
-
Which.which('ls').should == '/bin/ls'
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should find commands with absolute paths" do
|
27
|
-
Which.which('/bin/ls').should == '/bin/ls'
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should not find commands that do not exist" do
|
31
|
-
[ 'wrong', 'cp', '/usr/bin/du' ].each do |unknown|
|
32
|
-
Which.which(unknown).should be_nil
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|