App-MtAws
view release on metacpan or search on metacpan
t/lib/TestUtils.pm view on Meta::CPAN
# mt-aws-glacier - Amazon Glacier sync client
# Copyright (C) 2012-2014 Victor Efimov
# http://mt-aws.com (also http://vs-dev.com) vs@vs-dev.com
# License: GPLv3
#
# This file is part of "mt-aws-glacier"
#
# mt-aws-glacier is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# mt-aws-glacier is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
package TestUtils;
use FindBin;
use lib "$FindBin::RealBin/../lib";
use strict;
use warnings;
use App::MtAws::ConfigDefinition;
use App::MtAws::ConfigEngine;
use Test::More;
use Exporter;
use Encode;
use Carp;
use IO::Pipe;
use B();
use File::Temp qw/tempdir/;
our %disable_validations;
our @EXPORT = qw/fake_config config_create_and_parse disable_validations no_disable_validations warning_fatal
capture_stdout capture_stderr assert_raises_exception ordered_test test_fast_ok fast_ok with_fork
can_work_with_non_utf8_files get_temp_dir is_iv_without_pv is_posix_root JSON_XS_TRUE JSON_XS_FALSE plan_tests/;
our %SPECIAL_EXPORT = map { $_ => undef } qw/w_fatal/;
sub import
{
@_ = grep { ! (exists $SPECIAL_EXPORT{$_} && ($SPECIAL_EXPORT{$_} = 1)) } @_;
warning_fatal() if ($SPECIAL_EXPORT{w_fatal}) ; # compile time warnings fatal
goto &Exporter::import;
}
use Test::Deep; # should be last line, after EXPORT stuff, otherwise versions ^(0\.089|0\.09[0-9].*) do something nastly with exports
use constant ALARM_FOR_FORK_TESTS => 30;
# different versions of JSON::XS documents different constants to use
# what is allowed in both cases - is the use of \0 and \1
use constant JSON_XS_TRUE => \1;
use constant JSON_XS_FALSE => \0;
# run time or compile time warnings fatal
sub warning_fatal
{
my ($skip_re) = @_;
$SIG{__WARN__} = sub {
if (!defined($skip_re) || $_[0] !~ $skip_re) {
confess "Termination after a warning: $_[0]"
}
};
}
sub get_temp_dir
{
$SIG{INT} = sub { exit(1); }; # Global signal, for cleaning temporary files
tempdir("__AppMtAws_t_${$}_XXXXXXXX", TMPDIR => 1, CLEANUP => 1); # pid needed cause child processes re-use random number generators
}
sub fake_config(@)
{
my ($cb, %data) = (pop @_, @_);
no warnings 'redefine';
local *App::MtAws::ConfigEngine::read_config = sub { %data ? { %data } : { (key=>'mykey', secret => 'mysecret', region => 'myregion') } };
disable_validations($cb);
}
sub no_disable_validations
{
local %disable_validations = ();
shift->();
}
sub disable_validations
{
my ($cb, @data) = (pop @_, @_);
local %disable_validations = @data ?
(
'override_validations' => {
map { $_ => undef } @data
},
) :
(
'override_validations' => {
journal => undef,
secret => undef,
key => undef,
dir => undef,
},
);
$cb->();
}
sub config_create_and_parse(@)
{
# use Data::Dumper;
# die Dumper {%disable_validations};
my $c = App::MtAws::ConfigDefinition::get_config(%disable_validations);
( run in 1.630 second using v1.01-cache-2.11-cpan-5837b0d9d2c )