[project @ 5726]
[audio-mpd.git] / lib / Audio / MPD / Test.pm
blob26e30f71dc7eb023a4e20e60faa052deeb7bc15a
2 # This program is free software; you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation; either version 2 of the License, or
5 # (at your option) any later version.
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
12 # You should have received a copy of the GNU General Public License
13 # along with this program; if not, write to the Free Software
14 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 package Audio::MPD::Test;
20 use strict;
21 use warnings;
23 use Exporter;
24 use FindBin qw[ $Bin ];
25 use Readonly;
28 use base qw[ Exporter ];
29 our @EXPORT = qw[ customize_test_mpd_configuration start_test_mpd stop_test_mpd ];
30 #our ($VERSION) = '$Rev: 5726 $' =~ /(\d+)/;
33 Readonly my $TEMPLATE => "$Bin/mpd-test/mpd.conf.template";
34 Readonly my $CONFIG => "$Bin/mpd-test/mpd.conf";
36 { # this will be run when Audio::MPD::Test will be use-d.
37 customize_test_mpd_configuration();
38 my $restart = _stop_user_mpd_if_needed();
39 start_test_mpd();
41 END {
42 stop_test_mpd();
43 return unless $restart; # no need to restart
44 system 'mpd 2>/dev/null' if $restart; # restart user mpd
45 sleep 1; # wait 1 second to let mpd start.
50 #--
51 # public subs
54 # customize_test_mpd_configuration( [$port] )
56 # Create a fake mpd configuration file, based on the file mpd.conf.template
57 # located in t/mpd-test. The string PWD will be replaced by the real path -
58 # ie, where the tarball has been untarred. The string PORT will be replaced
59 # by $port if specified, 6600 otherwise (MPD default).
61 sub customize_test_mpd_configuration {
62 my ($port) = @_;
63 $port ||= 6600;
65 # open template and config.
66 open my $in, '<', $TEMPLATE or die "can't open [$TEMPLATE]: $!\n";
67 open my $out, '>', $CONFIG or die "can't open [$CONFIG]: $!\n";
69 # replace string and fill in config file.
70 while ( defined( my $line = <$in> ) ) {
71 $line =~ s!PWD!$Bin/mpd-test!;
72 $line =~ s!PORT!$port!;
73 print $out $line;
76 # clean up.
77 close $in;
78 close $out;
80 # create a fake mpd db.
81 system( "mpd --create-db $CONFIG >/dev/null 2>&1" ) == 0
82 or die "could not create fake mpd database: $?\n";
87 # start_test_mpd()
89 # Start the fake mpd, and die if there were any error.
91 sub start_test_mpd {
92 my $output = qx[mpd $CONFIG 2>&1];
93 die "could not start fake mpd: $output\n" if $output;
94 sleep 1; # wait 1 second to let mpd start.
99 # stop_test_mpd()
101 # Kill the fake mpd.
103 sub stop_test_mpd {
104 system "mpd --kill $CONFIG 2>/dev/null";
105 sleep 1; # wait 1 second to free output device.
106 unlink "$Bin/mpd-test/state", "$Bin/mpd-test/music.db";
111 # private subs
115 # my $was_running = _stop_user_mpd_if_needed()
117 # This sub will check if mpd is currently running. If it is, force it to
118 # a full stop (unless MPD_TEST_OVERRIDE is not set).
120 # In any case, it will return a boolean stating whether mpd was running
121 # before forcing stop.
123 sub _stop_user_mpd_if_needed {
124 # check if mpd is running.
125 my $is_running = grep { /mpd$/ } qx[ ps -e ];
127 return 0 unless $is_running; # mpd does not run - nothing to do.
129 # check force stop.
130 die "mpd is running\n" unless $ENV{MPD_TEST_OVERRIDE};
131 system( 'mpd --kill 2>/dev/null') == 0 or die "can't stop user mpd: $?\n";
132 sleep 1; # wait 1 second to free output device
133 return 1;
139 __END__
141 =head1 NAME
143 Audio::MPD::Test - automate launching of fake mdp for testing purposes
146 =head1 SYNOPSIS
148 use Audio::MPD::Test; # die if error
149 [...]
150 stop_fake_mpd();
153 =head1 DESCRIPTION
155 =head2 General usage
157 This module will try to launch a new mpd server for testing purposes. This
158 mpd server will then be used during Audio::MPD tests.
160 In order to achieve this, the module will create a fake mpd.conf file with
161 the correct pathes (ie, where you untarred the module tarball). It will then
162 check if some mpd server is already running, and stop it if the
163 MPD_TEST_OVERRIDE environment variable is true (die otherwise). Last it will
164 run the test mpd with its newly created configuration file.
166 Everything described above is done automatically when the module is C<use>-d.
169 Once the tests are run, the mpd server will be shut down, and the original
170 one will be relaunched (if there was one).
172 Note that the test mpd will listen to C<localhost>, so you are on the safe
173 side. Note also that the test suite comes with its own ogg files - and yes,
174 we can redistribute them since it's only some random voice recordings :-)
177 =head2 Advanced usage
179 In case you want more control on the test mpd server, you can use the
180 following public methods:
182 =over 4
184 =item start_test_mpd()
186 Start the fake mpd, and die if there were any error.
188 =item stop_test_mpd()
190 Kill the fake mpd.
192 =item customize_test_mpd_configuration( [$port] )
194 Create a fake mpd configuration file, based on the file mpd.conf.template
195 located in t/mpd-test. The string PWD will be replaced by the real path -
196 ie, where the tarball has been untarred. The string PORT will be replaced
197 by $port if specified, 6600 otherwise (MPD default).
199 =back
201 This might be useful when trying to test connections with mpd server.
204 =head1 COPYRIGHT AND LICENSE
206 Copyright (c) 2007 Jerome Quelin
208 This program is free software; you can redistribute it and/or modify
209 it under the terms of the GNU General Public License as published by
210 the Free Software Foundation; either version 2 of the License, or
211 (at your option) any later version.
213 This program is distributed in the hope that it will be useful,
214 but WITHOUT ANY WARRANTY; without even the implied warranty of
215 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
216 GNU General Public License for more details.
218 =cut