diff --git a/github_action_utils.py b/github_action_utils.py index 6a30576..a3c9f1a 100644 --- a/github_action_utils.py +++ b/github_action_utils.py @@ -12,7 +12,6 @@ CommandTypes = Literal[ "add-mask", "debug", - "endgroup", "error", "group", "notice", @@ -35,19 +34,23 @@ def _print_command( command: CommandTypes, command_message: str, options_string: Union[str] = "", + escape_message: bool = True, ) -> None: """ Helper function to print GitHub action commands to the shell. + Docs: https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions :param command: command name from `CommandTypes` :param command_message: message string :returns: None """ - # https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions + if escape_message: + command_message = _escape_data(command_message) + echo_message = ( f"{COMMAND_MARKER}{command} " f"{options_string or ''}" - f"{COMMAND_MARKER}{_escape_data(command_message)}" + f"{COMMAND_MARKER}{command_message}" ) print(echo_message) @@ -148,7 +151,7 @@ def echo(message: Any) -> None: :param message: Any type of message e.g. string, number, list, dict :returns: None """ - print(_escape_data(message)) + print(message) def debug(message: str) -> None: @@ -161,10 +164,7 @@ def debug(message: str) -> None: :param message: message string :returns: None """ - _print_command( - "debug", - message, - ) + _print_command("debug", message, escape_message=False) def notice( @@ -202,6 +202,7 @@ def notice( line=line, end_line=end_line, ), + escape_message=False, ) @@ -240,6 +241,7 @@ def warning( line=line, end_line=end_line, ), + escape_message=False, ) @@ -278,6 +280,7 @@ def error( line=line, end_line=end_line, ), + escape_message=False, ) @@ -325,7 +328,7 @@ def start_group(title: str) -> None: :param title: title of the group :returns: None """ - _print_command("group", title) + _print_command("group", title, escape_message=False) def end_group() -> None: @@ -379,7 +382,7 @@ def begin_stop_commands(token: Union[str, None] = None) -> str: if not token: token = str(uuid.uuid1()) - _print_command("stop-commands", token) + _print_command("stop-commands", token, escape_message=False) return token diff --git a/tests/test_github_action_utils.py b/tests/test_github_action_utils.py index 54cbeca..e74ea42 100644 --- a/tests/test_github_action_utils.py +++ b/tests/test_github_action_utils.py @@ -121,9 +121,9 @@ def test__build_options_string(input_kwargs: Any, expected: str) -> None: "test_input,expected", [ ("test", "test\n"), - ("test\n", "test%0A\n"), - ("%test", "%25test\n"), - ("\rtest", "%0Dtest\n"), + ("test\n", "test\n\n"), + ("%test", "%test\n"), + ("\rtest", "\rtest\n"), ], ) def test_echo(capfd: Any, test_input: str, expected: str) -> None: @@ -136,9 +136,9 @@ def test_echo(capfd: Any, test_input: str, expected: str) -> None: "test_input,expected", [ ("test", "::debug ::test\n"), - ("test\n", "::debug ::test%0A\n"), - ("%test", "::debug ::%25test\n"), - ("\rtest", "::debug ::%0Dtest\n"), + ("test\n", "::debug ::test\n\n"), + ("%test", "::debug ::%test\n"), + ("\rtest", "::debug ::\rtest\n"), ], ) def test_debug(capfd: Any, test_input: str, expected: str) -> None: @@ -289,9 +289,9 @@ def test_get_user_input() -> None: "test_input,expected", [ ("test", "::group ::test\n"), - ("test\n", "::group ::test%0A\n"), - ("%test", "::group ::%25test\n"), - ("\rtest", "::group ::%0Dtest\n"), + ("test\n", "::group ::test\n\n"), + ("%test", "::group ::%test\n"), + ("\rtest", "::group ::\rtest\n"), ], ) def test_start_group(capfd: Any, test_input: str, expected: str) -> None: