From fe5c1375deb6db878b55e1c6e23e754cf2c259a4 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Thu, 13 Feb 2025 14:26:55 +0100 Subject: [PATCH] Bug 39130: Add unit tests for xgettext.pl Test plan: 1. Run `prove t/db_dependent/misc/translator/xgettext.pl.t` --- .../misc/translator/sample-not-working.tt | 3 + t/db_dependent/misc/translator/sample.tt | 9 +++ t/db_dependent/misc/translator/xgettext.pl.t | 64 +++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 t/db_dependent/misc/translator/sample-not-working.tt create mode 100644 t/db_dependent/misc/translator/xgettext.pl.t diff --git a/t/db_dependent/misc/translator/sample-not-working.tt b/t/db_dependent/misc/translator/sample-not-working.tt new file mode 100644 index 0000000000..5c1366cb78 --- /dev/null +++ b/t/db_dependent/misc/translator/sample-not-working.tt @@ -0,0 +1,3 @@ +

The whole file won't be translatable because of the following line:

+ 1 %]title="Don't use > in TT directives that are inside HTML tags"> +

Still not translatable

diff --git a/t/db_dependent/misc/translator/sample.tt b/t/db_dependent/misc/translator/sample.tt index 3fd4fca2a5..889c822d4b 100644 --- a/t/db_dependent/misc/translator/sample.tt +++ b/t/db_dependent/misc/translator/sample.tt @@ -34,3 +34,12 @@ [% BLOCK %] [% t('Inside block') | $raw %] [% END %] + +This should be picked by xgettext.pl + +alt text + + + + + diff --git a/t/db_dependent/misc/translator/xgettext.pl.t b/t/db_dependent/misc/translator/xgettext.pl.t new file mode 100644 index 0000000000..b6d2a4d1d3 --- /dev/null +++ b/t/db_dependent/misc/translator/xgettext.pl.t @@ -0,0 +1,64 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use File::Slurp; +use File::Temp qw(tempdir); +use FindBin qw($Bin); +use Locale::PO; +use Test::More tests => 20; + +my $tempdir = tempdir( CLEANUP => 1 ); + +write_file( "$tempdir/files", "$Bin/sample.tt" ); + +my $xgettext_cmd = "$Bin/../../../../misc/translator/xgettext.pl -o $tempdir/Koha.pot -f $tempdir/files"; + +system($xgettext_cmd); +system("msgcat -o $tempdir/Koha.sorted.pot -s $tempdir/Koha.pot"); +my $pot = Locale::PO->load_file_asarray("$tempdir/Koha.sorted.pot"); + +my @expected = ( + { + msgid => + '"%s %s %s %s %s %s %s %s %s %s [%% # it also works on multiple lines tnpx ( \'context\', \'{count} item\', \'{count} items\', count, { count = count, } ) | $raw %%] [%% # and t* calls can be nested tx(\'status is {status}\', { status = active ? t(\'active\') : t(\'inactive\') }) | $raw %%] [%%# but a TT comment won\'t get picked t(\'not translatable\') %%] %s %s %s "', + }, + { + msgid => '"Foo"', + }, + { + msgid => '"This should be picked by xgettext.pl"', + }, + { + msgid => '"alt text"', + }, + { + msgid => '"but this is (thanks to space before attribute name)"', + }, + { + msgid => '"foo title"', + }, +); + +for ( my $i = 0 ; $i < @expected ; $i++ ) { + for my $key (qw(msgid msgid_plural msgctxt)) { + my $expected = $expected[$i]->{$key}; + my $expected_str = defined $expected ? $expected : 'not defined'; + my $msg = $pot->[ $i + 1 ]; + if ($msg) { + is( $msg->$key, $expected, "$i: $key is $expected_str" ); + } else { + fail("$i: $key is $expected_str (no corresponding message in POT)"); + } + } +} + +is( scalar @$pot, 1 + scalar(@expected) ); + +write_file( "$tempdir/files", "$Bin/sample-not-working.tt" ); + +$xgettext_cmd = "$Bin/../../../../misc/translator/xgettext.pl -o $tempdir/Koha.pot -f $tempdir/files 2>/dev/null"; + +system($xgettext_cmd); +$pot = Locale::PO->load_file_asarray("$tempdir/Koha.pot"); +is( scalar @$pot, 0, 'xgettext.pl failed to generate a POT file because of incorrect structure' ); -- 2.39.5