|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:convert'; |
| 6 | +import 'dart:io'; |
| 7 | + |
| 8 | +import 'package:path/path.dart' as path; |
| 9 | + |
| 10 | +import 'customer_test.dart'; |
| 11 | + |
| 12 | +Future<bool> runTests({ |
| 13 | + int repeat = 1, |
| 14 | + bool skipOnFetchFailure = false, |
| 15 | + bool verbose = false, |
| 16 | + int numberShards = 1, |
| 17 | + int shardIndex = 0, |
| 18 | + List<File> files, |
| 19 | +}) async { |
| 20 | + if (verbose) |
| 21 | + print('Starting run_tests.dart...'); |
| 22 | + |
| 23 | + // Best attempt at evenly splitting tests among the shards |
| 24 | + final List<File> shardedFiles = <File>[]; |
| 25 | + for (int i = shardIndex; i < files.length; i += numberShards) { |
| 26 | + shardedFiles.add(files[i]); |
| 27 | + } |
| 28 | + |
| 29 | + int testCount = 0; |
| 30 | + int failures = 0; |
| 31 | + |
| 32 | + if (verbose) { |
| 33 | + final String s = files.length == 1 ? '' : 's'; |
| 34 | + final String ss = shardedFiles.length == 1 ? '' : 's'; |
| 35 | + print('${files.length} file$s specified. ${shardedFiles.length} test$ss in shard #$shardIndex.'); |
| 36 | + print(''); |
| 37 | + } |
| 38 | + |
| 39 | + if (verbose) { |
| 40 | + print('Tests in this shard:'); |
| 41 | + for (final File file in shardedFiles) |
| 42 | + print(file.path); |
| 43 | + } |
| 44 | + print(''); |
| 45 | + |
| 46 | + for (final File file in shardedFiles) { |
| 47 | + if (verbose) |
| 48 | + print('Processing ${file.path}...'); |
| 49 | + CustomerTest instructions; |
| 50 | + try { |
| 51 | + instructions = CustomerTest(file); |
| 52 | + } on FormatException catch (error) { |
| 53 | + print('ERROR: ${error.message}'); |
| 54 | + print(''); |
| 55 | + failures += 1; |
| 56 | + continue; |
| 57 | + } on FileSystemException catch (error) { |
| 58 | + print('ERROR: ${error.message}'); |
| 59 | + print(' ${file.path}'); |
| 60 | + print(''); |
| 61 | + failures += 1; |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + final Directory checkout = Directory.systemTemp.createTempSync('flutter_customer_testing.${path.basenameWithoutExtension(file.path)}.'); |
| 66 | + if (verbose) |
| 67 | + print('Created temporary directory: ${checkout.path}'); |
| 68 | + try { |
| 69 | + bool success; |
| 70 | + bool showContacts = false; |
| 71 | + for (final String fetchCommand in instructions.fetch) { |
| 72 | + success = await shell(fetchCommand, checkout, verbose: verbose, silentFailure: skipOnFetchFailure); |
| 73 | + if (!success) { |
| 74 | + if (skipOnFetchFailure) { |
| 75 | + if (verbose) { |
| 76 | + print('Skipping (fetch failed).'); |
| 77 | + } else { |
| 78 | + print('Skipping ${file.path} (fetch failed).'); |
| 79 | + } |
| 80 | + } else { |
| 81 | + print('ERROR: Failed to fetch repository.'); |
| 82 | + failures += 1; |
| 83 | + showContacts = true; |
| 84 | + } |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + assert(success != null); |
| 89 | + if (success) { |
| 90 | + if (verbose) |
| 91 | + print('Running tests...'); |
| 92 | + final Directory tests = Directory(path.join(checkout.path, 'tests')); |
| 93 | + // TODO(ianh): Once we have a way to update source code, run that command in each directory of instructions.update |
| 94 | + for (int iteration = 0; iteration < repeat; iteration += 1) { |
| 95 | + if (verbose && repeat > 1) |
| 96 | + print('Round ${iteration + 1} of $repeat.'); |
| 97 | + for (final String testCommand in instructions.tests) { |
| 98 | + testCount += 1; |
| 99 | + success = await shell(testCommand, tests, verbose: verbose); |
| 100 | + if (!success) { |
| 101 | + print('ERROR: One or more tests from ${path.basenameWithoutExtension(file.path)} failed.'); |
| 102 | + failures += 1; |
| 103 | + showContacts = true; |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + if (verbose && success) |
| 109 | + print('Tests finished.'); |
| 110 | + } |
| 111 | + if (showContacts) { |
| 112 | + final String s = instructions.contacts.length == 1 ? '' : 's'; |
| 113 | + print('Contact$s: ${instructions.contacts.join(", ")}'); |
| 114 | + } |
| 115 | + } finally { |
| 116 | + if (verbose) |
| 117 | + print('Deleting temporary directory...'); |
| 118 | + try { |
| 119 | + checkout.deleteSync(recursive: true); |
| 120 | + } on FileSystemException { |
| 121 | + print('Failed to delete "${checkout.path}".'); |
| 122 | + } |
| 123 | + } |
| 124 | + if (verbose) |
| 125 | + print(''); |
| 126 | + } |
| 127 | + if (failures > 0) { |
| 128 | + final String s = failures == 1 ? '' : 's'; |
| 129 | + print('$failures failure$s.'); |
| 130 | + return false; |
| 131 | + } |
| 132 | + print('$testCount tests all passed!'); |
| 133 | + return true; |
| 134 | +} |
| 135 | + |
| 136 | +final RegExp _spaces = RegExp(r' +'); |
| 137 | + |
| 138 | +Future<bool> shell(String command, Directory directory, { bool verbose = false, bool silentFailure = false }) async { |
| 139 | + if (verbose) |
| 140 | + print('>> $command'); |
| 141 | + Process process; |
| 142 | + if (Platform.isWindows) { |
| 143 | + process = await Process.start('CMD.EXE', <String>['/S', '/C', command], workingDirectory: directory.path); |
| 144 | + } else { |
| 145 | + final List<String> segments = command.trim().split(_spaces); |
| 146 | + process = await Process.start(segments.first, segments.skip(1).toList(), workingDirectory: directory.path); |
| 147 | + } |
| 148 | + final List<String> output = <String>[]; |
| 149 | + utf8.decoder.bind(process.stdout).transform(const LineSplitter()).listen(verbose ? printLog : output.add); |
| 150 | + utf8.decoder.bind(process.stderr).transform(const LineSplitter()).listen(verbose ? printLog : output.add); |
| 151 | + final bool success = await process.exitCode == 0; |
| 152 | + if (success || silentFailure) |
| 153 | + return success; |
| 154 | + if (!verbose) { |
| 155 | + print('>> $command'); |
| 156 | + output.forEach(printLog); |
| 157 | + } |
| 158 | + return success; |
| 159 | +} |
| 160 | + |
| 161 | +void printLog(String line) { |
| 162 | + print('| $line'.trimRight()); |
| 163 | +} |
0 commit comments