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
|
use strict;
use warnings;
use Test::More tests => 22;
use lib '../lib';
use Test::Deep;
use Config::JSON;
use File::Temp qw/ tempfile /;
use JSON;
my ($mainHandle, $mainConfigFile) = tempfile();
my ($firstIncludeHandle, $firstIncludeFile) = tempfile();
my ($secondIncludeHandle, $secondIncludeFile) = tempfile();
close($mainHandle);
close($firstIncludeHandle);
close($secondIncludeHandle);
# set up main config file with include section
if (open(my $file, ">", $mainConfigFile)) {
my $testData = <<END;
{
"dsn" : "DBI:mysql:test",
"user" : "tester",
"password" : "xxxxxx",
"colors" : [ "red", "green", "blue" ],
"stats" : {
"health" : 32,
"vitality" : 11
},
"this" : {
"that" : {
"scalar" : "foo",
"array" : ["foo", "bar"],
"hash" : {
"foo" : 1,
"bar" : 2
}
}
},
"includes" : [ "$firstIncludeFile", "$secondIncludeFile"]
}
END
print $file $testData;
close($file);
ok(1, "set up test data");
}
else {
ok(0, "set up test data");
}
# set up the first include file
if( open my $file, '>', $firstIncludeFile ) {
my $testData = <<END;
# config-file-type: JSON 1
{
"dsn" : "DBI:mysql:test",
"user" : "tester",
"password" : "xxxxxx",
"colors" : [ "red", "green", "blue" ],
"stats" : {
"health" : 32,
"vitality" : 11
},
"this" : {
"that" : {
"scalar" : "foo",
"array" : ["foo", "bar"],
"hash" : {
"foo" : 1,
"bar" : 2
}
}
}
}
END
print $file $testData;
close $file;
ok(1, "set up first include test data");
}
else {
ok(0, "set up first include file");
}
if( open my $file, '>', $secondIncludeFile ) {
my $testData = <<END;
# config-file-type: JSON 1
{
"dsn" : "DBI:mysql:test",
"user" : "tester",
"password" : "xxxxxx",
"colors" : [ "red", "green", "blue" ],
"stats" : {
"health" : 32,
"vitality" : 11
},
"this" : {
"that" : {
"scalar" : "foo",
"array" : ["foo", "bar"],
"hash" : {
"foo" : 1,
"bar" : 2
}
}
}
}
END
print $file $testData;
close $file;
ok(1, "set up second include test data");
}
else {
ok(0, "set up second include file");
}
# delete
my $mainConfig = Config::JSON->new($mainConfigFile);
$mainConfig->delete("dsn");
ok(!(defined getKey($mainConfigFile, "dsn")), "delete() writes changes for scalar in main file");
ok(!(defined getKey($firstIncludeFile, "dsn")), "delete() writes changes for scalar in first include file");
ok(!(defined getKey($secondIncludeFile, "dsn")), "delete() writes changes for scalar in second include file");
ok(!(defined $mainConfig->get("dsn")), "delete() works for scalar in main file");
$mainConfig->delete("stats/vitality");
ok(!(defined getKey($mainConfigFile, "stats/vitality")), "delete() multilevel works for main file");
ok(!(defined getKey($firstIncludeFile, "stats/vitality")), "delete() multilevel works for first include file");
ok(!(defined getKey($secondIncludeFile, "stats/vitality")), "delete() multilevel works for second include file");
ok(!(defined $mainConfig->get("stats/vitality")), "delete() multilevel works for main file");
ok(defined getKey($mainConfigFile, "stats"), "delete() multilevel - doesn't delete parent in main file");
ok(defined getKey($firstIncludeFile, "stats"), "delete() multilevel - doesn't delete parent in first include file");
ok(defined getKey($secondIncludeFile, "stats"), "delete() multilevel - doesn't delete parent in second include file");
ok(defined $mainConfig->get("stats"), "delete() multilevel - doesn't delete parent in main file");
$mainConfig->delete('this/that/hash');
ok(!(defined getKey($firstIncludeFile, "this/that/hash")), "delete() works for multilevel in first include file");
ok(!(defined getKey($secondIncludeFile, "this/that/hash")), "delete() works for multilevel in second include file");
ok(!(defined getKey($mainConfigFile, "this/that/hash")), "delete() works for multilevel in main file");
ok(!(defined $mainConfig->get("this/that/hash")), "delete() works for multilevel in main file");
ok(defined getKey($firstIncludeFile, 'this/that'), "delete() on multilevel doesn't delete parent in first include file");
ok(defined getKey($secondIncludeFile, 'this/that'), "delete() on multilevel doesn't delete parent in second include file");
ok(defined getKey($mainConfigFile, 'this/that'), "delete() on multilevel doesn't delete parent in main file");
#----------------------------------------------------
# get a value from a config file that has includes. Can't use Config::JSON
# because the main file includes the other files, which means the value we're
# looking for will be found regardless. that's what we're testing, so we have
# to use raw JSON.
sub getKey {
my $configFile = shift;
my $key = shift;
my @parts = split '/', $key;
my $lastPart = pop @parts;
open my $fh, '<', $configFile or die "open($configFile): $!";
my $raw = do {
local $/;
<$fh>;
};
close $fh;
my $data = JSON->new->relaxed(1)->decode($raw);
my $directive = $data;
for my $part (@parts) {
$directive = $directive->{$part};
}
return $directive->{$lastPart};
}
|