D4rtTestResult.fromTestProcess constructor

D4rtTestResult.fromTestProcess({
  1. required int exitCode,
  2. required String stdout,
  3. required String stderr,
  4. bool timedOut = false,
})

Parse a D4rtTestResult from the process output of a test runner.

The test runner's --test mode outputs a JSON line prefixed with ###D4RT_TEST_RESULT###. This factory parses that structured output. If no structured marker is found, raw stdout/stderr are used instead.

Implementation

factory D4rtTestResult.fromTestProcess({
  required int exitCode,
  required String stdout,
  required String stderr,
  bool timedOut = false,
}) {
  // Try to parse structured JSON output from --test mode.
  // The test runner outputs a JSON line starting with the marker.
  const marker = '###D4RT_TEST_RESULT###';
  final markerIndex = stdout.indexOf(marker);

  if (markerIndex >= 0) {
    final jsonStr = stdout.substring(markerIndex + marker.length).trim();
    try {
      final json = jsonDecode(jsonStr) as Map<String, dynamic>;
      final capturedOutput = json['output'] as String? ?? '';
      final capturedExceptions = (json['exceptions'] as List<dynamic>?)
              ?.map((e) => e.toString())
              .toList() ??
          [];

      return D4rtTestResult(
        timedOut: timedOut,
        exceptions: capturedExceptions,
        processOutput: capturedOutput,
        processError: stderr,
        exitCode: timedOut ? -1 : exitCode,
      );
    } catch (_) {
      // Fall through to raw output parsing
    }
  }

  // No structured output — treat stdout/stderr as raw output.
  return D4rtTestResult(
    timedOut: timedOut,
    exceptions: exitCode != 0 && !timedOut
        ? [stderr.isNotEmpty ? stderr : 'Process exited with code $exitCode']
        : [],
    processOutput: stdout,
    processError: stderr,
    exitCode: timedOut ? -1 : exitCode,
  );
}