From f1d3e947c7dacdce025715559d4a5eca08b968bf Mon Sep 17 00:00:00 2001 From: Xavier Guimard Date: Sat, 9 Jan 2016 19:22:31 +0000 Subject: [PATCH] Update File tests (utf8) (#827) --- .../lib/Lemonldap/NG/Common/Conf/File.pm | 6 ++- lemonldap-ng-common/t/02-Common-Conf-File.t | 41 ++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/File.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/File.pm index 1caa0bfa7..aa98f7a65 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/File.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/File.pm @@ -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; diff --git a/lemonldap-ng-common/t/02-Common-Conf-File.t b/lemonldap-ng-common/t/02-Common-Conf-File.t index e6428a0c4..2c6978ed5 100644 --- a/lemonldap-ng-common/t/02-Common-Conf-File.t +++ b/lemonldap-ng-common/t/02-Common-Conf-File.t @@ -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( '', ); + 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);