diff --git a/README.md b/README.md index bd5c019..476073e 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Utility modules written in Perl, for Perl. - Author: Ethan Kletschke -- Version: `0.0.1` +- Version: `0.0.5` - Developed on: Ubuntu (WSL) - License: MIT - Project metadata file: [`project.yaml`](./meta/project.yaml) diff --git a/meta/BACKLOG.md b/meta/BACKLOG.md index 34dc894..7f94574 100644 --- a/meta/BACKLOG.md +++ b/meta/BACKLOG.md @@ -3,11 +3,8 @@ - [ ] `TextFiles` Module - [ ] Log Parsing - [x] Log type counter - - [ ] Log type filter (works like UNIX `grep` but more features(?)) + - [x] Log type filter (works like UNIX `grep` but more features(?)) - [ ] CSV parsing - - [ ] - [ ] General File Handling - - [ ] `can_open()` subroutine - - [ ] `cant_open()` subroutine - - [ ] `file_exists()` subroutine - - [ ] `file_not_exists()` subroutine + - [x] `can_open()` subroutine + - [x] `cant_open()` subroutine diff --git a/meta/project.yaml b/meta/project.yaml index 7ba6233..bfcf027 100644 --- a/meta/project.yaml +++ b/meta/project.yaml @@ -1,12 +1,12 @@ project-meta: project-name: PerlUtils date-started: 2026-10-20 - last-updated: 2026-10-28 + last-updated: 2026-10-31 languages: - Perl status: Experimental license: MIT - version: 0.0.1 + version: 0.0.5 build: dev-environments: diff --git a/src/lib/Utils/TextFiles/LogFileParsing.pm b/src/lib/Utils/TextFiles/LogFileParsing.pm index f3742f4..53fa8b8 100644 --- a/src/lib/Utils/TextFiles/LogFileParsing.pm +++ b/src/lib/Utils/TextFiles/LogFileParsing.pm @@ -17,7 +17,7 @@ sub count_logs_by_type { # If the filename is not provided as an argument unless (defined($log_type) && defined($filename)) { # Abort the app (Temporary Code) - die "File name not provided"; + die "Missing Parameter(s)"; } # Open the specified log file for reading ('<') diff --git a/src/lib/Utils/TextFiles/Simple.pm b/src/lib/Utils/TextFiles/Simple.pm new file mode 100644 index 0000000..0df57a9 --- /dev/null +++ b/src/lib/Utils/TextFiles/Simple.pm @@ -0,0 +1,92 @@ +package Utils::TextFiles::Simple; + +use strict; +use warnings; +use Exporter 'import'; + +our @EXPORT_OK = qw( + can_open + cant_open +); + +sub can_open { + my ($filename) = @_; + + open my $fh, '<', $filename or return 0; + close $fh or return 0; + + return 1; +} + +sub cant_open { + my ($filename) = @_; + + open my $fh, '<', $filename or return 1; + close $fh or return 1; + + return 0; +} + +1; + +__END__ + +=pod + +=head1 NAME + +C - Simple utilities for handling text files. + +=head1 SYNPOSIS + + use Utils::TextFiles::Simple qw(can_open); + + unless (can_open("dev.log")) { + die "Can't open dev.log: $!"; + } + +=head1 DESCRIPTION + +The C module provides utilities for +simple file handling, such as testing if a file can be opened successfully. + +=head1 METHODS + +=head2 can_open + +Tests if a file can be opened successfully. + +=head3 PARAMETERS + +=over 4 + +=item C<$filename> + +The name of the file to try open. + +=back + +=head3 RETURN VALUE + +Returns C<1> if it can, or C<0> if it cannot be opened successfully. + +=head2 cant_open + +Tests if a file B be opened successfully. + +=head3 PARAMETERS + +=over 4 + +=item C<$filename> + +The name of the file to try open. + +=back + +=head3 RETURN VALUE + +Returns C<1> if it cannot be opened successfully, or +C<0> if it can open successfully. + +=cut diff --git a/src/t/TextFiles.t b/src/t/TextFiles.t index a310d99..b09f5c4 100644 --- a/src/t/TextFiles.t +++ b/src/t/TextFiles.t @@ -4,21 +4,68 @@ use lib 'lib'; use strict; use warnings; use Test::More; - +use Utils::TextFiles::Simple qw( + can_open + cant_open +); use Utils::TextFiles::LineCounter qw( linecount non_empty_linecount ); - use Utils::TextFiles::LogFileParsing qw( count_logs_by_type filter_logs ); +# Tests for Simple.pm +subtest "Simple" => sub { + subtest "can_open()" => sub { + open my $fh, '>', "Test.tmp" + or die "Could not create Test.tmp: $!"; + print $fh "I can be opened!\n"; + close $fh; + + is( + can_open("Test.tmp"), + 1, + "File can be opened and sub returns 0" + ); + + isnt( + can_open("Doesnt-Exist.tmp"), + 1, + "Non-existent file returns 0" + ); + + unlink "Test.tmp"; + }; + + subtest "cant_open()" => sub { + open my $fh, '>', "Exists.tmp" + or die "Could not open Exists.tmp: $!"; + close $fh; + + is( + cant_open("Exists.tmp"), + 0, + "File does open, and sub returns 0" + ); + + is( + cant_open("Non-Existent-File.tmp"), + 1, + "File cannot open, and sub returns 1" + ); + + unlink "Exists.tmp"; + }; +}; + +# Tests for LineCounter.pm subtest "LineCounter" => sub { subtest "linecount()" => sub { # Write a test file to test the function results against - open my $fh, ">", "LineCounter.tmp" or die "Could not create LineCounter.tmp: $!\n"; + open my $fh, ">", "LineCounter.tmp" or die "Could not create LineCounter.tmp: $!"; print $fh <<~EOF line 1 line 2 @@ -29,7 +76,7 @@ subtest "LineCounter" => sub { close $fh; # Write an empty test file - open $fh, '>', "empty.txt" or die "Could not create empty.txt: $!\n"; + open $fh, '>', "empty.txt" or die "Could not create empty.txt: $!"; close $fh; # Test that the linecount subroutine correctly returns a count of 4 @@ -63,7 +110,7 @@ subtest "LineCounter" => sub { subtest "non_empty_linecount()" => sub { # Write a test file to test the function results against - open my $fh, ">", "LineCounter.tmp" or die "Could not create LineCounter.tmp: $!\n"; + open my $fh, ">", "LineCounter.tmp" or die "Could not create LineCounter.tmp: $!"; print $fh <<~EOF line 1 line 2 @@ -90,12 +137,13 @@ subtest "LineCounter" => sub { "Doesn't count empty lines" ); - # Delete the testing file + # Delete the testing files unlink "LineCounter.tmp"; unlink "empty.txt"; }; }; +# Tests for LogFileParsing.pm subtest "LogFileParsing" => sub { subtest "count_logs_by_type()" => sub { # Write a test file to test the function results against @@ -165,4 +213,5 @@ subtest "LogFileParsing" => sub { }; }; -done_testing(); +# Check that the number of executed tests is correct +done_testing(3);