File Coverage

blib/lib/Test/Clustericious/Config.pm
Criterion Covered Total %
statement 89 94 94.6
branch 8 12 66.6
condition 4 12 33.3
subroutine 18 18 100.0
pod 4 4 100.0
total 123 140 87.8


line stmt bran cond sub pod time code
1             package Test::Clustericious::Config;
2              
3 10     10   30249 use strict;
  10         43  
  10         402  
4 10     10   238 use warnings;
  10         18  
  10         275  
5 10     10   117 use v5.10;
  10         32  
  10         897  
6              
7             BEGIN {
8 10 50   10   58 unless($INC{'File/HomeDir/Test.pm'})
9             {
10 10     10   750 eval q{ use File::HomeDir::Test };
  10         8472  
  10         289400  
  10         71  
11 10 50       8176 die $@ if $@;
12             }
13             }
14              
15 10     10   8095 use File::HomeDir;
  10         34690  
  10         746  
16 10     10   7406 use YAML::XS qw( DumpFile );
  10         28716  
  10         578  
17 10     10   74 use File::Path qw( mkpath );
  10         19  
  10         426  
18 10     10   5523 use Clustericious::Config;
  10         33  
  10         350  
19 10     10   8704 use Mojo::Loader;
  10         13497  
  10         98  
20              
21 10     10   334 use base qw( Test::Builder::Module Exporter );
  10         22  
  10         8159  
22              
23             our @EXPORT = qw( create_config_ok create_directory_ok home_directory_ok create_config_helper_ok );
24             our @EXPORT_OK = @EXPORT;
25             our %EXPORT_TAGS = ( all => \@EXPORT );
26              
27             my $config_dir;
28              
29             sub _init
30             {
31 10     10   100 $config_dir = File::HomeDir->my_home . "/etc";
32 10         2599 mkdir $config_dir;
33              
34 10         81 $ENV{CLUSTERICIOUS_CONF_DIR} = $config_dir;
35 10         94 Clustericious::Config->_testing(1);
36             }
37              
38 10     10   107194 BEGIN { _init() }
39              
40             # ABSTRACT: Test Clustericious::Config
41             our $VERSION = '0.29'; # VERSION
42              
43              
44             sub create_config_ok ($;$$)
45             {
46 10     10 1 2895 my($config_name, $config, $test_name) = @_;
47              
48 10         35 my $fn = "$config_name.conf";
49 10         47 $fn =~ s/::/-/g;
50            
51 10 100       42 unless(defined $config)
52             {
53 1         20 my $loader = Mojo::Loader->new;
54 1         9 my $caller = caller;
55 1         6 $loader->load($caller);
56 1         530 $config = $loader->data($caller, "etc/$fn");
57             }
58            
59 10         218 my $tb = __PACKAGE__->builder;
60 10         167 my $ok = 1;
61 10 50       42 unless(defined $config)
62             {
63 0         0 $config = "---\n";
64 0         0 $tb->diag("unable to locate text for $config_name");
65 0         0 $ok = 0;
66             }
67            
68 10         45 my $config_filename = "$config_dir/$fn";
69            
70 10         24 eval {
71 10 100       40 if(ref $config)
72             {
73 3         18 DumpFile($config_filename, $config);
74             }
75             else
76             {
77 7         874 open my $fh, '>', $config_filename;
78 7         126 print $fh $config;
79 7         413 close $fh;
80             }
81             };
82 10 50       932 if(my $error = $@)
83             {
84 0         0 $ok = 0;
85 0         0 $tb->diag("exception: $error");
86             }
87            
88 10   33     88 $test_name //= "create config for $config_name at $config_filename";
89            
90             # remove any cached copy if necessary
91 10         108 Clustericious::Config->_uncache($config_name);
92              
93 10         70 $tb->ok($ok, $test_name);
94 10         5384 return $config_filename;
95             }
96              
97              
98             sub create_directory_ok ($;$)
99             {
100 1     1 1 3 my($path, $test_name) = @_;
101            
102 1         3 my $fullpath = $path;
103 1         5 $fullpath =~ s{^/}{};
104 1         11 $fullpath = join('/', File::HomeDir->my_home, $fullpath);
105 1         589 mkpath $fullpath, 0, 0700;
106            
107 1   33     9 $test_name //= "create directory $fullpath";
108            
109 1         8 my $tb = __PACKAGE__->builder;
110 1         30 $tb->ok(-d $fullpath, $test_name);
111 1         256 return $fullpath;
112             }
113              
114              
115             sub home_directory_ok (;$)
116             {
117 2     2 1 482 my($test_name) = @_;
118            
119 2         17 my $fullpath = File::HomeDir->my_home;
120            
121 2   33     81 $test_name //= "home directory $fullpath";
122            
123 2         15 my $tb = __PACKAGE__->builder;
124 2         51 $tb->ok(-d $fullpath, $test_name);
125 2         520 return $fullpath;
126             }
127              
128              
129             sub create_config_helper_ok ($$;$)
130             {
131 1     1 1 14 my($helper_name, $helper_code, $test_name) = @_;
132            
133 1   33     8 $test_name //= "create config helper $helper_name";
134            
135 1         10 require Clustericious::Config::Helpers;
136 1         2 do {
137 10     10   81 no strict 'refs';
  10         18  
  10         1070  
138 1         2 *{"Clustericious::Config::Helpers::$helper_name"} = $helper_code;
  1         11  
139             };
140 1         4 push @Clustericious::Config::Helpers::EXPORT, $helper_name;
141            
142 1         17 my $tb = __PACKAGE__->builder;
143 1         19 $tb->ok(1, $test_name);
144 1         738 return;
145             }
146              
147             1;
148              
149              
150             __END__