I usually number my tests. I frequently find myself "wedging" tests between other tests as I run out of test numbers. This small program lets me renumber my tests on the fly. It pretends to be portable, but don't shoot me if it's not. I supports simply "mv"ing the tests or, if you prefer, "svn mv".
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec;
use Getopt::Long;
my $dir = ".";
my $test_num = 10;
my $step = 10;
GetOptions(
"dir=s" => \$dir,
"start=i" => \$test_num,
"step=i" => \$step,
"dry" => \my $dry_run,
"svn" => \my $svn,
);
unless ( -d $dir ) {
die "I cannot find the directory ($dir)";
}
my $tests = File::Spec->catfile( $dir, "*.t" );
if ( $tests =~ /^(.+)\*.t$/ ) {
$dir = $1; # now we have the directory with the separator
}
my @tests = glob $tests;
unless (@tests) {
die "No tests found in ($tests)";
}
{
no warnings 'numeric';
@tests =
map { s/^\d+//; $_ }
sort { $a <=> $b }
map { s{^$dir(\d+)/}{$1$dir$1}; $_ }
grep { /^\Q$dir\E\d+/ }
@tests; # only take numbered tests
}
unless ($dry_run) {
print <<" END_WARNING";
Warning: this is not a dry run and you're really going to
renumber the tests. Are you sure you want to continue? (y/n)
END_WARNING
my $response = <STDIN>;
unless ($response =~ /^[Yy]/) {
print "Answer was not 'y' or 'Y'. Aborting";
exit(0);
}
}
my $digits = length( $test_num + ( $step * @tests ) );
$test_num = $test_num - $step; # because of how we handle th
+e s///
foreach my $test (@tests) {
my $new_name = $test;
$new_name =~ s{(?<=\Q$dir\E)\d+}
{
$test_num += $step;
sprintf "%0${digits}d", $test_num;
}e;
next if $new_name eq $test;
my @command = ( "mv", $test, $new_name );
if ($svn) {
unshift @command, "svn";
}
if ($dry_run) {
print "@command\n";
}
else {
system(@command);
}
}