blob: 8de4fe60129676d05a0623ac9e9d8fe152f8aa77 [file] [log] [blame]
#!/usr/bin/env python
# Copyright 2018 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import argparse
import os
import re
import sys
def main():
parser = argparse.ArgumentParser(
sys.argv[0], description="Generate main file for Fuchsia dart test")
parser.add_argument(
"--out", help="Path to .dart file to generate", required=True)
parser.add_argument(
"--main-package", help="The main package", required=True)
parser.add_argument(
"--main", help="The name of the main file", required=True)
args = parser.parse_args()
print(args.out)
outfile = open(args.out, 'w')
# Ignores relative lib imports due to a few modules that complain about
# this. It is also possible that a future may be unawaited given that main
# may not always be synchronous across all functions.
outfile.write('''// Generated by ''')
outfile.write(os.path.basename(__file__))
outfile.write(
'''
// ignore_for_file: avoid_relative_lib_imports
import 'dart:async';
import 'package:flutter_driver/driver_extension.dart';
import 'package:flutter/services.dart';
''')
outfile.write(
"import 'package:%s/%s' as flutter_app_main;\n" %
(args.main_package, args.main))
outfile.write(
'''
void main(List<String> args) async {
// TODO(awdavies): Use the logger instead.
print('Overriding app main method because flutter_driver_extendable '
'is enabled in the build file');
try {
// Enables Flutter Driver VM service extension
//
// This extension is required for tests that use package:flutter_driver
// to drive applications from a separate process.
final handler = OptionalMethodChannel('flutter_driver/handler');
enableFlutterDriverExtension(handler: (String data) async {
return handler.invokeMethod(data);
});
// TODO(awdavies): Use the logger instead.
print('flutter driver extensions enabled.');
//ignore: avoid_catches_without_on_clauses
} catch (e) {
// TODO(awdavies): Use the logger instead.
// Noop.
print('flutter driver extensions not enabled. $e');
}
// Execute the main method of the app under test
try {
var res = (flutter_app_main.main as dynamic)(args);
if (res != null && res is Future) {
await res;
}
} on NoSuchMethodError catch(_) {
var res = (flutter_app_main.main as dynamic)();
if (res != null && res is Future) {
await res;
}
}
}
''')
outfile.close()
if __name__ == '__main__':
main()