Skip to content

Commit af90ad7

Browse files
committed
Improve spec-runner to fully cover warnings and errors
1 parent f81bc86 commit af90ad7

File tree

1 file changed

+105
-26
lines changed

1 file changed

+105
-26
lines changed

t/99_sass_specs.t

Lines changed: 105 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/perl
12
# -*- perl -*-
23

34
use strict;
@@ -19,6 +20,7 @@ sub new
1920
return bless {
2021
root => $root,
2122
parent => $parent,
23+
wtodo => $opt->{wtodo},
2224
todo => $opt->{todo},
2325
clean => $opt->{clean},
2426
style => $opt->{style},
@@ -47,14 +49,28 @@ package SPEC;
4749
################################################################################
4850

4951
use CSS::Sass;
52+
use Cwd qw(getcwd);
5053
use Carp qw(croak);
5154
use File::Spec::Functions;
5255

56+
my $cwd = getcwd;
57+
my $cwd_win = $cwd;
58+
my $cwd_nix = $cwd;
59+
$cwd_win =~ s/[\/\\]/\\/g;
60+
$cwd_nix =~ s/[\/\\]/\//g;
61+
5362
# everything is normalized
5463
my $norm_output = sub ($) {
5564
$_[0] =~ s/(?:\r?\n)+/\n/g;
5665
$_[0] =~ s/;(?:\s*;)+/;/g;
5766
$_[0] =~ s/;\s*}/}/g;
67+
# normalize debug entries
68+
$_[0] =~ s/[^\n]+(\d+) DEBUG: /$1: DEBUG: /g;
69+
# normalize directory entries
70+
$_[0] =~ s/\/libsass-todo-issues\//\/libsass-issues\//g;
71+
$_[0] =~ s/\/libsass-closed-issues\//\/libsass-issues\//g;
72+
$_[0] =~ s/\Q$cwd_win\E[\/\\]t[\/\\]sass-spec[\/\\]/\/sass\//g;
73+
$_[0] =~ s/\Q$cwd_nix\E[\/\\]t[\/\\]sass-spec[\/\\]/\/sass\//g;
5874
};
5975

6076
# only flagged stuff is cleaned
@@ -69,27 +85,62 @@ sub new
6985
my $pkg = $_[0];
7086
my $root = $_[1];
7187
my $file = $_[2];
88+
my $test = $_[3];
7289
return bless {
7390
root => $root,
7491
file => $file,
92+
test => $test,
7593
}, $pkg;
7694
}
7795

96+
sub errors
97+
{
98+
my ($spec) = @_;
99+
100+
local $/ = undef;
101+
return -f catfile($spec->{root}->{root}, "status");
102+
}
103+
78104
sub stderr
79105
{
80106
my ($spec) = @_;
81107

82108
local $/ = undef;
83-
my $path = catfile($_[0]->{root}->{root}, "error");
84-
return undef unless -f $path;
109+
my $path = catfile($spec->{root}->{root}, "error");
110+
return "" unless -f $path;
85111
open my $fh, "<:raw:utf8", $path or
86112
croak "Error opening <", $path, ">: $!";
87113
binmode $fh; my $stderr = join "\n", <$fh>;
114+
# fully remove debug messaged from error
115+
$stderr =~ s/[^\n]+(\d+) DEBUG: [^\n]*//g;
88116
$norm_output->($stderr);
117+
# clean todo warnings (remove all warning blocks)
118+
$stderr =~ s/^(?:DEPRECATION )?WARNING(?:[^\n]+\n)*\n*//gm;
89119
$stderr =~ s/\n.*\Z//s;
90120
return $stderr;
121+
}
122+
123+
sub stdmsg
124+
{
125+
my ($spec) = @_;
91126

127+
local $/ = undef;
128+
my $path = catfile($spec->{root}->{root}, "error");
129+
return '' unless -f $path;
130+
open my $fh, "<:raw:utf8", $path or
131+
croak "Error opening <", $path, ">: $!";
132+
binmode $fh; my $stderr = join "\n", <$fh>;
133+
$norm_output->($stderr);
134+
if ($spec->{test}->{wtodo}) {
135+
# clean todo warnings (remove all warning blocks)
136+
$stderr =~ s/^(?:DEPRECATION )?WARNING(?:[^\n]+\n)*\n*//gm;
137+
}
138+
# clean error messages
139+
$stderr =~ s/^Error(?:[^\n]+\n)*\n*//gm;
140+
$stderr =~ s/\n.*\Z//s;
141+
return $stderr;
92142
}
143+
93144
sub expected
94145
{
95146
my ($spec) = @_;
@@ -135,12 +186,22 @@ sub err
135186
{
136187
$_[0]->execute;
137188
my $err = $_[0]->{err};
138-
return $err unless defined $err;
189+
return "" unless defined $err;
139190
$norm_output->($err);
140191
$err =~ s/\n.*\Z//s;
141192
return $err;
142193
}
143194

195+
sub msg
196+
{
197+
$_[0]->execute;
198+
my $msg = $_[0]->{msg};
199+
return "" unless defined $msg;
200+
$norm_output->($msg);
201+
$msg =~ s/\n.*\Z//s;
202+
return $msg;
203+
}
204+
144205
sub execute
145206
{
146207

@@ -167,16 +228,18 @@ sub execute
167228
open OLDFH, '>&STDERR';
168229

169230
# redirect stderr to file
170-
open(STDERR, "+>", "specs.stderr.log"); select(STDERR); $| = 1;
231+
open(STDERR, "+>:raw:utf8", "specs.stderr.log"); select(STDERR); $| = 1;
171232
my $css = eval { $comp->compile_file($spec->{file}) }; my $err = $@;
172-
print STDERR "\n"; sysseek(STDERR, 0, 0); close(STDERR);
233+
sysseek(STDERR, 0, 0); sysread(STDERR, my $msg, 65536); close(STDERR);
173234

174235
# reset stderr
175236
open STDERR, '>&OLDFH';
176237

177238
# store the results
178239
$spec->{css} = $css;
179240
$spec->{err} = $err;
241+
$spec->{msg} = $msg;
242+
180243
# return the results
181244
return $css, $err;
182245

@@ -216,7 +279,8 @@ sub load_tests()
216279
{
217280

218281
# result
219-
my @specs; my $filter = qr/huge|unicode\/report/;
282+
my @specs; my $ignore = qr/huge|unicode\/report/;
283+
my $filter = qr/\Q$ARGV[0]\E/ if defined $ARGV[0];
220284
# initial spec test directory entry
221285
my $root = new DIR;
222286
$root->{start} = 0;
@@ -238,9 +302,11 @@ sub load_tests()
238302
$test->{start} = $yaml->{':start_version'};
239303
$test->{end} = $yaml->{':end_version'};
240304
$test->{ignore} = grep /^libsass$/i,
241-
@{$yaml->{':ignore_for'} || []};
305+
@{$yaml->{':ignore_for'} || []};
306+
$test->{wtodo} = grep /^libsass$/i,
307+
@{$yaml->{':warning_todo'} || []};
242308
$test->{todo} = grep /^libsass$/i,
243-
@{$yaml->{':todo'} || []};
309+
@{$yaml->{':todo'} || []};
244310
}
245311

246312
$test->{clean} = $parent->{clean} unless $test->{clean};
@@ -249,19 +315,24 @@ sub load_tests()
249315
$test->{start} = $parent->{start} unless $test->{start};
250316
$test->{end} = $parent->{end} unless $test->{end};
251317
$test->{ignore} = $parent->{ignore} unless $test->{ignore};
318+
$test->{wtodo} = $parent->{wtodo} unless $test->{wtodo};
252319
$test->{todo} = $parent->{todo} unless $test->{todo};
253320

254321
my $sass = catfile($dir, "input.sass");
255322
my $scss = catfile($dir, "input.scss");
256323
# have spec test
257324
if (-e $scss) {
258-
if (!$filter || !($scss =~ m/$filter/)) {
259-
push @specs, new SPEC($test, $scss);
325+
if (!$ignore || !($scss =~ m/$ignore/)) {
326+
if (!$filter || ($scss =~ m/$filter/)) {
327+
push @specs, new SPEC($test, $scss, $test);
328+
}
260329
}
261330
}
262331
elsif (-e $sass) {
263-
if (!$filter || !($sass =~ m/$filter/)) {
264-
push @specs, new SPEC($test, $sass);
332+
if (!$ignore || !($sass =~ m/$ignore/)) {
333+
if (!$filter || ($sass =~ m/$filter/)) {
334+
push @specs, new SPEC($test, $sass, $test);
335+
}
265336
}
266337
}
267338

@@ -285,27 +356,35 @@ sub load_tests()
285356
return @specs;
286357
}
287358

288-
use vars qw(@specs);
359+
use vars qw(@tests @specs);
289360
# specs must be loaded first
290361
# before registering tests
291-
BEGIN { @specs = grep {
292-
! $_->query('todo') &&
293-
! $_->query('ignore') &&
294-
$_->query('start') <= 3.4
295-
} load_tests }
362+
BEGIN {
363+
@tests = load_tests;
364+
@specs = grep {
365+
! $_->query('todo') &&
366+
! $_->query('ignore') &&
367+
$_->query('start') <= 3.4
368+
} @tests;
369+
}
370+
371+
# report todo tests
372+
# die join("\n", map {
373+
# $_->{root}->{root}
374+
# } grep {
375+
# $_->query('todo') &&
376+
# ! $_->query('ignore') &&
377+
# $_->query('start') <= 3.4
378+
# } @tests);
296379

297-
use Test::More tests => scalar @specs;
380+
use Test::More tests => 3 * scalar @specs;
298381
use Test::Differences;
299382

300383
# run tests after filtering
301384
foreach my $spec (@specs)
302385
{
303386
# compare the result with expected data
304-
if ($spec->err) {
305-
eq_or_diff ($spec->err, $spec->stderr, $spec->file)
306-
} elsif ($spec->expect) {
307-
eq_or_diff ($spec->result, $spec->expect, $spec->file)
308-
} else {
309-
eq_or_diff ($spec->result, $spec->result, $spec->file)
310-
}
387+
eq_or_diff ($spec->css, $spec->expect, "CSS: " . $spec->file);
388+
eq_or_diff ($spec->err, $spec->stderr, "Errors: " . $spec->file);
389+
eq_or_diff ($spec->msg, $spec->stdmsg, "Warnings: " . $spec->file);
311390
}

0 commit comments

Comments
 (0)