Skip to content

Instantly share code, notes, and snippets.

@xaicron
Created November 7, 2010 06:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xaicron/665984 to your computer and use it in GitHub Desktop.
Save xaicron/665984 to your computer and use it in GitHub Desktop.
use inc::Module::Install;
use Module::Install::TestTarget;
name 'MyApp';
all_from 'lib/MyApp.pm';
requires 'DBI';
requires 'JSON';
test_requires 'Test::More';
test_requires 'Test::mysqld';
tests 't/*.t t/*/*.t t/*/*/*.t';
author_tests 'xt';
default_test_target(
includes => ['t/lib'],
run_on_prepare => ['t/script/setup_mysqld.pl'],
);
auto_include();
WriteAll;
package Test::MyApp::mysqld;
use strict;
use warnings;
use Test::mysqld;
use JSON;
use DBI;
our $SKIP_DROP_DB_MAP = {
information_schema => 1,
mysql => 1,
test => 1,
};
sub setup {
my ($class, %config) = @_;
my $mysqld;
if (my $json = $ENV{TEST_MYSQLD}) {
my $obj = decode_json $json;
$mysqld = bless $obj, 'Test::mysqld';
}
else {
$mysqld = Test::mysqld->new(my_cnf => {
'skip-networking' => '',
%config,
}) or die $Test::mysqld::errstr;
}
return $mysqld;
}
sub cleanup {
my ($class, $mysqld) = @_;
my $dbh = DBI->connect($mysqld->dsn, '', '', {
AutoCommit => 1,
RaiseError => 1,
});
my $rs = $dbh->selectall_hashref('SHOW DATABASES', 'Database');
for my $dbname (keys %$rs) {
next if $SKIP_DROP_DB_MAP->{$dbname};
$dbh->do("DROP DATABASE $dbname");
}
}
1;
use Test::MyApp::mysqld;
use JSON;
$SIG{INT} = sub { CORE::exit 1 };
$mysqld = Test::MyApp::mysqld->setup;
$ENV{TEST_MYSQLD} = encode_json +{ %$mysqld };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment