Skip to content

Commit

Permalink
Fix which behavior when passed an empty string (#33150)
Browse files Browse the repository at this point in the history
* Fix behavior of Sys.which when passed an empty String argument

* Added test to check for fixed Sys.which behavior with empty string input

* Added test to check that Sys.which returns nothing when passed a blank
string

* Ensure that Sys.which returns a regular file and never a directory

* Moved new Sys.which tests into test/spawn.jl alongside the existing ones

* Remove new which tests from test/sysinfo.jl (they've moved to
test/spawn.jl)
  • Loading branch information
tgflynn authored and staticfloat committed Sep 4, 2019
1 parent b05b6d4 commit fa235cc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion base/sysinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@ for executable permissions only (with `.exe` and `.com` extensions added on
Windows platforms); no searching of `PATH` is performed.
"""
function which(program_name::String)
if isempty(program_name)
return nothing
end
# Build a list of program names that we're going to try
program_names = String[]
base_pname = basename(program_name)
Expand Down Expand Up @@ -501,7 +504,7 @@ function which(program_name::String)
for pname in program_names
program_path = joinpath(path_dir, pname)
# If we find something that matches our name and we can execute
if isexecutable(program_path)
if isfile(program_path) && isexecutable(program_path)
return realpath(program_path)
end
end
Expand Down
11 changes: 11 additions & 0 deletions test/spawn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,10 @@ withenv("PATH" => "$(Sys.BINDIR)$(psep)$(ENV["PATH"])") do
@test Sys.which(julia_exe) == realpath(julia_exe)
end

# Check that which behaves correctly when passed an empty string
@test isnothing(Base.Sys.which(""))


mktempdir() do dir
withenv("PATH" => "$(dir)$(psep)$(ENV["PATH"])") do
# Test that files lacking executable permissions fail Sys.which
Expand All @@ -572,8 +576,15 @@ mktempdir() do dir
@test Sys.which(foo_path) === nothing
end

end

# Ensure these tests are done only with a PATH of known contents
withenv("PATH" => "$(dir)") do
# Test that completely missing files also return nothing
@test Sys.which("this_is_not_a_command") === nothing

# Check that which behaves correctly when passed a blank string
@test isnothing(Base.Sys.which(" "))
end
end

Expand Down

0 comments on commit fa235cc

Please sign in to comment.