Introspection fix
[gnumeric.git] / test / t6150-ods-syntax.pl
blob515e3e091cb4f549c11e6026e849c9fc668e5a5a
1 #!/usr/bin/perl -w
2 # -----------------------------------------------------------------------------
4 use strict;
5 use lib ($0 =~ m|^(.*/)| ? $1 : ".");
6 use GnumericTest;
8 &message ("Check that the ods exporter produces valid files.");
10 my $xmllint = &GnumericTest::find_program ("xmllint");
11 my $unzip = &GnumericTest::find_program ("unzip");
13 my $format = "Gnumeric_OpenCalc:openoffice";
14 my $format_ext = "Gnumeric_OpenCalc:odf";
16 my $schemadir = "$topsrc/test/ods-schema";
17 my $schema = "$schemadir/OpenDocument-v1.2-os-schema.rng";
18 my $schema_ext = "$schemadir/OpenDocument-v1.2-os-ext-schema.rng";
19 my $schema_manifest = "$schemadir/OpenDocument-v1.2-os-manifest-schema.rng";
20 my $schema_ext_patch = "$topsrc/test/ods-ext-schema.patch";
22 my $cmd = ($ARGV[0] || '-');
23 if ($cmd eq 'download') {
24 &download ();
25 exit 0;
26 } elsif ($cmd eq 'make-schema-patch') {
27 &make_schema_patch ();
28 exit 0;
29 } elsif ($cmd eq 'make-schema-ext') {
30 &make_schema_ext ();
31 exit 0;
34 my $suggest_download = 0;
35 if (!-r $schema) {
36 print STDERR "Cannot find strict conformance schema\n";
37 $schema = undef;
38 $suggest_download = 1;
40 if (!-r $schema_ext) {
41 print STDERR "Cannot find extended conformance schema\n";
42 $schema_ext = undef;
43 # This is not a schema supplied by oasis
44 $suggest_download = 1;
46 if (!-r $schema_manifest) {
47 print STDERR "Cannot find manifest schema\n";
48 $schema_manifest = undef;
49 $suggest_download = 1;
52 print STDERR "NOTE: Suggest rerunning with argument \"download\" to obtain missing schemas\n"
53 if $suggest_download;
55 my $common_checker = "$xmllint --noout --nonet";
57 my $checker = $common_checker . ($schema ? " --relaxng $schema" : "");
58 my $checker_ext = $common_checker . ($schema_ext ? " --relaxng $schema_ext" : "");
59 my $manifest_checker = $common_checker . ($schema_manifest ? " --relaxng $schema_manifest" : "");
60 my %checkers = ( 0 => $checker,
61 1 => $checker_ext,
62 2 => $manifest_checker);
64 my @sources = &GnumericTest::corpus();
65 # xmllint hangs on these files. (Well, amath finishes but takes too
66 # long.)
67 @sources = grep { !m{(^|/)(amath|crlibm|gamma|numtheory)\.gnumeric$} } @sources;
69 my $nskipped = 0;
70 my $ngood = 0;
71 my $nbad = 0;
73 foreach my $src (@sources) {
74 if (!-r $src) {
75 $nskipped += 2;
76 next;
79 for (my $ext = 0; $ext <= 1; $ext++) {
80 print STDERR "Checking $src (", ($ext ? "extended" : "strict"), " conformance)\n";
82 my $tmp = $src;
83 $tmp =~ s|^.*/||;
84 $tmp =~ s|\..*|.ods|;
85 &GnumericTest::junkfile ($tmp);
86 my $cmd = "$ssconvert -T " . ($ext ? $format_ext : $format) . " $src $tmp";
87 print STDERR "# $cmd\n" if $GnumericTest::verbose;
88 system ($cmd);
89 if (!-r $tmp) {
90 print STDERR "ssconvert failed to produce $tmp\n";
91 die "Fail\n";
94 my %members;
95 foreach (`$unzip -v $tmp`) {
96 next unless /^----/ ... /^----/;
97 next unless m{^\s*\d.*\s(\S+)$};
98 my $member = $1;
99 if (exists $members{$member}) {
100 print STDERR "Duplicate member $member\n";
101 die "Fail\n";
103 $members{$member} = 1;
106 my @check_members = (['content.xml',$ext],
107 ['styles.xml',$ext],
108 ['META-INF/manifest.xml',2],
109 ['settings.xml',$ext],
110 ['meta.xml',$ext]);
111 foreach my $member (sort keys %members) {
112 push @check_members, [$member,$ext] if $member =~ m{^Graph\d+/content.xml$};
115 for (@check_members) {
116 my ($member,$typ) = @$_;
117 my $this_checker = $checkers{$typ};
118 my $cmd = "$unzip -p $tmp $member | $this_checker --noout -";
119 print STDERR "# $cmd\n" if $GnumericTest::verbose;
120 my $out = `$cmd 2>&1`;
121 if ($out ne '' && $out !~ /^- validates$/) {
122 print STDERR "While checking $member from $tmp:\n";
123 &GnumericTest::dump_indented ($out);
124 $nbad++;
125 } else {
126 $ngood++;
130 &GnumericTest::removejunk ($tmp);
134 &GnumericTest::report_skip ("No source files present") if $nbad + $ngood == 0;
136 if ($nskipped > 0) {
137 print STDERR "$nskipped files skipped.\n";
140 if ($nbad > 0) {
141 die "Fail\n";
142 } else {
143 print STDERR "Pass\n";
146 # -----------------------------------------------------------------------------
148 sub download {
149 my $src = "http://docs.oasis-open.org/office/v1.2/os";
151 if (!-d $schemadir) {
152 mkdir $schemadir or
153 die "$0: Cannot create directory $schemadir\n";
156 my $curl = &GnumericTest::find_program ("curl");
157 my $sha1sum = &GnumericTest::find_program ("sha1sum");
159 foreach ([scalar &File::Basename::fileparse ($schema),
160 "adc746cbb415ac3a17199442a15b38a5858fc7ef"],
161 [scalar &File::Basename::fileparse ($schema_manifest),
162 "661ab5bc695f9a8266e89cdf2747d8d76eacfedf"],
164 my ($b,$sum) = @$_;
166 my $fn = "$schemadir/$b";
167 my $tmpfn = "$fn.tmp";
169 my $had_it = (-r $fn);
170 if ($had_it) {
171 print STDERR "We already have $b\n";
172 } else {
173 print STDERR "Downloading $b...\n";
174 unlink $tmpfn;
176 my $cmd = "$curl -s -S -o $tmpfn $src/$b";
177 print STDERR "# $cmd\n";
178 my $code = system ("$cmd 2>&1 | sed -e 's/^/| /' ");
179 &GnumericTest::system_failure ($curl, $code) if $code;
182 my $cmd = &GnumericTest::quotearg ($sha1sum, ($had_it ? $fn : $tmpfn));
183 my $out = `$cmd 2>&1`;
184 die "$0: Unexpected output from $sha1sum\n" unless ($out =~ /^([a-f0-9]{40})\b/i);
185 my $act = lc ($1);
186 if ($act eq $sum) {
187 if (!$had_it) {
188 rename ($tmpfn, $fn) or
189 die "$0: Cannot rename temporary file into place: $!\n";
190 print STDERR "Download ok.\n";
192 } else {
193 print STDERR "NOTE: Expected checksum $sum, got $act.\n";
194 if (!$had_it) {
195 unlink $tmpfn;
196 print STDERR "ERROR: Download failure.\n";
197 exit 1;
202 &make_schema_ext () unless -e $schema_ext;
205 # -----------------------------------------------------------------------------
207 sub make_schema_ext {
208 my $dir = "$topsrc/test";
209 my $o = length ($dir) + 1;
211 if (-e $schema_ext) {
212 print STDERR "ERROR: Extended schema already exists.\n";
213 print STDERR "If you really want to update it, remove it first.\n";
214 exit 1;
217 if (!-r $schema || !-r $schema_ext_patch) {
218 print STDERR "ERROR: Making extended schema requires both $schema and $schema_ext_patch\n";
219 exit 1;
222 my $cmd = &GnumericTest::quotearg ("cp", $schema, $schema_ext);
223 print STDERR "# $cmd\n";
224 system ($cmd);
226 $cmd =
227 "(cd " . &GnumericTest::quotearg ($dir) .
228 " && " .
229 &GnumericTest::quotearg ("patch", "-i", substr($schema_ext_patch,$o), substr($schema_ext,$o)) .
230 ")";
231 print STDERR "# $cmd\n";
232 system ($cmd);
235 # -----------------------------------------------------------------------------
237 sub make_schema_patch {
238 my $dir = "$topsrc/test";
239 my $o = length ($dir) + 1;
241 if (!-r $schema || !-r $schema_ext) {
242 print STDERR "ERROR: Making patch requires both $schema and $schema_ext\n";
243 exit 1;
246 my $cmd =
247 "(cd " . &GnumericTest::quotearg ($dir) .
248 " && " .
249 &GnumericTest::quotearg ("diff", "-u", substr($schema,$o), substr($schema_ext,$o)) .
250 " >" . &GnumericTest::quotearg (substr($schema_ext_patch,$o)) .
251 ")";
252 print STDERR "# $cmd\n";
253 system ($cmd);
256 # -----------------------------------------------------------------------------