forked from lambdaclass/erlang-katana
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from inaka/jfacorro.74.ktn_os.command
[Closes #74] ktn_os:command/[1,2]
- Loading branch information
Showing
4 changed files
with
149 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
%% @doc Utility functions to run commands in the underlying OS. | ||
-module(ktn_os). | ||
|
||
-export([command/1, command/2]). | ||
|
||
-type opts() :: #{log_fun => fun((iodata()) -> any()), timeout => integer()}. | ||
-type exit_status() :: integer(). | ||
|
||
-spec command(iodata()) -> {exit_status(), string()}. | ||
command(Cmd) -> | ||
Opts = #{log_fun => fun error_logger:info_msg/1}, | ||
command(Cmd, Opts). | ||
|
||
-spec command(iodata(), opts()) -> {exit_status(), string()}. | ||
command(Cmd, Opts) -> | ||
PortOpts = [stream, exit_status, eof], | ||
Port = open_port({spawn, shell_cmd()}, PortOpts), | ||
erlang:port_command(Port, make_cmd(Cmd)), | ||
get_data(Port, Opts, []). | ||
|
||
-spec get_data(port(), opts(), [string()]) -> {exit_status(), string()}. | ||
get_data(Port, Opts, Data) -> | ||
%% Get timeout option or an hour if undefined. | ||
Timeout = maps:get(timeout, Opts, 600000), | ||
receive | ||
{Port, {data, NewData}} -> | ||
case maps:get(log_fun, Opts, undefined) of | ||
Fun when is_function(Fun) -> Fun(NewData); | ||
undefined -> ok | ||
end, | ||
get_data(Port, Opts, [NewData | Data]); | ||
{Port, eof} -> | ||
port_close(Port), | ||
receive | ||
{Port, {exit_status, ExitStatus}} -> | ||
{ExitStatus, lists:flatten(lists:reverse(Data))} | ||
end | ||
after | ||
Timeout -> throw(timeout) | ||
end. | ||
|
||
-spec make_cmd(string()) -> iodata(). | ||
make_cmd(Cmd) -> | ||
%% We insert a new line after the command, in case the command | ||
%% contains a comment character. | ||
[$(, unicode:characters_to_binary(Cmd), "\n) </dev/null; exit\n"]. | ||
|
||
-spec shell_cmd() -> string(). | ||
shell_cmd() -> "sh -s unix:cmd 2>&1". |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
ktn_lists, | ||
ktn_maps, | ||
ktn_numbers, | ||
ktn_os, | ||
ktn_random, | ||
ktn_recipe, | ||
ktn_recipe_verify, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
-module(ktn_os_SUITE). | ||
|
||
-export([all/0]). | ||
|
||
-export([command/1]). | ||
|
||
-define(EXCLUDED_FUNS, | ||
[ | ||
module_info, | ||
all, | ||
test, | ||
init_per_suite, | ||
end_per_suite | ||
]). | ||
|
||
-type config() :: [{atom(), term()}]. | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% Common test | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-spec all() -> [atom()]. | ||
all() -> | ||
Exports = ?MODULE:module_info(exports), | ||
[F || {F, _} <- Exports, not lists:member(F, ?EXCLUDED_FUNS)]. | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% Test Cases | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-spec command(config()) -> ok. | ||
command(_Config) -> | ||
Opts = #{log_fun => fun(_) -> ok end}, | ||
|
||
{0, "/\n"} = ktn_os:command("cd /; pwd", Opts), | ||
|
||
{ok, Cwd} = file:get_cwd(), | ||
Result = Cwd ++ "\n", | ||
{0, Result} = ktn_os:command("pwd", Opts), | ||
|
||
{1, _} = ktn_os:command("pwd; ls w4th3v3r", Opts), | ||
|
||
Result2 = Result ++ "Hi\n", | ||
{0, Result2} = ktn_os:command("pwd; echo Hi", #{}), | ||
|
||
{0, "/\n"} = ktn_os:command("cd /; pwd"), | ||
|
||
ok = try ktn_os:command("sleep 5", #{timeout => 1000}) | ||
catch _:timeout -> ok end, | ||
|
||
Fun = fun() -> ktn_os:command("cd /; pwd") end, | ||
FilterFun = | ||
fun(Line) -> | ||
case re:run(Line, "=INFO REPORT==== .* ===") of | ||
nomatch -> false; | ||
{match, _}-> true | ||
end | ||
end, | ||
check_some_line_output(Fun, FilterFun), | ||
|
||
ct:comment("Check result when process is killed"), | ||
Self = self(), | ||
YesFun = fun() -> | ||
case ktn_os:command("yes > /dev/null") of | ||
{ExitStatus, _} when ExitStatus =/= 0 -> Self ! ok; | ||
_ -> Self ! error | ||
end | ||
end, | ||
erlang:spawn_link(YesFun), | ||
os:cmd("pkill yes"), | ||
ok = receive X -> X after 1000 -> error end. | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% Helper functions | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
check_some_line_output(Fun, FilterFun) -> | ||
ct:capture_start(), | ||
Fun(), | ||
ct:capture_stop(), | ||
Lines = ct:capture_get([]), | ||
ListFun = fun(Line) -> FilterFun(Line) end, | ||
[_ | _] = lists:filter(ListFun, Lines). |