Skip to content

Commit

Permalink
Travis: Automatically test KS submissions
Browse files Browse the repository at this point in the history
With @SirCmpwn's permission, this should download and test any mod from
KerbalStuff. This is only triggered on PRs, and will automatically fetch
the latest stable netkan.exe for testing.
  • Loading branch information
pjf committed Dec 13, 2014
1 parent eae4ed8 commit 6740b13
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.fatten.out
*.ckan
netkan.exe
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: c
language: csharp

install:
- sudo apt-get install python-demjson libtest-most-perl libjson-any-perl
- sudo apt-get install python-demjson libtest-most-perl libjson-any-perl libwww-mechanize-perl libipc-system-simple-perl

script:
- jsonlint -s -v NetKAN/*.netkan
Expand Down
77 changes: 77 additions & 0 deletions t/kerbalstuff.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/perl -w
use 5.010;
use strict;
use warnings;
use autodie qw(:all);
use Test::More;
use IPC::System::Simple qw(capture);
use WWW::Mechanize;

my $pr_branch = $ENV{TRAVIS_PULL_REQUEST};
my $netkan = "./netkan.exe";

if (! $pr_branch or $pr_branch eq "false") {
plan skip_all => "Not a pull request";
}

if (! -x $netkan) {

# If we don't already have a netkan executable, then go download the latest.
# This includes unstable netkan.exe builds, as submissions may reference
# experimental features.

my $agent = WWW::Mechanize->new( agent => "NetKAN travis testing" );
$agent->get("https://github.com/KSP-CKAN/CKAN/releases");
$agent->follow_link(text => 'netkan.exe');

open(my $fh, '>', $netkan);
binmode($fh);
print {$fh} $agent->content;
close($fh);
chmod(0755, $netkan);
}

# TODO: What if our PR is against something other than master?
my @changed_files = capture("git diff --name-only master");
chomp(@changed_files);

# Walk through our changed files. If any of them mention KS, then
# run netkan over them. (We have @sircmpwn's permission to make KS
# downloads during CI testing.)

foreach my $file (@changed_files) {
if (is_ks_file($file)) {
netkan_validate($file);
}
}

done_testing;

sub is_ks_file {
my ($file) = @_;

# Not a netkan file? Not something we want to test.
return 0 if ($file !~ m{\.netkan$});

local $/; # Slurp mode.

open(my $fh, '<', $file);
my $content = <$fh>;
close($fh);

return $content =~ m{#/ckan/kerbalstuff};
}

# Simply checks to see if netkan.exe runs without errors on this file
sub netkan_validate {
my ($file) = @_;

my $valid = eval {
system($netkan, $file);
return 1;
};

if ($@) { diag $@ }

ok($valid, $file);
}

0 comments on commit 6740b13

Please sign in to comment.