Skip to content

Commit 84a9d66

Browse files
committed
test(dart-sentry): add compilation tests for example_web
Added tests to check if example_web project can be compiled with build_runner. Resolves #1893
1 parent f66a7d5 commit 84a9d66

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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('dart pub get should run successfully', () async {
13+
final result = await _runProcess('dart pub get',
14+
workingDirectory: _exampleWebWorkingDir);
15+
expect(result.exitCode, 0,
16+
reason: 'Could run `dart pub get` for example_web. '
17+
'Likely caused by outdated dependencies');
18+
});
19+
test('dart run build_runner build should run successfully', () async {
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 result = await _runProcess(
25+
'dart run build_runner build -r web -o build --delete-conflicting-outputs',
26+
workingDirectory: _exampleWebWorkingDir);
27+
expect(result.exitCode, 0,
28+
reason: 'Could not compile example_web project');
29+
expect(
30+
result.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(result.stdout,
36+
contains('build_web_compilers:entrypoint on web/main.dart:Compiled'));
37+
});
38+
});
39+
}
40+
41+
/// Runs [command] with command's stdout and stderr being forwrarded to
42+
/// test runner's respective streams. It buffers stdout and returns it.
43+
///
44+
/// Returns [_CommandResult] with exitCode and stdout as a single sting
45+
Future<_CommandResult> _runProcess(String command,
46+
{String workingDirectory = '.'}) async {
47+
final parts = command.split(' ');
48+
assert(parts.isNotEmpty);
49+
final cmd = parts[0];
50+
final args = parts.skip(1).toList();
51+
final process =
52+
await Process.start(cmd, args, workingDirectory: workingDirectory);
53+
// forward standard streams
54+
unawaited(stderr.addStream(process.stderr));
55+
final buffer = <int>[];
56+
await for (final units in process.stdout) {
57+
buffer.addAll(units);
58+
stdout.add(units);
59+
}
60+
final processOut = utf8.decode(buffer);
61+
int exitCode = await process.exitCode;
62+
return _CommandResult(exitCode: exitCode, stdout: processOut);
63+
}
64+
65+
String get _exampleWebWorkingDir {
66+
return Directory.current.uri.resolve('./example_web').normalizePath().path;
67+
}
68+
69+
class _CommandResult {
70+
final int exitCode;
71+
final String stdout;
72+
73+
const _CommandResult({required this.exitCode, required this.stdout});
74+
}

0 commit comments

Comments
 (0)