From dbafdb5e2f4d379ea36d0c82df286339f86aa931 Mon Sep 17 00:00:00 2001 From: Kenta Sato Date: Fri, 18 Sep 2015 09:56:11 +0900 Subject: [PATCH 1/3] make which(str) return the path to the command --- base/file.jl | 19 +++++++++++++++++++ test/file.jl | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/base/file.jl b/base/file.jl index 94f753d8b93d8..43f0892890043 100644 --- a/base/file.jl +++ b/base/file.jl @@ -168,6 +168,25 @@ function mktempdir(parent=tempdir()) systemerror(:mktempdir, p == C_NULL) return bytestring(p) end + +# Find and return the path to a command. +function which(cmd::AbstractString) + if !isempty(dirname(cmd)) + # `cmd` seems to be a relative path + if isfile(cmd) + return Nullable(utf8(cmd)) + end + else + # find `cmd` from the PATH + for path in split(ENV["PATH"], ':') + cmd_path = joinpath(path, cmd) + if isfile(cmd_path) + return Nullable(utf8(cmd_path)) + end + end + end + return Nullable{UTF8String}() +end end @windows_only begin diff --git a/test/file.jl b/test/file.jl index ed3a385229bbd..d69b4ef40fb6f 100644 --- a/test/file.jl +++ b/test/file.jl @@ -36,6 +36,10 @@ end cd(pwd_) end +# which (TODO: tests for Windows) +@unix_only @test !isnull(which("ls")) +@unix_only @test isfile(get(which("ls"))) +@unix_only @test isnull(which("abracadabra")) ####################################################################### # This section tests some of the features of the stat-based file info # From 05bac1d09b2fba23b615b8bc1d4abb55442282f0 Mon Sep 17 00:00:00 2001 From: Kenta Sato Date: Thu, 24 Sep 2015 03:30:51 +0900 Subject: [PATCH 2/3] test on a random command --- test/file.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/file.jl b/test/file.jl index d69b4ef40fb6f..bfaab6d852a45 100644 --- a/test/file.jl +++ b/test/file.jl @@ -39,7 +39,7 @@ end # which (TODO: tests for Windows) @unix_only @test !isnull(which("ls")) @unix_only @test isfile(get(which("ls"))) -@unix_only @test isnull(which("abracadabra")) +@unix_only @test isnull(which("WhayXUbWtIo50h0")) ####################################################################### # This section tests some of the features of the stat-based file info # From cacea23af68930296e84a20e887c129218705e27 Mon Sep 17 00:00:00 2001 From: Kenta Sato Date: Thu, 24 Sep 2015 04:17:48 +0900 Subject: [PATCH 3/3] check if PATH exists in ENV --- base/file.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/file.jl b/base/file.jl index 43f0892890043..a553205d3ecea 100644 --- a/base/file.jl +++ b/base/file.jl @@ -176,7 +176,7 @@ function which(cmd::AbstractString) if isfile(cmd) return Nullable(utf8(cmd)) end - else + elseif haskey(ENV, "PATH") # find `cmd` from the PATH for path in split(ENV["PATH"], ':') cmd_path = joinpath(path, cmd)