App-DocKnot

 view release on metacpan or  search on metacpan

t/lib/Test/RRA/ModuleVersion.pm  view on Meta::CPAN


    # Scan for the version and replace it.
    open(my $in, q{<}, $file);
    open(my $out, q{>}, "$file.new");
  SCAN:
    while (defined(my $line = <$in>)) {
        if ($line =~ s{ $REGEX_VERSION_PACKAGE }{$1$version$3}xms) {
            print {$out} $line or die "$0: cannot write to $file.new: $!\n";
            last SCAN;
        }
        print {$out} $line or die "$0: cannot write to $file.new: $!\n";
    }

    # Copy the rest of the input file to the output file.
    print {$out} <$in> or die "$0: cannot write to $file.new: $!\n";
    close($out);
    close($in);

    # All done.  Rename the new file over top of the old file.
    rename("$file.new", $file);
    return;
}

# Act as a test suite.  Find all of the Perl modules under the provided root,
# if any, and check that the version for each module matches the version.
# Reports results with Test::More and sets up a plan based on the number of
# modules found.
#
# $root    - Directory under which to look for Perl modules
# $version - The version all those modules should have
#
# Returns: undef
#  Throws: Text exception on fatal errors
sub test_module_versions {
    my ($root, $version) = @_;
    my @modules = _module_files($root);

    # Output the plan.  Skip the test if there were no modules found.
    if (@modules) {
        plan tests => scalar(@modules);
    } else {
        plan skip_all => 'No Perl modules found';
        return;
    }

    # For each module, get the module version and compare.
    for my $module (@modules) {
        my $module_version = _module_version($module);
        is($module_version, $version, "Version for $module");
    }
    return;
}

# Update the versions of all modules to the current distribution version.
#
# $root    - Directory under which to look for Perl modules
# $version - The version all those modules should have
#
# Returns: undef
#  Throws: Text exception on fatal errors
sub update_module_versions {
    my ($root, $version) = @_;
    my @modules = _module_files($root);
    for my $module (@modules) {
        _update_module_version($module, $version);
    }
    return;
}

1;
__END__

=for stopwords
Allbery sublicense MERCHANTABILITY NONINFRINGEMENT rra-c-util versioning

=head1 NAME

Test::RRA::ModuleVersion - Check Perl module versions for consistency

=head1 SYNOPSIS

    use Test::RRA::ModuleVersion
      qw(test_module_versions update_module_versions);

    # Ensure all modules under perl/lib have a version of v3.1.2.
    test_module_versions('perl/lib', 'v3.1.2');

    # Update the version of those modules to v3.1.2.
    update_module_versions('perl/lib', 'v3.1.2');

=head1 DESCRIPTION

This module provides functions to test and update the versions of Perl
modules.  It helps with enforcing consistency of versioning across all modules
in a Perl distribution or embedded in a larger project containing non-Perl
code.  The calling script provides the version with which to be consistent
and the root directory under which modules are found.

=head1 FUNCTIONS

None of these functions are imported by default.  The ones used by a script
should be explicitly imported.

=over 4

=item test_module_versions(ROOT, VERSION)

Tests the version of all Perl modules under ROOT to ensure they match VERSION,
reporting the results with Test::More.  VERSION should include any leading
C<v>.

If the test configuration loaded by Test::RRA::Config contains a
@MODULE_VERSION_EXCLUDE variable, the module files listed there will be
ignored for this test.

This function sets up a plan based on the number of modules, so should be the
only testing function called in a test script.

=item update_module_versions(ROOT, VERSION)

Update the version of all Perl modules found under ROOT to VERSION, except for



( run in 0.488 second using v1.01-cache-2.11-cpan-39bf76dae61 )