From 9d3c2c29f6dd7f02babcd742b501890243ffcbb2 Mon Sep 17 00:00:00 2001 From: gravel Date: Fri, 19 May 2023 15:14:17 +0000 Subject: [PATCH] CI tests now colorless & fast --- .phpenv | 6 +++++- Makefile | 3 ++- php/utils/logging.php | 24 +++++++++++++++++++++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/.phpenv b/.phpenv index e9c3b31..0941227 100644 --- a/.phpenv +++ b/.phpenv @@ -11,7 +11,7 @@ include_once "$PROJECT_ROOT/php/utils/logging.php"; // Read the -v|--verbose option increasing logging verbosity to debug. - $options = getopt("v", ["verbose", "fast"]); + $options = getopt("v", ["verbose", "fast", "no-color"]); if (isset($options["v"]) or isset($options["verbose"])) { $LOGGING_VERBOSITY = LoggingVerbosity::Debug; } @@ -20,6 +20,10 @@ $FAST_FETCH_MODE = true; } + if (isset($options["no-color"])) { + LoggingVerbosity::$showColor = false; + } + // set timeout for file_get_contents() ini_set('default_socket_timeout', 6); // in seconds, default is 60 diff --git a/Makefile b/Makefile index 0ca5196..aec96fe 100644 --- a/Makefile +++ b/Makefile @@ -72,7 +72,8 @@ test-noninteractive: FLAGS = --verbose test-noninteractive: clean all # Run Continuous Integration tests. -test-ci: test-noninteractive +test-ci: FLAGS = --verbose --fast --no-color +test-ci: clean all # -- Aliases -- serve: server diff --git a/php/utils/logging.php b/php/utils/logging.php index a4f6b36..039705e 100644 --- a/php/utils/logging.php +++ b/php/utils/logging.php @@ -40,15 +40,23 @@ /** * Terminal escape sequence to clear formatting. */ - const COLOR_RESET = "\033[0m"; + private const COLOR_RESET = "\033[0m"; /** - * Returns the color marker for the given logging verbosity. + * Specifies whether to enable terminal colors. + */ + public static bool $showColor = true; + + /** + * Returns the color marker for the given logging verbosity if colors are enabled. * @param int $verbosity Logging verbosity to used for printing. * @return ?string Terminal escape sequence to color foreground text. */ static function getVerbosityColorMarker(int $verbosity): ?string { // See https://en.wikipedia.org/wiki/ANSI_escape_code#Colors for reference. + if (!LoggingVerbosity::$showColor) { + return ''; + } return match($verbosity) { LoggingVerbosity::Error => "\033[31m", LoggingVerbosity::Warning => "\033[93m", @@ -57,6 +65,16 @@ }; } + /** + * @return ?string Terminal escape sequence to turn off color if colors are enabled. + */ + static function getColorResetMarker(): ?string { + if (!LoggingVerbosity::$showColor) { + return ''; + } + return LoggingVerbosity::COLOR_RESET; + } + /** * Returns a pair of optíons trigerring the given verbosity. * @param int $verbosity Logging verbosity to set using flag. @@ -101,7 +119,7 @@ $runtime = runtime_str(); $marker = LoggingVerbosity::getVerbosityMarker($message_verbosity); $color_marker = LoggingVerbosity::getVerbosityColorMarker($message_verbosity); - $color_reset = LoggingVerbosity::COLOR_RESET; + $color_reset = LoggingVerbosity::getColorResetMarker(); // Need to concatenate marker to avoid interpolated array member syntax. fwrite(STDERR, $color_marker . "[$runtime] [$marker] $msg$color_reset" . PHP_EOL); }