Browse Source

Dpkg::Dist::Files: Add new load_dir method

This method scans all fragment files inside a directory and loads
each of them.
Guillem Jover 7 years ago
parent
commit
f89e053f3e

+ 15 - 0
scripts/Dpkg/Dist/Files.pm

@@ -20,6 +20,8 @@ use warnings;
 
 our $VERSION = '0.01';
 
+use IO::Dir;
+
 use Dpkg::Gettext;
 use Dpkg::ErrorHandling;
 
@@ -101,6 +103,19 @@ sub parse {
     return $count;
 }
 
+sub load_dir {
+    my ($self, $dir) = @_;
+
+    my $count = 0;
+    my $dh = IO::Dir->new($dir) or syserr(g_('cannot open directory %s'), $dir);
+
+    while (defined(my $file = $dh->read)) {
+        $count += $self->load("$dir/$file");
+    }
+
+    return $count;
+}
+
 sub get_files {
     my $self = shift;
 

+ 15 - 1
scripts/t/Dpkg_Dist_Files.t

@@ -16,7 +16,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 25;
+use Test::More tests => 26;
 
 use_ok('Dpkg::Dist::Files');
 
@@ -155,6 +155,20 @@ is_deeply($dist->get_file('another:filename'),
           'Get added file another:filename');
 is($dist->output, $expected, 'Added source files');
 
+$expected = <<'FILES';
+BY-HAND-file webdocs optional
+other_0.txt text optional
+pkg-arch_2.0.0_amd64.deb admin required
+pkg-frag-a_0.0_arch.type section priority
+pkg-frag-b_0.0_arch.type section priority
+pkg-indep_0.0.1-2_all.deb net standard
+pkg-templ_1.2.3_arch.type section priority
+FILES
+
+$dist->reset();
+$dist->load_dir($datadir) or error('cannot parse fragment files');
+is($dist->output(), $expected, 'Parse fragment directory');
+
 $expected = <<'FILES';
 pkg-arch_2.0.0_amd64.deb admin required
 pkg-indep_0.0.1-2_all.deb net standard

+ 1 - 0
scripts/t/Dpkg_Dist_Files/files-fragment-a

@@ -0,0 +1 @@
+pkg-frag-a_0.0_arch.type section priority

+ 1 - 0
scripts/t/Dpkg_Dist_Files/files-fragment-b

@@ -0,0 +1 @@
+pkg-frag-b_0.0_arch.type section priority