1#!perl
2use strict;
3BEGIN{ if (not $] < 5.006) { require warnings; warnings->import } }
4
5select(STDERR); $|=1;
6select(STDOUT); $|=1;
7
8use Test::More;
9use Config::Tiny;
10use IO::CaptureOutput qw/capture/;
11use File::Spec;
12use File::Temp qw/tempdir/;
13use File::Path qw/mkpath rmtree/;
14use t::Frontend;
15
16plan tests => 9;
17
18#--------------------------------------------------------------------------#
19# Fixtures
20#--------------------------------------------------------------------------#
21
22my $temp_home = File::Spec->catdir( File::Spec->tmpdir(), $$ );
23
24my $old_home = File::Spec->rel2abs( $temp_home );
25my $old_config_dir = File::Spec->catdir( $old_home, ".cpanreporter" );
26my $old_config_file = File::Spec->catfile( $old_config_dir, "config.ini" );
27my $new_home = $old_home . "-new";
28my $new_config_dir = File::Spec->catdir( $new_home, ".cpanreporter" );
29my $new_config_file = File::Spec->catfile( $new_config_dir, "config.ini" );
30
31my ($rc, $stdout, $stderr);
32my $email_line = "email_address = johndoe\@doe.org\n";
33
34mkpath $old_config_dir;
35open FILE, ">$old_config_file" or die $!;
36print FILE $email_line;
37close FILE;
38
39#--------------------------------------------------------------------------#
40# Mocking -- override support/system functions
41#--------------------------------------------------------------------------#
42
43BEGIN {
44    $INC{"File/HomeDir.pm"} = 1; # fake load
45}
46
47package File::HomeDir;
48our $VERSION = 999;
49sub my_documents { return $old_home };
50sub my_home { return $new_home };
51
52package main;
53
54#--------------------------------------------------------------------------#
55
56# Make sure nothing happens when OS is not Darwin
57
58{
59    local $^O = 'unknown';
60    require_ok('CPAN::Reporter::Config');
61    ok( -d $old_config_dir,
62        "non-darwin logic: old config dir still in place"
63    );
64    ok( ! -d $new_config_dir,
65        "non-darwin logic: new config dir not created"
66    );
67}
68
69# Reset %INC to get CPAN::Reporter to load again
70delete $INC{'CPAN/Reporter/Config.pm'};
71delete ${*CPAN::Reporter::Config}{$_} for ( keys %{*CPAN::Reporter::Config} );
72
73{
74    local $^O = 'darwin';
75    capture sub {
76        require_ok( "CPAN::Reporter::Config" );
77    };
78    ok( $INC{'CPAN/Reporter/Config.pm'},
79        "CPAN::Reporter::Config reloaded"
80    );
81    ok( ! -d $old_config_dir,
82        "darwin logic: old config-dir removed"
83    );
84    ok( -d $new_config_dir,
85        "darwin logic: new config-dir created"
86    );
87    open CONFIG, "<$new_config_file" or die $!;
88    is( scalar <CONFIG>, $email_line,
89        "darwin logic: new config contents correct"
90    );
91    close CONFIG;
92}
93
94# cleanup
95
96rmtree $new_home;
97rmtree $old_home;
98
99ok( ( ! -d $old_home) && ( ! -d $new_home ),
100    "cleaned up temp directories"
101);
102