File: filesystem2.t

package info (click to toggle)
libbadger-perl 0.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,400 kB
  • sloc: perl: 11,004; makefile: 9
file content (357 lines) | stat: -rw-r--r-- 9,403 bytes parent folder | download
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#============================================================= -*-perl-*-
#
# t/config/filesystem2.t
#
# Test the Badger::Config::Filesystem module some more.
#
# Copyright (C) 2008-2014 Andy Wardley.  All Rights Reserved.
#
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
#========================================================================

use Badger
    lib    => '../../lib',
    Utils  => 'Bin',
    Debug  => [import => ':all'];

use Badger::Test
    debug => 'Badger::Config Badger::Config::Filesystem',
    args  => \@ARGV;

eval "require Badger::Codec::YAML";
if ($@) {
    skip_all($@);
}
else {
    plan(41);
}

use Badger::Config::Filesystem;
my $pkg  = 'Badger::Config::Filesystem';
my $dir1 = Bin->dir('test_files/dir1');

my $config = $pkg->new(
    directory => $dir1,
    schemas   => {
        widgets => {
            tree_type => 'flat',
        },
        nibbles => {
            tree_type  => 'uri',
            uri_paths  => 'relative',
        },
        ents => {
            tree_type  => 'join',
            tree_joint => '_',
        },
        urls => {
            tree_type  => 'join',
            tree_joint => '.',
        },
    },
);
ok( $config, "Created $pkg object" );


#-----------------------------------------------------------------------------
# Simple configuration file
#-----------------------------------------------------------------------------

my $project = $config->get('project');
ok( $project, 'got project config' );
is( $project->{ name    }, 'Badger', 'got the project name' );
is( $project->{ version }, '123', 'got the project version' );
is( $config->get('project.author.name'), 'Andy Wardley', 'got the project.author.name' );

main->debug(
    "project: ",
    main->dump_data($project)
) if DEBUG;


#-----------------------------------------------------------------------------
# The 'nested' items have a sub-directory, but there's no main nested.yaml
#-----------------------------------------------------------------------------

my $nested = $config->get('nested');
ok( $nested, 'got nested config' );
main->debug(
    "nested: ",
    main->dump_data($nested)
) if DEBUG;

is( $nested->{ one }->{ one_a }, 'One A', 'got nested.one.one_a' );
is( $nested->{ two }->{ three }->{ three_a }, 'Three A', 'got nested.two.three.three_a' );

#-----------------------------------------------------------------------------
# widgets have both a widgets.yaml and a widgets sub-directory.  The schema
# defined for widgets above specifies the contents should be merged into a
# flat tree (tree_type => 'flat')
#-----------------------------------------------------------------------------

my $widgets = $config->get('widgets');
ok( $widgets, 'got widgets config' );
main->debug(
    "widgets: ",
    main->dump_data($widgets)
) if DEBUG;

is( $widgets->{ foo }->{ name }, 'The foo widget', 'got the foo widget' );
is( $widgets->{ bar }->{ name }, 'The bar widget', 'got the bar widget' );

# The _scheme_.tree_type in widgets.yaml says that bam and mam should also
# be in $widgets
is( $widgets->{ bam }->{ name }, 'Bam Extra Widget', 'got the bam widget' );
is( $widgets->{ mam }->{ name }, 'Mam Extra Widget', 'got the mam widget' );

is( $widgets->{ flip }->{ name }, 'Flip', 'got the flip widget name' );
is( $config->get('widgets.flop.name'), 'Flop', 'got the flop widget name' );

#-----------------------------------------------------------------------------
# A URI tree where files in directories under the top level directories get
# appropriate URIs, e.g. a 'baz' entry in 'foo/bar.yaml' becomes 'foo/bar/baz'
# in the master config hash.
#-----------------------------------------------------------------------------

my $nibbles = $config->get('nibbles');
main->debug(
    "nibbles: ",
    main->dump_data($nibbles)
) if DEBUG;

is(
    $nibbles->{ crackers },
    'to go with cheese',
    'got crackers from nibbles'
);
is(
    $config->get('nibbles.pickled_eggs'),
    'a bit of variety',
    'nibbles.picked_eggs'
);

# "tree_type: uri" in _schema_ forces items in cheeses.yaml to appear
# as cheese/xxx

is(
    $nibbles->{"cheese/cheddar"},
    'very tangy',
    'cheese/cheddar'
);

# As above, our selection of beers should be in drinks/beers

is(
    $nibbles->{"drinks/beers/ales"}->[0],
    'Tangle Foot',
    'a lovely beer'
);

# /cheese_knife is forced up a level to the root data hash and the
# "uri_paths: relative" option removes the leading slash

is(
    $nibbles->{ cheese_knife },
    'for cutting cheese',
    'cheese_knife'
);

# /bottle_opener is forced up two levels to the root data hash from drinks/beer
# As above, the "uri_paths: relative" option removes the leading slash

is(
    $nibbles->{ bottle_opener },
    'to open beer',
    'bottle_opener'
);

#-----------------------------------------------------------------------------
# Join binder.  Like the URI binder, this squashes nested data harvested
# from files, sub-directories and so on, into the main hash of configuration
# data, using underscores (by default) instead of slashes
#-----------------------------------------------------------------------------

my $ents = $config->get('ents');
main->debug(
    "entertainments: ",
    main->dump_data($ents)
) if DEBUG;

is(
    $ents->{ misc_I },
    'see',
    'I see',
);
is(
    $ents->{ misc_trees },
    'of green',
    'trees of green',
);
is(
    $ents->{ four_ten_volume },
    'This goes up to eleven',
    'the volume goes up to eleven',
);
is(
    $ents->{ four_twenty_music },
    'Pink Floyd',
    'four twenty music is Pink Floyd',
);

#-----------------------------------------------------------------------------
# Another join binder but this time with a custom joint of '.'
#-----------------------------------------------------------------------------

my $urls = $config->get('urls');
main->debug(
    "urls: ",
    main->dump_data($urls)
) if DEBUG;


#-----------------------------------------------------------------------------
# This also makes it useful for checking that we can defeat the default
# behaviour of get() to split dotted items.
#-----------------------------------------------------------------------------

is(
    $urls->{ home },
    '/index.html',
    'urls.home',
);

is(
    $urls->{'foo.about'},
    '/about_us.html',
    'urls.foo.about',
);

is(
    $config->get(['urls', 'foo.about']),
    '/about_us.html',
    "get(['urls', 'foo.about'])",
);

is(
    $config->get(['urls', 'foo.user', 'login']),
    '/auth/login',
    "get(['urls', 'foo.user', 'login'])",
);

is(
    $urls->{'foo.user'}->{'logout'},
    '/auth/logout',
    'urls.foo.user.logout',
);


#-----------------------------------------------------------------------------
# Directory config object with custom schema
#-----------------------------------------------------------------------------

my $dir2 = Bin->dir('test_files/dir2');

$config = $pkg->new(
    directory  => $dir2,
    file       => 'config',
    schemas    => {
        one    => {
            tree_type   => 'join',
            tree_joint  => '-',
        },
        three => {
            tree_type   => 'join',
            tree_joint  => '*',
        },
    }
);
ok( $config, "Created $pkg object" );

my $one = $config->get('one');
ok( $one, 'got tree one' );
is( join(', ', sort keys %$one), 'a, b, two-c, two-d', 'got joined keys' );


main->debug(
    "one: ",
    main->dump_data($one)
) if DEBUG;

my $three = $config->get('three');
ok( $three, 'got tree three' );
is(
    join(', ', sort keys %$three),
    'e, f, five*six*i, five*six*j, four*g, four*h, iv*i-j, iv*i-k',
    'got joined keys'
);

main->debug(
    "three: ",
    main->dump_data($three)
) if DEBUG;

#-----------------------------------------------------------------------------
# Same again but with default schema specified as 'schema' option
#-----------------------------------------------------------------------------

$config = $pkg->new(
    directory  => $dir2,
    schemas    => {
        one    => {
            tree_type  => 'join',
            tree_joint => '+',
        }
    }
);
$one = $config->get('one');
ok( $one, 'got tree one again' );
is( join(', ', sort keys %$one), 'a, b, two+c, two+d', 'got joined keys' );

#-----------------------------------------------------------------------------
# Another similar once with dedicated schema rule for item ten
#-----------------------------------------------------------------------------

$config = $pkg->new(
    directory   => $dir2,
    schemas     => {
        ten     => {
            tree_type => 'uri',
            uri_paths => 'absolute',
        }
    }
);
my $ten = $config->get('ten');
ok( $ten, 'got ten tree' );
main->debug(
    "ten: ",
    main->dump_data($ten)
) if DEBUG;
is(
    join(', ', sort keys %$ten),
    '/eleven/m, /eleven/n, /k, /l',
    'got ten keys'
);

#-----------------------------------------------------------------------------
# Same thing but using a config_file.
#-----------------------------------------------------------------------------

$config = $pkg->new(
    dir  => $dir2,
    file => 'config',
);
my $ten2 = $config->get('ten');
ok( $ten2, 'got ten tree' );
main->debug(
    "ten2: ",
    main->dump_data($ten2)
) if DEBUG;
is(
    join(', ', sort keys %$ten2),
    'eleven/m, eleven/n, k, l',
    'got ten keys'
);

my $ten3 = $config->get('ten');