Skip to content

Commit ae07124

Browse files
josevalimJosé Valim
authored andcommitted
Merge pull request #2789 from alco/system-cmd-path-resolution
System.cmd path resolution Signed-off-by: José Valim <[email protected]>
1 parent 1f744f4 commit ae07124

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/elixir/lib/system.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ defmodule System do
437437
if Path.type(command) == :absolute do
438438
command
439439
else
440-
:os.find_executable(command) || command
440+
:os.find_executable(command)
441441
end
442442

443443
{into, opts} = cmd_opts(opts, [:use_stdio, :exit_status, :binary, :hide, args: args], "")

lib/elixir/test/elixir/system_test.exs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,34 @@ defmodule SystemTest do
6565
opts = [into: [], cd: System.cwd!, env: %{"foo" => "bar"},
6666
arg0: "hecho", stderr_to_stdout: true, parallelism: true]
6767
assert {["hello\n"], 0} = System.cmd "echo", ["hello"], opts
68+
69+
with_tmp_dir(fn dir ->
70+
new_path = Path.join([dir, "echo2"])
71+
File.cp!(System.find_executable("echo"), new_path)
72+
assert :enoent = catch_error(System.cmd(new_path, ["hello"]))
73+
74+
File.cd!(dir)
75+
assert :enoent = catch_error(System.cmd("echo2", ["hello"]))
76+
assert {"hello\n", 0} = System.cmd(Path.join([System.cwd, "echo2"]), ["hello"])
77+
end)
6878
end
6979

7080
test "find_executable/1" do
7181
assert System.find_executable("erl")
7282
assert is_binary System.find_executable("erl")
7383
assert !System.find_executable("does-not-really-exist-from-elixir")
7484
end
85+
86+
defp with_tmp_dir(function) do
87+
dir = tmp_dir_name()
88+
tmp_dir = System.tmp_dir!
89+
tmp_path = Path.join [tmp_dir, dir]
90+
File.rm_rf! tmp_path
91+
File.mkdir_p! tmp_path
92+
File.cd! tmp_dir, fn -> function.(dir) end
93+
end
94+
95+
defp tmp_dir_name do
96+
:crypto.rand_bytes(4) |> Base.encode16
97+
end
7598
end

0 commit comments

Comments
 (0)