Skip to content

Commit

Permalink
NetKAN interactions + tests - KSP-CKAN#3 KSP-CKAN#4
Browse files Browse the repository at this point in the history
  • Loading branch information
techman83 committed May 31, 2015
1 parent f65d14b commit f21e221
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 2 deletions.
67 changes: 65 additions & 2 deletions lib/App/KSP_CKAN/Tools/NetKAN.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use autodie;
use Method::Signatures 20140224;
use Try::Tiny;
use File::Spec 'tmpdir';
use File::Basename qw(basename);
use File::Path qw(mkpath);
use Capture::Tiny qw(capture);
use Moo;
use namespace::clean;

Expand All @@ -23,7 +26,7 @@ use namespace::clean;
chache => "/path/to/cache",
token => $token,
file => '/path/to/file.netkan',
output => '/path/to/CKAN-meta',
ckan_meta => '/path/to/CKAN-meta',
);
=head1 DESCRIPTION
Expand All @@ -38,8 +41,68 @@ data and only run the inflater when required.
has 'netkan' => ( is => 'ro', required => 1 );
has 'cache' => ( is => 'ro', default => sub { File::Spec->tmpdir()."NetKAN-cache"; } );
has 'file' => ( is => 'ro', required => 1);
has 'output' => ( is => 'ro', required => 1 );
has 'ckan_meta' => ( is => 'ro', required => 1 );
has 'token' => ( is => 'ro' );
has 'rescan' => ( is => 'ro', default => sub { 1 } );
has '_output' => ( is => 'ro', lazy => 1, builder => 1 );
has '_cli' => ( is => 'ro', lazy => 1, builder => 1 );
has '_cache' => ( is => 'ro', lazy => 1, builder => 1 );

method _build__cache {
# We don't want stale test data
if ( ! -d $self->cache ) {
mkpath($self->cache);
}

return $self->cache;
}

method _build__output {
my $basename = basename($self->file, ".netkan");
if (! -d $self->ckan_meta."/".$basename ) {
mkdir $self->ckan_meta."/".$basename;
}
return $self->ckan_meta."/".$basename;
}

method _build__cli {
if ($self->token) {
return $self->netkan." --outputdir=".$self->_output." --cachedir=".$self->_cache." --github-token=".$self->token." ".$self->file;
} else {
return $self->netkan." --outputdir=".$self->_output." --cachedir=".$self->_cache." ".$self->file;
}
}

method _check_lite {
# TODO: Build a method to go and check if required full inflate
return 0;
}

method _parse_error($error) {
$error =~ m{^\[ERROR\].(.+)}m;
return $1;
}

=method inflate
$netkan->inflate;
Inflates our metadata.
=cut

method inflate {
if (! $self->rescan ) {
return;
}

my ($stderr, $stdout, $exit) = capture {
system($self->_cli);
};

# TODO: Logging
#$self->_parse_error($stdout) if $stdout;
return $exit;
}

1;
47 changes: 47 additions & 0 deletions t/App/KSP_CKAN/Tools/NetKAN.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,55 @@ use warnings;
use Test::Most;
use Test::Warnings;
use App::KSP_CKAN::Test;
use App::KSP_CKAN::Tools::Http;
use App::KSP_CKAN::Tools::Git;

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

# CKAN-meta
$test->create_repo("CKAN-meta");
my $ckan = App::KSP_CKAN::Tools::Git->new(
remote => $test->tmp."/data/CKAN-meta",
local => $test->tmp,
clean => 1,
);
$ckan->pull;

# Netkan
$test->create_repo("NetKAN");
my $netkan_git = App::KSP_CKAN::Tools::Git->new(
remote => $test->tmp."/data/NetKAN",
local => $test->tmp,
clean => 1,
);
$netkan_git->pull;

# netkan.exe
my $http = App::KSP_CKAN::Tools::Http->new();
$http->mirror( url => "http://ci.ksp-ckan.org:8080/job/NetKAN/lastSuccessfulBuild/artifact/netkan.exe", path => $test->tmp."/netkan.exe", exe => 1);

use_ok("App::KSP_CKAN::Tools::NetKAN");
my $netkan = App::KSP_CKAN::Tools::NetKAN->new(
netkan => $test->tmp."/netkan.exe",
cache => $test->tmp."/cache",
ckan_meta => $test->tmp."/CKAN-meta",
file => $test->tmp."/NetKAN/NetKAN/DogeCoinFlag.netkan"
);
is( $netkan->inflate, 0, "Return success correctly" );
my @files = glob($test->tmp."/CKAN-meta/DogeCoinFlag");
is( -e $files[0], 1, "Meta Data inflated" );

$netkan = App::KSP_CKAN::Tools::NetKAN->new(
netkan => $test->tmp."/netkan.exe",
cache => $test->tmp."/cache",
ckan_meta => $test->tmp."/CKAN-meta",
file => $test->tmp."/NetKAN/NetKAN/DogeCoinFlag-broken.netkan"
);
isnt( $netkan->inflate, 0, "Return failure correctly" );

# Cleanup after ourselves
$test->cleanup;

done_testing();
__END__
13 changes: 13 additions & 0 deletions t/data/NetKAN/NetKAN/DogeCoinFlag-broken.netkan
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"spec_version" : 1,
"identifier" : "DogeCoinFlag",
"name" : "Dogecoin Flag",
"abstract" : "Such flag. Very currency. Wow.",
"description" : "Adorn your craft with your favourite cryptocurrency. To the mün!",
"ksp_version" : "any",
"license" : "CC-BY",
"author" : "daviddwk",
"resources" : {
"homepage" : "https://www.reddit.com/r/dogecoin/comments/1tdlgg/i_made_a_more_accurate_dogecoin_and_a_ksp_flag/"
}
}
15 changes: 15 additions & 0 deletions t/data/NetKAN/NetKAN/DogeCoinFlag.netkan
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"spec_version" : 1,
"identifier" : "DogeCoinFlag",
"name" : "Dogecoin Flag",
"abstract" : "Such flag. Very currency. Wow.",
"description" : "Adorn your craft with your favourite cryptocurrency. To the mün!",
"$kref" : "#/ckan/github/pjf/DogeCoinFlag",
"$vref" : "#/ckan/ksp-avc",
"ksp_version" : "any",
"license" : "CC-BY",
"author" : "daviddwk",
"resources" : {
"homepage" : "https://www.reddit.com/r/dogecoin/comments/1tdlgg/i_made_a_more_accurate_dogecoin_and_a_ksp_flag/"
}
}

0 comments on commit f21e221

Please sign in to comment.