Handle empty string in System.shell (#13331)

This commit is contained in:
Marcus Kruse
2024-02-10 09:45:27 +01:00
committed by GitHub
parent e758fa1c6e
commit a210e5832e
2 changed files with 11 additions and 0 deletions
+6
View File
@@ -955,6 +955,12 @@ defmodule System do
@doc since: "1.12.0"
@spec shell(binary, keyword) :: {Collectable.t(), exit_status :: non_neg_integer}
def shell(command, opts \\ []) when is_binary(command) do
command |> String.trim() |> do_shell(opts)
end
defp do_shell("", _opts), do: {"", 0}
defp do_shell(command, opts) do
assert_no_null_byte!(command, "System.shell/2")
{close_stdin?, opts} = Keyword.pop(opts, :close_stdin, false)
+5
View File
@@ -209,6 +209,11 @@ defmodule SystemTest do
assert {"1\n2\n", 0} = System.shell("x=1; echo $x; echo '2'")
end
test "shell/1 with empty string" do
assert {"", 0} = System.shell("")
assert {"", 0} = System.shell(" ")
end
@tag timeout: 1_000
test "shell/1 returns when command awaits input" do
assert {"", 0} = System.shell("cat", close_stdin: true)