summaryrefslogtreecommitdiff
path: root/t/zz.exceptions.t
blob: f90c9de1f3d558252e3f4c3742cb752bd81b3eff (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use Test::More;

BEGIN {
  eval "use Test::Exception";

  plan skip_all => "Test Exceptions required to test croaks" if $@;
  plan tests => 12;
}

use CPAN::Mini::Inject;
use File::Path;
use Env;
use lib 't/lib';

sub chkcfg {
  return 1 if ( -r '/usr/local/etc/mcpani' );
  return 1 if ( -r '/etc/mcpani' );
}

my $prevhome;
if ( defined( $ENV{HOME} ) ) {
  $prevhome = $ENV{HOME};
  delete $ENV{HOME};
}

my $mcpanienv;
if ( defined( $ENV{MCPANI_CONFIG} ) ) {
  $mcpanienv = $ENV{MCPANI_CONFIG};
  delete $ENV{MCPANI_CONFIG};
}

# loadcfg()
SKIP: {
  skip 'Config file exists', 1 if chkcfg();
  my $mcpi = CPAN::Mini::Inject->new;
  dies_ok { $mcpi->loadcfg } 'No config file';
}

{
  # parsecfg()
  my $mcpi = CPAN::Mini::Inject->new;
  dies_ok { $mcpi->parsecfg( 't/.mcpani/config_bad' ); }
  'Missing config option';
}

# readlist()
SKIP: {
  skip 'User is superuser and can always read', 1 if $< == 0;
  skip 'User is generally superuser under cygwin and can read', 1 if $^O eq 'cygwin';

  my $mcpi = CPAN::Mini::Inject->new;

  rmtree( ['t/local/MYCPAN/modulelist'], 0, 1 );
  mkdir 't/local/MYCPAN';
  $mcpi->parsecfg( 't/.mcpani/config_noread' );
  dies_ok { $mcpi->readlist } 'unreadable file';
  rmtree( ['t/local/MYCPAN/modulelist'], 0, 1 );
}

{
  my $mcpi = CPAN::Mini::Inject->new;
  $mcpi->parsecfg( 't/.mcpani/config' );

  # add()
  dies_ok {
    $mcpi->add(
      module   => 'CPAN::Mini::Inject',
      authorid => 'SSORICHE',
      version  => '0.01'
    );
  }
  'Missing add param';

  dies_ok {
    $mcpi->add(
      module   => 'CPAN::Mini::Inject',
      authorid => 'SSORICHE',
      version  => '0.01',
      file     => 'blahblah'
    );
  }
  'Module file not readable';

  lives_ok {
    $mcpi->add(
      authorid => 'RWSTAUNER',
      file     => 't/local/mymodules/Dist-Metadata-Test-MetaFile-Only.tar.gz'
    );
  }
  'Ok without module/version when discoverable';

  lives_ok {
    $mcpi->add(
      module   => 'Who::Cares',
      version  => '1',
      authorid => 'RWSTAUNER',
      file     => 't/local/mymodules/not-discoverable.tar.gz'
    );
  }
  'Ok without module/version when specified';

  dies_ok {
    $mcpi->add(
      authorid => 'RWSTAUNER',
      file     => 't/local/mymodules/not-discoverable.tar.gz'
    );
  }
  'Dies without module/version when not discoverable';
}

{
  my $mcpi = CPAN::Mini::Inject->new;
  $mcpi->parsecfg( 't/.mcpani/config_norepo' );

  dies_ok {
    $mcpi->add(
      module   => 'CPAN::Mini::Inject',
      authorid => 'SSORICHE',
      version  => '0.01',
      file     => 'test-0.01.tar.gz'
    );
  }
  'Missing config repository';

}

SKIP: {
  skip "We don't have a r/o repo", 2;
  my $mcpi = CPAN::Mini::Inject->new;
  $mcpi->parsecfg( 't/.mcpani/config_read' );

  dies_ok {
    $mcpi->add(
      module   => 'CPAN::Mini::Inject',
      authorid => 'SSORICHE',
      version  => '0.01',
      file     => 'test-0.01.tar.gz'
    );
  }
  'read-only repository';

  $mcpi->{config}{remote} = "ftp://blahblah http://blah blah";
  dies_ok { $mcpi->testremote } 'No reachable site';

}

# writelist()
SKIP: {
  skip 'User is superuser and can always write', 1 if $< == 0;
  skip 'User is generally superuser under cygwin and can write', 1 if $^O eq 'cygwin';

  my $mcpi = CPAN::Mini::Inject->new;
  rmtree( ['t/local/MYCPAN/modulelist'], 0, 1 );
  mkdir 't/local/MYCPAN';
  $mcpi->parsecfg( 't/.mcpani/config_nowrite' );
  dies_ok { $mcpi->writelist } 'fail write file';
  rmtree( ['t/local/MYCPAN/modulelist'], 0, 1 );
}

# Setup routines
sub genmodlist {
  open( MODLIST, '>t/local/MYCPAN/modulelist' )
   or die "Can not create t/local/MYCPAN/modulelist: $!";
  print MODLIST << "EOF"
CPAN::Checksums                   1.016  A/AN/ANDK/CPAN-Checksums-1.016.tar.gz
CPAN::Mini                         0.18  R/RJ/RJBS/CPAN-Mini-0.18.tar.gz
CPANPLUS                         0.0499  A/AU/AUTRIJUS/CPANPLUS-0.0499.tar.gz
EOF
   ;
  close( MODLIST );
}