Skip to content

Commit dbd2f39

Browse files
committed
Added import test for parse_rc
Fixes #92
1 parent 6c6a7a8 commit dbd2f39

10 files changed

Lines changed: 289 additions & 0 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ script:
77
- perl Build.PL && ./Build && ./Build test
88
- cover -delete
99
- perl -MDevel::Cover t/engine.t
10+
- perl -MDevel::Cover t/importer.t
1011
after_success:
1112
- cover -report codecov
1213
perl:

t/data/importer/common.serge

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
job_template
2+
{
3+
name Test Job
4+
id test_job
5+
active YES
6+
source_language en
7+
destination_languages test
8+
source_dir ./resources
9+
db_source DBI:SQLite:dbname=:memory:
10+
db_namespace test_namespace
11+
ts_file_path ./test-output/po/%LOCALE%/%FILE%.po
12+
output_bom NO
13+
output_file_path ./test-output/localized-resources/%LOCALE%/%FILE%
14+
15+
callback_plugins
16+
{
17+
:test_language
18+
{
19+
plugin test_language
20+
phase get_translation_pre
21+
22+
data
23+
{
24+
save_translations YES
25+
}
26+
}
27+
}
28+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
files
2+
{
3+
0 1 1 test_job test_namespace application.rc 0
4+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
items
2+
{
3+
0 1 3 1 1 IDS_TEST_1 NO 0
4+
1 2 5 1 2 IDS_TEST_2 NO 0
5+
2 3 7 1 3 IDS_ERROR NO 0
6+
3 4 9 1 4 IDS_TEST_3 NO 0
7+
4 5 11 1 5 IDS_TEST_4 NO 0
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
properties
2+
{
3+
0 1 source:1 b88e2e32e7cd852f7d4b76ba913a78a1
4+
1 2 hash:1 b88e2e32e7cd852f7d4b76ba913a78a1
5+
2 3 size:1 923
6+
3 4 items:1 1,2,3,4,5
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
strings
2+
{
3+
0 1 2 `Value 1` NO 0
4+
1 2 4 `Value 2` NO 0
5+
2 3 6 `Error %s` NO 0
6+
3 4 8 `Value 3` NO 0
7+
4 5 10 `Value 4` NO 0
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
translations
2+
{
3+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Microsoft Visual C++ generated resource script.
2+
//
3+
#include "resource.h"
4+
5+
/////////////////////////////////////////////////////////////////////////////
6+
//
7+
// Test Version
8+
//
9+
10+
VS_VERSION_INFO VERSIONINFO
11+
VERSION 1,0,0,1111
12+
PRODUCTVERSION 4,0,0,1111
13+
FILEFLAGSMASK 0x12L
14+
BEGIN
15+
BLOCK "StringFileInfo"
16+
BEGIN
17+
BLOCK "040904b0"
18+
BEGIN
19+
VALUE "Sample Name", "Sample Name Value"
20+
VALUE "Description", "Good description"
21+
VALUE "Version", "1,0,0,1111"
22+
END
23+
END
24+
BLOCK "VarFileInfo"
25+
BEGIN
26+
VALUE "Alternative", 0x409, 1200
27+
END
28+
END
29+
30+
/////////////////////////////////////////////////////////////////////////////
31+
//
32+
// Test Strings
33+
//
34+
35+
STRINGTABLE
36+
BEGIN
37+
IDS_TEST_1 "Value 1"
38+
IDS_TEST_2 "Value 2"
39+
IDS_ERROR "Error %s"
40+
END
41+
42+
STRINGTABLE
43+
BEGIN
44+
IDS_TEST_3
45+
"Value 3"
46+
IDS_TEST_4 "Value 4"
47+
END
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
jobs
2+
{
3+
{
4+
@inherit ../../common.serge#job_template
5+
6+
source_match \.rc$
7+
8+
parser
9+
{
10+
plugin parse_rc
11+
}
12+
}
13+
}

t/importer.t

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
#!/usr/bin/env perl
2+
3+
use strict;
4+
5+
# HOW TO USE THIS TEST
6+
#
7+
# By default, this test runs over all directories in t/data/importer/. To run
8+
# the test only for specific directories, pass the directory names to this
9+
# script or assign them to the environment variable SERGE_IMPORTER_TESTS as a
10+
# comma-separated list. The following two examples are equivalent:
11+
#
12+
# perl t/importer.t parse_json parse_strings
13+
# SERGE_IMPORTER_TESTS=parse_json,parse_strings prove t/importer.t
14+
15+
BEGIN {
16+
use Cwd qw(abs_path);
17+
use File::Basename;
18+
use File::Spec::Functions qw(catfile);
19+
map { unshift(@INC, catfile(dirname(abs_path(__FILE__)), $_)) } qw(lib ../lib);
20+
}
21+
22+
use Data::Dumper;
23+
use File::Copy::Recursive qw/dircopy/;
24+
use File::Find qw(find);
25+
use File::Path;
26+
use File::Spec::Functions qw(catfile);
27+
use Getopt::Long;
28+
use Test::Config;
29+
use Test::Diff;
30+
use Test::More;
31+
use Test::DB::Dumper;
32+
use Serge::Importer;
33+
use Serge::Engine::Job;
34+
35+
$| = 1; # disable output buffering
36+
37+
# to get the same results between tests,
38+
# we override the `file_mtime` function to return a constant value;
39+
# Serge::Importer automatically imports this function from Serge::Util
40+
# (this is why it actually appears in Serge::Importer namespace)
41+
sub Serge::Importer::file_mtime {
42+
return 12345678;
43+
}
44+
45+
my $this_dir = dirname(abs_path(__FILE__));
46+
my $tests_dir = catfile($this_dir, 'data', 'importer');
47+
48+
my @importer_confs;
49+
50+
my ($init_references);
51+
52+
GetOptions("init" => \$init_references);
53+
54+
my @importer_dirs = @ARGV;
55+
if (my $env_dirs = $ENV{SERGE_IMPORTER_TESTS}) {
56+
push @importer_dirs, split(/,/, $env_dirs);
57+
}
58+
59+
unless (@importer_dirs) {
60+
find(sub {
61+
push @importer_confs, $File::Find::name if(-f $_ && /\.serge$/ && $_ ne 'common.serge');
62+
}, $tests_dir);
63+
} else {
64+
for my $dir (@importer_dirs) {
65+
find(sub {
66+
push @importer_confs, $File::Find::name if(-f $_ && /\.serge$/ && $_ ne 'common.serge');
67+
}, catfile($tests_dir, $dir));
68+
}
69+
}
70+
71+
sub delete_directory {
72+
my ($path, $ignore_errors) = @_;
73+
74+
my $err;
75+
76+
if (-e $path) {
77+
rmtree($path, { error => \$err });
78+
if (@$err && !$ignore_errors) {
79+
my $err_text = '';
80+
81+
map {
82+
foreach my $key (keys %$_) {
83+
$err_text .= $key.': '.$_->{$key}."\n";
84+
}
85+
} @$err;
86+
87+
BAIL_OUT("Directory '".$path."' couldn't be removed\n$err_text");
88+
}
89+
}
90+
}
91+
92+
for my $config_file (@importer_confs) {
93+
94+
subtest "Test config: $config_file" => sub {
95+
my $cfg = Test::Config->new($config_file);
96+
97+
SKIP: {
98+
my $ok = ok(defined $cfg, 'Config file read');
99+
skip "<$config_file>", $init_references ? 2 : 4 if !$ok;
100+
101+
my $err;
102+
103+
delete_directory($cfg->output_path);
104+
if ($init_references) {
105+
delete_directory($cfg->reference_output_path);
106+
}
107+
108+
my $engine = Serge::Importer->new();
109+
$engine->{optimizations} = undef; # force generate all the files
110+
$cfg->chdir;
111+
112+
foreach my $job_data (@{$cfg->{data}->{jobs}}) {
113+
my $job;
114+
115+
eval {
116+
$job = Serge::Engine::Job->new($job_data, $engine, $cfg->{base_dir});
117+
$engine->process_job($job);
118+
};
119+
120+
if ($@) {
121+
my $error = $@;
122+
# cleanup error message to avoid having file paths that will differ across installations
123+
$error =~ s/\s+$//sg;
124+
$error =~ s/ at .*? line \d+\.$//s;
125+
$error =~ s/ \(\@INC contains: .*\)$//s;
126+
$error =~ s/\@INC.+$/\@INC/s;
127+
128+
print "Job '$job_data->{id}' will be skipped: $error\n";
129+
130+
eval { mkpath($cfg->errors_path) };
131+
die "Couldn't create $cfg->errors_path: $@" if $@;
132+
my $filename = catfile($cfg->errors_path, $job_data->{id}.'.txt');
133+
open(OUT, ">$filename");
134+
binmode(OUT, ':unix :utf8');
135+
print OUT $error;
136+
close(OUT);
137+
}
138+
}
139+
140+
#ok(!$@, 'Processing all jobs in config file') or BAIL_OUT('Engine failed to run some of the jobs');
141+
142+
if ($cfg->can_dump_db) {
143+
my $dumper = Test::DB::Dumper->new($engine);
144+
$dumper->dump_l10n_tables($Test::DB::Dumper::TYPE_NEAT, $cfg->db_path);
145+
} else {
146+
print "Skipped dumping the database, as this is not applicable for this test\n";
147+
}
148+
149+
$engine->cleanup;
150+
151+
if ($init_references) {
152+
ok(dircopy($cfg->output_path, $cfg->reference_output_path), "Initialized ".$cfg->reference_output_path);
153+
} else {
154+
$ok &= dir_diff($cfg->errors_path, $cfg->reference_errors_path, { base_dir => $cfg->{base_dir} } );
155+
$ok &= dir_diff($cfg->db_path, $cfg->reference_db_path, { base_dir => $cfg->{base_dir} } );
156+
$ok &= dir_diff($cfg->ts_path, $cfg->reference_ts_path, { base_dir => $cfg->{base_dir} } );
157+
$ok &= dir_diff($cfg->data_path, $cfg->reference_data_path, { base_dir => $cfg->{base_dir} } ) if $cfg->output_lang_files;
158+
}
159+
160+
# Under Windows, deleting just created files may fail with 'Permission denied'
161+
# for an unknown reason, and only closing the process will release the file handles.
162+
# Since we will be removing test output at the beginning of each test anyway,
163+
# don't bail out this time if some files failed to be removed
164+
delete_directory($cfg->output_path, 1) if $ok;
165+
}
166+
}
167+
}
168+
169+
170+
done_testing();

0 commit comments

Comments
 (0)