Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: make which(str) return the path to the command #13197

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be OK for tests to fail if ls isn't on the path, but failing if abracadabra is on the path seems kind of wrong. You should find a more random non-existant path. Might I suggest something like vyqMEOu1xfKCePhtZro7NUKbDmOEtx5ZmVGfUH?


#######################################################################
# This section tests some of the features of the stat-based file info #
Expand Down