line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of Config-Model-Tester |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# This software is Copyright (c) 2013-2020 by Dominique Dumont. |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# This is free software, licensed under: |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# The GNU Lesser General Public License, Version 2.1, February 1999 |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
package Config::Model::Tester::Setup 4.007; |
11
|
|
|
|
|
|
|
# ABSTRACT: Common test setup functions for Config::Model |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
38
|
|
14
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
15
|
1
|
|
|
1
|
|
7
|
use locale; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
16
|
1
|
|
|
1
|
|
37
|
use utf8; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
17
|
1
|
|
|
1
|
|
38
|
use 5.10.1; |
|
1
|
|
|
|
|
4
|
|
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
1
|
|
7
|
use Test::More; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
11
|
|
20
|
1
|
|
|
1
|
|
349
|
use Log::Log4perl 1.11 qw(:easy :levels); |
|
1
|
|
|
|
|
18
|
|
|
1
|
|
|
|
|
8
|
|
21
|
1
|
|
|
1
|
|
984
|
use Path::Tiny; |
|
1
|
|
|
|
|
24
|
|
|
1
|
|
|
|
|
54
|
|
22
|
1
|
|
|
1
|
|
899
|
use Getopt::Long; |
|
1
|
|
|
|
|
10789
|
|
|
1
|
|
|
|
|
5
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# use eval so this module does not have a "hard" dependency on Config::Model |
25
|
|
|
|
|
|
|
# This way, Config::Model can build-depend on Config::Model::Tester without |
26
|
|
|
|
|
|
|
# creating a build dependency loop. |
27
|
|
|
|
|
|
|
eval { |
28
|
|
|
|
|
|
|
require Config::Model; |
29
|
|
|
|
|
|
|
require Config::Model::Exception; |
30
|
|
|
|
|
|
|
} ; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
require Exporter; |
33
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
34
|
|
|
|
|
|
|
our @EXPORT = qw(init_test setup_test_dir); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub init_test { |
37
|
0
|
|
|
0
|
1
|
|
my @option_specs = qw/trace error log/; |
38
|
0
|
|
|
|
|
|
push @option_specs, @_; |
39
|
|
|
|
|
|
|
|
40
|
0
|
0
|
|
|
|
|
GetOptions( \my %opts, @option_specs) |
41
|
|
|
|
|
|
|
|| die "Unknown option. Expected options are '--".join("', '--",@option_specs)."'\n"; |
42
|
|
|
|
|
|
|
|
43
|
0
|
0
|
|
|
|
|
if ($opts{error}) { |
44
|
0
|
|
|
|
|
|
Config::Model::Exception::Any->Trace(1); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
my $model = Config::Model->new( ); |
48
|
|
|
|
|
|
|
|
49
|
0
|
0
|
|
|
|
|
if ($opts{log}) { |
50
|
0
|
|
|
|
|
|
note("enabling logs and disabling test logs"); |
51
|
0
|
|
|
|
|
|
$model->initialize_log4perl; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
else { |
54
|
0
|
|
|
|
|
|
Log::Log4perl->easy_init( $ERROR ); |
55
|
0
|
|
|
|
|
|
require Test::Log::Log4perl; |
56
|
0
|
|
|
|
|
|
Test::Log::Log4perl->import; |
57
|
0
|
|
|
|
|
|
Test::Log::Log4perl->ignore_priority("info"); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
ok( $model, "compiled" ); |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
return ($model, $opts{trace}, \%opts); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub setup_test_dir { |
66
|
0
|
|
|
0
|
1
|
|
my %args = @_; |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
my $script = path($0); |
69
|
0
|
|
|
|
|
|
my $name = path($0)->basename('.t'); |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
my $wr_root = path('wr_root')->child($name); |
72
|
0
|
|
|
|
|
|
note("Running tests in $wr_root"); |
73
|
0
|
|
|
|
|
|
$wr_root->remove_tree; |
74
|
0
|
|
|
|
|
|
$wr_root->mkpath; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# TODO: remove stringify once Config::Model::Instance can handle Path::Tiny |
77
|
0
|
0
|
|
|
|
|
return $args{stringify} ? $wr_root->stringify.'/' : $wr_root; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
1; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
__END__ |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=pod |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=encoding UTF-8 |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 NAME |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Config::Model::Tester::Setup - Common test setup functions for Config::Model |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 VERSION |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
version 4.007 |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 SYNOPSIS |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# in t/some_test.t |
99
|
|
|
|
|
|
|
use warnings; |
100
|
|
|
|
|
|
|
use strict; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
use Config::Model::Tester::Setup qw/init_test setup_test_dir/; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
my ($model, $trace) = init_test(); |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# pseudo root where config files are written by config-model as setup |
107
|
|
|
|
|
|
|
# by init_test |
108
|
|
|
|
|
|
|
my $wr_root = setup_test_dir(); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 DESCRIPTION |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
This module provide 2 functions to setup a test environment that can |
113
|
|
|
|
|
|
|
be used in most test involving L<Config::Model>. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 FUNCTIONS |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head2 init_test |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Scan test command line options and initialise a L<Config::Model> object. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Returns a list containing a L<Config::Model> object, a boolean and a |
122
|
|
|
|
|
|
|
hash. The boolean is true if option C<--trace> was used on the command |
123
|
|
|
|
|
|
|
line. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Default command options are: |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=over |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=item * |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
C<--error>: When set, error handled by L<Config::Model::Exception> shows a |
132
|
|
|
|
|
|
|
strack trace when dying. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item * |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
C<--log>: When set, L<Log::Log4perl> uses the config from file |
137
|
|
|
|
|
|
|
C<~/.log4config-model> or the default config provided by |
138
|
|
|
|
|
|
|
L<Config::Model>. By default, only Error level and above are shown. |
139
|
|
|
|
|
|
|
Note that log tests are disabled when this option is set, so you may see a lot of |
140
|
|
|
|
|
|
|
harmless Warning messages during tests (which depend on the tests to be run). |
141
|
|
|
|
|
|
|
Experimental. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=back |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
More options can be passed to C<init_test> using option definitions |
146
|
|
|
|
|
|
|
like the one defined in L<Getopt::Long> . The value of the command |
147
|
|
|
|
|
|
|
line options are returned in the 3rd returned value. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
For instance, for a test named C<t/my_test.t> calling : |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
init_test('foo', 'bar=s') |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
The test file can be run with: |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
perl t/my_test.t --foo --bar=baz --log --trace |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
C<init_test> returns: |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
($model, 1, { foo => 1, bar => 'baz', log => 1 , trace => 1, error => 0 }) |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 setup_test_dir |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Cleanup and create a test directory in |
164
|
|
|
|
|
|
|
C<wr_root/test-script-name>. For instance this function creates |
165
|
|
|
|
|
|
|
directory C<wr_root/foo> for test C<t/foo.t> |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Returns a L<Path::Tiny> object of the test directory or a string if |
168
|
|
|
|
|
|
|
C<setup_test_dir> is called with C<< stringify => 1 >>. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 SEE ALSO |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=over 4 |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=item * |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
L<Config::Model> |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=item * |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
L<Test::More> |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=back |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 AUTHOR |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Dominique Dumont |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
This software is Copyright (c) 2013-2020 by Dominique Dumont. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
This is free software, licensed under: |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
The GNU Lesser General Public License, Version 2.1, February 1999 |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=cut |