# --------------------------------------------------------------------------------------------------------------
class ExpectedError:
    """
    Helper class used to prefix any pending error messages with [Expected Error]
    stdoutFailPatterns.exclude (defined in extension.toml) will cause these errors
    to be ignored when running tests.

    Note that it will prepend only the first error.

    Usage:
        with ExpectedError():
            function_that_produced_error_output()

    """

    def __enter__(self):
        # Preflush any output, otherwise it may be appended to the next statement.
        print("", flush=True)

        # Output the prefix string without a newline so that the error to be ignored will appear on
        # the same line. We do NOT want to flush this because that could allow output from another thread
        # to appear between the prefix and the error message.
        print("[Ignore this error/warning] ", end="", flush=False)

    def __exit__(self, exit_type, value, traceback):
        # Print a newline, to avoid actual errors being ignored.
        print("", flush=True)
