Box/Test.pm

filter: Statementstmt 75.0% 120 / 160 Branchbran 51.4% 35 / 68 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 77.2% 17 / 22 Podpod 0.0% 0 / 9 totaltotal 65.1% 172 / 264 CC 39 SCAR 27.9
CC
39
Cov
67%
copy_dir
72.0
clean_dir
16.2
unpack_mbox2maildir
13.4
CRAP 16.3
« Box/Net/Message.pm ↑ Summary Box/Message/Destructed.pm »
1 # Copyrights 2001-2020 by [Mark Overmeer].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 # This code is part of distribution Mail-Box. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package Mail::Box::Test;
10 42 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 42 $VERSION = '3.009';
12  
13 42 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 42 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 42 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 42 ▶use File::Copy 'copy';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 42 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 42 ▶use IO::File; # to overrule open()
Subroutine BEGIN: called CC=1 SCAR=0.0
21 42 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 42 ▶use File::Temp 'tempdir';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 42 ▶use Cwd qw(getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 42 ▶use Sys::Hostname qw(hostname);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 42 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27  
28 42 our @EXPORT =
29 qw/clean_dir copy_dir
30 unpack_mbox2mh unpack_mbox2maildir
31 compare_lists listdir
32 compare_message_prints reproducable_text
33 compare_thread_dumps
34  
35 $folderdir
36 $workdir
37 $src $unixsrc $winsrc
38 $fn $unixfn $winfn
39 $cpy $cpyfn
40 $raw_html_data
41 $crlf_platform $windows
42 /;
43  
44 42 our ($logfile, $folderdir);
45 42 our ($src, $unixsrc, $winsrc);
46 42 our ($fn, $unixfn, $winfn);
47 42 our ($cpy, $cpyfn);
48 42 our ($crlf_platform, $windows);
49 42 our $workdir;
50  
51 BEGIN {
52 42 ▶ $windows = $^O =~ m/mswin32/i;
Subroutine BEGIN: called CC=2 SCAR=6.9
53 42 $crlf_platform = $windows;
54  
55 42 $folderdir = File::Spec->catdir('t','folders');
56 42 $workdir = tempdir(CLEANUP => 1);
57  
58  
59 42 $logfile = File::Spec->catfile(getcwd(), 'run-log');
60 42 $unixfn = 'mbox.src';
61 42 $winfn = 'mbox.win';
62 42 $cpyfn = 'mbox.cpy';
63  
64 42 $unixsrc = File::Spec->catfile($folderdir, $unixfn);
65 42 $winsrc = File::Spec->catfile($folderdir, $winfn);
66 42 $cpy = File::Spec->catfile($workdir, $cpyfn);
67  
68 42 ▶ ($src, $fn) = $windows ? ($winsrc, $winfn) : ($unixsrc, $unixfn);
Branch: 1 branch
BranchTrueFalseTotal
$Mail::Box::Test::windows ? : 0 42 42
69  
70 # ensure to test the Perl Parser not the C-Parser (separate distribution)
71 42 require Mail::Box::Parser::Perl;
72 42 Mail::Box::Parser->defaultParserType( 'Mail::Box::Parser::Perl' );
73 }
74  
75 #
76 # CLEAN_DIR
77 # Clean a directory structure, typically created by unpack_mbox()
78 #
79  
80 sub clean_dir($);
81 sub clean_dir($)
82 10 ▶{ my $dir = shift;
Subroutine clean_dir: called CC=5 SCAR=27.8
Pod undocumented
83 10 local *DIR;
84 10 ▶ opendir DIR, $dir or return;
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $dir or return 10 0 10
85  
86 0 ▶ my @items = map { m/(.*)/ && "$dir/$1" } # untainted
Branch: 1 branch
BranchTrueFalseTotal
/(.*)/ and "$dir/$1" 0 0 0
87 grep !/^\.\.?$/, readdir DIR;
88 0 foreach (@items)
89 0 ▶ { if(-d) { clean_dir $_ }
Branch: 1 branch
BranchTrueFalseTotal
if (-d $_) { } 0 0 0
90 0 else { unlink $_ }
91 }
92  
93 0 closedir DIR;
94 0 rmdir $dir;
95 }
96  
97 #
98 # COPY_DIR FROM, TO
99 # Copy directory to other place (not recursively), cleaning the
100 # destination first.
101 #
102  
103 sub copy_dir($$)
104 0 ▶{ my ($orig, $dest) = @_;
Subroutine copy_dir: not called CC=8 SCAR=42.8
Pod undocumented
105  
106 0 clean_dir($dest);
107  
108 0 ▶ mkdir $dest
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dest or die "Cannot create copy destination ${dest}: $!\n" 0 0 0
109 or die "Cannot create copy destination $dest: $!\n";
110  
111 0 ▶ opendir ORIG, $orig
Branch: 1 branch
BranchTrueFalseTotal
opendir ORIG, $orig or die "Cannot open directory ${orig}: $!\n" 0 0 0
112 or die "Cannot open directory $orig: $!\n";
113  
114 0 ▶ foreach my $name (map { !m/^\.\.?$/ && m/(.*)/ ? $1 : () } readdir ORIG)
Branch: 1 branch
BranchTrueFalseTotal
!/^\.\.?$/ && /(.*)/ ? : 0 0 0
Condition0%0/3
not /^\.\.?$/ and /(.*)/
!ll&&!rl&&r
000
MC/DC0%0/2
not /^\.\.?$/ and /(.*)/
not /^\.\.?$/ /(.*)/
Truth table0%0/3
not(A) and B
A= not /^\.\.?$/B= /(.*)/
ABresult
0X0
100
111
115 0 { my $from = File::Spec->catfile($orig, $name);
116 0 ▶ next if -d $from;
Branch: 1 branch
BranchTrueFalseTotal
if -d $from 0 0 0
117  
118 0 my $to = File::Spec->catfile($dest, $name);
119 0 ▶ copy($from, $to) or die "Couldn't copy $from,$to: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
copy($from, $to) or die "Couldn't copy $from,${to}: $!\n" 0 0 0
120 }
121  
122 0 close ORIG;
123 }
124  
125 # UNPACK_MBOX2MH
126 # Unpack an mbox-file into an MH-directory.
127 # This skips message-nr 13 for testing purposes.
128 # Blanks before "From" are removed.
129  
130 sub unpack_mbox2mh($$)
131 5 ▶{ my ($file, $dir) = @_;
Subroutine unpack_mbox2mh: called CC=8 SCAR=20.8
Pod undocumented
132 5 clean_dir($dir);
133  
134 5 mkdir $dir, 0700;
135 5 my $count = 1;
136 5 my $blank;
137  
138 5 ▶ open FILE, $file or die;
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $file or die 0 5 5
139 5 open OUT, '>', File::Spec->devnull;
140  
141 5 ▶ while(<FILE>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 16740 5 16745
142 16740 ▶ { if( /^From / )
Branch: 1 branch
BranchTrueFalseTotal
if (/^From /) 225 16515 16740
143 225 { close OUT;
144 225 undef $blank;
145 225 ▶ open OUT, ">$dir/".$count++ or die;
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">$dir/" . $count++ or die 0 225 225
146 225 ▶ $count++ if $count==13; # skip 13 for test
Branch: 1 branch
BranchTrueFalseTotal
if $count == 13 5 220 225
147 225 next; # from line not included in file.
148 }
149  
150 16515 ▶ print OUT $blank
Branch: 1 branch
BranchTrueFalseTotal
if defined $blank 1775 14740 16515
151 if defined $blank;
152  
153 16515 ▶ if( m/^\015?\012$/ )
Branch: 1 branch
BranchTrueFalseTotal
if (/^\015?\012$/) 2000 14515 16515
154 2000 { $blank = $_;
155 2000 next;
156 }
157  
158 14515 undef $blank;
159 14515 print OUT;
160 }
161  
162 5 close OUT;
163 5 close FILE;
164 }
165  
166 # UNPACK_MBOX2MAILDIR
167 # Unpack an mbox-file into an Maildir-directory.
168  
169 42 our @maildir_names =
170 ( '8000000.localhost.23:2,'
171 , '90000000.localhost.213:2,'
172 , '110000000.localhost.12:2,'
173 , '110000001.l.42:2,'
174 , '110000002.l.42:2,'
175 , '110000002.l.43:2,'
176 , '110000004.l.43:2,'
177 , '110000005.l.43:2,'
178 , '110000006.l.43:2,'
179 , '110000007.l.43:2,D'
180 , '110000008.l.43:2,DF'
181 , '110000009.l.43:2,DFR'
182 , '110000010.l.43:2,DFRS'
183 , '110000011.l.43:2,DFRST'
184 , '110000012.l.43:2,F'
185 , '110000013.l.43:2,FR'
186 , '110000014.l.43:2,FRS'
187 , '110000015.l.43:2,FRST'
188 , '110000016.l.43:2,DR'
189 , '110000017.l.43:2,DRS'
190 , '110000018.l.43:2,DRST'
191 , '110000019.l.43:2,FS'
192 , '110000020.l.43:2,FST'
193 , '110000021.l.43:2,R'
194 , '110000022.l.43:2,RS'
195 , '110000023.l.43:2,RST'
196 , '110000024.l.43:2,S'
197 , '110000025.l.43:2,ST'
198 , '110000026.l.43:2,T'
199 , '110000027.l.43:2,'
200 , '110000028.l.43:2,'
201 , '110000029.l.43:2,'
202 , '110000030.l.43:2,'
203 , '110000031.l.43:2,'
204 , '110000032.l.43:2,'
205 , '110000033.l.43:2,'
206 , '110000034.l.43:2,'
207 , '110000035.l.43:2,'
208 , '110000036.l.43:2,'
209 , '110000037.l.43:2,'
210 , '110000038.l.43'
211 , '110000039.l.43'
212 , '110000040.l.43'
213 , '110000041.l.43'
214 , '110000042.l.43'
215 );
216  
217 sub unpack_mbox2maildir($$)
218 4 ▶{ my ($file, $dir) = @_;
Subroutine unpack_mbox2maildir: called CC=13 SCAR=25.9
Pod undocumented
219 4 clean_dir($dir);
220  
221 4 ▶ die unless @maildir_names==45;
Branch: 1 branch
BranchTrueFalseTotal
unless @Mail::Box::Test::maildir_names == 45 0 4 4
222  
223 4 ▶ mkdir $dir or die;
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dir or die 0 4 4
224 4 ▶ mkdir File::Spec->catfile($dir, 'cur') or die;
Branch: 1 branch
BranchTrueFalseTotal
mkdir "File::Spec"->catfile($dir, "cur") or die 0 4 4
225 4 ▶ mkdir File::Spec->catfile($dir, 'new') or die;
Branch: 1 branch
BranchTrueFalseTotal
mkdir "File::Spec"->catfile($dir, "new") or die 0 4 4
226 4 ▶ mkdir File::Spec->catfile($dir, 'tmp') or die;
Branch: 1 branch
BranchTrueFalseTotal
mkdir "File::Spec"->catfile($dir, "tmp") or die 0 4 4
227 4 my $msgnr = 0;
228  
229 4 ▶ open FILE, $file or die;
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $file or die 0 4 4
230 4 open OUT, '>', File::Spec->devnull;
231  
232 4 my $last_empty = 0;
233 4 my $blank;
234  
235 4 ▶ while(<FILE>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 13392 4 13396
236 13392 ▶ { if( m/^From / )
Branch: 1 branch
BranchTrueFalseTotal
if (/^From /) 180 13212 13392
237 180 { close OUT;
238 180 undef $blank;
239 180 my $now = time;
240 180 my $hostname = hostname;
241  
242 180 ▶ my $msgfile = File::Spec->catfile($dir
Branch: 1 branch
BranchTrueFalseTotal
$msgnr > 40 ? : 16 164 180
243 , ($msgnr > 40 ? 'new' : 'cur')
244 , $maildir_names[$msgnr++]
245 );
246  
247 180 ▶ open OUT, ">", $msgfile or die "Create $msgfile: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">", $msgfile or die "Create ${msgfile}: $!\n" 0 180 180
248 180 next; # from line not included in file.
249 }
250  
251 13212 ▶ print OUT $blank
Branch: 1 branch
BranchTrueFalseTotal
if defined $blank 1420 11792 13212
252 if defined $blank;
253  
254 13212 ▶ if( m/^\015?\012$/ )
Branch: 1 branch
BranchTrueFalseTotal
if (/^\015?\012$/) 1600 11612 13212
255 1600 { $blank = $_;
256 1600 next;
257 }
258  
259 11612 undef $blank;
260 11612 print OUT;
261 }
262  
263 4 close OUT;
264 4 close FILE;
265 }
266  
267 #
268 # Compare two lists.
269 #
270  
271 sub compare_lists($$)
272 7 ▶{ my ($first, $second) = @_;
Subroutine compare_lists: called CC=4 SCAR=14.1
Pod undocumented
273 #warn "[@$first]==[@$second]\n";
274 7 ▶ return 0 unless @$first == @$second;
Branch: 1 branch
BranchTrueFalseTotal
unless @$first == @$second 0 7 7
275 7 ▶ for(my $i=0; $i<@$first; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$first) 30 7 37
276 30 ▶ { return 0 unless $first->[$i] eq $second->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $first->[$i] eq $second->[$i] 0 30 30
277 }
278 7 1;
279 }
280  
281 #
282 # Compare the text of two messages, rather strict.
283 # On CRLF platforms, the Content-Length may be different.
284 #
285  
286 sub compare_message_prints($$$)
287 0 ▶{ my ($first, $second, $label) = @_;
Subroutine compare_message_prints: not called CC=2 SCAR=17.9
Pod undocumented
288  
289 0 ▶ if($crlf_platform)
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::Box::Test::crlf_platform) 0 0 0
290 0 { $first =~ s/Content-Length: (\d+)/Content-Length: <removed>/g;
291 0 $second =~ s/Content-Length: (\d+)/Content-Length: <removed>/g;
292 }
293  
294 0 is($first, $second, $label);
295 }
296  
297 #
298 # Strip message text down the things which are the same on all
299 # platforms and all situations.
300 #
301  
302 sub reproducable_text($)
303 0 ▶{ my $text = shift;
Subroutine reproducable_text: not called CC=2 SCAR=17.9
Pod undocumented
304 0 my @lines = split /^/m, $text;
305 0 foreach (@lines)
306 0 { s/((?:references|message-id|date|content-length)\: ).*/$1<removed>/i;
307 0 s/boundary-\d+/boundary-<removed>/g;
308 }
309 0 join '', @lines;
310 }
311  
312 #
313 # Compare two outputs of thread details.
314 # On CRLF platforms, the reported sizes are ignored.
315 #
316  
317 sub compare_thread_dumps($$$)
318 0 ▶{ my ($first, $second, $label) = @_;
Subroutine compare_thread_dumps: not called CC=2 SCAR=17.9
Pod undocumented
319  
320 0 ▶ if($crlf_platform)
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::Box::Test::crlf_platform) 0 0 0
321 0 { $first =~ s/^..../ /gm;
322 0 $second =~ s/^..../ /gm;
323 }
324  
325 0 is($first, $second, $label);
326 }
327  
328 #
329 # List directory
330 # This removes '.' and '..'
331 #
332  
333 sub listdir($)
334 0 ▶{ my $dir = shift;
Subroutine listdir: not called CC=2 SCAR=17.9
Pod undocumented
335 0 ▶ opendir LISTDIR, $dir or return ();
Branch: 1 branch
BranchTrueFalseTotal
opendir LISTDIR, $dir or return () 0 0 0
336 0 my @entities = grep !/^\.\.?$/, readdir LISTDIR;
337 0 closedir LISTDIR;
338 0 @entities;
339 }
340  
341 #
342 # A piece of HTML text which is used in some tests.
343 #
344  
345 42 our $raw_html_data = <<'TEXT';
346 <HTML>
347 <HEAD>
348 <TITLE>My home page</TITLE>
349 </HEAD>
350 <BODY BGCOLOR=red>
351  
352 <H1>Life according to Brian</H1>
353  
354 This is normal text, but not in a paragraph.<P>New paragraph
355 in a bad way.
356  
357 And this is just a continuation. When texts get long, they must be
358 auto-wrapped; and even that is working already.
359  
360 <H3>Silly subsection at once</H3>
361 <H1>and another chapter</H1>
362 <H2>again a section</H2>
363 <P>Normal paragraph, which contains an <IMG
364 SRC=image.gif>, some
365 <I>italics with linebreak
366 </I> and <TT>code</TT>
367  
368 <PRE>
369 And now for the preformatted stuff
370 it should stay as it was
371 even with strange blanks
372 and indentations
373 </PRE>
374  
375 And back to normal text...
376 <UL>
377 <LI>list item 1
378 <OL>
379 <LI>list item 1.1
380 <LI>list item 1.2
381 </OL>
382 <LI>list item 2
383 </UL>
384 </BODY>
385 </HTML>
386 TEXT
387  
388 42 1;
« Box/Net/Message.pm ↑ Summary Box/Message/Destructed.pm »