Skip to content

Commit a3cb947

Browse files
tgflynnKristofferC
authored andcommitted
Fix which behavior when passed an empty string (#33150)
* 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) (cherry picked from commit fa235cc)
1 parent 026619f commit a3cb947

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

base/sysinfo.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,9 @@ for executable permissions only (with `.exe` and `.com` extensions added on
368368
Windows platforms); no searching of `PATH` is performed.
369369
"""
370370
function which(program_name::String)
371+
if isempty(program_name)
372+
return nothing
373+
end
371374
# Build a list of program names that we're going to try
372375
program_names = String[]
373376
base_pname = basename(program_name)
@@ -410,7 +413,7 @@ function which(program_name::String)
410413
for pname in program_names
411414
program_path = joinpath(path_dir, pname)
412415
# If we find something that matches our name and we can execute
413-
if isexecutable(program_path)
416+
if isfile(program_path) && isexecutable(program_path)
414417
return realpath(program_path)
415418
end
416419
end

test/spawn.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,10 @@ withenv("PATH" => "$(Sys.BINDIR)$(psep)$(ENV["PATH"])") do
535535
@test Sys.which(julia_exe) == realpath(julia_exe)
536536
end
537537

538+
# Check that which behaves correctly when passed an empty string
539+
@test Base.Sys.which("") === nothing
540+
541+
538542
mktempdir() do dir
539543
withenv("PATH" => "$(dir)$(psep)$(ENV["PATH"])") do
540544
# Test that files lacking executable permissions fail Sys.which
@@ -551,8 +555,15 @@ mktempdir() do dir
551555
@test Sys.which(foo_path) === nothing
552556
end
553557

558+
end
559+
560+
# Ensure these tests are done only with a PATH of known contents
561+
withenv("PATH" => "$(dir)") do
554562
# Test that completely missing files also return nothing
555563
@test Sys.which("this_is_not_a_command") === nothing
564+
565+
# Check that which behaves correctly when passed a blank string
566+
@test Base.Sys.which(" ") === nothing
556567
end
557568
end
558569

0 commit comments

Comments
 (0)