This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
1567b3342fa9dee95c17c46174be33dbd0271602
[perl5.git] / ext / Pod-Html / t / pod2html-lib.pl
1 require Cwd;
2 require Pod::Html;
3 require Config;
4 use File::Spec::Functions ':ALL';
5 use File::Path 'remove_tree';
6 use File::Copy;
7
8 # make_test_dir and rem_test_dir dynamically create and remove testdir/test.lib.
9 # it is created dynamically to pass t/filenames.t, which does not allow '.'s in
10 # filenames as '.' is the directory separator on VMS. All tests that require
11 # testdir/test.lib to be present are skipped if test.lib cannot be created.
12 sub make_test_dir {
13     if (-d 'testdir/test.lib') {
14         warn "Directory 'test.lib' exists (it shouldn't yet) - removing it";
15         rem_test_dir();
16     }
17     mkdir('testdir/test.lib') or return "Could not make test.lib directory: $!\n";
18     copy('testdir/perlpodspec-copy.pod', 'testdir/test.lib/podspec-copy.pod')
19         or return "Could not copy perlpodspec-copy: $!";
20     copy('testdir/perlvar-copy.pod', 'testdir/test.lib/var-copy.pod')
21         or return "Could not copy perlvar-copy: $!";
22     return 0;
23 }
24
25 sub rem_test_dir {
26     remove_tree('testdir/test.lib')
27         or warn "Error removing temporary directory 'testdir/test.lib'";
28 }
29
30 sub convert_n_test {
31     my($podfile, $testname, @p2h_args) = @_;
32
33     my $cwd = Cwd::cwd();
34     my ($vol, $dir) = splitpath($cwd, 1);
35     my $relcwd = substr($dir, length(File::Spec->rootdir()));
36         
37     my $new_dir  = catdir $cwd, "t";
38     my $infile   = catfile $new_dir, "$podfile.pod";
39     my $outfile  = catfile $new_dir, "$podfile.html";
40     
41     # To add/modify args to p2h, use @p2h_args
42     Pod::Html::pod2html(
43         "--infile=$infile",
44         "--outfile=$outfile",
45         "--podpath=t",
46         "--htmlroot=/",
47         "--podroot=$cwd",
48         @p2h_args,
49     );
50
51
52     my ($expect, $result);
53     {
54         local $/;
55         # expected
56         $expect = <DATA>;
57         $expect =~ s/\[PERLADMIN\]/$Config::Config{perladmin}/;
58         $expect =~ s/\[RELCURRENTWORKINGDIRECTORY\]/$relcwd/g;
59         if (ord("A") == 193) { # EBCDIC.
60             $expect =~ s/item_mat_3c_21_3e/item_mat_4c_5a_6e/;
61         }
62
63         # result
64         open my $in, $outfile or die "cannot open $outfile: $!";
65         $result = <$in>;
66         close $in;
67     }
68
69     ok($expect eq $result, $testname) or do {
70         my $diff = '/bin/diff';
71         -x $diff or $diff = '/usr/bin/diff';
72         if (-x $diff) {
73             my $expectfile = "pod2html-lib.tmp";
74             open my $tmpfile, ">", $expectfile or die $!;
75             print $tmpfile $expect;
76             close $tmpfile;
77             my $diffopt = $^O eq 'linux' ? 'u' : 'c';
78             open my $diff, "diff -$diffopt $expectfile $outfile |" or die $!;
79             print "# $_" while <$diff>;
80             close $diff;
81             unlink $expectfile;
82         }
83     };
84
85     # pod2html creates these
86     1 while unlink $outfile;
87     1 while unlink "pod2htmd.tmp";
88 }
89
90 1;