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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
# -*- cperl -*-
# NOTE: backend can also be tested in model_test.d
use ExtUtils::testlib;
use Test::More;
use Test::Memory::Cycle;
use Config::Model;
use File::Path;
use File::Copy;
use Data::Dumper;
use Log::Log4perl qw(:easy);
use Test::Differences;
use Test::File::Contents;
use warnings;
no warnings qw(once);
use strict;
my $arg = shift || '';
my $trace = $arg =~ /t/ ? 1 : 0;
my $log = $arg =~ /l/ ? 1 : 0;
my $home = $ENV{HOME} || "";
my $log4perl_user_conf_file = "$home/.log4config-model";
if ( $log and -e $log4perl_user_conf_file ) {
Log::Log4perl::init($log4perl_user_conf_file);
}
else {
Log::Log4perl->easy_init( $log ? $WARN : $ERROR );
}
Config::Model::Exception::Any->Trace(1) if $arg =~ /e/;
ok( 1, "compiled" );
# pseudo root where config files are written by config-model
my $wr_root = 'wr_root_p/backend-ini-section-map';
my $head = << 'EOH';
## This file was written by cme command.
## You can run 'cme edit <application>' to modify this file.
## Run 'cme list' to get the list of applications available on your system
## You may also modify the content of this file with your favorite editor.
EOH
my @below_data = split /\n/, << 'EOD2' ;
[Low]
foo = bar
[Section1]
source = 1
[Section2]
source = 2
packages = g++-4.2-arm-linux-gnu linux-libc-dev-arm-cross
[Empty]
EOD2
my $w_file_below = join( "\n", $head, '', map { lc } @below_data[ 3 .. 9, 0 .. 2 ] );
# set_up data
my @general_data = split /\n/, << 'EOD1' ;
[General]
foo = bar
[Section1]
source = 1
[Section2]
source = 2
packages = g++-4.2-arm-linux-gnu linux-libc-dev-arm-cross
[Empty]
EOD1
my $w_file_general = join( "\n", $head, map { lc } @general_data[ 0 .. 9 ] );
# change delimiter comments
my %test_setup = (
SectionMapTop => [ \@general_data, 'general', $w_file_general ],
SectionMap => [ \@below_data, 'below', $w_file_below ],
);
my $model = Config::Model->new();
$model->create_config_class(
'name' => 'Section',
'element' => [
'source',
{
'value_type' => 'uniline',
'type' => 'leaf'
},
'packages',
{
'cargo' => {
'value_type' => 'uniline',
'type' => 'leaf'
},
'type' => 'list'
},
],
);
$model->create_config_class(
'name' => 'Below',
'element' => [
foo => { qw/type leaf value_type uniline/, },
],
);
$model->create_config_class(
name => 'SectionMapTop',
'rw_config' => {
'section_map' => { 'general' => '!' },
'backend' => 'ini_file',
'split_list_value' => '\\s+',
'join_list_value' => ' ',
'store_class_in_hash' => 'sections',
force_lc_section => 1,
},
element => [
'sections',
{
'cargo' => {
'type' => 'node',
'config_class_name' => 'Section'
},
'type' => 'hash',
'index_type' => 'string'
},
foo => { qw/type leaf value_type uniline/, },
] );
$model->create_config_class(
name => 'SectionMap',
'rw_config' => {
'section_map' => { 'low' => 'below' },
'backend' => 'ini_file',
'split_list_value' => '\\s+',
'join_list_value' => ' ',
'store_class_in_hash' => 'sections',
force_lc_section => 1,
},
element => [
'sections',
{
'cargo' => {
'type' => 'node',
'config_class_name' => 'Section'
},
'type' => 'hash',
'index_type' => 'string'
},
below => { qw/type node config_class_name Below/, },
foo => { qw/type leaf value_type uniline/, },
] );
# cleanup before tests
rmtree($wr_root);
foreach my $test_class ( sort keys %test_setup ) {
my @orig = @{ $test_setup{$test_class}[0] };
my $test_path = $test_setup{$test_class}[1];
my $written_file = $test_setup{$test_class}[2];
ok( 1, "Starting $test_class tests in $test_path dir" );
my $test1 = 'ini1';
my $wr_dir = "$wr_root/$test_path/$test1";
my $conf_file = "/etc/test.ini";
my $abs_conf_file = "$wr_dir$conf_file";
mkpath( $wr_dir . '/etc', { mode => oct(755) } )
|| die "can't mkpath: $!";
open my $conf, '>', $abs_conf_file || die "can't open $abs_conf_file: $!";
print $conf map { "$_\n" } @orig;
close $conf;
my $i_test = $model->instance(
instance_name => $test_path,
root_class_name => $test_class,
root_dir => $wr_dir,
config_file => $conf_file,
);
ok( $i_test, "Created $test_class instance" );
my $i_root = $i_test->config_root;
my $orig = $i_root->dump_tree;
print $orig if $trace;
is( $i_root->needs_save, 0, "check data does not need to be saved" );
is( $i_root->grab_value("sections:section2 packages:0"),
"g++-4.2-arm-linux-gnu", "check auto-split 1/2" );
is( $i_root->grab_value("sections:section2 packages:1"),
"linux-libc-dev-arm-cross", "check auto-split 2/2" );
# force write back
$i_root->needs_save(1);
$i_test->write_back( config_file => $conf_file );
ok( 1, "IniFile write back done" );
my $ini_file = $wr_dir . '/etc/test.ini';
ok( -e $ini_file, "check that config file $ini_file was written" );
file_contents_eq_or_diff $ini_file, $written_file, "check file $ini_file content";
# create another instance to read the IniFile that was just written
my $wr_dir2 = "$wr_root/$test_path/ini2";
mkpath( $wr_dir2 . '/etc', { mode => oct(755) } ) || die "can't mkpath: $!";
copy( $wr_dir . '/etc/test.ini', $wr_dir2 . '/etc/' )
or die "can't copy from test1 to test2: $!";
my $i2_test = $model->instance(
instance_name => $test_path . '2',
root_class_name => $test_class,
root_dir => $wr_dir2,
config_file => $conf_file,
);
ok( $i2_test, "Created instance" );
my $i2_root = $i2_test->config_root;
my $p2_dump = $i2_root->dump_tree;
$i_root->load('sections~empty');
my $orig_fixed = $i_root->dump_tree;
eq_or_diff(
[ split /\n/, $p2_dump ],
[ split /\n/, $orig_fixed ],
"compare original data with 2nd instance data"
);
}
memory_cycle_ok($model);
done_testing;
|