File: exceptions.t

package info (click to toggle)
libcpan-mini-inject-perl 1.012-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 352 kB
  • sloc: perl: 732; makefile: 4
file content (363 lines) | stat: -rw-r--r-- 10,049 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
use strict;
use warnings;

use Test::More;

BEGIN {
	eval "use Test::Exception";
	plan skip_all => "Test::Exception required for exceptions.t" if $@;
	}

use File::Spec::Functions qw(catfile);
use File::Path qw(make_path);
use File::Temp ();
use Socket qw(getaddrinfo);

use lib 't/lib';
use Local::utils;

my $class = 'CPAN::Mini::Inject';

$SIG{'INT'} = sub { print "\nCleaning up before exiting\n"; exit 1 };
my $temp_dir = File::Temp::tempdir(CLEANUP=>1);

subtest 'sanity' => sub {
	use_ok $class or BAIL_OUT( "Could not load $class: $@" );
	can_ok $class, 'new';
	isa_ok $class->new, $class;
	};

subtest 'config problems' => sub {
	subtest 'no config' => sub {
		delete local $ENV{HOME};
		delete local $ENV{MCPANI_CONFIG};

		SKIP: {
			skip 'Global config file exists. Cannot test no config situation.', 1 if global_config_exists();
			my $mcpi = $class->new;
			isa_ok $mcpi, $class;
			dies_ok { $mcpi->loadcfg } 'No config file';
			}
		};

	subtest 'bad config' => sub {
		my $tmp_config_file = catfile $temp_dir, 'bad_config';
		subtest 'create bad config file' => sub {
			my $fh;
			if( open $fh, '>', $tmp_config_file ) {
				print {$fh} <<'HERE';
# This file is missing a local setting.
remote : http://www.cpan.org
repository: t/local/MYCPAN
passive: yes
This line will be ignored
HERE
				ok close($fh), "created bad config file";
				}
			else {
				fail("could not create config with missing local setting");
				}
			};

		ok -e $tmp_config_file, 'bad config with missing local setting file exists';

		my $mcpi = $class->new;
		isa_ok $mcpi, $class;
		local $SIG{__WARN__} = sub {1}; # suppress warning about "This line will be ignored"
		dies_ok { $mcpi->parsecfg( $tmp_config_file ); } 'Missing local setting blows up';
		};

	subtest 'unreadable' => sub {
		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 $repo_dir = catfile $temp_dir, 'injects';
			ok make_path($repo_dir), "make_path for injects/ succeeded";

			my $tmp_config_file = catfile $temp_dir, 'bad_config';
			my $fh;
			if(open $fh, '>', $tmp_config_file) {
				print {$fh} "Hello";
				close $fh;
				chmod 0111, $tmp_config_file;
				is( mode($tmp_config_file), 0111, 'mode for config is 0111' );
				ok -e $tmp_config_file, 'config file exists';
				ok ! -r $tmp_config_file, 'config file is not readable';
				}
			else {
				fail("Could not create an unreadable file");
				}

			my $mcpi = $class->new;
			isa_ok $mcpi, $class;

			dies_ok { $mcpi->parsecfg($tmp_config_file) } 'unreadable file';
			like $@, qr/Could not read file/, 'exception has expected message';
			chmod 0644, $tmp_config_file;
			}
		};

	subtest 'no repo config' => sub {
		my $tmp_config_file = catfile $temp_dir, 'bad_config';
		subtest 'create no repo config file' => sub {
			my $fh;
			if(open $fh, '>', $tmp_config_file) {
				print {$fh} "local: t/local/CPAN\nremote: http://www.cpan.org\n";
				close $fh;
				ok -e $tmp_config_file, 'config file exists';
				ok -r $tmp_config_file, 'config file is readable';
				}
			else {
				fail("Could not create no repo config file");
				}
			};

		my $mcpi = $class->new;
		isa_ok $mcpi, $class;

		lives_ok { $mcpi->parsecfg($tmp_config_file) } 'no repo config file parses';
		dies_ok {
			$mcpi->add(
			  module   => 'CPAN::Mini::Inject',
			  authorid => 'SSORICHE',
			  version  => '0.01',
			  file     => 'test-0.01.tar.gz'
			);
			} 'Missing config repository';
		like $@, qr/no repository configured/, 'exception has expected message';
		};

	subtest 'read-only repo' => sub {
		SKIP: {
			skip 'this system does not do file modes', 3 unless has_modes();
			my $tmp_config_file = catfile $temp_dir, 'bad_config';

			my $repo_dir = catfile $temp_dir, 'read-only-injects';
			subtest 'create read-only repo dir' => sub {
				ok make_path($repo_dir), 'created repo dir';
				chmod 0555, $repo_dir;
				is mode($repo_dir), 0555, 'repo dir has mode 444';
				ok ! -w $repo_dir, 'repo dir is not writable';
				};

			subtest 'create read-only repo config file' => sub {
				my $fh;
				if(open $fh, '>', $tmp_config_file) {
				print {$fh} <<"HERE";
local: $temp_dir
remote: http://www.cpan.org
repository: $repo_dir
HERE
					close $fh;
					ok -e $tmp_config_file, 'config file exists';
					ok -r $tmp_config_file, 'config file is readable';
					}
				else {
					fail("Could not create read-only repo config file");
					}
				};

			subtest 'try to add to read-only repo' => sub {
				my $mcpi = $class->new;
				isa_ok $mcpi, $class;

				lives_ok { $mcpi->parsecfg($tmp_config_file) } 'read-only repo config file parses';
				dies_ok {
					$mcpi->add(
					  module   => 'CPAN::Mini::Inject',
					  authorid => 'SSORICHE',
					  version  => '0.01',
					  file     => 'test-0.01.tar.gz'
					);
				  }
				  'read-only repository';
				like $@, qr/cannot write to repository/, 'exception has expected message';
				};

			chmod 755, $repo_dir;
			};
		}
	};

subtest 'add exceptions' => sub {
	my $repo_dir = catfile $temp_dir, 'injects';
	subtest 'create repo dir' => sub {
		ok make_path($repo_dir), 'created repo dir' unless -d $repo_dir;
		chmod 0755, $repo_dir;
		is mode($repo_dir), 0755, 'repo dir has mode 444' if has_modes();
		ok -r $repo_dir, 'repo dir is readable';
		ok -w $repo_dir, 'repo dir is writable';
		};

	my $tmp_config_file = catfile $temp_dir, 'good_config';
	subtest 'create config file' => sub {
		my $fh;
		if(open $fh, '>', $tmp_config_file) {
			print {$fh} <<"HERE";
local: $temp_dir
remote : http://localhost:11027
repository: $repo_dir
dirmode: 0775
passive: yes
HERE
			close $fh;
			ok -e $tmp_config_file, 'config file exists';
			ok -r $tmp_config_file, 'config file is readable';
			}
		else {
			fail("Could not create config file");
			}
		};

	my $mcpi = $class->new;
	isa_ok $mcpi, $class;

	lives_ok { $mcpi->parsecfg( $tmp_config_file ) } 'parsecfg works';

	subtest 'missing file param' => sub {
		dies_ok {
			$mcpi->add(
				module   => 'CPAN::Mini::Inject',
				authorid => 'SSORICHE',
				version  => '0.01'
				);
			} 'Missing add param';
		like $@, qr/required option not specified: file/,  'exception has expected message';
		};

	subtest 'module file is missing' => sub {
		dies_ok {
			$mcpi->add(
				module   => 'CPAN::Mini::Inject',
				authorid => 'SSORICHE',
				version  => '0.01',
				file     => 'blahblah'
				);
		} 'Module file not readable';
		like $@, qr/cannot read module file: blahblah/,  'exception has expected message';
		};

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

	subtest 'not discoverable' => sub {
		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';
  		};

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

subtest 'remote problems' => sub {
	my $repo_dir = catfile $temp_dir, 'injects';
	subtest 'create repo dir' => sub {
		ok make_path($repo_dir), 'created repo dir' unless -d $repo_dir;
		chmod 0755, $repo_dir;
		is mode($repo_dir), 0755, 'repo dir has mode 755' if has_modes();
		ok -r $repo_dir, 'repo dir is readable';
		ok -w $repo_dir, 'repo dir is writable';
		};

	subtest 'unreachable remote' => sub {
		plan skip_all => "No network tests during Debian build" if $ENV{NO_NETWORK};
		my $unreachable_host = 'com';
		my $url = 'http://$host/';

		my ($lookup_error, @result) = getaddrinfo $unreachable_host, 'http';

		SKIP: {
			plan skip_all => 'bad host resolves, so cannot test that'
				unless $lookup_error;

			my $tmp_config_file = catfile $temp_dir, 'good_config';
			subtest 'create config file' => sub {
				my $fh;
				if(open $fh, '>', $tmp_config_file) {
					print {$fh} <<"HERE";
local: $temp_dir
remote: $url
repository: $repo_dir
dirmode: 0775
passive: yes
HERE
					close $fh;
					ok -e $tmp_config_file, 'config file exists';
					ok -r $tmp_config_file, 'config file is readable';
					}
				else {
					fail("Could not create config file");
					}
				};

			my $mcpi = $class->new;
			isa_ok $mcpi, $class;
			lives_ok { $mcpi->parsecfg( $tmp_config_file ) } 'parsecfg works';
			diag "trying to connect to a bad site: this might take a minute";
			dies_ok { $mcpi->testremote } 'No reachable site';
			like $@, qr/unable to connect/, 'exception has expected message';
			}
		};
	};

# writelist()
subtest 'writelist' => sub {
	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 $repo_dir = catfile $temp_dir, 'injects';
		subtest 'create repo dir' => sub {
			ok make_path($repo_dir), 'created repo dir' unless -d $repo_dir;
			chmod 0555, $repo_dir;
			is mode($repo_dir), 0555, 'repo dir has mode 555';
			ok -r $repo_dir, 'repo dir is readable';
			ok ! -w $repo_dir, 'repo dir is not writable';
			};

		my $tmp_config_file = catfile $temp_dir, 'config';
		subtest 'create config file' => sub {
			my $fh;
			if(open $fh, '>', $tmp_config_file) {
				print {$fh} <<"HERE";
local: $temp_dir
remote : http://www.cpan.org
repository: $repo_dir
HERE
				close $fh;
				ok -e $tmp_config_file, 'config file exists';
				ok -r $tmp_config_file, 'config file is readable';
				}
			else {
				fail("Could not create config file");
				}
			};

		my $mcpi = $class->new;
		isa_ok $mcpi, $class;
		lives_ok { $mcpi->parsecfg( $tmp_config_file ) } 'parsecfg works';
		dies_ok { $mcpi->writelist } 'fail write file';
		like $@, qr//, 'exception has expected message';
		}
	};

done_testing();