Skip to content

Commit

Permalink
Update tests to make it work on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
akash-akya committed Sep 21, 2024
1 parent 11d6849 commit d8ae757
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 19 deletions.
6 changes: 3 additions & 3 deletions lib/ex_cmd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ defmodule ExCmd do
With stderr set to :redirect_to_stdout
```
iex> ExCmd.stream!(["sh", "-c", "echo foo; echo bar >> /dev/stderr"], stderr: :redirect_to_stdout)
iex> ExCmd.stream!(["sh", "-c", "echo foo; echo bar >&2"], stderr: :redirect_to_stdout)
...> |> Enum.into("")
"foo\nbar\n"
```
With stderr set to :disable
```
iex> ExCmd.stream!(["sh", "-c", "echo foo; echo bar >> /dev/stderr"], stderr: :disable)
iex> ExCmd.stream!(["sh", "-c", "echo foo; echo bar >&2"], stderr: :disable)
...> |> Enum.to_list()
["foo\n"]
```
Expand Down Expand Up @@ -224,7 +224,7 @@ defmodule ExCmd do
Stream with stderr redirected to stdout
```
ExCmd.stream!(["sh", "-c", "echo foo; echo bar >> /dev/stderr"], stderr: :redirect_to_stdout)
ExCmd.stream!(["sh", "-c", "echo foo; echo bar >&2"], stderr: :redirect_to_stdout)
|> Stream.map(&IO.write/1)
|> Stream.run()
```
Expand Down
6 changes: 3 additions & 3 deletions lib/ex_cmd/process.ex
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ defmodule ExCmd.Process do
# sleep command does not watch for stdin or stdout, so closing the
# pipe does not terminate the sleep command.
iex> {:ok, p} = Process.start_link(~w(sleep 100000000)) # sleep indefinitely
iex> Process.await_exit(p, 200) # ensure `await_exit` finish within `200ms`. By default it waits for 5s
iex> Process.await_exit(p, 500) # ensure `await_exit` finish within `500ms`. By default it waits for 5s
{:error, :killed} # command exit due to SIGTERM
```
Expand All @@ -205,9 +205,9 @@ defmodule ExCmd.Process do
Run a command without any input or output
```
iex> {:ok, p} = Process.start_link(["sh", "-c", "exit 1"])
iex> {:ok, p} = Process.start_link(["sh", "-c", "exit 2"])
iex> Process.await_exit(p)
{:ok, 1}
{:ok, 2}
```
Single process reading and writing to the command
Expand Down
Binary file modified priv/odu_darwin_amd64
Binary file not shown.
Binary file modified priv/odu_darwin_arm64
Binary file not shown.
Binary file modified priv/odu_linux_amd64
Binary file not shown.
Binary file modified priv/odu_linux_arm64
Binary file not shown.
Binary file modified priv/odu_windows_amd64.exe
Binary file not shown.
Binary file modified priv/odu_windows_arm64.exe
Binary file not shown.
34 changes: 22 additions & 12 deletions test/ex_cmd/process_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule ExCmd.ProcessTest do
assert :ok == Process.close_stdin(s)
assert :eof == Process.read(s)

assert {:ok, 0} == Process.await_exit(s, 150)
assert {:ok, 0} == Process.await_exit(s, 200)

:timer.sleep(100)
refute Elixir.Process.alive?(s.pid)
Expand Down Expand Up @@ -88,7 +88,7 @@ defmodule ExCmd.ProcessTest do

assert {:ok, "==foo==\n"} = Process.read(s, 100)
assert :eof = Process.read(s, 100)
assert {:ok, 0} = Process.await_exit(s, 150)
assert {:ok, 0} = Process.await_exit(s, 200)
end

test "stderr redirect_to_stdout" do

Check failure on line 94 in test/ex_cmd/process_test.exs

View workflow job for this annotation

GitHub Actions / Windows OTP 27 / Elixir 1.17

test pipes stderr redirect_to_stdout (ExCmd.ProcessTest)
Expand All @@ -103,7 +103,7 @@ defmodule ExCmd.ProcessTest do

assert {:ok, "==foo==\n==bar==\n"} = Process.read(s, 100)
assert :eof = Process.read(s, 100)
assert {:ok, 0} = Process.await_exit(s, 150)
assert {:ok, 0} = Process.await_exit(s, 200)
end

test "if pipe gets closed on pipe owner exit normally" do
Expand Down Expand Up @@ -195,12 +195,12 @@ defmodule ExCmd.ProcessTest do

test "if await_exit closes stdin implicitly" do
{:ok, s} = Process.start_link(~w(cat))
assert {:ok, 0} = Process.await_exit(s, 150)
assert {:ok, 0} = Process.await_exit(s, 200)
end

test "if await_exit kills the program" do
{:ok, s} = Process.start_link(~w(sleep 1000))
assert {:error, :killed} = Process.await_exit(s, 150)
{:ok, s} = Process.start_link(~w(sleep 10000))
assert_killed(Process.await_exit(s, 500))
end

test "if external program terminates on process exit" do
Expand Down Expand Up @@ -259,8 +259,8 @@ defmodule ExCmd.ProcessTest do

assert {:ok, "ignored signals\n" <> _} = Process.read(s)

# attempt to kill the process after 100ms
assert {:error, :killed} = Process.await_exit(s, 200)
# attempt to kill the process after 200ms
assert_killed(Process.await_exit(s, 200))

refute os_process_alive?(os_pid)
refute Elixir.Process.alive?(s.pid)
Expand All @@ -273,7 +273,7 @@ defmodule ExCmd.ProcessTest do

@tag skip: Application.compile_env!(:ex_cmd, :current_os) == :windows
test "check command that does not take any input or produce output" do
{:ok, s} = Process.start_link(["sh", "-c", "./forever.sh"])
{:ok, s} = Process.start_link(["sh", "-c", fixture("forever.sh")])
assert ret = Process.await_exit(s)
# process might die with different reason due to race condition
assert ret in [{:error, :killed}, {:ok, 127}]
Expand Down Expand Up @@ -361,7 +361,7 @@ defmodule ExCmd.ProcessTest do
end

# external process will be killed with SIGTERM (143)
assert {:error, :killed} = Process.await_exit(s, 150)
assert_killed(Process.await_exit(s, 200))

# wait for messages to propagate, if there are any
:timer.sleep(100)
Expand Down Expand Up @@ -407,7 +407,7 @@ defmodule ExCmd.ProcessTest do
:owner_changed -> :ok
end

assert {:error, :killed} = Process.await_exit(s, 200)
assert_killed(Process.await_exit(s, 200))

refute os_process_alive?(os_pid)
assert {:error, :epipe} == Task.await(task)
Expand Down Expand Up @@ -560,7 +560,7 @@ defmodule ExCmd.ProcessTest do
{:ok, s} = Process.start_link(~w(sh -c pwd), cd: parent)
{:ok, dir} = Process.read(s)

assert String.trim(dir) == parent
assert Path.basename(String.trim(dir)) == Path.basename(parent)
assert {:ok, 0} = Process.await_exit(s)
end

Expand Down Expand Up @@ -705,4 +705,14 @@ defmodule ExCmd.ProcessTest do
raise "recv timeout"
end
end

def assert_killed(ret) do
if Application.fetch_env!(:ex_cmd, :current_os) == :windows do
# Windows does not have a way to distinguish between signals and exit-status
# Golang returns exit_status as 1 for kill
assert ret == {:ok, 1}
else
assert ret == {:error, :killed}
end
end
end
Empty file modified test/scripts/forever.sh
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion test/scripts/write_stderr.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash

echo "$1" >> /dev/stderr
echo "$1" >&2

0 comments on commit d8ae757

Please sign in to comment.