Skip to content

Commit d237be1

Browse files
DRMacIverclaude
andcommitted
Default report_multiple_failures to false and rethrow the failure directly
Multi-failure reporting is debugger-hostile — it aggregates several shrinking runs into one synthesized AssertionError, losing the real exception's type and stack trace. That matters more in Java, so default it off. When off (the new default), the run keeps the actual Throwable from the minimal example's final replay and rethrows it directly — no origin map, no report synthesis — so the test fails with the property's own exception (type, message, stack trace intact). Multi-failure mode is unchanged and still builds the aggregated "Hegel found N failing examples" report. A failure with no Java exception to rethrow (a health-check abort, or the replay phase disabled) falls back to the engine's diagnostic. settings.reportMultipleFailures is now a primitive boolean (always applied to the engine). The drawn-values trace ("xs = [0, 0];") still prints on the final replay in both modes, so the counterexample is shown regardless. Update affected tests and the README quickstart (the default now surfaces the bare opentest4j.AssertionFailedError, not the wrapped report). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 9cc3ad0 commit d237be1

6 files changed

Lines changed: 169 additions & 65 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,13 @@ class SortTest {
8484
```
8585

8686
Run it with `mvn test`. Hegel finds the bug and shrinks it to the minimal failing input. It prints
87-
the shrunk draws (each labelled `draw` shows as an assignment; here we named it `xs`):
87+
the shrunk draws (the optional label names each one; here we passed `"xs"`) and then fails the test
88+
with the property's own assertion error:
8889

8990
```
9091
xs = [0, 0];
91-
```
92-
93-
and fails the test with:
94-
95-
```
96-
java.lang.AssertionError: Hegel found 1 failing example:
97-
98-
Failure reported by C caller: AssertionFailedError at SortTest.java:16
9992
100-
org.opentest4j.AssertionFailedError: expected: <2> but was: <1>
93+
org.opentest4j.AssertionFailedError: expected: <2> but was: <1>
10194
```
10295

10396
`[0, 0]` is the simplest input that triggers the bug: two equal elements, which `mySort`

‎src/main/java/dev/hegel/Runner.java‎

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.lang.foreign.MemorySegment;
55
import java.util.HashMap;
66
import java.util.Map;
7+
import java.util.function.BiConsumer;
78
import java.util.function.Consumer;
89

910
/**
@@ -40,14 +41,45 @@ static void run(
4041
throw backend(lib, "hegel_run_start");
4142
}
4243
try {
43-
Map<String, String> panicByOrigin = new HashMap<>();
44-
loop(lib, run, settings, body, out, panicByOrigin);
44+
// Default (report_multiple_failures off): keep the actual exception so we can rethrow it
45+
// directly — best for debuggers and stack traces, and no origin tracking. Only the
46+
// multiple-failures mode needs to stitch each captured message back onto a distinct,
47+
// engine-deduped failure, so it alone builds the origin map.
48+
Throwable[] captured = {null};
49+
Map<String, String> panicByOrigin =
50+
settings.reportMultipleFailures ? new HashMap<>() : null;
51+
loop(
52+
lib,
53+
run,
54+
settings.singleTestCase,
55+
body,
56+
out,
57+
(origin, e) -> {
58+
captured[0] = e;
59+
if (panicByOrigin != null) {
60+
String message = describe(e);
61+
out.println(message);
62+
panicByOrigin.put(origin, message);
63+
}
64+
});
4565
MemorySegment result = lib.runResult(run);
4666
if (isNull(result)) {
4767
throw backend(lib, "hegel_run_result");
4868
}
4969
if (!lib.resultPassed(result)) {
50-
throw buildFailure(lib, result, panicByOrigin);
70+
if (panicByOrigin != null) {
71+
throw buildFailure(lib, result, panicByOrigin);
72+
}
73+
// Rethrow the body's own exception (always unchecked, from Consumer#accept) directly.
74+
if (captured[0] instanceof Error error) {
75+
throw error;
76+
}
77+
if (captured[0] instanceof RuntimeException re) {
78+
throw re;
79+
}
80+
// No Java exception to rethrow (a health-check abort, or a failure found with the
81+
// replay phase disabled): surface the engine's own diagnostic.
82+
throw fallbackFailure(lib, result);
5183
}
5284
} finally {
5385
lib.runFree(run);
@@ -60,10 +92,10 @@ static void run(
6092
private static void loop(
6193
Libhegel lib,
6294
MemorySegment run,
63-
Settings settings,
95+
boolean single,
6496
Consumer<TestCase> body,
6597
PrintStream out,
66-
Map<String, String> panicByOrigin) {
98+
BiConsumer<String, Throwable> onReportedFailure) {
6799
while (true) {
68100
MemorySegment tc = lib.nextTestCase(run);
69101
if (isNull(tc)) {
@@ -73,7 +105,7 @@ private static void loop(
73105
}
74106
return;
75107
}
76-
driveOneCase(lib, tc, settings.singleTestCase, body, out, panicByOrigin);
108+
driveOneCase(lib, tc, single, body, out, onReportedFailure);
77109
}
78110
}
79111

@@ -83,7 +115,7 @@ static void driveOneCase(
83115
boolean single,
84116
Consumer<TestCase> body,
85117
PrintStream out,
86-
Map<String, String> panicByOrigin) {
118+
BiConsumer<String, Throwable> onReportedFailure) {
87119
boolean reporting = single || lib.isFinalReplay(tc);
88120
TestCase testCase = new TestCase(new LiveDataSource(lib, tc), reporting, out);
89121
int status;
@@ -100,17 +132,13 @@ static void driveOneCase(
100132
} catch (Throwable e) {
101133
status = Abi.STATUS_INTERESTING;
102134
origin = originOf(e);
103-
// The engine dedups failures by `origin` (a source location) and reports its own
104-
// diagnostic, but it never sees the Java exception message. We capture that message and
105-
// stitch it back in per origin in buildFailure. Do it only on the case the engine
106-
// actually reports — the final replay of the minimal example — exactly as
107-
// hegel_test_case_is_final_replay is meant to gate (single-test-case mode has no replay,
108-
// so its one case reports directly). Capturing on every shrink probe would be wasted
109-
// work and risks attaching a non-minimal message.
135+
// Hand the failing exception to the run only on the case the engine actually reports —
136+
// the final replay of the minimal example — exactly as hegel_test_case_is_final_replay
137+
// is meant to gate (single-test-case mode has no replay, so its one case reports
138+
// directly). The drawn values are printed separately by TestCase under this same flag, so
139+
// the counterexample is shown whether or not the exception is rethrown.
110140
if (reporting) {
111-
String message = describe(e);
112-
panicByOrigin.put(origin, message);
113-
out.println(message);
141+
onReportedFailure.accept(origin, e);
114142
}
115143
}
116144
int rc = lib.markComplete(tc, status, origin);
@@ -128,9 +156,7 @@ static void applySettings(Libhegel lib, MemorySegment s, Settings st, Map<String
128156
lib.settingsSeed(s, st.seed, true);
129157
}
130158
lib.settingsDerandomize(s, st.derandomize != null ? st.derandomize : ci);
131-
if (st.reportMultipleFailures != null) {
132-
lib.settingsReportMultipleFailures(s, st.reportMultipleFailures);
133-
}
159+
lib.settingsReportMultipleFailures(s, st.reportMultipleFailures);
134160
if (st.singleTestCase) {
135161
lib.settingsMode(s, Abi.MODE_SINGLE_TEST_CASE);
136162
}
@@ -213,6 +239,13 @@ static AssertionError buildFailure(
213239
return new AssertionError(sb.toString());
214240
}
215241

242+
/** Failure with no Java exception to rethrow: surface the engine's own diagnostic. */
243+
private static AssertionError fallbackFailure(Libhegel lib, MemorySegment result) {
244+
MemorySegment failure = lib.resultFailure(result, 0);
245+
return new AssertionError(
246+
pick(lib.failureDiagnostic(failure), lib.failurePanicMessage(failure)));
247+
}
248+
216249
private static String pick(String diagnostic, String panic) {
217250
if (diagnostic != null && !diagnostic.isEmpty()) {
218251
return diagnostic;

‎src/main/java/dev/hegel/Settings.java‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,18 @@ enum DbMode {
2525

2626
private static final Settings DEFAULTS =
2727
new Settings(
28-
100, false, 0L, null, DbMode.DEFAULT, null, 0, null, Verbosity.NORMAL, false, null, null);
28+
100,
29+
false,
30+
0L,
31+
null,
32+
DbMode.DEFAULT,
33+
null,
34+
0,
35+
null,
36+
Verbosity.NORMAL,
37+
false,
38+
false,
39+
null);
2940

3041
final long testCases;
3142
final boolean hasSeed;
@@ -37,7 +48,9 @@ enum DbMode {
3748
final Integer phasesMask; // null = leave the engine default (all phases)
3849
final Verbosity verbosity;
3950
final boolean singleTestCase;
40-
final Boolean reportMultipleFailures;
51+
// Default false: a single, directly-rethrown failure is far friendlier to debuggers and stack
52+
// traces than an aggregated report — and that matters more in Java than elsewhere.
53+
final boolean reportMultipleFailures;
4154
final String name;
4255

4356
private Settings(
@@ -51,7 +64,7 @@ private Settings(
5164
Integer phasesMask,
5265
Verbosity verbosity,
5366
boolean singleTestCase,
54-
Boolean reportMultipleFailures,
67+
boolean reportMultipleFailures,
5568
String name) {
5669
this.testCases = testCases;
5770
this.hasSeed = hasSeed;
@@ -87,7 +100,7 @@ private Settings copy(
87100
Integer phasesMask,
88101
Verbosity verbosity,
89102
boolean singleTestCase,
90-
Boolean reportMultipleFailures,
103+
boolean reportMultipleFailures,
91104
String name) {
92105
return new Settings(
93106
testCases,
@@ -315,6 +328,9 @@ public Settings singleTestCase(boolean single) {
315328

316329
/**
317330
* Control whether the run keeps searching for additional distinct failures after the first.
331+
* Defaults to {@code false}: a single failure is rethrown directly (preserving its type and stack
332+
* trace, which is friendlier to debuggers); enabling this instead aggregates the distinct
333+
* failures into one report.
318334
*
319335
* @param yes whether to report multiple failures
320336
* @return a new settings instance

‎src/test/java/dev/hegel/CoverageTest.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,17 @@ void nextTestCaseNullWithNullLastErrorCompletesNormally() {
116116

117117
@Test
118118
void describeHandlesNullMessage() {
119+
// describe() is exercised only by the multiple-failures report path.
119120
FakeLibhegel fake = new FakeLibhegel();
120121
fake.finalReplay = true;
121-
run(
122+
Runner.run(
122123
fake,
124+
Settings.defaults().noDatabase().reportMultipleFailures(true),
123125
tc -> {
124126
throw new IllegalStateException(); // null message
125-
});
127+
},
128+
NO_CI,
129+
new PrintStream(new ByteArrayOutputStream(), true, StandardCharsets.UTF_8));
126130
assertEquals(Abi.STATUS_INTERESTING, fake.markedStatuses.get(0));
127131
}
128132

‎src/test/java/dev/hegel/EndToEndTest.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ void booleansAndListsDrawNativeValues() {
3939

4040
@Test
4141
void failingPropertyThrowsAssertionErrorWithCounterexample() {
42+
// By default Hegel rethrows the property's own assertion failure directly (no wrapper), so
43+
// the surfaced message is the user's, carrying the shrunk value (the engine shrinks to 11).
4244
AssertionError err =
4345
assertThrows(
4446
AssertionError.class,
@@ -51,6 +53,27 @@ void failingPropertyThrowsAssertionErrorWithCounterexample() {
5153
// Property is false for x > 10; engine should shrink toward 11.
5254
assertTrue(x <= 10, "x was too big: " + x);
5355
}));
56+
assertTrue(err.getMessage().contains("x was too big: 11"), err.getMessage());
57+
assertTrue(!err.getMessage().contains("failing example"), err.getMessage());
58+
}
59+
60+
@Test
61+
void reportMultipleFailuresProducesAggregateReport() {
62+
// Opt-in multi-failure mode wraps the result in an aggregate "Hegel found ..." report
63+
// (rather than the default direct rethrow), exercising the engine's failure-enumeration API.
64+
AssertionError err =
65+
assertThrows(
66+
AssertionError.class,
67+
() ->
68+
Hegel.with()
69+
.reportMultipleFailures(true)
70+
.seed(123)
71+
.noDatabase()
72+
.check(
73+
tc -> {
74+
int x = tc.draw(integers(0, 1000));
75+
assertTrue(x <= 10, "x too big: " + x);
76+
}));
5477
assertTrue(err.getMessage().contains("failing example"), err.getMessage());
5578
}
5679

0 commit comments

Comments
 (0)