| 1 | package Test::BDD::Infrastructure::Logfile; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | our $VERSION = '1.005'; # VERSION | |||||||||
| 7 | # ABSTRACT: cucumber step definitions for watching log files | ||||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use Test::More; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Test::BDD::Cucumber::StepFile qw( Given When Then ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 0 | ▶ | sub S { Test::BDD::Cucumber::StepFile::S } | ||||||||
|
Subroutine
S: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 13 | |||||||||||
| 14 | 1 | ▶ | use File::Slurp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use File::stat qw(stat); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | |||||||||||
| 18 | 0 | ▶ | Given qr/^the log file (.*) is (searched|watched)$/, sub { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 19 | 0 | my $file = $1; | |||||||||
| 20 | 0 | my $mode = $2; | |||||||||
| 21 | 0 | ▶ | if( ! -e $file ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | 0 | fail("the log file $file does not exist!"); | |||||||||
| 23 | } | ||||||||||
| 24 | 0 | S->{'search_file'} = $file; | |||||||||
| 25 | 0 | ▶ | S->{'search_offset'} = | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | $mode =~ /^watched/ ? stat($file)->size : 0; | ||||||||||
| 27 | 1 | }; | |||||||||
| 28 | |||||||||||
| 29 | 0 | ▶ | sub read_file_offset { | ||||||||
|
Subroutine
read_file_offset: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 30 | 0 | my ( $path, $offset ) = @_; | |||||||||
| 31 | 0 | ▶ | if( ! defined $offset ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 0 | die('no offset given for logfile!'); | |||||||||
| 33 | } | ||||||||||
| 34 | 0 | my $fh = IO::File->new($path, 'r'); | |||||||||
| 35 | 0 | diag("reading logfile with offset $offset"); | |||||||||
| 36 | 0 | $fh->seek($offset, 0); | |||||||||
| 37 | 0 | my ( @lines, $lines ); | |||||||||
| 38 | 0 | ▶ | if( wantarray ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 0 | @lines = read_file( $fh ); | |||||||||
| 40 | 0 | $lines = join('',@lines); | |||||||||
| 41 | } else { | ||||||||||
| 42 | 0 | $lines = read_file( $fh ); | |||||||||
| 43 | } | ||||||||||
| 44 | 0 | $fh->close; | |||||||||
| 45 | 0 | diag("lines written to log file during test:\n$lines-----END OF LOG-----"); | |||||||||
| 46 | 0 | ▶ | if( wantarray ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 0 | return @lines; | |||||||||
| 48 | } | ||||||||||
| 49 | 0 | return $lines; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 0 | ▶ | Then qr/^the log file must ((?:not )?contain) the message (.*)$/, sub { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 53 | 0 | my $match = $1; | |||||||||
| 54 | 0 | my $pattern = $2; | |||||||||
| 55 | 0 | my @lines = read_file_offset( S->{'search_file'}, S->{'search_offset'} ); | |||||||||
| 56 | my @found = grep { | ||||||||||
| 57 | 0 | $lines[$_] =~ /\Q$pattern\E/ | |||||||||
| 58 | } 0..$#lines; | ||||||||||
| 59 | 0 | ▶ | if( @found ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 0 | diag("found pattern $pattern on lines ".join(', ', @found)); | |||||||||
| 61 | } | ||||||||||
| 62 | 0 | ▶ | if( $match eq 'not contain' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | cmp_ok( scalar(@found), '==', 0, 'random string should not be found'); | |||||||||
| 64 | } else { | ||||||||||
| 65 | 0 | cmp_ok( scalar(@found), '==', 1, 'random string must be found 1 time'); | |||||||||
| 66 | } | ||||||||||
| 67 | 1 | }; | |||||||||
| 68 | |||||||||||
| 69 | 1 | 1; | |||||||||
| 70 | |||||||||||
| 71 | __END__ | ||||||||||