File Coverage

blib/lib/Test/Corpus/Audio/MPD.pm
Criterion Covered Total %
statement 27 56 48.2
branch 0 12 0.0
condition 0 2 0.0
subroutine 9 14 64.2
pod n/a
total 36 84 42.8


line stmt bran cond sub pod time code
1             #
2             # This file is part of Test-Corpus-Audio-MPD
3             #
4             # This software is copyright (c) 2009 by Jerome Quelin.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9 1     1   855 use 5.008;
  1         5  
  1         40  
10 1     1   5 use strict;
  1         2  
  1         35  
11 1     1   17 use warnings;
  1         2  
  1         79  
12              
13             package Test::Corpus::Audio::MPD;
14             {
15             $Test::Corpus::Audio::MPD::VERSION = '1.120990';
16             }
17             # ABSTRACT: automate launching of fake mdp for testing purposes
18              
19 1     1   962 use File::Copy qw{ copy };
  1         2664  
  1         75  
20 1     1   791 use File::ShareDir::PathClass qw{ dist_dir };
  1         141992  
  1         10  
21 1     1   248 use File::Temp qw{ tempdir };
  1         3  
  1         108  
22 1     1   5 use Path::Class;
  1         2  
  1         53  
23 1     1   1135 use Readonly;
  1         3310  
  1         71  
24              
25 1     1   8 use base qw{ Exporter };
  1         2  
  1         1124  
26             our @EXPORT = qw{
27             customize_test_mpd_configuration
28             playlist_dir
29             start_test_mpd stop_test_mpd
30             };
31              
32             Readonly my $SHAREDIR => dist_dir('Test-Corpus-Audio-MPD');
33             Readonly my $TEMPLATE => $SHAREDIR->file( 'mpd.conf.template' );
34             Readonly my $TMPDIR => dir( tempdir( CLEANUP=>1 ) );
35             Readonly my $CONFIG => $TMPDIR->file( 'mpd.conf' );
36             Readonly my $PLAYLISTDIR => $TMPDIR->subdir( 'playlists' );
37              
38              
39             { # this will be run when module will be use-d
40              
41             # check if mpd (the real music player daemon, not freebsd's
42             # multilink ppp daemon
43             my $output = qx{ mpd --version 2>&1 } or die "mpd not installed";
44             die "installed mpd is not music player daemon"
45             unless $output =~ /Music Player Daemon/;
46              
47             my $restart = 0;
48             my $stopit = 0;
49              
50             $restart = _stop_user_mpd_if_needed();
51             customize_test_mpd_configuration();
52             $stopit = start_test_mpd();
53              
54             END {
55             stop_test_mpd() if $stopit;
56             return unless $restart; # no need to restart
57             system 'mpd 2>/dev/null'; # restart user mpd
58             sleep 1; # wait 1 second to let mpd start.
59             }
60             }
61              
62              
63             # -- public subs
64              
65              
66             sub customize_test_mpd_configuration {
67 0     0     my ($port) = @_;
68 0   0       $port ||= 6600;
69              
70             # open template and config.
71 0 0         open my $in, '<', $TEMPLATE or die "can't open [$TEMPLATE]: $!";
72 0 0         open my $out, '>', $CONFIG or die "can't open [$CONFIG]: $!";
73              
74             # replace string and fill in config file.
75 0           while ( defined( my $line = <$in> ) ) {
76 0           $line =~ s!PWD!$SHAREDIR!;
77 0           $line =~ s!TMP!$TMPDIR!;
78 0           $line =~ s!PORT!$port!;
79 0           print $out $line;
80             }
81              
82             # clean up.
83 0           close $in;
84 0           close $out;
85              
86             # copy the playlists. playlist need to be in a writable directory,
87             # since tests will create and remove some playlists.
88 0           $PLAYLISTDIR->mkpath;
89 0           copy( glob("$SHAREDIR/playlists/*"), $PLAYLISTDIR );
90             }
91              
92              
93              
94 0     0     sub playlist_dir { $PLAYLISTDIR }
95              
96              
97              
98             sub start_test_mpd {
99 0     0     my $output = qx{ mpd $CONFIG 2>&1 };
100 0           my $rv = $? >>8;
101 0 0         die "could not start fake mpd: $output\n" if $rv;
102 0           sleep 1; # wait 1 second to let mpd start.
103 0           return 1;
104             }
105              
106              
107              
108             sub stop_test_mpd {
109 0     0     system "mpd --kill $CONFIG 2>/dev/null";
110 0           sleep 1; # wait 1 second to free output device.
111 0           unlink "$TMPDIR/state", "$TMPDIR/music.db";
112             }
113              
114              
115             # -- private subs
116              
117             #
118             # my $was_running = _stop_user_mpd_if_needed()
119             #
120             # This sub will check if mpd is currently running. If it is, force it to
121             # a full stop (unless MPD_TEST_OVERRIDE is not set).
122             #
123             # In any case, it will return a boolean stating whether mpd was running
124             # before forcing stop.
125             #
126             sub _stop_user_mpd_if_needed {
127             # check if mpd is running.
128 0     0     my $is_running = grep { /\s+mpd$/ } qx{ ps -e };
  0            
129              
130 0 0         return 0 unless $is_running; # mpd does not run - nothing to do.
131              
132             # check force stop.
133 0 0         die "mpd is running\n" unless $ENV{MPD_TEST_OVERRIDE};
134 0 0         system( 'mpd --kill 2>/dev/null') == 0 or die "can't stop user mpd: $?\n";
135 0           sleep 1; # wait 1 second to free output device
136 0           return 1;
137             }
138              
139              
140             1;
141              
142              
143             =pod
144              
145             =head1 NAME
146              
147             Test::Corpus::Audio::MPD - automate launching of fake mdp for testing purposes
148              
149             =head1 VERSION
150              
151             version 1.120990
152              
153             =head1 SYNOPSIS
154              
155             use Test::Corpus::Audio::MPD; # die if error
156             [...]
157             stop_test_mpd();
158              
159             =head1 DESCRIPTION
160              
161             This module will try to launch a new mpd server for testing purposes.
162             This mpd server will then be used during L
163             or L tests.
164              
165             In order to achieve this, the module will create a fake F file
166             with the correct pathes (ie, where you untarred the module tarball). It
167             will then check if some mpd server is already running, and stop it if
168             the C environment variable is true (die otherwise).
169             Last it will run the test mpd with its newly created configuration file.
170              
171             Everything described above is done automatically when the module
172             is C-d.
173              
174             Once the tests are run, the mpd server will be shut down, and the
175             original one will be relaunched (if there was one).
176              
177             Note that the test mpd will listen to C, so you are on the
178             safe side. Note also that the test suite comes with its own ogg files.
179             Those files are 2 seconds tracks recording my voice saying ok, and are
180             freely redistributable under the same license as the code itself.
181              
182             In case you want more control on the test mpd server, you can use the
183             supplied public methods. This might be useful when trying to test
184             connections with mpd server.
185              
186             =head1 METHODS
187              
188             =head2 customize_test_mpd_configuration( [$port] );
189              
190             Create a fake mpd configuration file, based on the file
191             F located in F subdir. The string PWD will be
192             replaced by the real path (ie, where the tarball has been untarred),
193             while TMP will be replaced by a new temp directory. The string PORT will
194             be replaced by C<$port> if specified, 6600 otherwise (MPD's default).
195              
196             =head2 my $dir = playlist_dir();
197              
198             Return the temp dir where the test playlists will be stored.
199              
200             =head2 start_test_mpd();
201              
202             Start the fake mpd, and die if there were any error.
203              
204             =head2 stop_test_mpd();
205              
206             Kill the fake mpd.
207              
208             =head1 SEE ALSO
209              
210             You can look for information on this module at:
211              
212             =over 4
213              
214             =item * Search CPAN
215              
216             L
217              
218             =item * See open / report bugs
219              
220             L
221              
222             =item * Mailing-list (same as L)
223              
224             L
225              
226             =item * Git repository
227              
228             L
229              
230             =item * AnnoCPAN: Annotated CPAN documentation
231              
232             L
233              
234             =item * CPAN Ratings
235              
236             L
237              
238             =back
239              
240             =head1 AUTHOR
241              
242             Jerome Quelin
243              
244             =head1 COPYRIGHT AND LICENSE
245              
246             This software is copyright (c) 2009 by Jerome Quelin.
247              
248             This is free software; you can redistribute it and/or modify it under
249             the same terms as the Perl 5 programming language system itself.
250              
251             =cut
252              
253              
254             __END__