Skip to content

Commit

Permalink
Add beam-wire script with checks for all but method chain & serial
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce Armstrong committed Apr 24, 2015
1 parent 6a09c7e commit 0d5b259
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ my %WriteMakefileArgs = (
"Module::Build" => "0.28"
},
"DISTNAME" => "Beam-Wire",
"EXE_FILES" => [],
"EXE_FILES" => ['script/beam-wire'],
"LICENSE" => "perl",
"NAME" => "Beam::Wire",
"PREREQ_PM" => {
Expand Down
34 changes: 34 additions & 0 deletions lib/Beam/Wire.pm
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,40 @@ sub _load_config {
return "HASH" eq ref $loader ? (values(%{$loader}))[0] : {};
}

sub validate {
my @valid_dependency_nodes = qw( class method args extends lifecycle on config );
my ( $self, $instantiate ) = @_;
while ( my ( $name, $v ) = each %{ $self->{config} } ) {
my %config = %{ $self->get_config($name) };
%config = $self->merge_config(%config);

if ( exists $config{value} && ( exists $config{class} || exists $config{extends})) {
Beam::Wire::Exception::InvalidConfig->throw(
name => $name,
file => $self->file,
error => '"value" cannot be used with "class" or "extends"',
);
}

if ( $config{config} ) {
my $conf_path = path( $config{config} );
if ( $self->file ) {
$conf_path = path( $self->file )->parent->child($conf_path);
}
%config = %{ $self->_load_config("$conf_path") };
}

unless ( $config{value} || $config{class} || $config{extends} ) {
warn "$name in " . $self->file . " missing 'value', 'class', or 'extends'; ignoring...\n";
next;
}

eval "require " . $config{class} if $config{class};
#TODO: check method chain & serial
$self->get($name) if $instantiate;
}
}

=head1 EXCEPTIONS
If there is an error internal to Beam::Wire, an exception will be thrown. If there is an
Expand Down
46 changes: 46 additions & 0 deletions script/beam-wire
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!perl

use strict;
use warnings;

use Getopt::Long;
use Pod::Usage 'pod2usage';

GetOptions(
'a|all' => \my $all,
'i|instantiate' => \my $inst,
'l|lib' => \my $lib,
'h|?|help' => \my $help,
);

pod2usage( -msg => "Please provide a configuration file\n" ) unless @ARGV;

if ($help) {
pod2usage(
-msg => 'test',
-verbose => 1
);
}
push @INC, 'lib' if $lib;

use Beam::Wire;
my $wire = Beam::Wire->new( file => $ARGV[0] );
$wire->validate($inst);

=head1 NAME
beam-wire - Validate Beam::Wire configuration files
=head1 SYNOPSIS
Usage: beam-wire [options] config
=head1 OPTIONS
-a, --all Warn about all errors found instead of quitting after the first
-i, --instantiate Attempt to instantiate all dependencies
-l, --lib Add 'lib' to the path for checking dependency class names
-h, --help Display this help
-? Display this help
=cut
13 changes: 13 additions & 0 deletions t/exception.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use Path::Tiny qw( path );
my $SHARE_DIR = path( $Bin, 'share' );

use Beam::Wire;
sub invalid ($$$$);

subtest 'config file does not exist' => sub {
throws_ok { Beam::Wire->new( file => 'DOES_NOT_EXIST.yml' ) } 'Beam::Wire::Exception::Constructor';
Expand Down Expand Up @@ -41,6 +42,7 @@ subtest "extend a service that doesn't exist" => sub {
throws_ok { $wire->get( 'foo' ) } 'Beam::Wire::Exception::NotFound';
is $@->name, 'bar';
like $@, qr{\QService 'bar' not found}, 'stringifies';
invalid $wire, 'bar', 'NotFound', qr{\QService 'bar' not found};
};

subtest "service with both value and class/extends" => sub {
Expand All @@ -59,6 +61,7 @@ subtest "service with both value and class/extends" => sub {
throws_ok { $wire->get( 'foo' ) } 'Beam::Wire::Exception::InvalidConfig';
is $@->name, 'foo';
like $@, qr{\QInvalid config for service 'foo': "value" cannot be used with "class" or "extends"}, 'stringifies';
invalid $wire, 'foo', 'InvalidConfig', qr{\QInvalid config for service 'foo': "value" cannot be used with "class" or "extends"};
};
subtest "extends + value" => sub {
my $wire;
Expand All @@ -78,6 +81,7 @@ subtest "service with both value and class/extends" => sub {
throws_ok { $wire->get( 'foo' ) } 'Beam::Wire::Exception::InvalidConfig';
is $@->name, 'foo';
like $@, qr{\QInvalid config for service 'foo': "value" cannot be used with "class" or "extends"}, 'stringifies';
invalid $wire, 'foo', 'InvalidConfig', qr{\QInvalid config for service 'foo': "value" cannot be used with "class" or "extends"};
};
subtest "value in extended service" => sub {
my $wire;
Expand All @@ -97,6 +101,7 @@ subtest "service with both value and class/extends" => sub {
throws_ok { $wire->get( 'foo' ) } 'Beam::Wire::Exception::InvalidConfig';
is $@->name, 'foo';
like $@, qr{\QInvalid config for service 'foo': "value" cannot be used with "class" or "extends"}, 'stringifies';
invalid $wire, 'foo', 'InvalidConfig', qr{\QInvalid config for service 'foo': "value" cannot be used with "class" or "extends"};
};

subtest "exception shows file name" => sub {
Expand All @@ -122,4 +127,12 @@ subtest "service with both value and class/extends" => sub {
};
};

sub invalid ($$$$) {
my ( $wire, $name, $exception, $error_regex ) = @_;
local $Test::Builder::Level = $Test::Builder::Level + 1;
throws_ok { $wire->validate() } "Beam::Wire::Exception::$exception";
is $@->name, $name;
like $@, $error_regex;
}

done_testing;

0 comments on commit 0d5b259

Please sign in to comment.