From be99a04dacc2ac77b445aff54df225c4cf951076 Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Wed, 8 Feb 2017 10:05:36 -0800 Subject: [PATCH] Make bot tests portable to also run on Windows (#7954) --- .travis.yml | 5 +- dev/bots/pubspec.yaml | 5 ++ dev/bots/setup.sh | 16 +--- dev/bots/test.dart | 153 ++++++++++++++++++++++++++++++++++++++ dev/bots/test.sh | 72 +----------------- dev/bots/travis_setup.sh | 17 +++++ dev/bots/travis_upload.sh | 19 +++++ 7 files changed, 201 insertions(+), 86 deletions(-) create mode 100644 dev/bots/pubspec.yaml create mode 100644 dev/bots/test.dart create mode 100755 dev/bots/travis_setup.sh create mode 100755 dev/bots/travis_upload.sh diff --git a/.travis.yml b/.travis.yml index 358e8954a0..be1a44b271 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,9 +11,10 @@ install: - gem install coveralls-lcov - npm install -g firebase-tools@">=3.0.4 <3.1.0" before_script: - - ./dev/bots/setup.sh + - ./dev/bots/travis_setup.sh script: - - ./dev/bots/test.sh + - ./bin/cache/dart-sdk/bin/dart ./dev/bots/test.dart + - ./dev/bots/travis_upload.sh after_success: - (cd packages/flutter && coveralls-lcov coverage/lcov.info) cache: diff --git a/dev/bots/pubspec.yaml b/dev/bots/pubspec.yaml new file mode 100644 index 0000000000..cec42d5f8e --- /dev/null +++ b/dev/bots/pubspec.yaml @@ -0,0 +1,5 @@ +name: tests_on_bots +description: Script to run all tests on bots. + +dependencies: + path: ^1.4.0 diff --git a/dev/bots/setup.sh b/dev/bots/setup.sh index 89a30db5b8..561b369fe5 100755 --- a/dev/bots/setup.sh +++ b/dev/bots/setup.sh @@ -1,17 +1,5 @@ #!/bin/bash -set -e -echo $KEY_FILE | base64 --decode > ../gcloud_key_file.json +# TODO(goderbauer): delete this when all callsites are updated to travis_setup.sh. -set -x - -if [ -n "$TRAVIS" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then - export CLOUDSDK_CORE_DISABLE_PROMPTS=1 - curl https://sdk.cloud.google.com | bash -fi - -# disable analytics on the bots and download Flutter dependencies -./bin/flutter config --no-analytics - -# run pub get in all the repo packages -./bin/flutter update-packages +./dev/bots/travis_setup.sh diff --git a/dev/bots/test.dart b/dev/bots/test.dart new file mode 100644 index 0000000000..2e52f1a1d4 --- /dev/null +++ b/dev/bots/test.dart @@ -0,0 +1,153 @@ +import 'dart:io'; +import 'dart:async'; + +import 'package:path/path.dart' as p; + +String flutterRoot = p.dirname(p.dirname(p.dirname(p.fromUri(Platform.script)))); +String flutter = p.join(flutterRoot, 'bin', Platform.isWindows ? 'flutter.bat' : 'flutter'); +String dart = p.join(flutterRoot, 'bin', 'cache', 'dart-sdk', 'bin', Platform.isWindows ? 'dart.exe' : 'dart'); +String flutterTestArgs = Platform.environment['FLUTTER_TEST_ARGS']; + +/// When you call this, you can set FLUTTER_TEST_ARGS to pass custom +/// arguments to flutter test. For example, you might want to call this +/// script using FLUTTER_TEST_ARGS=--local-engine=host_debug_unopt to +/// use your own build of the engine. +Future main() async { + // Analyze all the Dart code in the repo. + await _runFlutterAnalyze(flutterRoot, + options: ['--flutter-repo'], + ); + + // Verify that the tests actually return failure on failure and success on success. + String automatedTests = p.join(flutterRoot, 'dev', 'automated_tests'); + await _runFlutterTest(automatedTests, + script: p.join('test_smoke_test', 'fail_test.dart'), + expectFailure: true, + printOutput: false, + ); + await _runFlutterTest(automatedTests, + script: p.join('test_smoke_test', 'pass_test.dart'), + printOutput: false, + ); + await _runFlutterTest(automatedTests, + script: p.join('test_smoke_test', 'crash1_test.dart'), + expectFailure: true, + printOutput: false, + ); + await _runFlutterTest(automatedTests, + script: p.join('test_smoke_test', 'crash2_test.dart'), + expectFailure: true, + printOutput: false + ); + await _runFlutterTest(automatedTests, + script: p.join('test_smoke_test', 'syntax_error_test.broken_dart'), + expectFailure: true, + printOutput: false, + ); + await _runFlutterTest(automatedTests, + script: p.join('test_smoke_test', 'missing_import_test.broken_dart'), + expectFailure: true, + printOutput: false, + ); + await _runCmd(flutter, ['drive', '--use-existing-app', '-t', p.join('test_driver', 'failure.dart')], + workingDirectory: p.join(flutterRoot, 'packages', 'flutter_driver'), + expectFailure: true, + printOutput: false, + ); + + List coverageFlags = []; + if (Platform.environment['TRAVIS'] != null && Platform.environment['TRAVIS_PULL_REQUEST'] == 'false') + coverageFlags.add('--coverage'); + + // Run tests. + await _runFlutterTest(p.join(flutterRoot, 'packages', 'flutter'), + options: coverageFlags, + ); + await _runAllDartTests(p.join(flutterRoot, 'packages', 'flutter_driver')); + await _runFlutterTest(p.join(flutterRoot, 'packages', 'flutter_test')); + await _runAllDartTests(p.join(flutterRoot, 'packages', 'flutter_tools'), + environment: { 'FLUTTER_ROOT': flutterRoot }, + ); + + await _runAllDartTests(p.join(flutterRoot, 'dev', 'devicelab')); + await _runFlutterTest(p.join(flutterRoot, 'dev', 'manual_tests')); + await _runFlutterTest(p.join(flutterRoot, 'examples', 'hello_world')); + await _runFlutterTest(p.join(flutterRoot, 'examples', 'layers')); + await _runFlutterTest(p.join(flutterRoot, 'examples', 'stocks')); + await _runFlutterTest(p.join(flutterRoot, 'examples', 'flutter_gallery')); + + await _runCmd(dart, [p.join(flutterRoot, 'dev', 'tools', 'mega_gallery.dart')], + workingDirectory: flutterRoot, + ); + await _runFlutterAnalyze(p.join(flutterRoot, 'dev', 'benchmarks', 'mega_gallery'), + options: ['--watch', '--benchmark'], + ); + + print('\x1B[32mDONE: All tests successful.\x1B[0m'); +} + +Future _runCmd(String executable, List arguments, { + String workingDirectory, + Map environment, + bool expectFailure: false, + bool printOutput: true, +}) async { + String cmd = '${p.relative(executable)} ${arguments.join(' ')}'; + print('>>> RUNNING in \x1B[34m${p.relative(workingDirectory)}\x1B[0m: \x1B[33m$cmd\x1B[0m'); + + Process process = await Process.start(executable, arguments, + workingDirectory: workingDirectory, + environment: environment, + ); + + if (printOutput) { + stdout.addStream(process.stdout); + stderr.addStream(process.stderr); + } + + int exitCode = await process.exitCode; + if ((exitCode == 0) == expectFailure) { + print( + '\x1B[31m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1B[0m\n' + '\x1B[1mERROR:\x1B[31m Last command exited with $exitCode (expected: ${expectFailure ? 'non-zero' : 'zero'}).\x1B[0m\n' + '\x1B[31m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1B[0m' + ); + exit(1); + } +} + +Future _runFlutterTest(String workingDirectory, { + String script, + bool expectFailure: false, + bool printOutput: true, + List options: const [], +}) { + List args = ['test']..addAll(options); + if (flutterTestArgs != null) + args.add(flutterTestArgs); + if (script != null) + args.add(script); + return _runCmd(flutter, args, + workingDirectory: workingDirectory, + expectFailure: expectFailure, + printOutput: printOutput, + ); +} + +Future _runAllDartTests(String workingDirectory, { + Map environment, +}) { + List args = ['--checked', p.join('test', 'all.dart')]; + return _runCmd(dart, args, + workingDirectory: workingDirectory, + environment: environment, + ); +} + +Future _runFlutterAnalyze(String workingDirectory, { + List options: const [] +}) { + return _runCmd(flutter, ['analyze']..addAll(options), + workingDirectory: workingDirectory, + ); +} diff --git a/dev/bots/test.sh b/dev/bots/test.sh index 860e4ee69e..c91a2ecdac 100755 --- a/dev/bots/test.sh +++ b/dev/bots/test.sh @@ -1,73 +1,5 @@ #!/bin/bash -# When you call this, you can set FLUTTER_TEST_ARGS to pass custom -# arguments to flutter test. For example, you might want to call this -# script using FLUTTER_TEST_ARGS=--local-engine=host_debug_unopt to -# use your own build of the engine. -# -# On Travis, this script additionally collects coverage and uploads -# the coverage and the generated documentation to the cloud. +# TODO(goderbauer): delete this when all callsites are updated to test.dart. -export PATH="$PWD/bin:$PWD/bin/cache/dart-sdk/bin:$PATH" - -trap detect_error_on_exit EXIT HUP INT QUIT TERM - -detect_error_on_exit() { - exit_code=$? - { set +x; } 2>/dev/null - if [[ $exit_code -ne 0 ]]; then - echo -e "\x1B[31m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1B[0m" - echo -e "\x1B[1mError:\x1B[31m script exited early due to error ($exit_code)\x1B[0m" - echo -e "\x1B[31m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1B[0m" - fi -} - -set -ex - -# analyze all the Dart code in the repo -flutter analyze --flutter-repo - -# verify that the tests actually return failure on failure and success on success -(cd dev/automated_tests; ! flutter test $FLUTTER_TEST_ARGS test_smoke_test/fail_test.dart > /dev/null) -(cd dev/automated_tests; flutter test $FLUTTER_TEST_ARGS test_smoke_test/pass_test.dart > /dev/null) -(cd dev/automated_tests; ! flutter test $FLUTTER_TEST_ARGS test_smoke_test/crash1_test.dart > /dev/null) -(cd dev/automated_tests; ! flutter test $FLUTTER_TEST_ARGS test_smoke_test/crash2_test.dart > /dev/null) -(cd dev/automated_tests; ! flutter test $FLUTTER_TEST_ARGS test_smoke_test/syntax_error_test.broken_dart > /dev/null) -(cd dev/automated_tests; ! flutter test $FLUTTER_TEST_ARGS test_smoke_test/missing_import_test.broken_dart > /dev/null) -(cd packages/flutter_driver; ! flutter drive --use-existing-app -t test_driver/failure.dart >/dev/null 2>&1) - -COVERAGE_FLAG= -if [ -n "$TRAVIS" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then - COVERAGE_FLAG=--coverage -fi - -SRC_ROOT=$PWD - -# run tests -(cd packages/flutter; flutter test $FLUTTER_TEST_ARGS $COVERAGE_FLAG) -(cd packages/flutter_driver; dart -c test/all.dart) -(cd packages/flutter_test; flutter test) -(cd packages/flutter_tools; FLUTTER_ROOT=$SRC_ROOT dart -c test/all.dart) - -(cd dev/devicelab; dart -c test/all.dart) -(cd dev/manual_tests; flutter test) -(cd examples/hello_world; flutter test) -(cd examples/layers; flutter test) -(cd examples/stocks; flutter test) -(cd examples/flutter_gallery; flutter test) - -# generate and analyze our large sample app -dart dev/tools/mega_gallery.dart -(cd dev/benchmarks/mega_gallery; flutter analyze --watch --benchmark) - -if [ -n "$COVERAGE_FLAG" ]; then - GSUTIL=$HOME/google-cloud-sdk/bin/gsutil - GCLOUD=$HOME/google-cloud-sdk/bin/gcloud - - $GCLOUD auth activate-service-account --key-file ../gcloud_key_file.json - STORAGE_URL=gs://flutter_infra/flutter/coverage/lcov.info - $GSUTIL cp packages/flutter/coverage/lcov.info $STORAGE_URL -fi - -# generate the API docs, upload them -dev/bots/docs.sh +./bin/cache/dart-sdk/bin/dart ./dev/bots/test.dart diff --git a/dev/bots/travis_setup.sh b/dev/bots/travis_setup.sh new file mode 100755 index 0000000000..89a30db5b8 --- /dev/null +++ b/dev/bots/travis_setup.sh @@ -0,0 +1,17 @@ +#!/bin/bash +set -e + +echo $KEY_FILE | base64 --decode > ../gcloud_key_file.json + +set -x + +if [ -n "$TRAVIS" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then + export CLOUDSDK_CORE_DISABLE_PROMPTS=1 + curl https://sdk.cloud.google.com | bash +fi + +# disable analytics on the bots and download Flutter dependencies +./bin/flutter config --no-analytics + +# run pub get in all the repo packages +./bin/flutter update-packages diff --git a/dev/bots/travis_upload.sh b/dev/bots/travis_upload.sh new file mode 100755 index 0000000000..0231e23f4d --- /dev/null +++ b/dev/bots/travis_upload.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +set -ex + +export PATH="$PWD/bin:$PWD/bin/cache/dart-sdk/bin:$PATH" + +LCOV_FILE=./packages/flutter/coverage/lcov.info + +if [ -n "$TRAVIS" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ -a "$LCOV_FILE" ]; then + GSUTIL=$HOME/google-cloud-sdk/bin/gsutil + GCLOUD=$HOME/google-cloud-sdk/bin/gcloud + + $GCLOUD auth activate-service-account --key-file ../gcloud_key_file.json + STORAGE_URL=gs://flutter_infra/flutter/coverage/lcov.info + $GSUTIL cp $LCOV_FILE $STORAGE_URL +fi + +# generate the API docs, upload them +./dev/bots/docs.sh