import 'dart:convert'; import 'package:flutter_test/flutter_test.dart'; import '../../../tool/memwatch/summary.dart'; const _root = '/work/app'; String _event(Map event) => jsonEncode(event); List _run({List> tests = const [], List extra = const []}) => [ _event({'type': 'suite', 'suite': {'id': 0, 'path': '$_root/test/a_test.dart'}}), for (final t in tests) ...[ _event({'type': 'testStart', 'test': {'id': t['id'], 'name': t['name'], 'suiteID': 0}}), for (final p in (t['prints'] as List? ?? const [])) _event({'type': 'print', 'testID': t['id'], 'message': p}), if (t['error'] != null) _event({'type': 'error', 'testID': t['id'], 'error': t['error'], 'stackTrace': t['stack'] ?? ''}), if (t['done'] != false) _event({ 'type': 'testDone', 'testID': t['id'], 'result': t['error'] == null ? 'success' : 'failure', 'skipped': t['skipped'] ?? false, 'hidden': t['hidden'] ?? false, }), ], ...extra, ]; String _summary(List events, {int status = 1, List logTail = const []}) => summarize( events, status: status, peakMb: 812, seconds: 9, target: 'test/a', logPath: '/tmp/memwatch/out.x', jsonPath: '/tmp/memwatch/out.x.json', logTail: logTail, appPackage: 'pipelines_app', appRoot: _root, ); void main() { test('a passing run is the verdict line and the log paths only', () { final summary = _summary( _run(tests: [ {'id': 1, 'name': 'loading $_root/test/a_test.dart', 'hidden': true}, {'id': 2, 'name': 'ok', 'prints': ['noise']}, {'id': 3, 'name': 'skip me', 'skipped': true}, ]), status: 0, ); expect(summary.split('\n'), [ 'memwatch: PASS: 1 passed, 0 failed, 1 skipped; peak 812 MB; 9s; exit 0 (test/a)', 'full output: /tmp/memwatch/out.x', 'json results: /tmp/memwatch/out.x.json', ]); }); test('a failure shows its error and app frames, not framework frames', () { final summary = _summary(_run(tests: [ { 'id': 2, 'name': 'group adds', 'error': "Expected: <2>\n Actual: <3>", 'stack': 'package:matcher expect\n' 'package:flutter_test/src/widget_tester.dart 473:18 expect\n' 'test/a_test.dart 11:5 main.\n', }, ])); expect(summary, contains('memwatch: FAIL: 0 passed, 1 failed, 0 skipped')); expect(summary, contains('FAILED test/a_test.dart: group adds')); expect(summary, contains('Actual: <3>')); expect(summary, contains('test/a_test.dart 11:5')); expect(summary, isNot(contains('package:matcher'))); expect(summary, isNot(contains('widget_tester.dart'))); }); test('a flooding failure is bounded and keeps the exception before prints', () { final flood = [for (var i = 0; i < 5000; i++) 'flood $i ${'y' * 500}']; final exception = [ '══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞══', 'The following TestFailure was thrown running a test:', 'Expected: exactly one matching candidate', for (var i = 0; i < 200; i++) '#$i frame (package:flutter/src/widgets/framework.dart:$i:1)', '#201 main. (file://$_root/test/a_test.dart:20:5)', ].join('\n'); final summary = _summary(_run(tests: [ for (var id = 2; id < 52; id++) {'id': id, 'name': 'fails $id', 'prints': [...flood, exception], 'error': 'Test failed. See exception logs above.'}, ])); expect(summary.length, lessThan(maxDetailChars + 4000)); expect(summary, contains('Expected: exactly one matching candidate')); expect(summary, contains('(test/a_test.dart:20:5)')); expect(summary, isNot(contains('framework.dart'))); expect(summary, contains('printed output (last 8 of 5000 lines):')); expect(summary, contains('... and 20 more failures (see full log)')); expect(summary.indexOf('EXCEPTION CAUGHT'), lessThan(summary.indexOf('printed output'))); }); test('a killed run names the tests still running', () { final summary = _summary( _run(tests: [ {'id': 2, 'name': 'finished'}, {'id': 3, 'name': 'runaway', 'done': false}, ]), status: 137, ); expect(summary, startsWith('memwatch: KILLED: 1 passed, 0 failed')); expect(summary, contains('RUNNING when killed: test/a_test.dart: runaway')); }); test('a run without results shows the bounded log tail', () { final summary = _summary(const [], logTail: ['Error: no test files', 'x' * 1000]); expect(summary, contains('no test results were recorded; last log lines:')); expect(summary, contains(' Error: no test files')); expect(summary.length, lessThan(800)); }); }