OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 |
| 5 library test.kernel.closures.suite; |
| 6 |
| 7 import 'dart:async' show |
| 8 Future; |
| 9 |
| 10 import 'dart:io' show |
| 11 Directory, |
| 12 File, |
| 13 Platform; |
| 14 |
| 15 import 'package:analyzer/src/generated/sdk.dart' show |
| 16 DartSdk; |
| 17 |
| 18 import 'package:kernel/analyzer/loader.dart' show |
| 19 DartLoader, |
| 20 DartOptions, |
| 21 createDartSdk; |
| 22 |
| 23 import 'package:kernel/target/targets.dart' show |
| 24 Target, |
| 25 TargetFlags, |
| 26 getTarget; |
| 27 |
| 28 import 'package:kernel/repository.dart' show |
| 29 Repository; |
| 30 |
| 31 import 'package:kernel/testing/kernel_chain.dart' show |
| 32 MatchExpectation, |
| 33 Print, |
| 34 ReadDill, |
| 35 SanityCheck, |
| 36 WriteDill; |
| 37 |
| 38 import 'package:testing/testing.dart' show |
| 39 Chain, |
| 40 ChainContext, |
| 41 Result, |
| 42 StdioProcess, |
| 43 Step, |
| 44 TestDescription, |
| 45 runMe; |
| 46 |
| 47 import 'package:kernel/ast.dart' show |
| 48 Program; |
| 49 |
| 50 import 'package:kernel/transformations/closure_conversion.dart' as |
| 51 closure_conversion; |
| 52 |
| 53 import 'package:package_config/discovery.dart' show |
| 54 loadPackagesFile; |
| 55 |
| 56 class TestContext extends ChainContext { |
| 57 final Uri vm; |
| 58 |
| 59 final Uri packages; |
| 60 |
| 61 final DartOptions options; |
| 62 |
| 63 final DartSdk dartSdk; |
| 64 |
| 65 final List<Step> steps; |
| 66 |
| 67 TestContext(String sdk, this.vm, Uri packages, bool strongMode, |
| 68 this.dartSdk, bool updateExpectations) |
| 69 : packages = packages, |
| 70 options = new DartOptions(strongMode: strongMode, sdk: sdk, |
| 71 packagePath: packages.toFilePath()), |
| 72 steps = <Step>[ |
| 73 const Kernel(), |
| 74 const Print(), |
| 75 const SanityCheck(), |
| 76 const ClosureConversion(), |
| 77 const Print(), |
| 78 const SanityCheck(), |
| 79 new MatchExpectation( |
| 80 ".expect", updateExpectations: updateExpectations), |
| 81 const WriteDill(), |
| 82 const ReadDill(), |
| 83 const Run(), |
| 84 ]; |
| 85 |
| 86 Future<DartLoader> createLoader() async { |
| 87 Repository repository = new Repository(); |
| 88 return new DartLoader(repository, options, await loadPackagesFile(packages), |
| 89 dartSdk: dartSdk); |
| 90 } |
| 91 } |
| 92 |
| 93 enum Environment { |
| 94 directory, |
| 95 file, |
| 96 } |
| 97 |
| 98 Future<String> getEnvironmentVariable( |
| 99 String name, Environment kind, String undefined, notFound(String n)) async { |
| 100 String result = Platform.environment[name]; |
| 101 if (result == null) { |
| 102 throw undefined; |
| 103 } |
| 104 switch (kind) { |
| 105 case Environment.directory: |
| 106 if (!await new Directory(result).exists()) throw notFound(result); |
| 107 break; |
| 108 |
| 109 case Environment.file: |
| 110 if (!await new File(result).exists()) throw notFound(result); |
| 111 break; |
| 112 } |
| 113 return result; |
| 114 } |
| 115 |
| 116 Future<bool> fileExists(Uri base, String path) async { |
| 117 return await new File.fromUri(base.resolve(path)).exists(); |
| 118 } |
| 119 |
| 120 Future<TestContext> createContext( |
| 121 Chain suite, Map<String, String> environment) async { |
| 122 const String suggestion = """Try building the patched SDK by running |
| 123 'tools/build.py patched_sdk'"""; |
| 124 |
| 125 // TODO(karlklose): The path is different on MacOS. |
| 126 String sdk = "out/DebugX64/patched_sdk/"; |
| 127 Uri sdkUri = Uri.base.resolve(sdk); |
| 128 const String asyncDart = "lib/async/async.dart"; |
| 129 if (!await fileExists(sdkUri, asyncDart)) { |
| 130 throw "Couldn't find the patched SDK. $suggestion"; |
| 131 } |
| 132 const String asyncSources = "lib/async/async_sources.gypi"; |
| 133 if (await fileExists(sdkUri, asyncSources)) { |
| 134 throw "Found '$asyncSources' in '$sdk', so it isn't a patched SDK. " |
| 135 "$suggestion"; |
| 136 } |
| 137 |
| 138 // TODO(karlklose): select the VM based on the mode. |
| 139 Uri vm = Uri.base.resolve("out/ReleaseX64/dart"); |
| 140 |
| 141 Uri packages = Uri.base.resolve(".packages"); |
| 142 bool strongMode = false; |
| 143 bool updateExpectations = environment["updateExpectations"] != "false"; |
| 144 return new TestContext(sdk, vm, packages, strongMode, |
| 145 createDartSdk(sdk, strongMode: strongMode), updateExpectations); |
| 146 } |
| 147 |
| 148 class Kernel extends Step<TestDescription, Program, TestContext> { |
| 149 const Kernel(); |
| 150 |
| 151 String get name => "kernel"; |
| 152 |
| 153 Future<Result<Program>> run( |
| 154 TestDescription description, TestContext testContext) async { |
| 155 try { |
| 156 DartLoader loader = await testContext.createLoader(); |
| 157 Target target = getTarget("vm", |
| 158 new TargetFlags(strongMode: testContext.options.strongMode)); |
| 159 String path = description.file.path; |
| 160 Uri uri = Uri.base.resolve(path); |
| 161 Program program = loader.loadProgram(uri, target: target); |
| 162 for (var error in loader.errors) { |
| 163 return fail(program, "$error"); |
| 164 } |
| 165 target.transformProgram(program); |
| 166 return pass(program); |
| 167 } catch (e, s) { |
| 168 return crash(e, s); |
| 169 } |
| 170 } |
| 171 } |
| 172 |
| 173 class ClosureConversion extends Step<Program, Program, TestContext> { |
| 174 const ClosureConversion(); |
| 175 |
| 176 String get name => "closure conversion"; |
| 177 |
| 178 Future<Result<Program>> run(Program program, TestContext testContext) async { |
| 179 try { |
| 180 program = closure_conversion.transformProgram(program); |
| 181 return pass(program); |
| 182 } catch (e, s) { |
| 183 return crash(e, s); |
| 184 } |
| 185 } |
| 186 } |
| 187 |
| 188 class Run extends Step<Uri, int, TestContext> { |
| 189 const Run(); |
| 190 |
| 191 String get name => "run"; |
| 192 |
| 193 Future<Result<int>> run(Uri uri, TestContext context) async { |
| 194 File generated = new File.fromUri(uri); |
| 195 StdioProcess process; |
| 196 try { |
| 197 process = await StdioProcess.run( |
| 198 context.vm.toFilePath(), [generated.path, "Hello, World!"]); |
| 199 print(process.output); |
| 200 } finally { |
| 201 generated.parent.delete(recursive: true); |
| 202 } |
| 203 return process.toResult(); |
| 204 } |
| 205 } |
| 206 |
| 207 main(List<String> arguments) => runMe(arguments, createContext, "testing.json"); |
OLD | NEW |