File Coverage

blib/lib/Config/Model/Tester/Setup.pm
Criterion Covered Total %
statement 32 59 54.2
branch 0 10 0.0
condition n/a
subroutine 11 13 84.6
pod 2 2 100.0
total 45 84 53.5


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