forked from KSP-CKAN/NetKAN-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Config loader + tests - KSP-CKAN#3 KSP-CKAN#4
- Loading branch information
Showing
3 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters