Skip to content

Commit

Permalink
added extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xaicron committed Apr 12, 2012
1 parent 3c91a40 commit d63c6ee
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 0 deletions.
66 changes: 66 additions & 0 deletions xt/01_basic.t
@@ -0,0 +1,66 @@
use strict;
use warnings;
use Test::More;
use xt::Util;

note explain my $meta_data = make_meta_data(*DATA);

subtest requires => sub {
my $requires = $meta_data->{requires};
is $requires->{Plack}, '0.9986';
is $requires->{'SQL::Maker'}, 0;
};

subtest recommends => sub {
my $recommends = $meta_data->{recommends};
is $recommends->{'JSON::XS'}, '2.0';
is $recommends->{'Test::TCP'}, '1.12';
};

done_testing;

__DATA__
@@ Makefile.PL
use inc::Module::Install;
cpanfile;
name 'Dummy';
all_from 'lib/Dummy.pm';
tests 't/*.t';
auto_include;
WriteAll;
@@ cpanfile
requires 'Plack' => '0.9986';
requires 'SQL::Maker';
recommends 'JSON::XS', '2.0';
on 'test' => sub {
requires 'Test::More', '>= 0.96, < 2.0';
recommends 'Test::TCP', '1.12';
};
@@ lib/Dummy.pm
package Dummy;
use 5.006;
our $VERSION = '0.1';
1;
__END__
=pod
=head1 AUTHOR
Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
=head1 COPYRIGHT
Copyright 2012- Tatsuhiko Miyagawa
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
50 changes: 50 additions & 0 deletions xt/02_author.t
@@ -0,0 +1,50 @@
use strict;
use warnings;
use Test::More;
use xt::Util;
use Test::Requires 'Module::Install::AuthorRequires';

eval { make_meta_data(*DATA) };
like $@, qr/\QCan't find author dependency Dummmmmmmmmmmmmmmmmy\E/;

done_testing;

__DATA__
@@ Makefile.PL
use inc::Module::Install;
cpanfile;
name 'Dummy';
all_from 'lib/Dummy.pm';
tests 't/*.t';
auto_include;
WriteAll;
@@ cpanfile
on 'develop' => sub {
requires 'Dummmmmmmmmmmmmmmmmy';
};
@@ lib/Dummy.pm
package Dummy;
use 5.006;
our $VERSION = '0.1';
1;
__END__
=pod
=head1 AUTHOR
Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
=head1 COPYRIGHT
Copyright 2012- Tatsuhiko Miyagawa
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
102 changes: 102 additions & 0 deletions xt/Util.pm
@@ -0,0 +1,102 @@
package xt::Util;

use strict;
use warnings;
use Test::Builder;
use File::Temp qw(tempdir);
use File::Path qw(mkpath);
use File::Basename qw(dirname);
use Cwd qw(getcwd);
use Config qw(%Config);
use YAML::Tiny;
use Capture::Tiny qw(capture_merged);
use base qw(Exporter);

use constant DMAKE => $^O eq 'MSWin32' && $Config{make} =~ /dmake(?:\.exe)?$/i;
use constant NMAKE => $^O eq 'MSWin32' && $Config{make} =~ /nmake(?:\.exe)?$/i;
use constant MAKE => $Config{make} || 'make';

our @EXPORT = qw/make_meta_data unpack_tree build run_make DMAKE NMAKE/;

sub make_meta_data {
my $fh = shift;

local $ENV{PERL5LIB} = join $Config{path_sep},
map { File::Spec->rel2abs($_) } @INC;

my $cwd = getcwd;
my $tmpdir = tempdir CLEANUP => 1;
chdir $tmpdir or die $!;

unpack_tree($fh);

my $yaml = eval {
build($cwd);
YAML::Tiny->read('META.yml');
};
chdir $cwd or die $!;

die $@ if $@;

return $yaml->[0];
}

sub build {
my $distdir = shift;
die "Makefile.PL not found" unless -f 'Makefile.PL';
my $tb = Test::Builder->new;
$tb->note( run_cmd(qq{$^X Makefile.PL}) );
run_make();
}

sub run_cmd {
my ($cmd) = @_;
my $result = capture_merged {
system $cmd;
};
die "`$cmd` failed ($result)" if $?;
return $result;
}

sub run_make {
run_cmd(join ' ', MAKE, @_);
}

sub _parse_data {
my $fh = shift;
my ($data, $path);
while (<$fh>) {
if (/^\@\@/) {
($path) = $_ =~ /^\@\@ (.*)/;
next;
}
$data->{$path} .= $_;
}
close $fh;
return $data;
}

sub unpack_tree {
my $data = _parse_data(shift);

for my $path (keys %$data) {
my $dir = dirname($path);
unless (-e $dir) {
mkpath($dir) or die "Cannot mkpath '$dir': $!";
}

my $content = $data->{$path};
open my $out, '>', $path or die "Cannot open '$path' for writing: $!";
print $out $content;
close $out;
}
}

sub _regex {
my @target = @_;
my $regex = join '|', @target;
return qr/$regex/;
}

1;
__END__

0 comments on commit d63c6ee

Please sign in to comment.