File 0157-escript_SUITE-retrieve-escript-s-stderr-via-temp-fil.patch of Package erlang
From 4dddf85c7d8c5ef742d86052c91391e9edaba05c Mon Sep 17 00:00:00 2001
From: Mikael Pettersson <mikpelinux@gmail.com>
Date: Sun, 20 Sep 2020 17:21:55 +0200
Subject: [PATCH 7/9] escript_SUITE: retrieve escript's stderr via temp file
instead of merging with stdout
---
lib/stdlib/test/escript_SUITE.erl | 42 ++++++++++++++++++++++++++++---
1 file changed, 39 insertions(+), 3 deletions(-)
diff --git a/lib/stdlib/test/escript_SUITE.erl b/lib/stdlib/test/escript_SUITE.erl
index 251d449d94..52c10711f4 100644
--- a/lib/stdlib/test/escript_SUITE.erl
+++ b/lib/stdlib/test/escript_SUITE.erl
@@ -976,16 +976,29 @@ run_with_opts(Dir, Opts, Cmd0, Expected) ->
end,
do_run(Dir, Cmd, Expected).
-do_run(Dir, Cmd, Expected0) ->
+do_run(Dir, Cmd, Expected) ->
+ case tempnam("escript_stderr") of
+ {ok, StdErrFile} ->
+ do_run(Dir, Cmd, Expected, StdErrFile);
+ {error, Reason} ->
+ io:format("Failed to create temp file: ~p\n", [Reason]),
+ ct:fail(failed)
+ end.
+
+do_run(Dir, Cmd0, Expected0, StdErrFile) ->
+ Cmd = Cmd0 ++ " 2> " ++ StdErrFile,
io:format("Run: ~p\n", [Cmd]),
Expected = iolist_to_binary(expected_output(Expected0, Dir)),
Env = [{"PATH",Dir++":"++os:getenv("PATH")},
{"ERL_FLAGS",false},{"ERL_AFLAGS",false}],
- Port = open_port({spawn,Cmd}, [stderr_to_stdout,exit_status,eof,in,{env,Env}]),
- Res = get_data(Port, []),
+ Port = open_port({spawn,Cmd}, [exit_status,eof,in,{env,Env}]),
+ StdOut = get_data(Port, []),
receive
{Port,{exit_status,ExitCode}} ->
+ {ok, StdErr} = file:read_file(StdErrFile),
+ file:delete(StdErrFile),
+ Res = [StdErr, StdOut],
Actual = iolist_to_binary([Res,"ExitCode:"++integer_to_list(ExitCode)]),
case matches(Expected, Actual) of
true ->
@@ -1017,6 +1030,29 @@ delete_first(_X, [], _Acc) -> false;
delete_first(X, [X | Tail], Acc) -> lists:reverse(Acc, Tail);
delete_first(X, [Y | Tail], Acc) -> delete_first(X, Tail, [Y | Acc]).
+tempnam(Prefix) ->
+ Dir = os:getenv("TMPDIR", "/tmp"),
+ Pid = os:getpid(),
+ %% Using rand's functional API requires knowing the set of available
+ %% algorithms, and which one is default. Unfortunately both properties
+ %% tend to change between OTP releases.
+ Algorithm = exsss,
+ RandState = rand:seed_s(Algorithm, os:timestamp()),
+ tempnam(filename:join(Dir, Prefix ++ "_" ++ Pid ++ "_"), RandState, _Retries = 10).
+
+tempnam(_Prefix, _RandState, _Retries = 0) -> {error, ebusy};
+tempnam(Prefix, RandState0, Retries) ->
+ {RandNr, RandState} = rand:uniform_s(1 bsl 32, RandState0),
+ File = Prefix ++ integer_to_list(RandNr),
+ case file:open(File, [write, exclusive, raw]) of
+ {ok, IoDev} ->
+ ok = file:close(IoDev),
+ ok = file:delete(File),
+ {ok, File};
+ {error, _Reason} ->
+ tempnam(Prefix, RandState, Retries - 1)
+ end.
+
get_data(Port, SoFar) ->
receive
{Port,{data,Bytes}} ->
--
2.26.2