File: 70_darwin_move_config.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 (102 lines) | stat: -rw-r--r-- 2,720 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
92
93
94
95
96
97
98
99
100
101
102
#!perl
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::Spec;
use File::Temp qw/tempdir/;
use File::Path qw/mkpath rmtree/;
use lib 't/lib';
use Frontend;

plan tests => 9;

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

my $temp_home = File::Spec->catdir( File::Spec->tmpdir(), $$ );

my $old_home = File::Spec->rel2abs( $temp_home );
my $old_config_dir = File::Spec->catdir( $old_home, ".cpanreporter" );
my $old_config_file = File::Spec->catfile( $old_config_dir, "config.ini" );
my $new_home = $old_home . "-new";
my $new_config_dir = File::Spec->catdir( $new_home, ".cpanreporter" );
my $new_config_file = File::Spec->catfile( $new_config_dir, "config.ini" );

my ($rc, $stdout, $stderr);
my $email_line = "email_address = johndoe\@doe.org\n";

mkpath $old_config_dir;
open FILE, ">$old_config_file" or die $!;
print FILE $email_line;
close FILE;

#--------------------------------------------------------------------------#
# Mocking -- override support/system functions
#--------------------------------------------------------------------------#

BEGIN {
    $INC{"File/HomeDir.pm"} = 1; # fake load
}

package File::HomeDir;
our $VERSION = 999;
sub my_documents { return $old_home };
sub my_home { return $new_home };

package main;

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

# Make sure nothing happens when OS is not Darwin

{
    local $^O = 'unknown';
    require_ok('CPAN::Reporter::Config');
    ok( -d $old_config_dir,
        "non-darwin logic: old config dir still in place"
    );
    ok( ! -d $new_config_dir,
        "non-darwin logic: new config dir not created"
    );
}

# Reset %INC to get CPAN::Reporter to load again
delete $INC{'CPAN/Reporter/Config.pm'};
delete ${*CPAN::Reporter::Config}{$_} for ( keys %{*CPAN::Reporter::Config} );

{
    local $^O = 'darwin';
    capture {
        require_ok( "CPAN::Reporter::Config" );
    };
    ok( $INC{'CPAN/Reporter/Config.pm'},
        "CPAN::Reporter::Config reloaded"
    );
    ok( ! -d $old_config_dir,
        "darwin logic: old config-dir removed"
    );
    ok( -d $new_config_dir,
        "darwin logic: new config-dir created"
    );
    open CONFIG, "<$new_config_file" or die $!;
    is( scalar <CONFIG>, $email_line,
        "darwin logic: new config contents correct"
    );
    close CONFIG;
}

# cleanup

rmtree $new_home;
rmtree $old_home;

ok( ( ! -d $old_home) && ( ! -d $new_home ),
    "cleaned up temp directories"
);