Debian Codegraph @

t/_version.t

raw
1 #!/usr/bin/perl
2
3 use warnings 'all';
4 use strict;
5 BEGIN {
6 if (-d "./lib") {
7 use lib "./lib";
8 }
9 if (-d "../lib") {
10 use lib "../lib";
11 }
12 }
13 use Test::Inter;
14 my $ti;
15
16 BEGIN {
17 $ti = new Test::Inter $0;
18 unless ($ENV{Date_Manip_RELEASE_TESTING}) {
19 $ti->skip_all('Author tests not required for installation (set Date_Manip_RELEASE_TESTING to test)');
20 }
21 }
22
23 # CPANTORPM-DEPREQ REQEXCL IO::File
24 # CPANTORPM-DEPREQ REQEXCL File::Find::Rule
25
26 use IO::File;
27 use File::Find::Rule;
28
29 ################################################################
30 # Test: the directory must contain the version number
31 #
32 # A module is in a directory (with major.minor version):
33 # My-Mod-Name-##.##
34
35 my $libdir = $ti->testdir('lib');
36 my $testdir = $ti->testdir();
37 my $moddir = $ti->testdir('mod');
38 my @dir = split(/\//,$moddir);
39 my $dir = pop(@dir);
40
41 my($mod,$vers,$valid);
42 if ($dir =~ /^(.*)\-(\d+\.\d+)$/) {
43 $mod = $1;
44 $vers = $2;
45 $valid = 1;
46 } else {
47 $valid = 0;
48 }
49
50 $ti->ok($valid,"Valid directory");
51 $ti->skip_all('Remaining tests require a valid directory') if (! defined $vers);
52
53 ################################################################
54 # Test: each .pm file must include a variable:
55 # $VERSION = '##.##'
56 # A file is invalid if it does not have this, or if the version does
57 # not match the version contained in the directory.
58 #
59 # If there is a file _version.ign, it should be a list of filename
60 # substrings to ignore (any .pm file with any of these substrings
61 # will be ignored).
62
63 my @ign = ();
64 if (-f "$testdir/_version.ign") {
65 open(IN,"$testdir/_version.ign");
66 @ign = <IN>;
67 close(IN);
68 chomp(@ign);
69 }
70
71 my $in = new IO::File;
72 my @files = File::Find::Rule->file()->name('*.pm')->in($libdir);
73
74 FILE:
75 foreach my $file (@files) {
76 my $f = $file;
77 $f =~ s,^$libdir/,,;
78 $f =~ s,^$testdir/,, if ($mod eq 'Test-Inter');
79
80 foreach my $ign (@ign) {
81 next FILE if ($file =~ /\Q$ign\E/);
82 }
83
84 $in->open($file);
85 my @tmp = <$in>;
86 chomp(@tmp);
87 my @v = grep /^\$VERSION\s*=\s*['"]\d+\.\d+['"];$/, @tmp;
88 if (! @v) {
89 $ti->ok(0,$f);
90 $ti->diag('File contains no valid version line');
91 } elsif (@v > 1) {
92 $ti->ok(0,$f);
93 $ti->diag('File contains multiple version lines');
94 } else {
95 $v[0] =~ /['"](\d+\.\d+)['"]/;
96 my $v = $1;
97 $ti->is($v,$vers,$f);
98 $ti->diag('File contains incorrect version number') if ($v ne $vers);
99 }
100 }
101
102 ################################################################
103 # Any .pm file with a version method can also be tested by getting
104 # the result of the version method.
105 #
106 # Note that the only module this test is not guaranteed to work on
107 # is Test::Inter because the version that is loaded above is not
108 # necessarily the one on disk.
109
110 my %required;
111 chdir($libdir);
112
113 FILE:
114 foreach my $file (@files) {
115 my $f = $file;
116 $f =~ s,^$libdir/,,;
117
118 foreach my $ign (@ign) {
119 next FILE if ($file =~ /\Q$ign\E/);
120 }
121
122 $in->open($file);
123 my @tmp = <$in>;
124 chomp(@tmp);
125 my $package;
126 foreach my $line (@tmp) {
127 if ($line =~ /^package (\S+)/) {
128 $package = $1;
129 $package =~ s,;$,,;
130 next;
131 }
132 if ($package && $line =~ /^\s*sub\s+version\s*/) {
133 if (! exists $required{$file} && $f ne 'Test/Inter.pm') {
134 require "./$f";
135 $required{$f} = 1;
136 }
137 my $v = eval "${package}::version()";
138 if (! $v) {
139 $ti->ok(0,$package);
140 $ti->diag('version method does not return a version');
141 next;
142 }
143 $ti->is($v,$vers,$package);
144 $ti->diag('version returns incorrect version number') if ($v ne $vers);
145 }
146 }
147 }
148
149 $ti->done_testing();
150