Update File tests (utf8) (#827)

This commit is contained in:
Xavier Guimard 2016-01-09 19:22:31 +00:00
parent fcc333e4e5
commit f1d3e947c7
2 changed files with 43 additions and 4 deletions

View File

@ -82,12 +82,13 @@ sub store {
my ( $self, $fields ) = @_;
my $mask = umask;
umask( oct('0027') );
unless ( open FILE, ">:encoding(UTF-8)", $self->_file( $fields->{cfgNum} ) )
unless ( open FILE, '>', $self->_file( $fields->{cfgNum} ) )
{
$Lemonldap::NG::Common::Conf::msg .= "Open file failed: $! \n";
$Lemonldap::NG::Common::Conf::msg = "Open file failed: $! \n";
$self->unlock;
return UNKNOWN_ERROR;
}
binmode(FILE);
print FILE JSON->new->canonical(1)->encode($fields);
close FILE;
umask($mask);
@ -109,6 +110,7 @@ sub load {
$f = encode( 'UTF-8', $f );
eval { $ret = decode_json($f) };
if ($@) {
print STDERR "$@\n";
$Lemonldap::NG::Common::Conf::msg .=
"JSON fails to read file: $@ \n";
return undef;

View File

@ -5,7 +5,8 @@
# change 'tests => 1' to 'tests => last_test_to_print';
use Test::More tests => 2;
use strict;
use Test::More;
BEGIN { use_ok('Lemonldap::NG::Common::Conf') }
#########################
@ -19,9 +20,45 @@ ok(
$h = new Lemonldap::NG::Common::Conf(
{
type => 'File',
dirName => ".",
dirName => "t/",
}
),
'type => file',
);
my $count = 2;
my @test = (
# simple ascii
{ cfgNum => 1, test => 'ascii' },
# utf-8
{ cfgNum => 1, test => 'Русский' },
# compatible utf8/latin-1 char but with different codes
{ cfgNum => 1, test => 'éà' }
);
for ( my $i = 0 ; $i < @test ; $i++ ) {
ok( $h->store( $test[$i] ) == 1, "Test $i is stored" )
or print STDERR "$Lemonldap::NG::Common::Conf::msg $!";
$count++;
if ( -x '/usr/bin/file' ) {
eval {
open F, 'file t/lmConf-1.js |';
$_ = join( '', <F> );
close F;
ok( /(ascii|utf-?8)/si, "File is $1 encoded" );
$count++;
};
}
my $cfg;
ok( $cfg = $h->load(1), "Test $i can be read" )
or print STDERR $Lemonldap::NG::Common::Conf::msg;
ok( $cfg->{test} eq $test[$i]->{test}, "Test $i is restored" );
$count += 2;
}
unlink 't/lmConf-1.js';
done_testing($count);