Skip to content

Commit

Permalink
Config loader + tests - KSP-CKAN#3 KSP-CKAN#4
Browse files Browse the repository at this point in the history
  • Loading branch information
techman83 committed Jun 28, 2015
1 parent 3900ae7 commit 0599735
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
71 changes: 71 additions & 0 deletions lib/App/KSP_CKAN/Tools/Config.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package App::KSP_CKAN::Tools::Config;

use v5.010;
use strict;
use warnings;
use autodie;
use Method::Signatures 20140224;
use Config::Tiny;
use Carp qw( croak );
use Moo;
use namespace::clean;

# ABSTRACT: A lite wrapper around Config::Tiny

# VERSION: Generated by DZP::OurPkg:Version

=head1 SYNOPSIS
use App::KSP_CKAN::Git;
my $git = App::KSP_CKAN::Config->new(
file => "/path/to/file",
);
=head1 DESCRIPTION
Provides a config object for KSP-CKAN
=cut

has 'file' => ( is => 'ro', default => sub { $ENV{HOME}."/.ksp-ckan" } );
has '_config' => ( is => 'ro', lazy => 1, builder => 1 );
has 'CKAN_meta' => ( is => 'ro', lazy => 1, builder => 1 );
has 'NetKAN' => ( is => 'ro', lazy => 1, builder => 1 );
has 'GH_token' => ( is => 'ro', lazy => 1, builder => 1 );
has 'working' => ( is => 'ro', lazy => 1, builder => 1 );

method _build__config {
if ( ! -e $self->file ) {
croak( "No config at ".$self->file );
}
return Config::Tiny->read( $self->file );
}

method _build_CKAN_meta {
croak( "Missing 'CKAN_meta' from config" ) if ! $self->_config->{_}{'CKAN_meta'};
return $self->_config->{_}{'CKAN_meta'};
}

method _build_NetKAN {
croak( "Missing 'NetKAN' from config" ) if ! $self->_config->{_}{'NetKAN'};
return $self->_config->{_}{'NetKAN'};
}

method _build_GH_token {
if ( ! $self->_config->{_}{'GH_token'} ) {
return 0;
} else {
return $self->_config->{_}{'GH_token'};
}
}

method _build_working {
if ( ! $self->_config->{_}{'working'} ) {
return $ENV{HOME}."/CKAN-working";
} else {
return $self->_config->{_}{'working'};
}
}

1;
34 changes: 34 additions & 0 deletions t/App/KSP_CKAN/Tools/Config.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env perl -w

use lib 't/lib/';

use strict;
use warnings;
use Test::Most;
use Test::Warnings;
use App::KSP_CKAN::Test;

# Setup our environment
my $test = App::KSP_CKAN::Test->new();
$test->create_config;

use_ok("App::KSP_CKAN::Tools::Config");

my $config = App::KSP_CKAN::Tools::Config->new(
file => $test->tmp."/.ksp-ckan",
);

is($config->CKAN_meta, $test->_tmp."/data/CKAN-meta", "NetKAN loaded from config");
is($config->NetKAN, $test->_tmp."/data/NetKAN", "NetKAN loaded from config");
is($config->GH_token, "123456789", "GH_token loaded from config");
is($config->working, $test->_tmp, "working loaded from config");

$test->create_config( optional => 0 );
$config = App::KSP_CKAN::Tools::Config->new(
file => $test->tmp."/.ksp-ckan",
);

is($config->GH_token, 0, "GH_token returns false");
is($config->working, $ENV{HOME}."/CKAN-working", "working default generated");

done_testing();
32 changes: 32 additions & 0 deletions t/lib/App/KSP_CKAN/Test.pm
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,38 @@ Does what it says on the tin, cleans up our mess.
=cut

=method create_config
$test->create_config( optional => 0 );
Creates a dummy config file for testing. The 'optional'
defaults to true if unspecified, generating a test config
with optional values.
=cut

method create_config(:$optional = 1) {
open my $in, '>', $self->_tmp."/.ksp-ckan";
print $in "CKAN_meta=".$self->_tmp."/data/CKAN-meta\n";
print $in "NetKAN=".$self->_tmp."/data/NetKAN\n";

if ($optional) {
print $in "GH_token=123456789\n";
print $in "working=".$self->_tmp."\n";
}

close $in;
return;
}

=method cleanup
$test->cleanup;
Does what it says on the tin, cleans up our mess.
=cut

method cleanup {
if ( -d $self->_tmp ) {
remove_tree($self->_tmp);
Expand Down

0 comments on commit 0599735

Please sign in to comment.