Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
5315a2e6
Commit
5315a2e6
authored
Dec 08, 2021
by
Matthias Käppler
Committed by
Michael Kozono
Dec 08, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rewrite Gitlab::Utils.which function
parent
a5caf589
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
13 deletions
+15
-13
lib/gitlab/utils.rb
lib/gitlab/utils.rb
+6
-10
spec/lib/gitlab/utils_spec.rb
spec/lib/gitlab/utils_spec.rb
+9
-3
No files found.
lib/gitlab/utils.rb
View file @
5315a2e6
...
@@ -120,18 +120,14 @@ module Gitlab
...
@@ -120,18 +120,14 @@ module Gitlab
Random
.
rand
(
Float
::
MAX
.
to_i
).
to_s
(
36
)
Random
.
rand
(
Float
::
MAX
.
to_i
).
to_s
(
36
)
end
end
#
See: http://stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby
#
Behaves like `which` on Linux machines: given PATH, try to resolve the given
#
Cross-platform way of finding an executable in the $PATH
.
#
executable name to an absolute path, or return nil
.
#
#
# which('ruby') #=> /usr/bin/ruby
# which('ruby') #=> /usr/bin/ruby
def
which
(
cmd
,
env
=
ENV
)
def
which
(
filename
)
exts
=
env
[
'PATHEXT'
]
?
env
[
'PATHEXT'
].
split
(
';'
)
:
[
''
]
ENV
[
'PATH'
]
&
.
split
(
File
::
PATH_SEPARATOR
)
&
.
each
do
|
path
|
full_path
=
File
.
join
(
path
,
filename
)
env
[
'PATH'
].
split
(
File
::
PATH_SEPARATOR
).
each
do
|
path
|
return
full_path
if
File
.
executable?
(
full_path
)
exts
.
each
do
|
ext
|
exe
=
File
.
join
(
path
,
"
#{
cmd
}#{
ext
}
"
)
return
exe
if
File
.
executable?
(
exe
)
&&
!
File
.
directory?
(
exe
)
end
end
end
nil
nil
...
...
spec/lib/gitlab/utils_spec.rb
View file @
5315a2e6
...
@@ -249,10 +249,16 @@ RSpec.describe Gitlab::Utils do
...
@@ -249,10 +249,16 @@ RSpec.describe Gitlab::Utils do
end
end
describe
'.which'
do
describe
'.which'
do
it
'finds the full path to an executable binary'
do
before
do
expect
(
File
).
to
receive
(
:executable?
).
with
(
'/bin/sh'
).
and_return
(
true
)
stub_env
(
'PATH'
,
'/sbin:/usr/bin:/home/joe/bin'
)
end
it
'finds the full path to an executable binary in order of appearance'
do
expect
(
File
).
to
receive
(
:executable?
).
with
(
'/sbin/tool'
).
ordered
.
and_return
(
false
)
expect
(
File
).
to
receive
(
:executable?
).
with
(
'/usr/bin/tool'
).
ordered
.
and_return
(
true
)
expect
(
File
).
not_to
receive
(
:executable?
).
with
(
'/home/joe/bin/tool'
)
expect
(
which
(
'
sh'
,
'PATH'
=>
'/bin'
)).
to
eq
(
'/bin/sh
'
)
expect
(
which
(
'
tool'
)).
to
eq
(
'/usr/bin/tool
'
)
end
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment