Box/Test.pm

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