Skip to content

Commit

Permalink
Default System.cmd to File.cwd to fix running in slave node
Browse files Browse the repository at this point in the history
  • Loading branch information
mobileoverlord committed Oct 11, 2018
1 parent 8daec24 commit 0b5e094
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/elixir/lib/system.ex
Expand Up @@ -634,6 +634,12 @@ defmodule System do
:os.find_executable(cmd) || :erlang.error(:enoent, [command, args, opts])
end

opts =
case File.cwd() do
{:ok, cwd} -> Keyword.put_new(opts, :cd, cwd)
_ -> opts
end

{into, opts} = cmd_opts(opts, [:use_stdio, :exit_status, :binary, :hide, args: args], "")
{initial, fun} = Collectable.into(into)

Expand Down
31 changes: 31 additions & 0 deletions lib/elixir/test/elixir/system_test.exs
Expand Up @@ -177,6 +177,17 @@ defmodule SystemTest do
after
File.rm_rf!(tmp_path(@echo))
end

test "cmd/3 uses cwd of slave node instead of master" do
{:ok, node} = start_slave_node()
home = System.user_home()

:rpc.call(node, File, :cd!, [home])
{cwd, 0} = :rpc.call(node, System, :cmd, ["pwd", []])
assert home == String.trim(cwd)
after
Node.stop()
end
end

test "find_executable/1" do
Expand Down Expand Up @@ -247,4 +258,24 @@ defmodule SystemTest do
test "otp_release/0" do
assert is_binary(System.otp_release())
end

defp start_slave_node() do
:os.cmd('epmd -daemon')
rand = System.unique_integer([:positive])

host_node = :"#{rand}_master@127.0.0.1"
{:ok, _pid} = :net_kernel.start([host_node])

path =
:code.get_path()
|> Enum.map(&to_string/1)
|> Enum.map(&"-pa #{&1}")
|> Enum.join(" ")

vm_args = '-setcookie #{:erlang.get_cookie()} -mode interactive #{path}'

slave_node = :"#{rand}_slave"

:slave.start('127.0.0.1', slave_node, vm_args)
end
end

0 comments on commit 0b5e094

Please sign in to comment.