Don't import files if they don't have the supportted format extension,
Matthew Horsfall [Sun, 24 Jul 2011 20:33:33 +0000 (16:33 -0400)]
plus some tests for video_files_from_dir()

lib/App/IdiotBox.pm
lib/App/IdiotBox/Common.pm [new file with mode: 0644]
lib/App/IdiotBox/Importer.pm
t/importer/video_files_from_dir.t [new file with mode: 0644]

index b1fc058..081327e 100644 (file)
@@ -1,5 +1,6 @@
 package App::IdiotBox;
 
+use App::IdiotBox::Common qw(@SupportedFormats);
 use Web::Simple __PACKAGE__;
 use FindBin;
 use HTML::Zoom;
@@ -123,7 +124,7 @@ sub show_video {
     -e join('/', $self->config->{base_dir}, $_)
   } map {
     join('/', $video->bucket->slug, $video->slug, $video->file_name.".$_")
-  } qw(flv m4v);
+  } @SupportedFormats;
   $self->html_response(video => sub {
     my $video_url = 
       $self->base_url
diff --git a/lib/App/IdiotBox/Common.pm b/lib/App/IdiotBox/Common.pm
new file mode 100644 (file)
index 0000000..34bf001
--- /dev/null
@@ -0,0 +1,13 @@
+package App::IdiotBox::Common;
+
+use strict;
+use warnings;
+
+require Exporter;
+*import = \&Exporter::import;
+
+our @EXPORT_OK = qw(@SupportedFormats);
+
+our @SupportedFormats = qw(flv m4v);
+
+1;
index 030de6e..a19451c 100644 (file)
@@ -2,12 +2,15 @@ package App::IdiotBox::Importer;
 
 use strict;
 use warnings FATAL => 'all';
+use App::IdiotBox::Common qw(@SupportedFormats);
 use Cwd;
 use IO::All;
 use ExtUtils::MakeMaker qw(prompt);
 use File::Spec::Functions qw(catfile catdir);
 use POSIX qw(strftime);
 
+my $supported_formats_re = join('|', @SupportedFormats);
+
 sub log_info (&) { print $_[0]->(), "\n"; }
 
 sub run {
@@ -101,7 +104,7 @@ sub video_files_from_dir {
   my ($class, $dir) = @_;
   my %videos;
   foreach my $file (io($dir)->all_files) {
-    $file->filename =~ /^([^\.]+)\.([^\.]+)$/ or next;
+    $file->filename =~ /^([^\.]+)\.($supported_formats_re)$/ or next;
     push(@{$videos{$1}||=[]}, $2);
   }
   \%videos;
diff --git a/t/importer/video_files_from_dir.t b/t/importer/video_files_from_dir.t
new file mode 100644 (file)
index 0000000..45e0f80
--- /dev/null
@@ -0,0 +1,54 @@
+use strict;
+use warnings FATAL => 'all';
+use autodie;
+
+use Test::More;
+
+use Cwd qw(getcwd);
+use File::Temp qw(tempdir);
+
+# Chdir to test directory for laziness
+my $original_dir = getcwd;
+END {
+       chdir $original_dir;
+}
+
+my $temp_dir = tempdir(CLEANUP => 1);
+chdir($temp_dir);
+
+use App::IdiotBox::Importer;
+
+my $method = 'video_files_from_dir';
+
+# No videos
+my $videos = App::IdiotBox::Importer->$method($temp_dir);
+is_deeply($videos, {}, 'No files reported from empty directory');
+
+# Unknown extensions
+open(undef, '>', 'fake.mp3');
+open(undef, '>', 'fake2.aac');
+
+$videos = App::IdiotBox::Importer->$method($temp_dir);
+is_deeply($videos, {}, 'No files reported from empty directory');
+
+# Known file types
+open(undef, '>', 'good.m4v');
+open(undef, '>', 'good2.flv');
+
+my $expect = {
+       'good'  => [ 'm4v' ],
+       'good2' => [ 'flv' ],
+};
+
+$videos = App::IdiotBox::Importer->$method($temp_dir);
+is_deeply($videos, $expect, 'Known filetypes detected');
+
+# Grouping by file name without extension
+open(undef, '>', 'good.flv');
+
+unshift @{ $expect->{good} }, 'flv';
+
+$videos = App::IdiotBox::Importer->$method($temp_dir);
+is_deeply($videos, $expect, 'Known filetypes detected');
+
+done_testing;