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