File: 72_rename_history.t

package info (click to toggle)
libcpan-reporter-perl 1.2020-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,752 kB
  • sloc: perl: 5,440; makefile: 2
file content (91 lines) | stat: -rw-r--r-- 2,673 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use strict;
BEGIN{ if (not $] < 5.006) { require warnings; warnings->import } }

select(STDERR); $|=1;
select(STDOUT); $|=1;

use Test::More;
use Config::Tiny;
use Capture::Tiny qw/capture/;
use File::Copy qw/copy/;
use File::Path qw/mkpath/;
use File::Spec::Functions qw/catdir catfile rel2abs/;
use File::Temp qw/tempdir/;
use lib 't/lib';
use Frontend;
use MockHomeDir;

#plan 'no_plan';
plan tests => 12;

#--------------------------------------------------------------------------#
# Fixtures
#--------------------------------------------------------------------------#


my $config_dir = catdir( MockHomeDir::home_dir, ".cpanreporter" );
my $config_file = catfile( $config_dir, "config.ini" );

my $old_history_file = catfile( $config_dir, "history.db" );
my $new_history_file = catfile( $config_dir, "reports-sent.db" );

my $sample_old_file = catfile(qw/t history history.db/); 
my $sample_new_file = catfile(qw/t history reports-sent.db/); 

my ($rc, $stdout, $stderr);

#--------------------------------------------------------------------------#

sub re_require {
    delete $INC{'CPAN/Reporter/History.pm'};
    eval {
        ($stdout, $stderr) = capture {
            require_ok( "CPAN::Reporter::History" );
        };
    };
    die $@ if $@;
    return 1;
}

sub mtime {
    return (stat shift)[9];
}

sub read_file {
    my $fh = IO::File->new(shift);
    local $/;
    return scalar <$fh>;
}

#--------------------------------------------------------------------------##
# begin testing
#--------------------------------------------------------------------------#

mkpath( $config_dir );
ok( -d $config_dir, "temporary config dir created" );
ok( ! -f $old_history_file && ! -f $new_history_file, "no history files yet");

# Nothing should be created if nothing exists
re_require();
ok( ! -f $old_history_file && ! -f $new_history_file, "still no history files");

# If old history exists, convert it
copy( $sample_old_file, $old_history_file);
ok( -f $old_history_file, "copied sample old history file to config directory");
re_require();
like( $stdout, "/Upgrading automatically/", "saw upgrading notice" );
ok( -f $old_history_file, "old history file still exists" );
ok( -f $new_history_file, "new history file was created" );

my $expected_file = scalar read_file($sample_new_file);
$expected_file =~ s/VERSION/$CPAN::Reporter::History::VERSION/;
is( scalar read_file($new_history_file), $expected_file,
    "new history contents as expected"
);

# If new history exists, leave it alone
my $mtime = mtime( $new_history_file );
sleep(2); # ensure mtime check works
re_require();
is( mtime($new_history_file), $mtime, "new history file unmodified" );