Просмотр исходного кода

test(meck): add meck unload after meck load

zhanghongtong 5 лет назад
Родитель
Сommit
3ef08f9ce3

+ 152 - 96
apps/emqx_management/test/emqx_mgmt_SUITE.erl

@@ -25,7 +25,7 @@
 
 -include_lib("common_test/include/ct.hrl").
 
--define(LOG_LEVELS,["debug", "error", "info"]).
+-define(LOG_LEVELS, ["debug", "error", "info"]).
 -define(LOG_HANDLER_ID, [file, default]).
 
 all() ->
@@ -75,7 +75,8 @@ t_app(_Config) ->
     emqx_mgmt_auth:del_app(<<"app_id">>),
     %% Use the default application secret
     application:set_env(emqx_management, application, [{default_secret, <<"public">>}]),
-    {ok, AppSecret1} = emqx_mgmt_auth:add_app(<<"app_id">>, <<"app_name">>, <<"app_desc">>, true, undefined),
+    {ok, AppSecret1} = emqx_mgmt_auth:add_app(
+                         <<"app_id">>, <<"app_name">>, <<"app_desc">>, true, undefined),
     ?assert(emqx_mgmt_auth:is_authorized(<<"app_id">>, AppSecret1)),
     ?assertEqual(AppSecret1, emqx_mgmt_auth:get_appsecret(<<"app_id">>)),
     ?assertEqual(AppSecret1, <<"public">>),
@@ -84,7 +85,9 @@ t_app(_Config) ->
     emqx_mgmt_auth:del_app(<<"app_id">>),
     application:set_env(emqx_management, application, []),
     %% Specify the application secret
-    {ok, AppSecret2} = emqx_mgmt_auth:add_app(<<"app_id">>, <<"app_name">>, <<"secret">>, <<"app_desc">>, true, undefined),
+    {ok, AppSecret2} = emqx_mgmt_auth:add_app(
+                         <<"app_id">>, <<"app_name">>, <<"secret">>,
+                         <<"app_desc">>, true, undefined),
     ?assert(emqx_mgmt_auth:is_authorized(<<"app_id">>, AppSecret2)),
     ?assertEqual(AppSecret2, emqx_mgmt_auth:get_appsecret(<<"app_id">>)),
     ?assertEqual({<<"app_id">>, AppSecret2, <<"app_name">>, <<"app_desc">>, true, undefined},
@@ -93,44 +96,49 @@ t_app(_Config) ->
     ok.
 
 t_log_cmd(_) ->
-    print_mock(),
+    mock_print(),
     lists:foreach(fun(Level) ->
                       emqx_mgmt_cli:log(["primary-level", Level]),
-                      ?assertEqual(Level++"\n", emqx_mgmt_cli:log(["primary-level"]))
+                      ?assertEqual(Level ++ "\n", emqx_mgmt_cli:log(["primary-level"]))
                   end, ?LOG_LEVELS),
     lists:foreach(fun(Level) ->
                      emqx_mgmt_cli:log(["set-level", Level]),
-                     ?assertEqual(Level++"\n", emqx_mgmt_cli:log(["primary-level"]))
+                     ?assertEqual(Level ++ "\n", emqx_mgmt_cli:log(["primary-level"]))
                   end, ?LOG_LEVELS),
     [lists:foreach(fun(Level) ->
-                         ?assertEqual(Level++"\n", emqx_mgmt_cli:log(["handlers", "set-level",
+                         ?assertEqual(Level ++ "\n", emqx_mgmt_cli:log(["handlers", "set-level",
                                                                       atom_to_list(Id), Level]))
                       end, ?LOG_LEVELS)
         || #{id := Id} <- emqx_logger:get_log_handlers()],
     meck:unload().
 
 t_mgmt_cmd(_) ->
-    ct:pal("start testing the mgmt command"),
-    print_mock(),
-    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["lookup", "emqx_appid"]), "Not Found.")),
-    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["delete", "emqx_appid"]), "ok")),
-    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["insert", "emqx_appid", "emqx_name"]), "AppSecret:")),
-    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["insert", "emqx_appid", "emqx_name"]), "Error:")),
-    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["lookup", "emqx_appid"]), "app_id:")),
-    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["update", "emqx_appid", "ts"]), "update successfully")),
-    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["delete", "emqx_appid"]), "ok")),
+    % ct:pal("start testing the mgmt command"),
+    mock_print(),
+    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(
+                                      ["lookup", "emqx_appid"]), "Not Found.")),
+    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(
+                                      ["insert", "emqx_appid", "emqx_name"]), "AppSecret:")),
+    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(
+                                      ["insert", "emqx_appid", "emqx_name"]), "Error:")),
+    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(
+                                      ["lookup", "emqx_appid"]), "app_id:")),
+    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(
+                                      ["update", "emqx_appid", "ts"]), "update successfully")),
+    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(
+                                      ["delete", "emqx_appid"]), "ok")),
     ok = emqx_mgmt_cli:mgmt(["list"]),
     meck:unload().
 
 t_status_cmd(_) ->
-    ct:pal("start testing status command"),
-    print_mock(),
+    % ct:pal("start testing status command"),
+    mock_print(),
     ?assertMatch({match, _}, re:run(emqx_mgmt_cli:status([]), "is running")),
     meck:unload().
 
 t_broker_cmd(_) ->
-    ct:pal("start testing the broker command"),
-    print_mock(),
+    % ct:pal("start testing the broker command"),
+    mock_print(),
     ?assertMatch({match, _}, re:run(emqx_mgmt_cli:broker([]), "sysdescr")),
     ?assertMatch({match, _}, re:run(emqx_mgmt_cli:broker(["stats"]), "subscriptions.shared")),
     ?assertMatch({match, _}, re:run(emqx_mgmt_cli:broker(["metrics"]), "bytes.sent")),
@@ -138,13 +146,13 @@ t_broker_cmd(_) ->
     meck:unload().
 
 t_clients_cmd(_) ->
-    ct:pal("start testing the client command"),
-    print_mock(),
+    % ct:pal("start testing the client command"),
+    mock_print(),
     process_flag(trap_exit, true),
-    {ok, T} = emqtt:start_link([{host, "localhost"},
-                                {clientid, <<"client12">>},
+    {ok, T} = emqtt:start_link([{clientid, <<"client12">>},
                                 {username, <<"testuser1">>},
-                                {password, <<"pass1">>}]),
+                                {password, <<"pass1">>}
+                               ]),
     {ok, _} = emqtt:connect(T),
     timer:sleep(300),
     emqx_mgmt_cli:clients(["list"]),
@@ -153,8 +161,9 @@ t_clients_cmd(_) ->
     timer:sleep(500),
     ?assertMatch({match, _}, re:run(emqx_mgmt_cli:clients(["show", "client12"]), "Not Found")),
     receive
-        {'EXIT', T, Reason} ->
-            ct:pal("Connection closed: ~p~n", [Reason])
+        {'EXIT', T, _} ->
+            ok
+            % ct:pal("Connection closed: ~p~n", [Reason])
     after
         500 ->
             erlang:error("Client is not kick")
@@ -181,64 +190,82 @@ raw_send_serialize(Packet) ->
     emqx_frame:serialize(Packet).
 
 t_vm_cmd(_) ->
-    ct:pal("start testing the vm command"),
-    print_mock(),
-    [[?assertMatch({match, _}, re:run(Result, Name)) || Result <- emqx_mgmt_cli:vm([Name])] || Name <- ["load", "memory", "process", "io", "ports"]],
-    [?assertMatch({match, _}, re:run(Result, "load")) || Result <- emqx_mgmt_cli:vm(["load"])],
-    [?assertMatch({match, _}, re:run(Result, "memory"))|| Result <- emqx_mgmt_cli:vm(["memory"])],
-    [?assertMatch({match, _}, re:run(Result, "process")) || Result <- emqx_mgmt_cli:vm(["process"])],
-    [?assertMatch({match, _}, re:run(Result, "io")) || Result <- emqx_mgmt_cli:vm(["io"])],
-    [?assertMatch({match, _}, re:run(Result, "ports")) || Result <- emqx_mgmt_cli:vm(["ports"])],
-    meck:unload().
+    % ct:pal("start testing the vm command"),
+    mock_print(),
+    [[?assertMatch({match, _}, re:run(Result, Name))
+      || Result <- emqx_mgmt_cli:vm([Name])]
+     || Name <- ["load", "memory", "process", "io", "ports"]],
+    [?assertMatch({match, _}, re:run(Result, "load"))
+     || Result <- emqx_mgmt_cli:vm(["load"])],
+    [?assertMatch({match, _}, re:run(Result, "memory"))
+     || Result <- emqx_mgmt_cli:vm(["memory"])],
+    [?assertMatch({match, _}, re:run(Result, "process"))
+     || Result <- emqx_mgmt_cli:vm(["process"])],
+    [?assertMatch({match, _}, re:run(Result, "io"))
+     || Result <- emqx_mgmt_cli:vm(["io"])],
+    [?assertMatch({match, _}, re:run(Result, "ports"))
+     || Result <- emqx_mgmt_cli:vm(["ports"])],
+    unmock_print().
 
 t_trace_cmd(_) ->
-    ct:pal("start testing the trace command"),
-    print_mock(),
+    % ct:pal("start testing the trace command"),
+    mock_print(),
     logger:set_primary_config(level, debug),
-    {ok, T} = emqtt:start_link([{host, "localhost"},
-                                {clientid, <<"client">>},
-                                {username, <<"testuser">>},
-                                {password, <<"pass">>}
-                               ]),
+    {ok, T} = emqtt:start_link([{clientid, <<"client">>},
+                                 {username, <<"testuser">>},
+                                 {password, <<"pass">>}
+                                ]),
     emqtt:connect(T),
     emqtt:subscribe(T, <<"a/b/c">>),
-    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["start", "client", "client", "log/clientid_trace.log"]), "successfully")),
-    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["stop", "client", "client"]), "successfully")),
-    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["start", "client", "client", "log/clientid_trace.log", "error"]), "successfully")),
-    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["stop", "client", "client"]), "successfully")),
-    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["start", "topic", "a/b/c", "log/clientid_trace.log"]), "successfully")),
-    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["stop", "topic", "a/b/c"]), "successfully")),
-    ?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["start", "topic", "a/b/c", "log/clientid_trace.log", "error"]), "successfully")),
+    Trace1 = emqx_mgmt_cli:trace(["start", "client", "client",
+                                  "log/clientid_trace.log"]),
+    ?assertMatch({match, _}, re:run(Trace1, "successfully")),
+    Trace2 = emqx_mgmt_cli:trace(["stop", "client", "client"]),
+    ?assertMatch({match, _}, re:run(Trace2, "successfully")),
+    Trace3 = emqx_mgmt_cli:trace(["start", "client", "client",
+                                  "log/clientid_trace.log",
+                                  "error"]),
+    ?assertMatch({match, _}, re:run(Trace3, "successfully")),
+    Trace4 = emqx_mgmt_cli:trace(["stop", "client", "client"]),
+    ?assertMatch({match, _}, re:run(Trace4, "successfully")),
+    Trace5 = emqx_mgmt_cli:trace(["start", "topic", "a/b/c",
+                                  "log/clientid_trace.log"]),
+    ?assertMatch({match, _}, re:run(Trace5, "successfully")),
+    Trace6 = emqx_mgmt_cli:trace(["stop", "topic", "a/b/c"]),
+    ?assertMatch({match, _}, re:run(Trace6, "successfully")),
+    Trace7 = emqx_mgmt_cli:trace(["start", "topic", "a/b/c",
+                                  "log/clientid_trace.log", "error"]),
+    ?assertMatch({match, _}, re:run(Trace7, "successfully")),
     logger:set_primary_config(level, error),
-    meck:unload().
+    unmock_print().
 
 t_router_cmd(_) ->
-    ct:pal("start testing the router command"),
-    print_mock(),
-    {ok, T} = emqtt:start_link([{host, "localhost"},
-                                {clientid, <<"client1">>},
-                                {username, <<"testuser1">>},
-                                {password, <<"pass1">>}]),
+    % ct:pal("start testing the router command"),
+    mock_print(),
+    {ok, T} = emqtt:start_link([{clientid, <<"client1">>},
+                                 {username, <<"testuser1">>},
+                                 {password, <<"pass1">>}
+                                ]),
     emqtt:connect(T),
     emqtt:subscribe(T, <<"a/b/c">>),
-    {ok, T1} = emqtt:start_link([{host, "localhost"},
-                                 {clientid, <<"client2">>},
-                                 {username, <<"testuser2">>},
-                                 {password, <<"pass2">>}
-                                ]),
+    {ok, T1} = emqtt:start_link([{clientid, <<"client2">>},
+                                  {username, <<"testuser2">>},
+                                  {password, <<"pass2">>}
+                                 ]),
+
     emqtt:connect(T1),
     emqtt:subscribe(T1, <<"a/b/c/d">>),
     ?assertMatch({match, _}, re:run(emqx_mgmt_cli:routes(["list"]), "a/b/c | a/b/c")),
     ?assertMatch({match, _}, re:run(emqx_mgmt_cli:routes(["show", "a/b/c"]), "a/b/c")),
-    meck:unload().
+    unmock_print().
 
 t_subscriptions_cmd(_) ->
-    ct:pal("Start testing the subscriptions command"),
-    print_mock(),
-    {ok, T3} = emqtt:start_link([{host, "localhost"},
-                                 {clientid, <<"client">>},
+    % ct:pal("Start testing the subscriptions command"),
+    mock_print(),
+    {ok, T3} = emqtt:start_link([{clientid, <<"client">>},
                                  {username, <<"testuser">>},
-                                 {password, <<"pass">>}]),
+                                 {password, <<"pass">>}
+                                ]),
     {ok, _} = emqtt:connect(T3),
     {ok, _, _} = emqtt:subscribe(T3, <<"b/b/c">>),
     timer:sleep(300),
@@ -246,56 +273,85 @@ t_subscriptions_cmd(_) ->
      || Result <- emqx_mgmt_cli:subscriptions(["show", <<"client">>])],
     ?assertEqual(emqx_mgmt_cli:subscriptions(["add", "client", "b/b/c", "0"]), "ok~n"),
     ?assertEqual(emqx_mgmt_cli:subscriptions(["del", "client", "b/b/c"]), "ok~n"),
-    meck:unload().
+    unmock_print().
 
 t_listeners_cmd(_) ->
-    print_mock(),
+    mock_print(),
     ?assertEqual(emqx_mgmt_cli:listeners([]), ok),
-    ?assertEqual(emqx_mgmt_cli:listeners(["stop", "wss", "8084"]), "Stop wss listener on 8084 successfully.\n"),
-    meck:unload().
+    ?assertEqual(
+       emqx_mgmt_cli:listeners(["stop", "wss", "8084"]),
+       "Stop wss listener on 8084 successfully.\n"
+      ),
+    unmock_print().
 
 t_plugins_cmd(_) ->
-    print_mock(),
+    mock_print(),
     meck:new(emqx_plugins, [non_strict, passthrough]),
     meck:expect(emqx_plugins, load, fun(_) -> ok end),
     meck:expect(emqx_plugins, unload, fun(_) -> ok end),
     meck:expect(emqx_plugins, reload, fun(_) -> ok end),
     ?assertEqual(emqx_mgmt_cli:plugins(["list"]), ok),
-    ?assertEqual(emqx_mgmt_cli:plugins(["unload", "emqx_auth_mnesia"]), "Plugin emqx_auth_mnesia unloaded successfully.\n"),
-    ?assertEqual(emqx_mgmt_cli:plugins(["load", "emqx_auth_mnesia"]),"Plugin emqx_auth_mnesia loaded successfully.\n"),
-    ?assertEqual(emqx_mgmt_cli:plugins(["unload", "emqx_management"]), "Plugin emqx_management can not be unloaded.~n"),
-    meck:unload().
+    ?assertEqual(
+       emqx_mgmt_cli:plugins(["unload", "emqx_auth_mnesia"]),
+       "Plugin emqx_auth_mnesia unloaded successfully.\n"
+      ),
+    ?assertEqual(
+       emqx_mgmt_cli:plugins(["load", "emqx_auth_mnesia"]),
+       "Plugin emqx_auth_mnesia loaded successfully.\n"
+      ),
+    ?assertEqual(
+       emqx_mgmt_cli:plugins(["unload", "emqx_management"]),
+       "Plugin emqx_management can not be unloaded.~n"
+      ),
+    unmock_print().
 
 t_modules_cmd(_) ->
-    print_mock(),
+    mock_print(),
     meck:new(emqx_modules, [non_strict, passthrough]),
     meck:expect(emqx_modules, load, fun(_) -> ok end),
     meck:expect(emqx_modules, unload, fun(_) -> ok end),
     meck:expect(emqx_modules, reload, fun(_) -> ok end),
     ?assertEqual(emqx_mgmt_cli:modules(["list"]), ok),
-    ?assertEqual(emqx_mgmt_cli:modules(["load", "emqx_mod_presence"]),"Module emqx_mod_presence loaded successfully.\n"),
-    ?assertEqual(emqx_mgmt_cli:modules(["unload", "emqx_mod_presence"]), "Module emqx_mod_presence unloaded successfully.\n"),
-    meck:unload().
+    ?assertEqual(emqx_mgmt_cli:modules(["load", "emqx_mod_presence"]),
+                 "Module emqx_mod_presence loaded successfully.\n"),
+    ?assertEqual(emqx_mgmt_cli:modules(["unload", "emqx_mod_presence"]),
+                 "Module emqx_mod_presence unloaded successfully.\n"),
+    unmock_print().
 
 t_cli(_) ->
-    print_mock(),
+    mock_print(),
     ?assertMatch({match, _}, re:run(emqx_mgmt_cli:status([""]), "status")),
-    [?assertMatch({match, _}, re:run(Value, "broker")) || Value <- emqx_mgmt_cli:broker([""])],
-    [?assertMatch({match, _}, re:run(Value, "cluster")) || Value <- emqx_mgmt_cli:cluster([""])],
-    [?assertMatch({match, _}, re:run(Value, "clients")) || Value <- emqx_mgmt_cli:clients([""])],
-    [?assertMatch({match, _}, re:run(Value, "routes")) || Value <- emqx_mgmt_cli:routes([""])],
-    [?assertMatch({match, _}, re:run(Value, "subscriptions")) || Value <- emqx_mgmt_cli:subscriptions([""])],
-    [?assertMatch({match, _}, re:run(Value, "plugins")) || Value <- emqx_mgmt_cli:plugins([""])],
-    [?assertMatch({match, _}, re:run(Value, "listeners")) || Value <- emqx_mgmt_cli:listeners([""])],
-    [?assertMatch({match, _}, re:run(Value, "vm")) || Value <- emqx_mgmt_cli:vm([""])],
-    [?assertMatch({match, _}, re:run(Value, "mnesia")) || Value <- emqx_mgmt_cli:mnesia([""])],
-    [?assertMatch({match, _}, re:run(Value, "trace")) || Value <- emqx_mgmt_cli:trace([""])],
-    [?assertMatch({match, _}, re:run(Value, "mgmt")) || Value <- emqx_mgmt_cli:mgmt([""])],
-    meck:unload().
+    [?assertMatch({match, _}, re:run(Value, "broker"))
+     || Value <- emqx_mgmt_cli:broker([""])],
+    [?assertMatch({match, _}, re:run(Value, "cluster"))
+     || Value <- emqx_mgmt_cli:cluster([""])],
+    [?assertMatch({match, _}, re:run(Value, "clients"))
+     || Value <- emqx_mgmt_cli:clients([""])],
+    [?assertMatch({match, _}, re:run(Value, "routes"))
+     || Value <- emqx_mgmt_cli:routes([""])],
+    [?assertMatch({match, _}, re:run(Value, "subscriptions"))
+     || Value <- emqx_mgmt_cli:subscriptions([""])],
+    [?assertMatch({match, _}, re:run(Value, "plugins"))
+     || Value <- emqx_mgmt_cli:plugins([""])],
+    [?assertMatch({match, _}, re:run(Value, "listeners"))
+     || Value <- emqx_mgmt_cli:listeners([""])],
+    [?assertMatch({match, _}, re:run(Value, "vm"))
+     || Value <- emqx_mgmt_cli:vm([""])],
+    [?assertMatch({match, _}, re:run(Value, "mnesia"))
+     || Value <- emqx_mgmt_cli:mnesia([""])],
+    [?assertMatch({match, _}, re:run(Value, "trace"))
+     || Value <- emqx_mgmt_cli:trace([""])],
+    [?assertMatch({match, _}, re:run(Value, "mgmt"))
+     || Value <- emqx_mgmt_cli:mgmt([""])],
+    unmock_print().
 
-print_mock() ->
+mock_print() ->
+    catch meck:unload(emqx_ctl),
     meck:new(emqx_ctl, [non_strict, passthrough]),
     meck:expect(emqx_ctl, print, fun(Arg) -> emqx_ctl:format(Arg) end),
     meck:expect(emqx_ctl, print, fun(Msg, Arg) -> emqx_ctl:format(Msg, Arg) end),
     meck:expect(emqx_ctl, usage, fun(Usages) -> emqx_ctl:format_usage(Usages) end),
-    meck:expect(emqx_ctl, usage, fun(Cmd, Descr) -> emqx_ctl:format_usage(Cmd, Descr) end). 
+    meck:expect(emqx_ctl, usage, fun(Cmd, Descr) -> emqx_ctl:format_usage(Cmd, Descr) end).
+
+unmock_print() ->
+    meck:unload(emqx_ctl).

+ 14 - 7
apps/emqx_recon/test/emqx_recon_SUITE.erl

@@ -45,7 +45,7 @@ end_per_suite(_Config) ->
     application:stop(emqx).
 
 cli_memory(_) ->
-    print_mock(),
+    mock_print(),
     Output = emqx_recon_cli:cmd(["memory"]),
     Zip = lists:zip(Output, [ ?output_patterns("usage/current")
                             , ?output_patterns("usage/max")
@@ -58,10 +58,11 @@ cli_memory(_) ->
                             ]),
     %ct:pal("=======~p", [Zip]),
     [?assertMatch({match, _}, re:run(Line, Match, [{capture,all,list}]))
-     || {Line, Match} <- Zip].
+     || {Line, Match} <- Zip],
+    unmock_print().
 
 cli_allocated(_) ->
-    print_mock(),
+    mock_print(),
     Output = emqx_recon_cli:cmd(["allocated"]),
     Zip = lists:zip(Output, [ ?output_patterns("binary_alloc/current")
                             , ?output_patterns("driver_alloc/current")
@@ -84,13 +85,15 @@ cli_allocated(_) ->
                             ]),
     ct:pal("=======~p", [Zip]),
     [?assertMatch({match, _}, re:run(Line, Match, [{capture,all,list}]))
-     || {Line, Match} <- Zip].
+     || {Line, Match} <- Zip],
+    unmock_print().
 
 cli_bin_leak(_) ->
-    print_mock(),
+    mock_print(),
     Output = emqx_recon_cli:cmd(["bin_leak"]),
     [?assertMatch({match, _}, re:run(Line, "current_function", [{capture,all,list}]))
-     || Line <- Output].
+     || Line <- Output],
+    unmock_print().
 
 cli_node_stats(_) ->
     emqx_recon_cli:cmd(["node_stats"]).
@@ -109,9 +112,13 @@ start_apps(App, DataDir) ->
     [application:set_env(App, Par, Value) || {Par, Value} <- Vals],
     application:ensure_all_started(App).
 
-print_mock() ->
+mock_print() ->
+    catch meck:unload(emqx_ctl),
     meck:new(emqx_ctl, [non_strict, passthrough]),
     meck:expect(emqx_ctl, print, fun(Arg) -> emqx_ctl:format(Arg) end),
     meck:expect(emqx_ctl, print, fun(Msg, Arg) -> emqx_ctl:format(Msg, Arg) end),
     meck:expect(emqx_ctl, usage, fun(Usages) -> emqx_ctl:format_usage(Usages) end).
 
+unmock_print() ->
+    meck:unload().
+

+ 14 - 6
apps/emqx_rule_engine/test/emqx_rule_engine_SUITE.erl

@@ -468,7 +468,7 @@ t_show_resource_type_api(_Config) ->
 %%------------------------------------------------------------------------------
 
 t_rules_cli(_Config) ->
-    print_mock(),
+    mock_print(),
     RCreate = emqx_rule_engine_cli:rules(["create",
                                           "select * from \"t1\" where topic='t1'",
                                           "[{\"name\":\"inspect\", \"params\": {\"arg1\": 1}}]",
@@ -500,10 +500,11 @@ t_rules_cli(_Config) ->
     RShow2 = emqx_rule_engine_cli:rules(["show", RuleId]),
     ?assertMatch({match, _}, re:run(RShow2, "Cannot found")),
     %ct:pal("RShow2 : ~p", [RShow2]),
+    unmock_print(),
     ok.
 
 t_actions_cli(_Config) ->
-    print_mock(),
+    mock_print(),
     RList = emqx_rule_engine_cli:actions(["list"]),
     ?assertMatch({match, _}, re:run(RList, "inspect")),
     %ct:pal("RList : ~p", [RList]),
@@ -511,10 +512,11 @@ t_actions_cli(_Config) ->
     RShow = emqx_rule_engine_cli:actions(["show", "inspect"]),
     ?assertMatch({match, _}, re:run(RShow, "inspect")),
     %ct:pal("RShow : ~p", [RShow]),
+    unmock_print(),
     ok.
 
 t_resources_cli(_Config) ->
-    print_mock(),
+    mock_print(),
     RCreate = emqx_rule_engine_cli:resources(["create", "built_in", "{\"a\" : 1}", "-d", "test resource"]),
     ResId = re:replace(re:replace(RCreate, "Resource\s", "", [{return, list}]), "\screated\n", "", [{return, list}]),
 
@@ -536,10 +538,11 @@ t_resources_cli(_Config) ->
     RShow2 = emqx_rule_engine_cli:resources(["show", ResId]),
     ?assertMatch({match, _}, re:run(RShow2, "Cannot found")),
     %ct:pal("RShow2 : ~p", [RShow2]),
+    unmock_print(),
     ok.
 
 t_resource_types_cli(_Config) ->
-    print_mock(),
+    mock_print(),
     RList = emqx_rule_engine_cli:resource_types(["list"]),
     ?assertMatch({match, _}, re:run(RList, "built_in")),
     %ct:pal("RList : ~p", [RList]),
@@ -547,6 +550,7 @@ t_resource_types_cli(_Config) ->
     RShow = emqx_rule_engine_cli:resource_types(["show", "inspect"]),
     ?assertMatch({match, _}, re:run(RShow, "inspect")),
     %ct:pal("RShow : ~p", [RShow]),
+    unmock_print(),
     ok.
 
 %%------------------------------------------------------------------------------
@@ -562,7 +566,7 @@ t_topic_func(_Config) ->
 %%------------------------------------------------------------------------------
 
 t_add_get_remove_rule(_Config) ->
-    print_mock(),
+    mock_print(),
     RuleId0 = <<"rule-debug-0">>,
     ok = emqx_rule_registry:add_rule(make_simple_rule(RuleId0)),
     ?assertMatch({ok, #rule{id = RuleId0}}, emqx_rule_registry:get_rule(RuleId0)),
@@ -575,6 +579,7 @@ t_add_get_remove_rule(_Config) ->
     ?assertMatch({ok, #rule{id = RuleId1}}, emqx_rule_registry:get_rule(RuleId1)),
     ok = emqx_rule_registry:remove_rule(Rule1),
     ?assertEqual(not_found, emqx_rule_registry:get_rule(RuleId1)),
+    unmock_print(),
     ok.
 
 t_add_get_remove_rules(_Config) ->
@@ -2435,7 +2440,7 @@ set_special_configs(emqx_rule_engine) ->
 set_special_configs(_App) ->
     ok.
 
-print_mock() ->
+mock_print() ->
     catch meck:unload(emqx_ctl),
     meck:new(emqx_ctl, [non_strict, passthrough]),
     meck:expect(emqx_ctl, print, fun(Arg) -> emqx_ctl:format(Arg) end),
@@ -2443,6 +2448,9 @@ print_mock() ->
     meck:expect(emqx_ctl, usage, fun(Usages) -> emqx_ctl:format_usage(Usages) end),
     meck:expect(emqx_ctl, usage, fun(Cmd, Descr) -> emqx_ctl:format_usage(Cmd, Descr) end).
 
+unmock_print() ->
+    meck:unload(emqx_ctl).
+
 t_load_providers(_) ->
     error('TODO').
 

+ 19 - 10
elvis.config

@@ -1,26 +1,35 @@
+%% -*-: erlang -*-
 [
  {
    elvis,
    [
     {config,
      [
-      {dirs => ["apps"],
+      #{dirs => ["apps/**/src", "src"],
         filter => "*.erl",
-        ruleset => erl_files,
-#        rules => [
-#            {elvis_style, max_module_length, #{}},
-#            {elvis_style, no_common_caveats_call, #{}}
-#          ]
-       },
-      {dirs => ["."],
+        ruleset => erl_files
+        %rules => [
+        %    {elvis_style, max_module_length, #{}},
+        %    {elvis_style, no_common_caveats_call, #{}}
+        %  ]
+        },
+      #{dirs => ["apps/**/test", "test"],
+        filter => "*.erl",
+        rules => [
+           {elvis_text_style, line_length, #{ limit => 100
+                                             , skip_comments => false }},
+           {elvis_style, dont_repeat_yourself, #{ min_complexity => 100 }}
+          ]
+        },
+      #{dirs => ["."],
         filter => "Makefile",
         ruleset => makefiles
        },
-      {dirs => ["."],
+      #{dirs => ["."],
         filter => "rebar.config",
         ruleset => rebar_config
        },
-      {dirs => ["."],
+      #{dirs => ["."],
         filter => "elvis.config",
         ruleset => elvis_config
        }

+ 2 - 1
rebar.config.erl

@@ -23,7 +23,8 @@ config() ->
     ].
 
 plugins() ->
-    [ {relup_helper,{git,"https://github.com/emqx/relup_helper", {branch,"master"}}}
+    [ {relup_helper,{git,"https://github.com/emqx/relup_helper", {branch,"master"}}},
+      {er_coap_client, {git, "https://github.com/emqx/er_coap_client", {tag, "v1.0"}}}
     ].
 
 test_deps() ->

+ 1 - 1
scripts/elvis-check.sh

@@ -31,7 +31,7 @@ for file in $(git_diff); do
         # not .erl file
         continue
     fi
-    if ! ./elvis rock "$file"; then
+    if ! ./elvis rock "$file" -c elvis.config; then
         bad_file_count=$(( bad_file_count + 1))
     fi
 done

+ 3 - 1
test/emqx_ctl_SUITE.erl

@@ -106,11 +106,13 @@ with_ctl_server(Fun) ->
 
 mock_print() ->
     %% proxy usage/1,2 and print/1,2 to format_xx/1,2 funcs
+    catch meck:unload(emqx_ctl),
     meck:new(emqx_ctl, [non_strict, passthrough]),
     meck:expect(emqx_ctl, print, fun(Arg) -> emqx_ctl:format(Arg) end),
     meck:expect(emqx_ctl, print, fun(Msg, Arg) -> emqx_ctl:format(Msg, Arg) end),
     meck:expect(emqx_ctl, usage, fun(Usages) -> emqx_ctl:format_usage(Usages) end),
-    meck:expect(emqx_ctl, usage, fun(CmdParams, CmdDescr) -> emqx_ctl:format_usage(CmdParams, CmdDescr) end).
+    meck:expect(emqx_ctl, usage, fun(CmdParams, CmdDescr) ->
+                                         emqx_ctl:format_usage(CmdParams, CmdDescr) end).
 
 unmock_print() ->
     meck:unload(emqx_ctl).