diff options
-rw-r--r-- | Isocheck.pm | 90 | ||||
-rw-r--r-- | t/00_load.t | 10 | ||||
-rw-r--r-- | t/parse_image_file_name.t | 126 |
3 files changed, 226 insertions, 0 deletions
diff --git a/Isocheck.pm b/Isocheck.pm new file mode 100644 index 0000000..fe0b85b --- /dev/null +++ b/Isocheck.pm @@ -0,0 +1,90 @@ +# This file is part of the Mageia project +# +# Copyright (C) 2011 Damien Lallement <dams@mageia.org> +# (C) 2011 Romain d'Alverny <rda@mageia.org> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Library General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Library General Public License for more details. +# +# You should have received a copy of the GNU Library General Public License +# along with this library; see the file COPYING.LIB. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +# Boston, MA 02110-1301, USA. +# + +=head1 NAME + + Isocheck - miscellaneous functions + +=head1 SYNOPSIS + + use Isocheck; + +=head1 EXPORTS + +=over + +=item parse_image_file_name(STRING) + +return a populated hash if param is a valid image file name +(see https://wiki.mageia.org/en/Product_naming ). + +return empty hash if invalid. + +=back + +=cut + +package Isocheck; + +use strict; +use warnings; + +our $VERSION = '0.2.0'; + +use base 'Exporter'; + +our @EXPORT = qw(parse_image_file_name); + +sub parse_image_file_name { + my ($name) = @_; + + return unless $name =~ m/^ + ( + (\w+) # name + - + (\d+) # version + (?:-((?:nightly|alpha|beta|RC)\d*))? # release + (?:-(.+))? # variant + - + (i586|x86_64|dual) # arch + (?:-(CD|DVD|BR))? # medium + (?:-(build_\w+))? # build + ) + \. + (\w+) # extension + $/x; + + my %info = ( + full => $1, + name => $2, + version => $3, + defined $4 ? (release => $4) : (), + defined $5 ? (variant => $5) : (), + arch => $6, + defined $7 ? (medium => $7) : (), + defined $8 ? (build => $8) : (), + ext => $9 + ); + + return %info; +} + +1;
\ No newline at end of file diff --git a/t/00_load.t b/t/00_load.t new file mode 100644 index 0000000..4a923e4 --- /dev/null +++ b/t/00_load.t @@ -0,0 +1,10 @@ +#!/usr/bin/perl -w + +use Test::More tests => 2; + +BEGIN { +use_ok( 'Isocheck' ); +require_ok('Isocheck'); +} + +diag( "Testing Isocheck $Isocheck::VERSION" );
\ No newline at end of file diff --git a/t/parse_image_file_name.t b/t/parse_image_file_name.t new file mode 100644 index 0000000..4243514 --- /dev/null +++ b/t/parse_image_file_name.t @@ -0,0 +1,126 @@ + +use Test::Most; +use Isocheck; + +bail_on_fail; + +my %valid_names = ( + 'Mageia-2-i586-DVD.iso' => { + full => 'Mageia-2-i586-DVD', + name => 'Mageia', + version => '2', + arch => 'i586', + medium => 'DVD', + ext => 'iso' + }, + 'Mageia-2-alpha1-dual-CD.iso' => { + full => 'Mageia-2-alpha1-dual-CD', + name => 'Mageia', + version => '2', + arch => 'dual', + release => 'alpha1', + medium => 'CD', + ext => 'iso' + }, + 'Mageia-2-alpha3-LiveCD-GNOME-Europe1-i586-CD.iso' => { + full => 'Mageia-2-alpha3-LiveCD-GNOME-Europe1-i586-CD', + name => 'Mageia', + version => '2', + arch => 'i586', + release => 'alpha3', + variant => 'LiveCD-GNOME-Europe1', + medium => 'CD', + ext => 'iso' + }, + 'Mageia-2-beta2-Vagrant-i586-build_123AA3.box' => { + full => 'Mageia-2-beta2-Vagrant-i586-build_123AA3', + release => 'beta2', + name => 'Mageia', + version => '2', + arch => 'i586', + ext => 'box', + build => 'build_123AA3', + variant => 'Vagrant' + }, + 'Mageia-1-x86_64-CD.iso' => { + full => 'Mageia-1-x86_64-CD', + name => 'Mageia', + version => '1', + arch => 'x86_64', + medium => 'CD', + ext => 'iso' + }, + 'Mageia-3-RC-LiveCD-KDE-Asia-x86_64.iso' => { + full => 'Mageia-3-RC-LiveCD-KDE-Asia-x86_64', + release => 'RC', + variant => 'LiveCD-KDE-Asia', + name => 'Mageia', + version => '3', + arch => 'x86_64', + ext => 'iso' + }, + 'Mageia-2-Vagrant-i586.box' => { + full => 'Mageia-2-Vagrant-i586', + name => 'Mageia', + version => '2', + arch => 'i586', + ext => 'box', + variant => 'Vagrant' + }, + 'Mageia-3-nightly20111202-i586-DVD.iso' => { + full => 'Mageia-3-nightly20111202-i586-DVD', + name => 'Mageia', + version => '3', + arch => 'i586', + medium => 'DVD', + ext => 'iso', + release => 'nightly20111202' + }, + 'Mageia-2-nightly20111202-LiveCD-KDE-Asia-x86_64-build_123.iso' => { + full => 'Mageia-2-nightly20111202-LiveCD-KDE-Asia-x86_64-build_123', + name => 'Mageia', + version => '2', + arch => 'x86_64', + ext => 'iso', + variant => 'LiveCD-KDE-Asia', + release => 'nightly20111202', + build => 'build_123' + }, + 'MagToo-4-nightly2-aDevice-i586-build_123_qa_pass.iso' => { + full => 'MagToo-4-nightly2-aDevice-i586-build_123_qa_pass', + name => 'MagToo', + version => '4', + arch => 'i586', + ext => 'iso', + variant => 'aDevice', + release => 'nightly2', + build => 'build_123_qa_pass' + } +); + +my @invalid_names = ( + 'mageia-2-i586-Dvd.iso', + 'mageia-i586-DVD.iso', + 'Mageia-2-i586-dvd.iso', + 'Mageia-1-x86_64-arm.iso', + 'Mageia-1-RC-LiveCD-GNOME-Africa.iso', + 'Mandriva-i586-DVD.iso', + 'Mageia-1.iso' +); + +plan tests => scalar (keys %valid_names) + scalar @invalid_names; + +my %got; + +foreach (@invalid_names) { + %got = parse_image_file_name($_); + is_deeply(\%got, {}, "$_ is invalid."); +} + +foreach my $name (keys %valid_names) { + my $fields = $valid_names{$name}; + %got = parse_image_file_name($name); + is_deeply(\%got, $fields, "$name is valid."); +} + +done_testing();
\ No newline at end of file |