[project @ 5554]
[audio-mpd.git] / lib / Audio / MPD / Test.pm
blob65479b6d85a4f538966e92ab4f7e24b7a4528712
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 ];
32 Readonly my $TEMPLATE => "$Bin/mpd-test/mpd.conf.template";
33 Readonly my $CONFIG => "$Bin/mpd-test/mpd.conf";
35 { # this will be run when Audio::MPD::Test will be use-d.
36 customize_test_mpd_configuration();
37 my $restart = _stop_user_mpd_if_needed();
38 start_test_mpd();
40 END {
41 stop_test_mpd();
42 return unless $restart; # no need to restart
43 system 'mpd 2>/dev/null' if $restart; # restart user mpd
44 sleep 1; # wait 1 second to let mpd start.
49 #--
50 # public subs
53 # customize_test_mpd_configuration( [$port] )
55 # Create a fake mpd configuration file, based on the file mpd.conf.template
56 # located in t/mpd-test. The string PWD will be replaced by the real path -
57 # ie, where the tarball has been untarred. The string PORT will be replaced
58 # by $port if specified, 6600 otherwise (MPD default).
60 sub customize_test_mpd_configuration {
61 my ($port) = @_;
62 $port ||= 6600;
64 # open template and config.
65 open my $in, '<', $TEMPLATE or die "can't open [$TEMPLATE]: $!\n";
66 open my $out, '>', $CONFIG or die "can't open [$CONFIG]: $!\n";
68 # replace string and fill in config file.
69 while ( defined( my $line = <$in> ) ) {
70 $line =~ s!PWD!$Bin/mpd-test!;
71 $line =~ s!PORT!$port!;
72 print $out $line;
75 # clean up.
76 close $in;
77 close $out;
82 # start_test_mpd()
84 # Start the fake mpd, and die if there were any error.
86 sub start_test_mpd {
87 system( "mpd $CONFIG 2>/dev/null" ) == 0
88 or die "could not start fake mpd: $?\n";
89 sleep 1; # wait 1 second to let mpd start.
94 # stop_test_mpd()
96 # Kill the fake mpd.
98 sub stop_test_mpd {
99 system "mpd --kill $CONFIG 2>/dev/null";
100 sleep 1; # wait 1 second to free output device.
101 unlink "$Bin/mpd-test/state", "$Bin/mpd-test/music.db";
106 # private subs
110 # my $was_running = _stop_user_mpd_if_needed()
112 # This sub will check if mpd is currently running. If it is, force it to
113 # a full stop (unless MPD_TEST_OVERRIDE is not set).
115 # In any case, it will return a boolean stating whether mpd was running
116 # before forcing stop.
118 sub _stop_user_mpd_if_needed {
119 # check if mpd is running.
120 my $is_running = grep { /mpd$/ } qx[ ps -e ];
122 return 0 unless $is_running; # mpd does not run - nothing to do.
124 # check force stop.
125 die "mpd is running\n" unless $ENV{MPD_TEST_OVERRIDE};
126 system( 'mpd --kill 2>/dev/null') == 0 or die "can't stop user mpd: $?\n";
127 sleep 1; # wait 1 second to free output device
128 return 1;
134 __END__
136 =head1 NAME
138 Audio::MPD::Test - automate launching of fake mdp for testing purposes
141 =head1 SYNOPSIS
143 use Audio::MPD::Test; # die if error
144 [...]
145 stop_fake_mpd();
148 =head1 DESCRIPTION
150 =head2 General usage
152 This module will try to launch a new mpd server for testing purposes. This
153 mpd server will then be used during Audio::MPD tests.
155 In order to achieve this, the module will create a fake mpd.conf file with
156 the correct pathes (ie, where you untarred the module tarball). It will then
157 check if some mpd server is already running, and stop it if the
158 MPD_TEST_OVERRIDE environment variable is true (die otherwise). Last it will
159 run the test mpd with its newly created configuration file.
161 Everything described above is done automatically when the module is C<use>-d.
164 Once the tests are run, the mpd server will be shut down, and the original
165 one will be relaunched (if there was one).
167 Note that the test mpd will listen to C<localhost>, so you are on the safe
168 side. Note also that the test suite comes with its own ogg files - and yes,
169 we can redistribute them since it's only some random voice recordings :-)
172 =head2 Advanced usage
174 In case you want more control on the test mpd server, you can use the
175 following public methods:
177 =over 4
179 =item start_test_mpd()
181 Start the fake mpd, and die if there were any error.
183 =item stop_test_mpd()
185 Kill the fake mpd.
187 =item customize_test_mpd_configuration( [$port] )
189 Create a fake mpd configuration file, based on the file mpd.conf.template
190 located in t/mpd-test. The string PWD will be replaced by the real path -
191 ie, where the tarball has been untarred. The string PORT will be replaced
192 by $port if specified, 6600 otherwise (MPD default).
194 =back
196 This might be useful when trying to test connections with mpd server.
199 =head1 COPYRIGHT AND LICENSE
201 Copyright (c) 2007 Jerome Quelin
203 This program is free software; you can redistribute it and/or modify
204 it under the terms of the GNU General Public License as published by
205 the Free Software Foundation; either version 2 of the License, or
206 (at your option) any later version.
208 This program is distributed in the hope that it will be useful,
209 but WITHOUT ANY WARRANTY; without even the implied warranty of
210 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
211 GNU General Public License for more details.
213 =cut