| 1 | package Test::BDD::Cucumber::Definitions::File; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Const::Fast; | ||
| 7 | use DDP ( show_unicode => 1 ); | ||
| 8 | use Exporter qw(import); | ||
| 9 | use File::Slurper; | ||
| 10 | use File::Spec::Functions qw(catdir splitdir); | ||
| 11 | use File::Basename qw(dirname); | ||
| 12 | use IO::Capture::Stderr; | ||
| 13 | use IPC::Run3; | ||
| 14 | use Test::BDD::Cucumber::Definitions qw(S :validator); | ||
| 15 | use Test::More; | ||
| 16 | use Try::Tiny; | ||
| 17 | |||
| 18 | our $VERSION = '0.41'; | ||
| 19 | |||
| 20 | our @EXPORT_OK = qw(File); | ||
| 21 | |||
| 22 | const my %TYPES => ( | ||
| 23 | 'regular file' => sub { return -f $_[0] }, | ||
| 24 | 'directory' => sub { return -d $_[0] }, | ||
| 25 | 'symbolic link' => sub { return -l $_[0] }, | ||
| 26 | 'fifo' => sub { return -p $_[0] }, | ||
| 27 | 'socket' => sub { return -S $_[0] }, | ||
| 28 | 'block special file' => sub { return -b $_[0] }, | ||
| 29 | 'character special file' => sub { return -c $_[0] }, | ||
| 30 | ); | ||
| 31 | |||
| 32 | ## no critic [Subroutines::RequireArgUnpacking] | ||
| 33 | |||
| 34 | sub File { | ||
| 35 | return __PACKAGE__; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub path_set { | ||
| 39 | my $self = shift; | ||
| 40 | my ($path) = validator_n->(@_); | ||
| 41 | |||
| 42 | S->{File} = __PACKAGE__; | ||
| 43 | |||
| 44 | S->{_File}->{path} = $path; | ||
| 45 | |||
| 46 | return 1; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub exists_yes { | ||
| 50 | my $self = shift; | ||
| 51 | |||
| 52 | my @dirs = splitdir( dirname( S->{_File}->{path} ) ); | ||
| 53 | |||
| 54 | my $dirname = q{}; | ||
| 55 | |||
| 56 | for my $dir (@dirs) { | ||
| 57 | $dirname = catdir( $dirname, $dir ); | ||
| 58 | |||
| 59 | # intermediate dir exists | ||
| 60 | if ( !-e $dirname ) { | ||
| 61 | fail('Intermediate directories exist'); | ||
| 62 | diag( sprintf( q{Missing directory '%s': %s}, $dirname, $! ) ); | ||
| 63 | |||
| 64 | return; | ||
| 65 | } | ||
| 66 | |||
| 67 | my $dirpath = _dirpath($dirname); | ||
| 68 | |||
| 69 | # intermediate dir is available | ||
| 70 | if ( !-e $dirpath ) { | ||
| 71 | fail('Intermediate directories are available'); | ||
| 72 | diag( sprintf( q{Unavailable directory '%s': %s}, $dirpath, $! ) ); | ||
| 73 | |||
| 74 | return; | ||
| 75 | } | ||
| 76 | |||
| 77 | } | ||
| 78 | |||
| 79 | pass('Intermediate directories exist and are available'); | ||
| 80 | |||
| 81 | if ( !ok( -e S->{_File}->{path}, "File exists" ) ) { | ||
| 82 | diag( sprintf( q{File '%s': %s}, S->{_File}->{path}, $! ) ); | ||
| 83 | |||
| 84 | return; | ||
| 85 | } | ||
| 86 | |||
| 87 | return 1; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub exists_no { | ||
| 91 | my $self = shift; | ||
| 92 | |||
| 93 | my @dirs = splitdir( dirname( S->{_File}->{path} ) ); | ||
| 94 | |||
| 95 | my $dirname = q{}; | ||
| 96 | |||
| 97 | for my $dir (@dirs) { | ||
| 98 | $dirname = catdir( $dirname, $dir ); | ||
| 99 | |||
| 100 | # intermediate dir no exists | ||
| 101 | if ( !-e $dirname ) { | ||
| 102 | pass('Intermediate directory not exists'); | ||
| 103 | diag( sprintf( q{Missing directory '%s': %s}, $dirname, $! ) ); | ||
| 104 | |||
| 105 | return 1; | ||
| 106 | } | ||
| 107 | |||
| 108 | my $dirpath = _dirpath($dirname); | ||
| 109 | |||
| 110 | # intermediate dir is available | ||
| 111 | if ( !-e $dirpath ) { | ||
| 112 | fail('Intermediate directories is available'); | ||
| 113 | diag( sprintf( q{Unavailable directory '%s': %s}, $dirpath, $! ) ); | ||
| 114 | |||
| 115 | return; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | pass('Intermediate catalogs (if any) are available'); | ||
| 120 | |||
| 121 | return if ( !ok( !-e S->{_File}->{path}, 'File not exists' ) ); | ||
| 122 | |||
| 123 | return 1; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub type_is { | ||
| 127 | my $self = shift; | ||
| 128 | my ($type) = validator_n->(@_); | ||
| 129 | |||
| 130 | return if !File->exists_yes(); | ||
| 131 | |||
| 132 | if ( !ok( exists $TYPES{$type}, 'A valid file type is specified' ) ) { | ||
| 133 | diag( 'Unknown file type ' . np $type); | ||
| 134 | |||
| 135 | return; | ||
| 136 | } | ||
| 137 | |||
| 138 | if ( !ok( $TYPES{$type}->( S->{_File}->{path} ), "File type is a $type" ) ) { | ||
| 139 | diag( sprintf( q{ Error: %s}, $! ) ); | ||
| 140 | |||
| 141 | _stat( S->{_File}->{path} ); | ||
| 142 | |||
| 143 | return; | ||
| 144 | } | ||
| 145 | |||
| 146 | return 1; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub read_text { | ||
| 150 | my $self = shift; | ||
| 151 | my ($encoding) = validator_n->(@_); | ||
| 152 | |||
| 153 | return _read($encoding); | ||
| 154 | } | ||
| 155 | |||
| 156 | sub read_binary { | ||
| 157 | my $self = shift; | ||
| 158 | |||
| 159 | return _read(); | ||
| 160 | } | ||
| 161 | |||
| 162 | sub content { | ||
| 163 | my $self = shift; | ||
| 164 | |||
| 165 | return S->{_File}->{content}; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub _read { | ||
| 169 | my ($encoding) = @_; | ||
| 170 | |||
| 171 | my $error; | ||
| 172 | |||
| 173 | my $capture = IO::Capture::Stderr->new(); | ||
| 174 | |||
| 175 | $capture->start(); | ||
| 176 | |||
| 177 | S->{_File}->{content} = try { | ||
| 178 | if ( defined $encoding ) { | ||
| 179 | return File::Slurper::read_text( S->{_File}->{path}, $encoding ); | ||
| 180 | } | ||
| 181 | else { | ||
| 182 | return File::Slurper::read_binary( S->{_File}->{path} ); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | catch { | ||
| 186 | $error = $_[0]; | ||
| 187 | |||
| 188 | return; | ||
| 189 | }; | ||
| 190 | |||
| 191 | $capture->stop(); | ||
| 192 | |||
| 193 | if ( !ok( !$error, qq{File read} ) ) { | ||
| 194 | diag($error); | ||
| 195 | |||
| 196 | for ( $capture->read ) { | ||
| 197 | diag("Warning: $_"); | ||
| 198 | } | ||
| 199 | |||
| 200 | return; | ||
| 201 | } | ||
| 202 | |||
| 203 | return 1; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub _stat { | ||
| 207 | my ($filename) = @_; | ||
| 208 | |||
| 209 | run3( [ 'stat', $filename ], undef, \my $out, \my $err ); | ||
| 210 | |||
| 211 | diag( $err || $out ); | ||
| 212 | |||
| 213 | return; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub _dirpath { | ||
| 217 | my ($dirname) = @_; | ||
| 218 | |||
| 219 | if ( $dirname ne q{/} ) { | ||
| 220 | $dirname .= q{/}; | ||
| 221 | } | ||
| 222 | |||
| 223 | $dirname .= q{.}; | ||
| 224 | |||
| 225 | return $dirname; | ||
| 226 | } | ||
| 227 | |||
| 228 | 1; |