test/util/reindex - ktap

Data types defined

Source code

  1. #!/usr/bin/env perl

  2. # reindex
  3. # reindex .t files for Test::Base based test files
  4. # Copyright (C) Yichun Zhang (agentzh)

  5. use strict;
  6. use warnings;

  7. use Getopt::Std;

  8. my %opts;
  9. getopts('hb:', \%opts);
  10. if ($opts{h} or ! @ARGV) {
  11.     die "Usage: reindex [-b 0] t/*.t\n";
  12. }

  13. my $init = $opts{b};
  14. $init = 1 if not defined $init;

  15. my @files = map glob, @ARGV;
  16. for my $file (@files) {
  17.     next if -d $file or $file !~ /\.t_?$/;
  18.     reindex($file);
  19. }

  20. sub reindex {
  21.     my $file = $_[0];
  22.     open my $in, $file or
  23.         die "Can't open $file for reading: $!";
  24.     my @lines;
  25.     my $counter = $init;
  26.     my $changed;
  27.     while (<$in>) {
  28.         s/\r$//;
  29.         my $num;
  30.         s/ ^ === \s+ TEST \s+ (\d+)/$num=$1; "=== TEST " . $counter++/xie;
  31.         next if !defined $num;
  32.         if ($num != $counter-1) {
  33.             $changed++;
  34.         }
  35.     } continue {
  36.         push @lines, $_;
  37.     }
  38.     close $in;
  39.     my $text = join '', @lines;
  40.     $text =~ s/(?x) \n+ === \s+ TEST/\n\n\n\n=== TEST/ixsg;
  41.     $text =~ s/__(DATA|END)__\n+=== TEST/__${1}__\n\n=== TEST/;
  42.     #$text =~ s/\n+$/\n\n/s;
  43.     if (! $changed and $text eq join '', @lines) {
  44.         warn "reindex: $file:\tskipped.\n";
  45.         return;
  46.     }
  47.     open my $out, "> $file" or
  48.         die "Can't open $file for writing: $!";
  49.     binmode $out;
  50.     print $out $text;
  51.     close $out;

  52.     warn "reindex: $file:\tdone.\n";
  53. }