From 04008b578e344f8e6d6f77257215a07cd655da33 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Fri, 4 Feb 2022 21:44:00 +0100 Subject: [PATCH] Improving the config tests. --- Makefile.PL | 3 ++ bin/doctor_agustin.pl | 36 ++------------------- lib/DoctorAgustin/Config.pm | 62 +++++++++++++++++++++++-------------- 3 files changed, 45 insertions(+), 56 deletions(-) diff --git a/Makefile.PL b/Makefile.PL index ae6f48f..f34418a 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -7,5 +7,8 @@ WriteMakefile( test => { TESTS => 't/*.t' }, PREREQ_PM => { 'Term::ReadLine::Gnu' => 0, + 'Test::Most' => 0, + 'Test::MockModule' => 0, + 'Test::MockObject' => 0, }, ); diff --git a/bin/doctor_agustin.pl b/bin/doctor_agustin.pl index b3c1aac..4fd141c 100755 --- a/bin/doctor_agustin.pl +++ b/bin/doctor_agustin.pl @@ -6,38 +6,8 @@ use strict; use warnings; use JSON; -use Path::Tiny; -use DoctorAgustin::Logger; +use DoctorAgustin::Config; - -my $logger = DoctorAgustin::Logger->new; -my $term = Term::ReadLine->new('DoctorAgustin'); - -if (! -e $CONFIG_FILE ) { - $logger->log_info(q(Config file not detected)); - create_config_file; -} - -if (! -f $CONFIG_FILE ) { - $logger->log_error(qq($CONFIG_FILE is not a plain file.)); - die; -} - -sub create_config_file { - if (! -e $CONFIG_DIR ) { - $logger->log_info(qq(Creating $CONFIG_DIR)); - eval { - path($CONFIG_DIR)->mkpath; - }; - if ($@) { - $logger->log_error(qq(Unable to create directory $CONFIG_DIR: $@)); - die; - } - } - my $token = $term->readline('Telegram token:'); - my $config_contents = { token => $token }; - $config_contents = encode_json ($config_contents); - - path($CONFIG_FILE)->spew_utf8($config_contents); -} +my $config = DoctorAgustin::Config->new; +say $config->telegram_token; diff --git a/lib/DoctorAgustin/Config.pm b/lib/DoctorAgustin/Config.pm index 706e3db..a511cee 100644 --- a/lib/DoctorAgustin/Config.pm +++ b/lib/DoctorAgustin/Config.pm @@ -9,26 +9,40 @@ use Term::ReadLine; use Const::Fast; use JSON; +use Path::Tiny; -sub HOME { $ENV{HOME} } -sub CONFIG_DIR {"@{[HOME]}/.config/doctoragustin" } +use DoctorAgustin::Logger; + +sub HOME { $ENV{HOME} } +sub CONFIG_DIR { "@{[HOME]}/.config/doctoragustin" } sub CONFIG_FILE { "@{[CONFIG_DIR]}/config.json" } sub new { my $class = shift; - my $self = bless {}, $class; + my $self = bless {}, $class; $self->_create_config_file_if_not_exists; return $self; } -sub config { +sub logger { my $self = shift; - if (!defined $self->{config}) { - if (! -f CONFIG_FILE ) { - $logger->log_error(qq(@{CONFIG_FILE]} is not a plain file, unable to read config.)); - die; + if ( !defined $self->{logger} ) { + my $logger = DoctorAgustin::Logger->new; + $self->{logger} = $logger; } - my $config = decode_json(path(CONFIG_FILE)->slurp_utf8) + return $self->{logger}; +} + +sub _config { + my $self = shift; + if ( !defined $self->{config} ) { + if ( !-f CONFIG_FILE ) { + $self->logger->log_error( + qq(@{[CONFIG_FILE]} is not a plain file, unable to read config.) + ); + die; + } + my $config = decode_json( path(CONFIG_FILE)->slurp_utf8 ); $self->{config} = $config; } return $self->{config}; @@ -36,8 +50,8 @@ sub config { sub telegram_token { my $self = shift; - if (!defined $self->{telegram_token}) { - my $config = $self->config; + if ( !defined $self->{telegram_token} ) { + my $config = $self->_config; $self->{telegram_token} = $config->{telegram_token}; } return $self->{telegram_token}; @@ -45,28 +59,30 @@ sub telegram_token { sub _create_config_file_if_not_exists { my $self = shift; - if (! -e CONFIG_FILE ) { - $logger->log_info(q(Config file not detected)); - $self->create_config_file; + if ( !-e CONFIG_FILE ) { + $self->logger->log_info(q(Config file not detected)); + $self->_create_config_file; } } sub _create_config_file { - if (! -e CONFIG_DIR ) { + my $self = shift; + my $logger = $self->logger; + if ( !-e CONFIG_DIR ) { $logger->log_info(qq(Creating @{[CONFIG_DIR]})); - eval { - path(CONFIG_DIR)->mkpath; - }; + eval { path(CONFIG_DIR)->mkpath; }; if ($@) { - $logger->log_error(qq(Unable to create directory @{[CONFIG_DIR]}: $@)); + $logger->log_error( + qq(Unable to create directory @{[CONFIG_DIR]}: $@)); die; } } - my $token = $term->readline('Telegram token:'); - my $config_contents = { token => $token }; - $config_contents = encode_json ($config_contents); + my $term = Term::ReadLine->new('Doctor Agustín'); + my $token = $term->readline('Telegram token:'); + my $config_contents = { telegram_token => $token }; + $config_contents = encode_json($config_contents); - path($CONFIG_FILE)->spew_utf8($config_contents); + path(CONFIG_FILE)->spew_utf8($config_contents); } 1;