File: module-starter.t

package info (click to toggle)
libmodule-starter-perl 1.750%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 348 kB
  • sloc: perl: 3,245; makefile: 2
file content (383 lines) | stat: -rw-r--r-- 10,478 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!perl -T

=head1 DESCRIPTION

module-starter.t is a collection of tests to ensure that the
C<module-starter> script behaves correctly.

We test...

=over 4

=item * options processing

=item * correct file layout of generated package

=item * successful make and test runs of generated package

=back

=cut

use strict;
use warnings;

use Test::More;
plan skip_all => "these tests must be completely rewritten";

use File::Spec;
use File::Temp qw/ tempdir /;
use File::Find;

use Carp qw/ carp /;

use Module::Starter::BuilderSet;

# Since we're making system calls from this test, we have to be extra
# paranoid.

# Clean up the environment
my $old_path = $ENV{PATH};

$ENV{PATH} = "";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

my $test_name        = __FILE__;

(my $dist_dir)       =
  (File::Spec->rel2abs($test_name) =~ m{^(.+)$test_name$});

# $dest_dir may not have had the taint adequately scrubbed from it, so
# we'll check to see if it contains some of the files we expect it to

my $test_libpath     = catfile($dist_dir, 't', 'lib');
my $test_filename    = catfile($dist_dir, $test_name);
my $changes_filename = catfile($dist_dir, 'Changes');
my $test_dir         = catfile($dist_dir, 't');
my $module_starter   = catfile($dist_dir, 'blib', 'script', 'module-starter');
my $template_dir     = catfile($dist_dir, 't', 'data', 'templates');
my $config_dir       = catfile($dist_dir, 't', 'data');
my $config_file      = catfile($config_dir, 'config');

my $temp_dir         = tempdir( CLEANUP => 1 );

my $author           = 'C.J. Adams-Collier';
my $email            = 'cjac@colliertech.org';

my %options =
  ( author => $author,
    email  => $email,
    force  => undef,
  );

sub generated_files {
  my $opts = shift;
  my @modules =
    (ref $opts->{module} eq 'ARRAY' ? @{ $opts->{module} } : $opts->{module});

  my $starter_dir = $modules[0];

  $starter_dir =~ s/::/-/g;

  my %generated_files =
    (
     $starter_dir                                 => 'd',
     catfile($starter_dir, 'lib')                 => 'd',
     catfile($starter_dir, 'MANIFEST')            => 'f',
     catfile($starter_dir, 't')                   => 'd',
     catfile($starter_dir, 't', 'pod-coverage.t') => 'f',
     catfile($starter_dir, 't', '00-load.t')      => 'f',
     catfile($starter_dir, 't', 'boilerplate.t')  => 'f',
     catfile($starter_dir, 't', 'pod.t')          => 'f',
     catfile($starter_dir, 't', 'manifest.t')     => 'f',
     catfile($starter_dir, 'README')              => 'f',
     catfile($starter_dir, 'Changes')             => 'f',
     catfile($starter_dir, 'ignores.txt')         => 'f',
    );

  foreach my $module (@modules){
    my @parts = split(/::/, $module);

    my $pm = pop @parts;

    my $filename = catfile(@parts, "${pm}.pm");
    $generated_files{catfile($starter_dir, 'lib', $filename)} = 'f';

    while(@parts){
      my $dirname = catfile(@parts);
      $generated_files{catfile($starter_dir, 'lib', $dirname)} = 'd';
      pop @parts;
    }
  }

  my $builder_set = Module::Starter::BuilderSet->new;
  my @builders = $builder_set->check_compatibility($opts->{builder});
  foreach my $builder (@builders){
    my $build_file = $builder_set->file_for_builder($builder);
    $generated_files{catfile($starter_dir, $build_file)} = 'f';
  }

  return %generated_files;
}

sub check_generated_files {
  my $opts = shift;

  my %generated_files = generated_files($opts);

  my $all_files_correct = 1;
  foreach my $k (keys %generated_files) {
    my $v = $generated_files{$k};
    if ($v eq 'f') {
      $all_files_correct = 0 unless -f $k;
    } elsif ($v eq 'd') {
      $all_files_correct = 0 unless -d $k;
    } else {
      # Not a directory or file?  Weird.
      $all_files_correct = 0;
    }
  }

  my @modules =
    (ref $opts->{module} eq 'ARRAY' ? @{ $opts->{module} } : $opts->{module});

  my $starter_dir = $modules[0];

  $starter_dir =~ s/::/-/g;

 TODO: {
    local $TODO = "need to generate META.yml";
    ok(-f catfile($starter_dir, 'META.yml'), "META.yml exists");
  };

  ok($all_files_correct, "All files present and accounted for");

  my $num_extra_files = 0;

  find({ wanted   => sub {
             unless( exists $generated_files{$File::Find::name} ){
                 $num_extra_files++;
                 carp("found extra file: $File::Find::name");
             }
         },
         no_chdir => 1
       },
       $starter_dir
      );

  is($num_extra_files, 0, "No extra files");

}

sub catfile {
  File::Spec->catfile('a','b') =~ /^a(.+)b$/;
  my $separator = $1;

  my @parts = @_;

  return join($separator,
              # strip trailing directory separators
              map { my $part = $_; $part =~ s/$separator$//; $part } @parts
              );
}

sub build_module_starter {
  my $opts = shift;

  $opts->{module} = "" unless exists $opts->{module};
  $opts->{builder} = "" unless exists $opts->{builder};

  my $starter_dir =
    (ref $opts->{module} eq 'ARRAY' ? $opts->{module}->[0] : $opts->{module} );

  $starter_dir =~ s/::/-/g;

  # Now to try to build the Starter module...
  chdir( catfile($temp_dir,$starter_dir) );

  (my($path, $perl)) = $^X =~ /^(.+)(perl.*)$/i;

  $perl = catfile($path,$perl);

  (my @dirs) = ( $old_path =~ /([^;:]+)(?:;|:|$)/g );

  my $path_sep;

  if ($old_path eq join(":", @dirs)) {
    $path_sep = ":";
  } elsif ($old_path eq join(";", @dirs)) {
    $path_sep = ";";
  }

  my $builder_set = Module::Starter::BuilderSet->new;

  # Use only the supported builders which are not mutually exclusive
  my @builders;
  # Capture warnings printed to STDERR
  {
      local *STDERR;
      open STDERR, q{>}, File::Spec->devnull();

      @builders = $builder_set->check_compatibility($opts->{builder});
  }

  foreach my $builder ( @builders ){
    my @commands = $builder_set->instructions_for_builder($builder);

    # ensure that we use the correct perl
    @commands =
      map { my $cmd = $_; $cmd =~ s/\bperl\b/$perl/; $cmd } @commands;

    my %commands;
    my %build_path;

    # Find tools needed by the builder
    my @deps = $builder_set->deps_for_builder($builder);
    foreach my $dir ( @dirs ) {
      foreach my $dependency ( @deps ){
        if( grep { -x catfile($dir, $_) } @{ $dependency->{aliases} } ){
          $build_path{$dir} = 1;
          $commands{$dependency->{command}} = 1;
        }
      }
    }

    my $build_path = join($path_sep, keys %build_path);
    my $env = "PERL5LIB=\$PERL5LIB:$test_libpath PATH=$build_path";

  SKIP: {
      skip( "Can't find dependencies for $builder", int(@commands) )
        if grep { !exists( $commands{ $_->{command} } ) } @deps;

      foreach my $command (@commands){
        my $cmd = "$env $command > /dev/null 2>&1";
        diag "RUNNING: $cmd";
        if( $command !~ /install/ ){
            system( $cmd );

            is($?, 0, "$builder: $cmd");

        }else{
        TODO: {
            local $TODO = "install tests not yet designed";

            is(1, 0, "$builder: $cmd");
          };
        }
      }
    }
  }

  chdir( $temp_dir );
}

sub run_module_starter {
  my %opts = @_;

  my $command = $module_starter;
  my @option_string = ("");

  foreach my $k (keys %opts) {
    my $v = $opts{$k};
    if( ref $v eq 'ARRAY' &&
        int( @$v ) > 1
      ){

      # If the option is multi-valued, we will test both formats:
      #
      # --option=value0 --option=value1
      # and
      # --option=value0,value1

      $option_string[1] = $option_string[0] unless($option_string[1]);

      $option_string[0] .= join( " ", map { " --$k='$_'" } @$v );

      my $COMMA= q{,};
      $option_string[1] .= " --$k=" . join( $COMMA, @$v );

    }else{
      # in case anyone ever decides to pass a single-valued arrayref
      $v = $v->[0] if ref $v eq 'ARRAY';

      # Make sure we append to the multi-value option string if it
      # exists
      for(my $i = 0; $i < int(@option_string) ; $i++ ){
        # Flags have no value
        $option_string[$i] .= " --$k" . ($v ? "='$v'" : '');
      }
    }
  }

  my $starter_dir = 
    (ref $opts{module} eq 'ARRAY' ? $opts{module}->[0] : $opts{module} );

  $starter_dir =~ s/::/-/g;

  foreach my $options ( @option_string ){

    my $env = "PERL5LIB=\$PERL5LIB:$test_libpath";
    $env .= " MODULE_STARTER_DIR=$config_dir" if -f $config_file;

    system( "$env $command $options 2>&1 >/dev/null" );
    is($?, 0, "$env $command $options" );

    check_generated_files(\%opts);

    build_module_starter(\%opts);
  }
}

ok( -f $changes_filename, '[paranoia] Dist dir contains Changes file' );
ok( -d $test_dir,         '[paranoia] Dist dir contains t/' );
ok( -f $module_starter,   '[paranoia] module_starter file exists' );
ok( -x $module_starter,   '[paranoia] module_starter is executable' );

chdir( $temp_dir );

# Compute the number of tests for the plan with these variables:
#
# $x:    no. of command line argument formats (1 for single args, 2 for multi)
# $y:    no. of builders.  if none passed, uses default builder
# z($y): no. of commands in builder $y's @instructions list
# tests += $x *      # $x command line argument formats
#          ( 1 +     # module-starter ran correctly
#            3 +     # files generated correctly
#            ( $y *  # $y builders
#              z($y) # number of commands in this builder $y's @instructions
#             )
#           )

# run with one module.
# default builder has 4 commands.
# tests += 1 * ( 1 + 3 + ( 1 * 4 ) ) = 8
run_module_starter( %options, module => 'Foo::Bar' );

# run with two modules.
# default builder has 4 commands.
# tests += 2 * ( 1 + 3 + ( 1 * 4 ) ) = 16
run_module_starter( %options, module => [ 'Foo::Bar', 'Foo::Baz' ] );

# run with two modules and a couple of builders.
# both builders have 4 commands.
# tests += 2 * ( 1 + 3 + ( 2 * 4 ) ) = 24
run_module_starter( %options,
                    module  => [ 'Foo::Bar', 'Foo::Baz' ],
                    builder => [ 'Module::Build', 'ExtUtils::MakeMaker' ],
                  );

# run with one module, default builder, and our example plug-in
# tests += 1 * ( 1 + 3 + ( 1 * 4 ) ) = 8
open my $fh, q{>}, $config_file or
  die "couldn't open config file '$config_file' for writing: $!";
print $fh "template_dir:  $template_dir\n";
close $fh;

run_module_starter ( %options,
                     module       => [ 'Foo::Bar' ],
                     plugin       => [ 'Module::Starter::TestPlugin' ],
                   );

unlink $config_file;

chdir( $dist_dir );