Commit | Line | Data |
81ba8d96 |
1 | # AutoLoader.t runs before this test, so it seems safe to assume that it will |
2 | # work. |
3 | |
e69a2255 |
4 | my($incdir, $lib); |
81ba8d96 |
5 | BEGIN { |
6 | chdir 't' if -d 't'; |
08cdd7a3 |
7 | if ($^O eq 'dos') { |
8 | print "1..0 # This test is not 8.3-aware.\n"; |
9 | exit 0; |
10 | } |
81ba8d96 |
11 | if ($^O eq 'MacOS') { |
12 | $incdir = ":auto-$$"; |
e69a2255 |
13 | $lib = '-I::lib:'; |
81ba8d96 |
14 | } else { |
15 | $incdir = "auto-$$"; |
e69a2255 |
16 | $lib = '"-I../lib"'; # ok on unix, nt, The extra \" are for VMS |
81ba8d96 |
17 | } |
7a752413 |
18 | unshift @INC, $incdir; |
19 | unshift @INC, '../lib'; |
81ba8d96 |
20 | } |
21 | my $runperl = "$^X $lib"; |
22 | |
23 | use warnings; |
24 | use strict; |
25 | use Test::More tests => 58; |
26 | use File::Spec; |
27 | use File::Find; |
28 | |
29 | require AutoSplit; # Run time. Check it compiles. |
30 | ok (1, "AutoSplit loaded"); |
31 | |
32 | END { |
33 | use File::Path; |
34 | print "# $incdir being removed...\n"; |
35 | rmtree($incdir); |
36 | } |
37 | |
38 | mkdir $incdir,0755; |
39 | |
40 | my @tests; |
41 | { |
42 | # local this else it buggers up the chomp() below. |
43 | # Hmm. Would be nice to have this as a regexp. |
44 | local $/ |
45 | = "################################################################\n"; |
46 | @tests = <DATA>; |
47 | close DATA; |
48 | } |
49 | |
e69a2255 |
50 | my $pathsep = $^O eq 'MSWin32' ? '\\' : $^O eq 'MacOS' ? ':' : '/'; |
51 | my $endpathsep = $^O eq 'MacOS' ? ':' : ''; |
52 | |
81ba8d96 |
53 | sub split_a_file { |
54 | my $contents = shift; |
55 | my $file = $_[0]; |
56 | if (defined $contents) { |
57 | open FILE, ">$file" or die "Can't open $file: $!"; |
58 | print FILE $contents; |
59 | close FILE or die "Can't close $file: $!"; |
60 | } |
61 | |
62 | # Assumption: no characters in arguments need escaping from the shell or perl |
63 | my $com = qq($runperl -e "use AutoSplit; autosplit (qw(@_))"); |
88e878ad |
64 | print "# command: $com\n"; |
81ba8d96 |
65 | # There may be a way to capture STDOUT without spawning a child process, but |
66 | # it's probably worthwhile spawning, as it ensures that nothing in AutoSplit |
67 | # can load functions from split modules into this perl. |
68 | my $output = `$com`; |
69 | warn "Exit status $? from running: >>$com<<" if $?; |
70 | return $output; |
71 | } |
72 | |
73 | my $i = 0; |
64a3d80f |
74 | my $dir = File::Spec->catdir($incdir, 'auto'); |
75 | if ($^O eq 'VMS') { |
76 | $dir = VMS::Filespec::unixify($dir); |
77 | $dir =~ s/\/$//; |
e69a2255 |
78 | } elsif ($^O eq 'MacOS') { |
79 | $dir =~ s/:$//; |
64a3d80f |
80 | } |
e69a2255 |
81 | |
81ba8d96 |
82 | foreach (@tests) { |
83 | my $module = 'A' . $i . '_' . $$ . 'splittest'; |
84 | my $file = File::Spec->catfile($incdir,"$module.pm"); |
85 | s/\*INC\*/$incdir/gm; |
86 | s/\*DIR\*/$dir/gm; |
87 | s/\*MOD\*/$module/gm; |
88e878ad |
88 | s/\*PATHSEP\*/$pathsep/gm; |
e69a2255 |
89 | s/\*ENDPATHSEP\*/$endpathsep/gm; |
64a3d80f |
90 | s#//#/#gm; |
81ba8d96 |
91 | # Build a hash for this test. |
92 | my %args = /^\#\#\ ([^\n]*)\n # Key is on a line starting ## |
93 | ((?:[^\#]+ # Any number of characters not # |
94 | | \#(?!\#) # or a # character not followed by # |
95 | | (?<!\n)\# # or a # character not preceded by \n |
96 | )*)/sgmx; |
a94ed19d |
97 | foreach ($args{Name}, $args{Require}, $args{Extra}) { |
81ba8d96 |
98 | chomp $_ if defined $_; |
99 | } |
5600ef16 |
100 | $args{Get} ||= ''; |
101 | |
81ba8d96 |
102 | my @extra_args = !defined $args{Extra} ? () : split /,/, $args{Extra}; |
103 | my ($output, $body); |
104 | if ($args{File}) { |
105 | $body ="package $module;\n" . $args{File}; |
106 | $output = split_a_file ($body, $file, $dir, @extra_args); |
107 | } else { |
108 | # Repeat tests |
109 | $output = split_a_file (undef, $file, $dir, @extra_args); |
110 | } |
111 | |
64a3d80f |
112 | if ($^O eq 'VMS') { |
113 | my ($filespec, $replacement); |
114 | while ($output =~ m/(\[.+\])/) { |
115 | $filespec = $1; |
116 | $replacement = VMS::Filespec::unixify($filespec); |
64a3d80f |
117 | $replacement =~ s/\/$//; |
18e548ce |
118 | $output =~ s/\Q$filespec\E/$replacement/; |
64a3d80f |
119 | } |
120 | } |
121 | |
81ba8d96 |
122 | # test n+1 |
5600ef16 |
123 | is($output, $args{Get}, "Output from autosplit()ing $args{Name}"); |
81ba8d96 |
124 | |
125 | if ($args{Files}) { |
126 | $args{Files} =~ s!/!:!gs if $^O eq 'MacOS'; |
127 | my (%missing, %got); |
128 | find (sub {$got{$File::Find::name}++ unless -d $_}, $dir); |
129 | foreach (split /\n/, $args{Files}) { |
130 | next if /^#/; |
64a3d80f |
131 | $_ = lc($_) if $^O eq 'VMS'; |
81ba8d96 |
132 | unless (delete $got{$_}) { |
133 | $missing{$_}++; |
134 | } |
135 | } |
136 | my @missing = keys %missing; |
137 | # test n+2 |
138 | unless (ok (!@missing, "Are any expected files missing?")) { |
139 | print "# These files are missing\n"; |
140 | print "# $_\n" foreach sort @missing; |
141 | } |
142 | my @extra = keys %got; |
143 | # test n+3 |
144 | unless (ok (!@extra, "Are any extra files present?")) { |
145 | print "# These files are unexpectedly present:\n"; |
146 | print "# $_\n" foreach sort @extra; |
147 | } |
148 | } |
149 | if ($args{Require}) { |
e69a2255 |
150 | $args{Require} =~ s|/|:|gm if $^O eq 'MacOS'; |
81ba8d96 |
151 | my $com = 'require "' . File::Spec->catfile ('auto', $args{Require}) . '"'; |
88e878ad |
152 | $com =~ s{\\}{/}gm if ($^O eq 'MSWin32'); |
81ba8d96 |
153 | eval $com; |
154 | # test n+3 |
155 | ok ($@ eq '', $com) or print "# \$\@ = '$@'\n"; |
156 | if (defined $body) { |
157 | eval $body or die $@; |
158 | } |
159 | } |
160 | # match tests to check for prototypes |
161 | if ($args{Match}) { |
162 | local $/; |
163 | my $file = File::Spec->catfile($dir, $args{Require}); |
164 | open IX, $file or die "Can't open '$file': $!"; |
165 | my $ix = <IX>; |
166 | close IX or die "Can't close '$file': $!"; |
167 | foreach my $pat (split /\n/, $args{Match}) { |
168 | next if $pat =~ /^\#/; |
169 | like ($ix, qr/^\s*$pat\s*$/m, "match $pat"); |
170 | } |
171 | } |
172 | # code tests contain eval{}ed ok()s etc |
173 | if ($args{Tests}) { |
174 | foreach my $code (split /\n/, $args{Tests}) { |
175 | next if $code =~ /^\#/; |
176 | defined eval $code or fail(), print "# Code: $code\n# Error: $@"; |
177 | } |
178 | } |
975263bc |
179 | if (my $sleepfor = $args{Sleep}) { |
180 | # We need to sleep for a while |
181 | # Need the sleep hack else the next test is so fast that the timestamp |
182 | # compare routine in AutoSplit thinks that it shouldn't split the files. |
183 | my $time = time; |
184 | my $until = $time + $sleepfor; |
185 | my $attempts = 3; |
186 | do { |
187 | sleep ($sleepfor) |
188 | } while (time < $until && --$attempts > 0); |
189 | if ($attempts == 0) { |
190 | printf << "EOM", time; |
191 | # Attempted to sleep for $sleepfor second(s), started at $time, now %d. |
192 | # sleep attempt ppears to have failed; some tests may fail as a result. |
193 | EOM |
194 | } |
195 | } |
81ba8d96 |
196 | unless ($args{SameAgain}) { |
197 | $i++; |
198 | rmtree($dir); |
199 | mkdir $dir, 0775; |
200 | } |
201 | } |
202 | |
203 | __DATA__ |
204 | ## Name |
205 | tests from the end of the AutoSplit module. |
206 | ## File |
207 | use AutoLoader 'AUTOLOAD'; |
208 | {package Just::Another; |
209 | use AutoLoader 'AUTOLOAD'; |
210 | } |
211 | @Yet::Another::AutoSplit::ISA = 'AutoLoader'; |
212 | 1; |
213 | __END__ |
214 | sub test1 ($) { "test 1"; } |
215 | sub test2 ($$) { "test 2"; } |
216 | sub test3 ($$$) { "test 3"; } |
217 | sub testtesttesttest4_1 { "test 4"; } |
218 | sub testtesttesttest4_2 { "duplicate test 4"; } |
219 | sub Just::Another::test5 { "another test 5"; } |
220 | sub test6 { return join ":", __FILE__,__LINE__; } |
221 | package Yet::Another::AutoSplit; |
222 | sub testtesttesttest4_1 ($) { "another test 4"; } |
223 | sub testtesttesttest4_2 ($$) { "another duplicate test 4"; } |
224 | package Yet::More::Attributes; |
225 | sub test_a1 ($) : locked :locked { 1; } |
226 | sub test_a2 : locked { 1; } |
227 | # And that was all it has. You were expected to manually inspect the output |
228 | ## Get |
229 | Warning: AutoSplit had to create top-level *DIR* unexpectedly. |
e69a2255 |
230 | AutoSplitting *INC**PATHSEP**MOD*.pm (*DIR**PATHSEP**MOD**ENDPATHSEP*) |
88e878ad |
231 | *INC**PATHSEP**MOD*.pm: some names are not unique when truncated to 8 characters: |
e69a2255 |
232 | directory *DIR**PATHSEP**MOD**ENDPATHSEP*: |
81ba8d96 |
233 | testtesttesttest4_1.al, testtesttesttest4_2.al truncate to testtest |
e69a2255 |
234 | directory *DIR**PATHSEP*Yet*PATHSEP*Another*PATHSEP*AutoSplit*ENDPATHSEP*: |
81ba8d96 |
235 | testtesttesttest4_1.al, testtesttesttest4_2.al truncate to testtest |
236 | ## Files |
237 | *DIR*/*MOD*/autosplit.ix |
238 | *DIR*/*MOD*/test1.al |
239 | *DIR*/*MOD*/test2.al |
240 | *DIR*/*MOD*/test3.al |
241 | *DIR*/*MOD*/testtesttesttest4_1.al |
242 | *DIR*/*MOD*/testtesttesttest4_2.al |
243 | *DIR*/Just/Another/test5.al |
244 | *DIR*/*MOD*/test6.al |
245 | *DIR*/Yet/Another/AutoSplit/testtesttesttest4_1.al |
246 | *DIR*/Yet/Another/AutoSplit/testtesttesttest4_2.al |
247 | *DIR*/Yet/More/Attributes/test_a1.al |
248 | *DIR*/Yet/More/Attributes/test_a2.al |
249 | ## Require |
250 | *MOD*/autosplit.ix |
251 | ## Match |
252 | # Need to find these lines somewhere in the required file |
253 | sub test1\s*\(\$\); |
254 | sub test2\s*\(\$\$\); |
255 | sub test3\s*\(\$\$\$\); |
256 | sub testtesttesttest4_1\s*\(\$\); |
257 | sub testtesttesttest4_2\s*\(\$\$\); |
258 | sub test_a1\s*\(\$\)\s*:\s*locked\s*:\s*locked\s*; |
259 | sub test_a2\s*:\s*locked\s*; |
260 | ## Tests |
261 | is (*MOD*::test1 (1), 'test 1'); |
262 | is (*MOD*::test2 (1,2), 'test 2'); |
263 | is (*MOD*::test3 (1,2,3), 'test 3'); |
264 | ok (!defined eval "*MOD*::test1 () eq 'test 1'" and $@ =~ /^Not enough arguments for *MOD*::test1/, "Check prototypes mismatch fails") or print "# \$\@='$@'"; |
265 | is (&*MOD*::testtesttesttest4_1, "test 4"); |
266 | is (&*MOD*::testtesttesttest4_2, "duplicate test 4"); |
267 | is (&Just::Another::test5, "another test 5"); |
268 | # very messy way to interpolate function into regexp, but it's going to be |
269 | # needed to get : for Mac filespecs |
57e04fb8 |
270 | like (&*MOD*::test6, qr!^\Q*INC**PATHSEP**MOD*\E\.pm \(autosplit into \Q@{[File::Spec->catfile('*DIR*','*MOD*', 'test6.al')]}\E\):\d+$!); |
81ba8d96 |
271 | ok (Yet::Another::AutoSplit->testtesttesttest4_1 eq "another test 4"); |
272 | ################################################################ |
273 | ## Name |
274 | missing use AutoLoader; |
275 | ## File |
276 | 1; |
277 | __END__ |
278 | ## Get |
279 | ## Files |
280 | # There should be no files. |
281 | ################################################################ |
282 | ## Name |
283 | missing use AutoLoader; (but don't skip) |
284 | ## Extra |
285 | 0, 0 |
286 | ## File |
287 | 1; |
288 | __END__ |
289 | ## Get |
e69a2255 |
290 | AutoSplitting *INC**PATHSEP**MOD*.pm (*DIR**PATHSEP**MOD**ENDPATHSEP*) |
81ba8d96 |
291 | ## Require |
292 | *MOD*/autosplit.ix |
293 | ## Files |
294 | *DIR*/*MOD*/autosplit.ix |
295 | ################################################################ |
296 | ## Name |
297 | Split prior to checking whether obsolete files get deleted |
298 | ## File |
299 | use AutoLoader 'AUTOLOAD'; |
300 | 1; |
301 | __END__ |
f687c583 |
302 | sub obsolete {our $hidden_a; return $hidden_a++;} |
81ba8d96 |
303 | sub gonner {warn "This gonner function should never get called"} |
304 | ## Get |
e69a2255 |
305 | AutoSplitting *INC**PATHSEP**MOD*.pm (*DIR**PATHSEP**MOD**ENDPATHSEP*) |
81ba8d96 |
306 | ## Require |
307 | *MOD*/autosplit.ix |
308 | ## Files |
309 | *DIR*/*MOD*/autosplit.ix |
310 | *DIR*/*MOD*/gonner.al |
311 | *DIR*/*MOD*/obsolete.al |
312 | ## Tests |
313 | is (&*MOD*::obsolete, 0); |
314 | is (&*MOD*::obsolete, 1); |
975263bc |
315 | ## Sleep |
6ed07066 |
316 | 4 |
81ba8d96 |
317 | ## SameAgain |
318 | True, so don't scrub this directory. |
81ba8d96 |
319 | IIRC DOS FAT filesystems have only 2 second granularity. |
320 | ################################################################ |
321 | ## Name |
322 | Check whether obsolete files get deleted |
323 | ## File |
324 | use AutoLoader 'AUTOLOAD'; |
325 | 1; |
326 | __END__ |
327 | sub skeleton {"bones"}; |
328 | sub ghost {"scream"}; # This definition gets overwritten with the one below |
329 | sub ghoul {"wail"}; |
330 | sub zombie {"You didn't use fire."}; |
331 | sub flying_pig {"Oink oink flap flap"}; |
332 | ## Get |
e69a2255 |
333 | AutoSplitting *INC**PATHSEP**MOD*.pm (*DIR**PATHSEP**MOD**ENDPATHSEP*) |
81ba8d96 |
334 | ## Require |
335 | *MOD*/autosplit.ix |
336 | ## Files |
337 | *DIR*/*MOD*/autosplit.ix |
338 | *DIR*/*MOD*/skeleton.al |
339 | *DIR*/*MOD*/zombie.al |
340 | *DIR*/*MOD*/ghost.al |
341 | *DIR*/*MOD*/ghoul.al |
342 | *DIR*/*MOD*/flying_pig.al |
343 | ## Tests |
344 | is (&*MOD*::skeleton, "bones", "skeleton"); |
ea071790 |
345 | eval {&*MOD*::gonner}; ok ($@ =~ m!^Can't locate auto/*MOD*/gonner.al in \@INC!, "Check &*MOD*::gonner is now a gonner") or print "# \$\@='$@'\n"; |
975263bc |
346 | ## Sleep |
6ed07066 |
347 | 4 |
81ba8d96 |
348 | ## SameAgain |
349 | True, so don't scrub this directory. |
350 | ################################################################ |
351 | ## Name |
352 | Check whether obsolete files remain when keep is 1 |
353 | ## Extra |
354 | 1, 1 |
355 | ## File |
356 | use AutoLoader 'AUTOLOAD'; |
357 | 1; |
358 | __END__ |
359 | sub ghost {"bump"}; |
360 | sub wraith {9}; |
361 | ## Get |
e69a2255 |
362 | AutoSplitting *INC**PATHSEP**MOD*.pm (*DIR**PATHSEP**MOD**ENDPATHSEP*) |
81ba8d96 |
363 | ## Require |
364 | *MOD*/autosplit.ix |
365 | ## Files |
366 | *DIR*/*MOD*/autosplit.ix |
367 | *DIR*/*MOD*/skeleton.al |
368 | *DIR*/*MOD*/zombie.al |
369 | *DIR*/*MOD*/ghost.al |
370 | *DIR*/*MOD*/ghoul.al |
371 | *DIR*/*MOD*/wraith.al |
372 | *DIR*/*MOD*/flying_pig.al |
373 | ## Tests |
374 | is (&*MOD*::ghost, "bump"); |
375 | is (&*MOD*::zombie, "You didn't use fire.", "Are our zombies undead?"); |
975263bc |
376 | ## Sleep |
6ed07066 |
377 | 4 |
81ba8d96 |
378 | ## SameAgain |
379 | True, so don't scrub this directory. |
380 | ################################################################ |
381 | ## Name |
d1be9408 |
382 | Without the timestamp check make sure that nothing happens |
81ba8d96 |
383 | ## Extra |
384 | 0, 1, 1 |
385 | ## Require |
386 | *MOD*/autosplit.ix |
387 | ## Files |
388 | *DIR*/*MOD*/autosplit.ix |
389 | *DIR*/*MOD*/skeleton.al |
390 | *DIR*/*MOD*/zombie.al |
391 | *DIR*/*MOD*/ghost.al |
392 | *DIR*/*MOD*/ghoul.al |
393 | *DIR*/*MOD*/wraith.al |
394 | *DIR*/*MOD*/flying_pig.al |
395 | ## Tests |
396 | is (&*MOD*::ghoul, "wail", "still haunted"); |
397 | is (&*MOD*::zombie, "You didn't use fire.", "Are our zombies still undead?"); |
975263bc |
398 | ## Sleep |
6ed07066 |
399 | 4 |
81ba8d96 |
400 | ## SameAgain |
401 | True, so don't scrub this directory. |
402 | ################################################################ |
403 | ## Name |
d1be9408 |
404 | With the timestamp check make sure that things happen (stuff gets deleted) |
81ba8d96 |
405 | ## Extra |
406 | 0, 1, 0 |
407 | ## Get |
e69a2255 |
408 | AutoSplitting *INC**PATHSEP**MOD*.pm (*DIR**PATHSEP**MOD**ENDPATHSEP*) |
81ba8d96 |
409 | ## Require |
410 | *MOD*/autosplit.ix |
411 | ## Files |
412 | *DIR*/*MOD*/autosplit.ix |
413 | *DIR*/*MOD*/ghost.al |
414 | *DIR*/*MOD*/wraith.al |
415 | ## Tests |
416 | is (&*MOD*::wraith, 9); |
ea071790 |
417 | eval {&*MOD*::flying_pig}; ok ($@ =~ m!^Can't locate auto/*MOD*/flying_pig.al in \@INC!, "There are no flying pigs") or print "# \$\@='$@'\n"; |