File: MPD.pm

package info (click to toggle)
libnet-async-mpd-perl 0.005-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 236 kB
  • sloc: perl: 766; makefile: 6
file content (302 lines) | stat: -rw-r--r-- 6,480 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
use strict;
use warnings;

package Test::Server::MPD;

our $VERSION = '0';

use Moo;

use IO::Async::Loop;
use File::Share qw( dist_file );
use File::Which qw( which );
use Path::Tiny qw( path );
use Types::Path::Tiny qw( File Dir );
use Types::Standard qw( Str Int HashRef ArrayRef Undef );
use Net::EmptyPort qw( empty_port check_port );

has port => (
  is => 'ro',
  isa => Int,
  lazy => 1,
  default => sub {
    my $port = $ENV{MPD_PORT} // 6600;
    check_port($port) ? empty_port() : $port;
  },
);

has host => (
  is => 'ro',
  isa => Str,
  lazy => 1,
  default => 'localhost',
);

has template => (
  is => 'rw',
  lazy => 1,
  isa => File,
  coerce => 1,
  default => sub { path( dist_file('Net-Async-MPD', 'mpd.conf.template') ) },
);

has profiles => (
  is => 'rw',
  isa => HashRef[ArrayRef],
  lazy => 1,
  default => sub { {} }
);

has root => (
  is => 'ro',
  lazy => 1,
  isa => Dir,
  coerce => 1,
  default => sub { Path::Tiny::tempdir() }
);

has config => (
  is => 'ro',
  lazy => 1,
  isa => File,
  coerce => 1,
  default => sub { $_[0]->_populate_config }
);

has bin => (
  is => 'ro',
  lazy => 1,
  isa => File,
  coerce => 1,
  default => sub {
    which 'mpd'
      or die 'Could not find MPD executable in PATH. Try setting it manually', "\n";
  }
);

has _pid => (
  is => 'rw',
  isa => Int|Undef,
);

sub BUILD {
  my ($self) = @_;

  $self->root->child('playlists')->mkpath;
  $self->root->child('music')->mkpath;
}

sub _populate_config {
  my ($self) = @_;

  my $template = $self->template->slurp;

  foreach my $method (qw( port root )) {
    my $value = $self->$method;
    $template =~ s/\{\{ $method \}\}/$value/g;
  }

  my $host = $self->host;
  $template =~ s/\{\{ host \}\}/$host/g;

  my $profiles = q{};
  foreach my $password (keys %{$self->profiles}) {
    my @permissions = @{$self->profiles->{$password}};
    $profiles .=
        qq{password\t"$password\@}
      . join(',', @permissions)
      . qq{"\n}
  }

  $profiles = qq{default_permissions\t"read,add,control,admin"\n}
    if $profiles eq q{};

  $template =~ s/\{\{ profiles \}\}\s*\n/$profiles/g;

  my $config = $self->root->child('mpd.conf');
  $config->spew($template);

  return $config;
}

sub is_running { defined $_[0]->_pid }

sub start {
  my ($self) = @_;

  my $loop = IO::Async::Loop->new;
  my $start = $loop->new_future;

  $self->_pid(
    $loop->run_child(
      command => [ $self->bin, $self->config->realpath ],
      on_finish => sub {
        my ($pid, $exitcode, $stdout, $stderr) = @_;
        return $start->fail('Could not start MPD server: ' . $stdout)
          if $exitcode != 0;

        $start->done;
      },
    )
  );

  $start->get;
  return $self->_pid;
}

sub stop {
  my ($self) = @_;

  return unless $self->is_running;

  my $loop = IO::Async::Loop->new;
  my $stop = $loop->new_future;

  $loop->run_child(
    command => [ $self->bin, '--kill', $self->config->realpath ],
    on_finish => sub {
      my ($pid, $exitcode, $stdout, $stderr) = @_;

      return $stop->fail('Could not stop MPD server: ' . $stdout)
        if $exitcode != 0;

      $self->_pid(undef);
      $stop->done;
    },
  );

  return $stop->get;
}

1;

__END__

=encoding utf-8

=head1 NAME

Test::Server::MPD - Create MPD test servers at whim

=head1 SYNOPSIS

    use Test::Server::MPD;

    my $server = Test::Server::MPD->new(
      port => '12345', # defaults to 6600 or an empty port
      profiles => {
        'letmein' => [qw( read control )],
      },
    );
    $server->start;

    ...

    # You might want to put this in and END block, or
    # guard it with Scope::Guard, in case your test dies
    $server->stop;

=head1 DESCRIPTION

This module makes it easy to start and stop MPD server instances for testing.

=head1 ATTRIBUTES

=over 4

=item C<config>

The path to a configfile for the server. If this attribute is set, then that
file will be used to start the server. If not provided, then the file provided
as the C<template> attribute will be populated with the rest of the object's
attributes.

=item C<template>

The path to a config template file. Defaults to a file shipped with this
distribution. The template uses a very limited version of the mustache
templating system. It recognises the following keys: C<port>, C<root>, C<host>.

The special key C<profiles> populates the C<password> attributes of the MPD
config files. If no C<profiles> are provided, this key will instead populate
the C<default_permissions> value with all permissions.

=item C<port>

The port on which the server will be listening. Defaults to the value of the
C<MPD_PORT> environment variable, or 6600 if undefined. If 6600 is already in
use (likely by another MPD server), an empty port will be found using
L<Net::EmptyPort>.

Note that it is possible (however unlikely) that the port that was found to
be empty is actually not by the time the server starts.

=item C<host>

The host to use for the server. Defaults to C<localhost>.

=item C<profiles>

A hash reference with as many keys as profiles to use. Keys are the passwords,
and the values (which should be array references) indicate the permissions
for that profile.

=item C<bin>

The path to the MPD binary. Defaults to the one found by L<File::Which>. This
value is not checked until the server is C<start>ed (or C<stop>ped).

=back

=head1 METHODS

=over 4

=item C<start>

Start the MPD server using the object's C<config> file, or one created by the
object's C<template>. Returns the C<pid> of the server process.

This method will throw an exception if the C<bin> attribute is not set to a
plain file, or if there was an error starting the server.

=item C<stop>

Stop the MPD server.

This method will throw an exception if the C<bin> attribute is not set to a
plain file, or if there was an error stopping the server.

=back

=head1 SEE ALSO

=over 4

=item L<Test::Corpus::Audio::MPD>

The inspiration for this module, used by L<POE::Component::Client::MPD> and
L<Audio::MPD>. The fact that it cannot be made to coexist with a running
instance of MPD, and that it starts its server automatically upon use,
partly explain the existance of this module.

=back

=head1 AUTHOR

=over 4

=item *

José Joaquín Atria <jjatria@cpan.org>

=back

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by José Joaquín Atria.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut