Added better handling of 1.0000 to upgrade_db.pl
[dbsrgits/DBM-Deep.git] / t / 44_upgrade_db.t
1 $|++;
2 use strict;
3 use Test::More;
4
5 # Add skips here
6 BEGIN {
7     my @failures;
8     eval { use Pod::Usage; }; push @failures, 'Pod::Usage' if $@;
9     eval { use IO::Scalar; }; push @failures, 'IO::Scalar' if $@;
10     if ( @failures ) {
11         my $missing = join ',', @failures;
12         plan skip_all => "'$missing' must be installed to run these tests";
13     }
14 }
15
16 plan tests => 116;
17
18 use t::common qw( new_fh );
19 use File::Spec;
20 use Test::Deep;
21
22 my $PROG = File::Spec->catfile( qw( utils upgrade_db.pl ) );
23
24 my $short = get_pod( $PROG, 0 );
25 my $long = get_pod( $PROG, 1 );
26
27 is( run_prog( $PROG ), "Missing required parameters.\n$long", "Failed no params" );
28 is( run_prog( $PROG, '-input foo' ), "Missing required parameters.\n$long", "Failed only -input" );
29 is( run_prog( $PROG, '-output foo' ), "Missing required parameters.\n$long", "Failed only -output" );
30 is(
31     run_prog( $PROG, '-input foo', '-output foo' ),
32     "Cannot use the same filename for both input and output.\n$short",
33     "Failed same name",
34 );
35
36 is(
37     run_prog( $PROG, '-input foo', '-output bar' ),
38     "'foo' is not a file.\n$short",
39     "Failed input does not exist",
40 );
41
42 my (undef, $input_filename) = new_fh();
43 my (undef, $output_filename) = new_fh();
44
45 is(
46     run_prog( $PROG, "-input $input_filename", "-output $output_filename" ),
47     "'$input_filename' is not a DBM::Deep file.\n$short",
48     "Input is not a DBM::Deep file",
49 );
50
51 # All files are of the form:
52 #   $db->{foo} = [ 1 .. 3 ];
53
54 my @input_files = (
55     '0-983',
56     '0-99_04',
57     '1-0000',
58 );
59
60 my @output_versions = (
61     '0.91', '0.92', '0.93', '0.94', '0.95', '0.96', '0.97', '0.98',
62     '0.981', '0.982', '0.983',
63     '0.99_01', '0.99_02', '0.99_03', '0.99_04',
64     '1.00', '1.000', '1.0000',
65 );
66
67 foreach my $input_filename (
68     map { 
69         File::Spec->catfile( qw( t etc ), "db-$_" )
70     } @input_files
71 ) {
72     foreach my $v ( @output_versions ) {
73         #print "$input_filename => $output_filename ($v)\n";
74         my $output = run_prog(
75             $PROG,
76             "-input $input_filename",
77             "-output $output_filename",
78             "-version $v",
79         );
80
81         if ( $input_filename =~ /_/ ) {
82             is(
83                 $output, "'$input_filename' is a dev release and not supported.\n$short",
84                 "Input file is a dev release - not supported",
85             );
86
87             next;
88         }
89
90         if ( $v =~ /_/ ) {
91             is(
92                 $output, "-version '$v' is a dev release and not supported.\n$short",
93                 "Output version is a dev release - not supported",
94             );
95
96             next;
97         }
98
99         # Now, read the output file with the right version.
100         ok( !$output, "A successful run produces no output" );
101         die "$output\n" if $output;
102
103         my $db;
104         if ( $v =~ /^0/ ) {
105             push @INC, File::Spec->catdir( 'utils', 'lib' );
106             eval "use DBM::Deep::09830";
107             $db = DBM::Deep::09830->new( $output_filename );
108         }
109         elsif ( $v =~ /^1/ ) {
110             push @INC, 'lib';
111             eval "use DBM::Deep";
112             $db = DBM::Deep->new( $output_filename );
113         }
114         else {
115             die "How did we get here?!\n";
116         }
117
118         ok( $db, "Writing to version $v made a file" );
119
120         cmp_deeply(
121             $db->export,
122             { foo => [ 1 .. 3 ] },
123             "We can read the output file",
124         );
125     }
126 }
127
128 ################################################################################
129
130 sub run_prog {
131     #print "Launching '@_'\n";
132     #XXX This needs to be made OS-portable
133     open( my $fh, '-|', "@_ 2>&1" ) or die "Cannot launch '@_': $!\n";
134     return join '', <$fh>;
135 }
136
137 # In 5.8, we could have used in-memory filehandles and done
138 #     open( my $fh, '>', \my $pod ) or die "Cannot open in-memory filehandle: $!\n";
139 #     ...
140 #     return $pod;
141 # However, DBM::Deep supports 5.6, so this set of contortions will do the trick.
142 sub get_pod {
143     my ($p,$v) = @_;
144
145     my ($fh, $fn) = new_fh();
146     close $fh;
147     open $fh, '>', $fn;
148
149     pod2usage({
150         -input   => $p,
151         -output  => $fh,
152         -verbose => $v,
153         -exitval => 'NOEXIT',
154     });
155
156     close $fh;
157     open $fh, '<', $fn;
158     return join '', <$fh>;
159 }