File: inject.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 (310 lines) | stat: -rw-r--r-- 8,701 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
use strict;
use warnings;

use Test::More;

use File::Path qw(make_path);
use File::Copy;
use File::Temp ();
use File::Basename;
use File::Spec::Functions qw(catfile);
use Compress::Zlib;

use lib qw(t/lib);
use Local::utils;

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

my $temp_dir = File::Temp::tempdir(CLEANUP=>1);

=begin comment

C<remote> is the URL for the repo from which we'll download latest versions

C<local> is our MiniCPAN

C<repository> is the dir where we will keep the modules to inject

=end comment

=cut

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

subtest 'setup directories in temp dir' => sub {
	my @dirs = (
		[ qw(modules) ],
		[ qw(authors) ],
		[ qw(injects) ],
		);

	foreach my $dir ( @dirs ) {
		my $path = catfile $temp_dir, @$dir;
		make_path( $path );
		ok -d $path, "Path for <@$dir> exists";
		}
	};

my $t_local = catfile qw(t local);
subtest 'check local dir' => sub {
	ok -d $t_local, 'local directory exists';
	};

subtest 'copy initial files' => sub {
	my $modules_base = catfile $temp_dir, 'modules';
	ok -d $modules_base, 'modules dir exists';

	my $authors_base = catfile $temp_dir, 'authors';
	ok -d $authors_base, 'authors dir exists';

	subtest 'packages' => sub {
		my $file = '02packages.details.txt.gz';
		my $destination = catfile $modules_base, $file;
		my $rc = copy(
		  catfile( $t_local, 'CPAN', 'modules', "$file.original" ),
		  $destination
		);
		ok $rc, 'File::Copy worked';
		ok -e $destination, 'Copied packages file to temp_dir';
		ok chmod(0666, $destination), 'chmod packages to 0666';
		};

	subtest 'mailrc' => sub {
		my $file = '01mailrc.txt.gz';
		my $destination   = catfile $authors_base, $file;
		my $rc = copy(
		  catfile( $t_local, "$file.original" ),
		  $destination
		);
		ok $rc, 'File::Copy worked';
		ok -e $destination, 'Copied mailrc file to temp_dir';
		ok chmod(0666, $destination), 'chmod mailrc to 0666';
		};
	};

sub get_module_details {
	my( $dist_sources ) = @_;
	my @modules = (
		{
		module   => 'CPAN::Mini::Inject',
		authorid => 'SSORICHE',
		version  => '0.01',
		file     => catfile( $dist_sources, 'CPAN-Mini-Inject-0.01.tar.gz' ),
		},
		{
		module   => 'CPAN::Mini::Inject',
		authorid => 'SSORICHE',
		version  => '0.02',
		file     => catfile( $dist_sources, 'CPAN-Mini-Inject-0.01.tar.gz' ),
		},
		{
		module   => 'CPAN::Mini',
		authorid => 'RJBS',
		version  => '0.17',
		file     => catfile( $dist_sources, 'CPAN-Mini-0.17.tar.gz' ),
		},
		);
	}

sub test_inject {
	my( $mcpi, @modules ) = @_;
	foreach my $module ( @modules ) {
		ok -e $module->{file}, "module file <$module->{file}> exists";
		$mcpi = $mcpi->add( %$module );
		}

	subtest 'writelist' => sub {
		ok $mcpi->writelist, 'inject modules';
		};

	subtest 'inject' => sub {
		ok $mcpi->inject( $ENV{TEST_VERBOSE} // 0 ), 'copy modules';
		};

	subtest 'packages' => sub {
		my $packages = catfile $temp_dir, 'modules', '02packages.details.txt.gz';
		ok -e $packages, '02packages exists';
		my $gz = gzopen($packages, 'rb');

		my $line;
		my $expected_lines;
		HEADER: while( $gz->gzreadline($line) > 0 ) {
			last HEADER if $line eq "\n";
			my $rc = like $line, qr/\A ([a-z-]+) : \x{20}+ (.*)/ix, 'header format is correct';
			if( $line =~ m/\A ([a-z-]+) : \x{20}+ (.*)/ix and $1 eq 'Line-Count') {
				$expected_lines = $2;
				}
			}

		my $count = 0;
		PACKAGES: while( $gz->gzreadline($line) > 0 ) {
			$count++;
			chomp($line);
			my( $module, $version, $path ) = split /\s+/, $line;
			like $module, qr/\A [A-Za-z0-9_]+ (:: [A-Za-z0-9_]+ )* \z/x, 'module name matches';
			}

		is $count, $expected_lines, 'Line-Count matches lines';
		};

	subtest 'check the result' => sub {
		my $authors_dir = catfile $temp_dir, 'authors';
		ok -e $authors_dir, 'authors dir exists';

		foreach my $module ( @modules ) {
			subtest "check $module->{file}" => sub {
				my $author_stub = catfile(
					$authors_dir,
					'id',
					substr( $module->{authorid}, 0, 1 ),
					substr( $module->{authorid}, 0, 2 ),
					$module->{authorid}
					);
				ok -d $author_stub, "author directory $author_stub for $module->{authorid} exists";
				is( mode($author_stub), 0775, 'author dir mode is 775' ) if has_modes();

				my $module_basename = basename $module->{file};
				my $module_path = catfile $author_stub, $module_basename;
				ok -e $module_path, "$module_basename exists in local";
				is( mode($module_path), 0664, 'moduole filr is mode is 664' ) if has_modes();

				subtest 'CHECKSUMS' => sub {
					my $checksums_path = catfile $author_stub, 'CHECKSUMS';
					my $rc = ok -e $checksums_path, "CHECKSUMS file for $module->{authorid} exists";
					is( mode($checksums_path), 0664, 'checksum file mode is 664' ) if has_modes();

					if( $rc ) {
						my $rc = open my $chk, '<', $checksums_path;
						my $checksum_text = join "", <$chk>;
						close $chk;
						unlike $checksum_text, qr{\Q$authors_dir\E/id}, "root path isn't leaked to checksums";
						}
					else {
						fail "Can't check CHECKSUMS since it doesn't exist";
						}
					};
				};
			}
		};
	};

subtest 'inject the modules' => sub {
	my $dist_sources = catfile $t_local, 'mymodules';
	ok -d $dist_sources, 'Dist sources directory exists';
	my @modules = get_module_details( $dist_sources );

	my $tmp_config_file;
	subtest 'make config' => sub {
		$tmp_config_file = write_config(
			local      => $temp_dir,
			repository => catfile( $temp_dir, 'injects' ),
			);
		ok -e $tmp_config_file, 'configuration file exists';
		};

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

	$mcpi = $mcpi->loadcfg( $tmp_config_file )->parsecfg->readlist;

	test_inject( $mcpi, @modules );
	};

subtest 'packages updated' => sub {
	my @goodfile = <DATA>;
	my $packages = catfile $temp_dir, 'modules', '02packages.details.txt.gz';
	ok -e $packages, 'packages files exists';

	ok( my $gzread = gzopen( $packages, 'rb' ), 'opened packages for reading' );

	my @packages;
	my $line;
	while ( $gzread->gzreadline( $line ) ) {
	  if ( $line =~ /^Written-By:/ ) {
		push( @packages, "Written-By:\n" );
		next;
	  }
	  if ( $line =~ /^Last-Updated:/ ) {
		push( @packages, "Last-Updated:\n" );
		next;
	  }
	  push( @packages, $line );
	}
	$gzread->gzclose;

	is_deeply( \@goodfile, \@packages, 'got expected packages file data' );
	};

subtest 'mailrc updated' => sub {
	my $mailrc = catfile $temp_dir, 'authors', '01mailrc.txt.gz';
	ok -e $mailrc, 'mailrc files exists';

	ok( my $gzauthread = gzopen( $mailrc, 'rb' ), 'opened mailrc for reading' );

	my %inject_authors = map { $_->{authorid} => 1 } get_module_details('');

	my $line;
	my %found_authors;
	while ( $gzauthread->gzreadline( $line ) ) {
		next unless $line =~ /\A alias \h+ ([A-Z]+)/x;
		$found_authors{$1}++;
		fail( "Found $1 $found_authors{$1} times" ) if $found_authors{$1} > 1;
		}
	$gzauthread->gzclose;

	foreach my $author ( keys %inject_authors ) {
		ok exists $found_authors{$author}, "Found $author in $mailrc";
		}
	};

subtest 'empty local' => sub {
	my $dist_sources = catfile $t_local, 'mymodules';
	ok -d $dist_sources, 'Dist sources directory exists';
	my @modules = get_module_details( $dist_sources );

	my $empty_temp_dir = File::Temp::tempdir(CLEANUP=>1);

	my $tmp_config_file;
	subtest 'make config' => sub {
		$tmp_config_file = write_config(
			local      => $empty_temp_dir,
			repository => catfile( $temp_dir, 'injects' ),
			);
		ok -e $tmp_config_file, 'configuration file exists';
		};

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

	$mcpi = $mcpi->loadcfg( $tmp_config_file )->parsecfg->readlist;

	my $warnings;
	local $SIG{__WARN__} = sub { $warnings = $_[0] };
	test_inject( $mcpi, @modules );
	like $warnings, qr/02packages.details.txt.gz> does not exist/, 'saw warning about missing package file';
	};


done_testing();

__DATA__
File:         02packages.details.txt
URL:          http://www.perl.com/CPAN/modules/02packages.details.txt
Description:  Package names found in directory $CPAN/authors/id/
Columns:      package name, version, path
Intended-For: Automated fetch routines, namespace documentation.
Written-By:
Line-Count:   7
Last-Updated:

abbreviation                       0.02  M/MI/MIYAGAWA/abbreviation-0.02.tar.gz
Acme::Code::Police               2.1828  O/OV/OVID/Acme-Code-Police-2.1828.tar.gz
BFD                                0.31  R/RB/RBS/BFD-0.31.tar.gz
CPAN::Mini                         0.17  R/RJ/RJBS/CPAN-Mini-0.17.tar.gz
CPAN::Mini::Inject                 0.02  S/SS/SSORICHE/CPAN-Mini-Inject-0.01.tar.gz
CPAN::Nox                          1.02  A/AN/ANDK/CPAN-1.76.tar.gz
CPANPLUS                          0.049  A/AU/AUTRIJUS/CPANPLUS-0.049.tar.gz