From fa235cc83ad13c301e1242ed1654429beaa5cb53 Mon Sep 17 00:00:00 2001 From: Tim Flynn Date: Wed, 4 Sep 2019 12:21:30 -0400 Subject: [PATCH] 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) --- base/sysinfo.jl | 5 ++++- test/spawn.jl | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/base/sysinfo.jl b/base/sysinfo.jl index 1cbe3a994bcd8..df71537ad05f5 100644 --- a/base/sysinfo.jl +++ b/base/sysinfo.jl @@ -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) @@ -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 diff --git a/test/spawn.jl b/test/spawn.jl index 535aec6b3f0c3..d62179724097f 100644 --- a/test/spawn.jl +++ b/test/spawn.jl @@ -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 @@ -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