Debian Codegraph @

t/issues/gh-634.t

raw
1 use strict;
2 use warnings;
3 use Test::More tests=> 3;
4 use File::Temp qw/tempdir/;
5 use Path::Tiny qw< path >;
6
7 my $log_dir = tempdir( CLEANUP => 1 );
8 sub config_location {
9 my ($app) = @_;
10 my %config_readers = map { $_->{name} => $_ } @{ $app->config_reader->config_readers };
11 return $config_readers{ 'Config::Any' }->config_location;
12 }
13
14 {
15 package LogDirSpecified;
16 use Dancer2;
17
18 set engines => {
19 logger => {
20 File => {
21 log_dir => $log_dir,
22 file_name => 'test_log.log',
23 }
24 }
25 };
26 set logger => 'file';
27 }
28
29 {
30 package NonExistLogDirSpecified;
31 use Dancer2;
32
33 set engines => {
34 logger => {
35 File => {
36 log_dir => "$log_dir/notexist",
37 file_name => 'test_log.log',
38 }
39 }
40 };
41 set logger => 'file';
42 }
43
44 {
45 package LogDirNotSpecified;
46 use Dancer2;
47
48 set logger => 'file';
49 }
50
51 my $check_cb = sub {
52 my ( $app, $dir, $file ) = @_;
53 my $logger = $app->logger_engine;
54
55 isa_ok( $logger, 'Dancer2::Logger::File' );
56 is(
57 $logger->environment,
58 $app->environment,
59 'Logger got correct environment',
60 );
61
62 is(
63 $logger->location,
64 config_location( $app ),
65 'Logger got correct location',
66 );
67
68 is(
69 $logger->log_dir,
70 $dir,
71 'Logger got correct log directory',
72 );
73
74 is(
75 $logger->file_name,
76 $file,
77 'Logger got correct filename',
78 );
79
80 is(
81 $logger->log_file,
82 path( $dir, $file )->stringify,
83 'Logger got correct log file',
84 );
85 };
86
87 subtest 'test Logger::File with log_dir specified' => sub {
88 plan tests => 6;
89 my $app = [
90 grep { $_->name eq 'LogDirSpecified' } @{ Dancer2->runner->apps }
91 ]->[0];
92
93 $check_cb->( $app, $log_dir, 'test_log.log' );
94 };
95
96 subtest 'test Logger::File with log_dir NOT specified' => sub {
97 plan tests => 6;
98 my $app = [
99 grep { $_->name eq 'LogDirNotSpecified' } @{ Dancer2->runner->apps }
100 ]->[0];
101
102 $check_cb->(
103 $app,
104 path( config_location( $app ), 'logs' )->stringify,
105 $app->environment . '.log',
106 );
107 };
108
109 subtest 'test Logger::File with non-existent log_dir specified' => sub {
110 plan tests => 6;
111
112 my $app = [
113 grep { $_->name eq 'NonExistLogDirSpecified'} @{ Dancer2->runner->apps }
114 ]->[0];
115
116 my $logger = $app->logger_engine;
117
118 $check_cb->(
119 $app,
120 "$log_dir/notexist",
121 'test_log.log',
122 );
123 };