From e92f990ce1c662e79ba4d2c75968e1e01af0a682 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 16 Apr 2024 12:06:54 +0100 Subject: [PATCH] Bug 35681: Add Koha::Installer::Output --- Koha/Installer/Output.pm | 98 +++++++++++++++++++++++++++++++++++++++ t/Koha/Installer/Output.t | 55 ++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 Koha/Installer/Output.pm create mode 100755 t/Koha/Installer/Output.t diff --git a/Koha/Installer/Output.pm b/Koha/Installer/Output.pm new file mode 100644 index 00000000000..6cb3d6a78d8 --- /dev/null +++ b/Koha/Installer/Output.pm @@ -0,0 +1,98 @@ +package Koha::Installer::Output; + +# Copyright 2024 Koha Development Team +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Exporter qw(import); +use Term::ANSIColor qw(:constants); + +our @EXPORT_OK = qw(say_warning say_failure say_success say_info); + +=head1 NAME + +Koha::Installer::Output - Module to provide colored output for Koha installer + +=head1 SYNOPSIS + + use Koha::Installer::Output qw(say_warning say_failure say_success say_info); + + # Output messages with appropriate colors + say_warning($fh, "This is a warning message"); + say_failure($fh, "This is a failure message"); + say_success($fh, "This is a success message"); + say_info($fh, "This is an info message"); + +=head1 DESCRIPTION + +This module provides methods to output messages with appropriate colors for different types of messages: +warnings, failures, successes, and informational messages. + +=head1 EXPORTS + +The following functions can be exported upon request: + +=over 4 + +=item * say_warning($fh, $msg) + +Output a warning message in yellow. + +=item * say_failure($fh, $msg) + +Output a failure message in red. + +=item * say_success($fh, $msg) + +Output a success message in green. + +=item * say_info($fh, $msg) + +Output an informational message in blue. + +=back + +=cut + +sub say_warning { + my ( $fh, $msg ) = @_; + say $fh YELLOW, "$msg", RESET; +} + +sub say_failure { + my ( $fh, $msg ) = @_; + say $fh RED, "$msg", RESET; +} + +sub say_success { + my ( $fh, $msg ) = @_; + say $fh GREEN, "$msg", RESET; +} + +sub say_info { + my ( $fh, $msg ) = @_; + say $fh BLUE, "$msg", RESET; +} + +=head1 AUTHORS + +Martin Renvoize + +=cut + +1; diff --git a/t/Koha/Installer/Output.t b/t/Koha/Installer/Output.t new file mode 100755 index 00000000000..b78b2cd85e5 --- /dev/null +++ b/t/Koha/Installer/Output.t @@ -0,0 +1,55 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 4; + +use Koha::Installer::Output qw(say_warning say_failure say_success say_info); + +# Set up a temporary file for testing output +my $temp_file = "test_output.log"; +open my $fh, '>', $temp_file or die "Cannot open $temp_file: $!"; + +# Redirect output to the temporary file +my $old_fh = select $fh; + +# Test the output functions +say_warning( $fh, "Testing warning message" ); +say_failure( $fh, "Testing failure message" ); +say_success( $fh, "Testing success message" ); +say_info( $fh, "Testing info message" ); + +# Restore the previous output filehandle +select $old_fh; + +# Close the temporary file +close $fh; + +# Read the contents of the temporary file for testing +open my $test_fh, '<', $temp_file or die "Cannot open $temp_file: $!"; +my @lines = <$test_fh>; +close $test_fh; + +# Test the output content +like( $lines[0], qr/\e\[\d+mTesting warning message\e\[0m/, "Warning message output with ANSI color code" ); +like( $lines[1], qr/\e\[\d+mTesting failure message\e\[0m/, "Failure message output with ANSI color code" ); +like( $lines[2], qr/\e\[\d+mTesting success message\e\[0m/, "Success message output with ANSI color code" ); +like( $lines[3], qr/\e\[\d+mTesting info message\e\[0m/, "Info message output with ANSI color code" ); + +# Remove the temporary file +unlink $temp_file; -- 2.44.0