From c6195fcf5066844e8e3bcd7efcbf415e47a606c8 Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Mon, 26 Feb 2024 09:31:06 -0300 Subject: [PATCH] test: make `clear_screen` safer Useful when iterating on the tests in a loop, to get rid of all the garbaged printed before the test itself beings. Only actually does anything if the environment variable `CLEAR_SCREEN` is set to `true` and only clears the screen the screen the first time it's encountered, so it's harmless otherwise. --- apps/emqx/test/emqx_common_test_helpers.erl | 24 +++++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/apps/emqx/test/emqx_common_test_helpers.erl b/apps/emqx/test/emqx_common_test_helpers.erl index c14fa02fa..a383e0b2c 100644 --- a/apps/emqx/test/emqx_common_test_helpers.erl +++ b/apps/emqx/test/emqx_common_test_helpers.erl @@ -1060,14 +1060,24 @@ expand_node_specs(Specs, CommonOpts) -> Specs ). -%% is useful when iterating on the tests in a loop, to get rid of all -%% the garbaged printed before the test itself beings. +%% Useful when iterating on the tests in a loop, to get rid of all the garbaged printed +%% before the test itself beings. +%% Only actually does anything if the environment variable `CLEAR_SCREEN' is set to `true' +%% and only clears the screen the screen the first time it's encountered, so it's harmless +%% otherwise. clear_screen() -> - io:format(standard_io, "\033[H\033[2J", []), - io:format(standard_error, "\033[H\033[2J", []), - io:format(standard_io, "\033[H\033[3J", []), - io:format(standard_error, "\033[H\033[3J", []), - ok. + Key = {?MODULE, clear_screen}, + case {os:getenv("CLEAR_SCREEN"), persistent_term:get(Key, false)} of + {"true", false} -> + io:format(standard_io, "\033[H\033[2J", []), + io:format(standard_error, "\033[H\033[2J", []), + io:format(standard_io, "\033[H\033[3J", []), + io:format(standard_error, "\033[H\033[3J", []), + persistent_term:put(Key, true), + ok; + _ -> + ok + end. with_mock(Mod, FnName, MockedFn, Fun) -> ok = meck:new(Mod, [non_strict, no_link, no_history, passthrough]),