Commit f1d3e947 authored by Yadd's avatar Yadd
Browse files

Update File tests (utf8) (#827)

parent fcc333e4
......@@ -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;
......
......@@ -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);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment