Skip to content

Commit

Permalink
[pigeon] Started transitioning to run_tests.dart / added a windows su…
Browse files Browse the repository at this point in the history
…pport (#519)
  • Loading branch information
gaaclarke authored Nov 23, 2021
1 parent 15c595c commit 37a0f92
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## NEXT

* [ci] Started transitioning to a Dart test runner, added windows support.

## 1.0.10

* [front-end] Made sure that explicit use of Object actually creates the codec
Expand Down
194 changes: 194 additions & 0 deletions packages/pigeon/bin/run_tests.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

////////////////////////////////////////////////////////////////////////////////
/// Script for executing the Pigeon tests
///
/// This currently only supports Windows tests.
///
/// usage: pub run pigeon:run_tests
////////////////////////////////////////////////////////////////////////////////
import 'dart:io' show Process, exit, stdout, stderr;
import 'package:args/args.dart';
import 'package:meta/meta.dart';
import 'package:pigeon/functional.dart';

const String _testFlag = 'test';
const String _listFlag = 'list';

@immutable
class _TestInfo {
const _TestInfo({required this.function, this.description});
final Future<int> Function() function;
final String? description;
}

const Map<String, _TestInfo> _tests = <String, _TestInfo>{
'windows_unittests': _TestInfo(
function: _runWindowsUnitTests,
description: 'Unit tests on generated Windows C++ code.'),
'android_unittests': _TestInfo(
function: _runAndroidUnitTests,
description: 'Unit tests on generated Java code.'),
'dart_compilation_tests': _TestInfo(
function: _runDartCompilationTests,
description: 'Compilation tests on generated Dart code.'),
'dart_unittests': _TestInfo(
function: _runDartUnitTests,
description: 'Unit tests on and analysis on Pigeon\'s implementation.'),
'flutter_unittests': _TestInfo(
function: _runFlutterUnitTests,
description: 'Unit tests on generated Dart code.'),
'ios_e2e_tests': _TestInfo(
function: _runIosE2eTests,
description: 'End-to-end Objective-C tests run on iOS Simulator'),
'ios_unittests': _TestInfo(
function: _runIosUnitTests,
description: 'Unit tests on generated Objective-C code.'),
'mock_handler_tests': _TestInfo(
function: _runMockHandlerTests,
description: 'Unit tests on generated Dart mock handler code.'),
};

Future<Process> _streamOutput(Future<Process> processFuture) async {
final Process process = await processFuture;
stdout.addStream(process.stdout);
stderr.addStream(process.stderr);
return process;
}

Future<int> _runAndroidUnitTests() async {
throw UnimplementedError('See run_tests.sh.');
}

Future<int> _runDartCompilationTests() async {
throw UnimplementedError('See run_tests.sh.');
}

Future<int> _runDartUnitTests() async {
throw UnimplementedError('See run_tests.sh.');
}

Future<int> _runFlutterUnitTests() async {
throw UnimplementedError('See run_tests.sh.');
}

Future<int> _runIosE2eTests() async {
throw UnimplementedError('See run_tests.sh.');
}

Future<int> _runIosUnitTests() async {
throw UnimplementedError('See run_tests.sh.');
}

Future<int> _runMockHandlerTests() async {
throw UnimplementedError('See run_tests.sh.');
}

Future<int> _runPigeon({
required String input,
String? cppHeaderOut,
String? cppSourceOut,
}) async {
const bool hasDart = false;
final List<String> args = <String>[
'pub',
'run',
'pigeon',
'--input',
input,
];
if (cppHeaderOut != null) {
args.addAll(<String>[
'--objc_header_out', // TODO(gaaclarke): Switch to c++.
cppHeaderOut,
]);
}
if (cppSourceOut != null) {
args.addAll(<String>[
'--objc_source_out', // TODO(gaaclarke): Switch to c++.
cppSourceOut,
]);
}
if (!hasDart) {
args.add('--one_language');
}
final Process generate = await _streamOutput(Process.start('dart', args));
final int generateCode = await generate.exitCode;
if (generateCode != 0) {
return generateCode;
}
return 0;
}

Future<int> _runWindowsUnitTests() async {
const String windowsUnitTestsPath = './platform_tests/windows_unit_tests';
final int generateCode = await _runPigeon(
input: './pigeons/message.dart',
cppHeaderOut: '$windowsUnitTestsPath/windows/test/message.g.h',
cppSourceOut: '$windowsUnitTestsPath/windows/test/message.g.cpp',
);
if (generateCode != 0) {
return generateCode;
}

final Process compile = await _streamOutput(Process.start(
'flutter', <String>['build', 'windows', '--debug'],
workingDirectory: '$windowsUnitTestsPath/example', runInShell: true));
final int compileCode = await compile.exitCode;
if (compileCode != 0) {
return compileCode;
}

final Process run = await _streamOutput(Process.start(
'$windowsUnitTestsPath/example/build/windows/plugins/windows_unit_tests/Debug/windows_unit_tests_test.exe',
<String>[]));

return run.exitCode;
}

Future<void> main(List<String> args) async {
final ArgParser parser = ArgParser()
..addOption(_testFlag, abbr: 't', help: 'Only run specified test.')
..addFlag(_listFlag,
negatable: false, abbr: 'l', help: 'List available tests.')
..addFlag('help',
negatable: false, abbr: 'h', help: 'Print this reference.');

final ArgResults argResults = parser.parse(args);
List<String> testsToRun = _tests.keys.toList();
if (argResults.wasParsed(_listFlag)) {
print('available tests:');
for (final MapEntry<String, _TestInfo> info in _tests.entries) {
final int tabCount = (4 - info.key.length / 8).toInt();
final String tabs = repeat('\t', tabCount).join('');
print('${info.key}$tabs- ${info.value.description}');
}
exit(0);
} else if (argResults.wasParsed('help')) {
print('''
Pigeon run_tests
usage: pub run pigeon:run_tests [-l | -t <test name>]
${parser.usage}''');
exit(0);
} else if (argResults.wasParsed(_testFlag)) {
testsToRun = <String>[argResults[_testFlag]];
}

for (final String test in testsToRun) {
final _TestInfo? info = _tests[test];
if (info != null) {
print('# Running $test');
final int testCode = await info.function();
if (testCode != 0) {
exit(testCode);
}
} else {
print('unknown test: $test');
exit(1);
}
}
exit(0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 30
compileSdkVersion 31

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.6.0'
repositories {
google()
jcenter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ FetchContent_MakeAvailable(googletest)
# directly into the test binary rather than using the DLL.
add_executable(${TEST_RUNNER}
test/pigeon_test.cpp
# TODO(gaaclarke): Add test/message.g.cpp.
# TODO(gaaclarke): Add test/message.g.h.
${PLUGIN_SOURCES}
)
apply_standard_settings(${TEST_RUNNER})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.g.h
*.g.cpp

0 comments on commit 37a0f92

Please sign in to comment.