Skip to content

Commit f8a479f

Browse files
committed
Allow some tests to compile-check solo.
Allows us to test items that might interfere with other tests when run in a giant EVAL'd environment. (E.g. having multiple exports of the same name that are only conflicting across multiple files) solo implies that don't have to wrap the code in anything.
1 parent a318b8d commit f8a479f

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

writing-docs/EXAMPLES.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ example in the code.
8989
$x = frob();
9090
=end code
9191

92+
### Complicated Examples
93+
94+
Some examples are too complicated to be run using our EVAL trick.
95+
Tag these with `:solo`, and they will be run as a separate standalone
96+
script. This is slower, so only use it on those examples that require
97+
it. Anything using `unit` or `export` is a good candidate. Note that
98+
using this tag means the code is not wrapped as it is for the EVAL path.
99+
92100
### Failures
93101

94102
Some examples fail with compile time exceptions and would interrupt the test

xt/examples-compilation.t

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use v6;
1010
}
1111

1212
use Test;
13+
use File::Temp;
1314

1415
use lib 'lib';
1516
use Pod::Convenience;
@@ -46,6 +47,7 @@ sub walk($arg) {
4647

4748
# Extract all the examples from the given files
4849
my @examples;
50+
4951
my $counts = BagHash.new;
5052
for @files -> $file {
5153
for extract-pod($file.IO).contents -> $chunk {
@@ -65,6 +67,7 @@ for @files -> $file {
6567
'ok-test', $chunk.config<ok-test> // "",
6668
'preamble', $chunk.config<preamble> // "",
6769
'method', $chunk.config<method> // "",
70+
'solo', $chunk.config<solo> // "",
6871
);
6972
}
7073
}
@@ -90,37 +93,52 @@ for @examples -> $eg {
9093
# Further wrap in an anonymous class (so bare method works)
9194
# Add in empty routine bodies if needed
9295

93-
my $code = 'no worries; ';
94-
$code ~= "if False \{\nclass :: \{\n";
95-
$code ~= $eg<preamble> ~ ";\n";
96-
97-
for $eg<contents>.lines -> $line {
98-
$code ~= $line;
99-
if $line.trim.starts-with(any(<multi method proto only sub>)) && !$line.trim.ends-with(any('}',',')) && $eg<method> eq "" {
100-
$code ~= " \{}";
101-
}
102-
if $eg<method> eq "" || $eg<method> eq "False" {
103-
$code ~= "\n";
96+
my $code;
97+
if $eg<solo> {
98+
$code = $eg<contents>;
99+
} else {
100+
$code = 'no worries; ';
101+
$code ~= "if False \{\nclass :: \{\n";
102+
$code ~= $eg<preamble> ~ ";\n";
103+
104+
for $eg<contents>.lines -> $line {
105+
$code ~= $line;
106+
if $line.trim.starts-with(any(<multi method proto only sub>)) && !$line.trim.ends-with(any('}',',')) && $eg<method> eq "" {
107+
$code ~= " \{}";
108+
}
109+
if $eg<method> eq "" || $eg<method> eq "False" {
110+
$code ~= "\n";
111+
}
104112
}
113+
$code ~= "\{}\n" if $eg<method> eq "True";
114+
$code ~= "\n}}";
105115
}
106-
$code ~= "\{}\n" if $eg<method> eq "True";
107-
$code ~= "\n}}";
108116

109117
my $msg = "$eg<file> chunk $eg<count> starts with “" ~ starts-with($eg<contents>) ~ "” compiles";
110118

111-
my $status;
119+
my $has-error;
112120
{
113-
temp $*OUT = open :w, $*SPEC.devnull;
114-
temp $*ERR = open :w, $*SPEC.devnull;
115-
try EVAL $code;
116-
$status = $!;
117-
close $*OUT;
118-
close $*ERR;
121+
# Does the test require its own file?
122+
if $eg<solo> {
123+
my ($tmp_fname, $tmp_io) = tempfile;
124+
$tmp_io.spurt: $code, :close;
125+
my $proc = Proc::Async.new($*EXECUTABLE, '-c', $tmp_fname);
126+
$proc.stdout.tap: {;};
127+
$proc.stderr.tap: {;};
128+
$has-error = ! await $proc.start;
129+
} else {
130+
temp $*OUT = open :w, $*SPEC.devnull;
131+
temp $*ERR = open :w, $*SPEC.devnull;
132+
try EVAL $code;
133+
$has-error = $!;
134+
close $*OUT;
135+
close $*ERR;
136+
}
119137
}
120138
todo(1) if $eg<todo>;
121-
if $status {
139+
if $has-error {
122140
diag $eg<contents>;
123-
diag $status;
141+
diag $has-error;
124142
flunk $msg;
125143
} else {
126144
pass $msg;

0 commit comments

Comments
 (0)