|
| 1 | +@TestOn('vm') |
| 2 | +import 'dart:async'; |
| 3 | +import 'dart:convert'; |
| 4 | +import 'dart:io'; |
| 5 | + |
| 6 | +import 'package:test/test.dart'; |
| 7 | + |
| 8 | +// Tests for the following issue |
| 9 | +// https://github.com/getsentry/sentry-dart/issues/1893 |
| 10 | +void main() { |
| 11 | + group('Compile example_web', () { |
| 12 | + test( |
| 13 | + 'dart pub get and compilation should run successfully', |
| 14 | + () async { |
| 15 | + final result = await _runProcess('dart pub get', |
| 16 | + workingDirectory: _exampleWebWorkingDir); |
| 17 | + expect(result.exitCode, 0, |
| 18 | + reason: 'Could run `dart pub get` for example_web. ' |
| 19 | + 'Likely caused by outdated dependencies'); |
| 20 | + // running this test locally require clean working directory |
| 21 | + final cleanResult = await _runProcess('dart run build_runner clean', |
| 22 | + workingDirectory: _exampleWebWorkingDir); |
| 23 | + expect(cleanResult.exitCode, 0); |
| 24 | + final compileResult = await _runProcess( |
| 25 | + 'dart run build_runner build -r web -o build --delete-conflicting-outputs', |
| 26 | + workingDirectory: _exampleWebWorkingDir); |
| 27 | + expect(compileResult.exitCode, 0, |
| 28 | + reason: 'Could not compile example_web project'); |
| 29 | + expect( |
| 30 | + compileResult.stdout, |
| 31 | + isNot(contains( |
| 32 | + 'Skipping compiling sentry_dart_web_example|web/main.dart')), |
| 33 | + reason: |
| 34 | + 'Could not compile main.dart, likely because of dart:io import.'); |
| 35 | + expect( |
| 36 | + compileResult.stdout, |
| 37 | + contains( |
| 38 | + 'build_web_compilers:entrypoint on web/main.dart:Compiled')); |
| 39 | + }, |
| 40 | + timeout: Timeout(const Duration(minutes: 1)), // double of detault timeout |
| 41 | + ); |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +/// Runs [command] with command's stdout and stderr being forwrarded to |
| 46 | +/// test runner's respective streams. It buffers stdout and returns it. |
| 47 | +/// |
| 48 | +/// Returns [_CommandResult] with exitCode and stdout as a single sting |
| 49 | +Future<_CommandResult> _runProcess(String command, |
| 50 | + {String workingDirectory = '.'}) async { |
| 51 | + final parts = command.split(' '); |
| 52 | + assert(parts.isNotEmpty); |
| 53 | + final cmd = parts[0]; |
| 54 | + final args = parts.skip(1).toList(); |
| 55 | + final process = |
| 56 | + await Process.start(cmd, args, workingDirectory: workingDirectory); |
| 57 | + // forward standard streams |
| 58 | + unawaited(stderr.addStream(process.stderr)); |
| 59 | + final buffer = <int>[]; |
| 60 | + final stdoutCompleter = Completer.sync(); |
| 61 | + process.stdout.listen( |
| 62 | + (units) { |
| 63 | + buffer.addAll(units); |
| 64 | + stdout.add(units); |
| 65 | + }, |
| 66 | + cancelOnError: true, |
| 67 | + onDone: () { |
| 68 | + stdoutCompleter.complete(); |
| 69 | + }, |
| 70 | + ); |
| 71 | + await stdoutCompleter.future; |
| 72 | + final processOut = utf8.decode(buffer); |
| 73 | + int exitCode = await process.exitCode; |
| 74 | + return _CommandResult(exitCode: exitCode, stdout: processOut); |
| 75 | +} |
| 76 | + |
| 77 | +String get _exampleWebWorkingDir { |
| 78 | + return '${Directory.current.path}${Platform.pathSeparator}example_web'; |
| 79 | +} |
| 80 | + |
| 81 | +class _CommandResult { |
| 82 | + final int exitCode; |
| 83 | + final String stdout; |
| 84 | + |
| 85 | + const _CommandResult({required this.exitCode, required this.stdout}); |
| 86 | +} |
0 commit comments