File: Helper.pm

package info (click to toggle)
libmojolicious-plugin-assetpack-perl 2.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,888 kB
  • sloc: perl: 1,503; javascript: 52; makefile: 8; sh: 2
file content (63 lines) | stat: -rw-r--r-- 1,370 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package t::Helper;
use Mojo::Base -strict;

use Mojo::File 'path';
use Mojo::Util;
use Mojo::Loader;
use Mojolicious::Plugin::AssetPack::Util;
use Mojolicious;
use Test::Mojo;
use Test::More ();

$ENV{MOJO_LOG_LEVEL} = $ENV{HARNESS_IS_VERBOSE} ? 'debug' : 'error';
$ENV{TEST_HOME}      = path(__FILE__)->to_abs->dirname;

END { cleanup() }

my %CREATED_FILES;

unless ($ENV{TEST_KEEP_FILES}) {
  my $spew = \&Mojo::File::spew;
  Mojo::Util::monkey_patch(
    'Mojo::File' => spew => sub {
      $CREATED_FILES{$_[0]} = 1 unless -e $_[0];
      goto $spew;
    }
  );
}

sub cleanup {
  $CREATED_FILES{path($ENV{TEST_HOME}, 'assets', $ENV{MOJO_ASSETPACK_DB_FILE})} = 1
    if $ENV{MOJO_ASSETPACK_DB_FILE} and !$ENV{TEST_KEEP_FILES};
  unlink $_ for keys %CREATED_FILES;
}

sub t {
  my $class = shift;
  my $args  = ref $_[0] ? shift : {@_};
  my $app   = Mojolicious->new;

  $ENV{MOJO_ASSETPACK_DB_FILE} = sprintf '%s.db', path($0)->basename;
  $class->cleanup unless state $cleaned_up++;
  ${$app->home} = $ENV{TEST_HOME};
  delete $app->log->{$_} for qw(handle path);
  $app->routes->get('/' => 'index');
  $app->plugin(AssetPack => $args);
  return Test::Mojo->new($app);
}

sub import {
  my $class  = shift;
  my $caller = caller;

  Mojo::Base->import('-strict');

  eval <<"HERE" or die $@;
  package $caller;
  use Test::More;
  use Test::Mojo;
  1;
HERE
}

1;