| OLD | NEW |
| 1 // Copyright (c) 2017, 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 /// Tests basic functionality of the API tree-shaker. | 5 /// Tests basic functionality of the API tree-shaker. |
| 6 /// | 6 /// |
| 7 /// Each input file is built and tree-shaken, then we check that the set of | 7 /// Each input file is built and tree-shaken, then we check that the set of |
| 8 /// libraries, classes, and members that are retained match those declared in an | 8 /// libraries, classes, and members that are retained match those declared in an |
| 9 /// expectations file. | 9 /// expectations file. |
| 10 /// | 10 /// |
| 11 /// Input files may contain markers to turn on flags that configure this | 11 /// Input files may contain markers to turn on flags that configure this |
| 12 /// runner. Currently only the following marker is recognized: | 12 /// runner. Currently only the following marker is recognized: |
| 13 /// @@SHOW_CORE_LIBRARIES@@ - whether to check for retained information from | 13 /// @@SHOW_CORE_LIBRARIES@@ - whether to check for retained information from |
| 14 /// the core libraries. By default this runner only checks for members of | 14 /// the core libraries. By default this runner only checks for members of |
| 15 /// pkg/front_end/testcases/shaker/lib/lib.dart. | 15 /// pkg/front_end/testcases/shaker/lib/lib.dart. |
| 16 library fasta.test.shaker_test; | 16 library fasta.test.shaker_test; |
| 17 | 17 |
| 18 import 'dart:async' show Future; | 18 import 'dart:async' show Future; |
| 19 import 'dart:convert' show JSON; | 19 import 'dart:convert' show JSON; |
| 20 import 'dart:io' show File; | 20 import 'dart:io' show File; |
| 21 | 21 |
| 22 export 'package:testing/testing.dart' show Chain, runMe; | 22 export 'package:testing/testing.dart' show Chain, runMe; |
| 23 import 'package:front_end/physical_file_system.dart'; | 23 import 'package:front_end/physical_file_system.dart'; |
| 24 import 'package:front_end/src/fasta/dill/dill_target.dart' show DillTarget; | 24 import 'package:front_end/src/fasta/dill/dill_target.dart' show DillTarget; |
| 25 import 'package:front_end/src/fasta/errors.dart' show InputError; | 25 import 'package:front_end/src/fasta/errors.dart' show InputError; |
| 26 import 'package:front_end/src/fasta/kernel/kernel_outline_shaker.dart'; |
| 26 import 'package:front_end/src/fasta/kernel/kernel_target.dart' | 27 import 'package:front_end/src/fasta/kernel/kernel_target.dart' |
| 27 show KernelTarget; | 28 show KernelTarget; |
| 28 import 'package:front_end/src/fasta/kernel/verifier.dart' show verifyProgram; | 29 import 'package:front_end/src/fasta/kernel/verifier.dart' show verifyProgram; |
| 29 import 'package:front_end/src/fasta/testing/kernel_chain.dart' show runDiff; | 30 import 'package:front_end/src/fasta/testing/kernel_chain.dart' show runDiff; |
| 30 import 'package:front_end/src/fasta/testing/patched_sdk_location.dart'; | 31 import 'package:front_end/src/fasta/testing/patched_sdk_location.dart'; |
| 31 import 'package:front_end/src/fasta/ticker.dart' show Ticker; | 32 import 'package:front_end/src/fasta/ticker.dart' show Ticker; |
| 32 import 'package:front_end/src/fasta/translate_uri.dart' show TranslateUri; | 33 import 'package:front_end/src/fasta/translate_uri.dart' show TranslateUri; |
| 33 import 'package:front_end/src/fasta/util/relativize.dart' show relativizeUri; | 34 import 'package:front_end/src/fasta/util/relativize.dart' show relativizeUri; |
| 34 import 'package:kernel/ast.dart' show Program; | 35 import 'package:kernel/ast.dart' show Program; |
| 35 import 'package:kernel/kernel.dart' show loadProgramFromBytes; | 36 import 'package:kernel/kernel.dart' show loadProgramFromBytes; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 58 new ExpectationSet.fromJsonList(JSON.decode(EXPECTATIONS)); | 59 new ExpectationSet.fromJsonList(JSON.decode(EXPECTATIONS)); |
| 59 | 60 |
| 60 TreeShakerContext(this.outlineUri, this.uriTranslator, this.outlineBytes, | 61 TreeShakerContext(this.outlineUri, this.uriTranslator, this.outlineBytes, |
| 61 bool updateExpectations) | 62 bool updateExpectations) |
| 62 : steps = <Step>[ | 63 : steps = <Step>[ |
| 63 const BuildProgram(), | 64 const BuildProgram(), |
| 64 new CheckShaker(updateExpectations: updateExpectations), | 65 new CheckShaker(updateExpectations: updateExpectations), |
| 65 ]; | 66 ]; |
| 66 | 67 |
| 67 Program loadPlatformOutline() { | 68 Program loadPlatformOutline() { |
| 68 // Note: we rebuild the platform outline on every test because the compiler | 69 // Note: we rebuild the platform outline on every test because the |
| 69 // currently mutates the in-memory representation of the program without | 70 // tree-shaker mutates the in-memory representation of the program without |
| 70 // cloning it. | 71 // cloning it. |
| 71 return loadProgramFromBytes(outlineBytes); | 72 return loadProgramFromBytes(outlineBytes); |
| 72 } | 73 } |
| 73 | 74 |
| 74 static create(Map<String, String> environment) async { | 75 static create(Map<String, String> environment) async { |
| 75 environment[ENABLE_FULL_COMPILE] = ""; | 76 environment[ENABLE_FULL_COMPILE] = ""; |
| 76 environment[AST_KIND_INDEX] = "${AstKind.Kernel.index}"; | 77 environment[AST_KIND_INDEX] = "${AstKind.Kernel.index}"; |
| 77 bool updateExpectations = environment["updateExpectations"] == "true"; | 78 bool updateExpectations = environment["updateExpectations"] == "true"; |
| 78 Uri sdk = await computePatchedSdk(); | 79 Uri sdk = await computePatchedSdk(); |
| 79 Uri outlineUri = sdk.resolve('outline.dill'); | 80 Uri outlineUri = sdk.resolve('outline.dill'); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 100 var dillTarget = new DillTarget( | 101 var dillTarget = new DillTarget( |
| 101 new Ticker(isVerbose: false), | 102 new Ticker(isVerbose: false), |
| 102 context.uriTranslator, | 103 context.uriTranslator, |
| 103 new VmFastaTarget(new TargetFlags(strongMode: false))); | 104 new VmFastaTarget(new TargetFlags(strongMode: false))); |
| 104 dillTarget.loader.appendLibraries(platformOutline); | 105 dillTarget.loader.appendLibraries(platformOutline); |
| 105 var sourceTarget = new KernelTarget( | 106 var sourceTarget = new KernelTarget( |
| 106 PhysicalFileSystem.instance, dillTarget, context.uriTranslator); | 107 PhysicalFileSystem.instance, dillTarget, context.uriTranslator); |
| 107 await dillTarget.buildOutlines(); | 108 await dillTarget.buildOutlines(); |
| 108 | 109 |
| 109 var inputUri = description.uri; | 110 var inputUri = description.uri; |
| 110 | |
| 111 /// We treat the lib.dart library as a special dependency that was | |
| 112 /// previously built. To do so, we build it and append it back to the | |
| 113 /// dillTarget before building the actual test. | |
| 114 var libUri = inputUri.resolve('lib/lib.dart'); | 111 var libUri = inputUri.resolve('lib/lib.dart'); |
| 115 sourceTarget.read(libUri); | 112 sourceTarget.read(libUri); |
| 116 dillTarget.loader.appendLibraries( | |
| 117 await sourceTarget.buildOutlines(), (uri) => uri == libUri); | |
| 118 | |
| 119 /// This new KernelTarget contains only sources from the test without | |
| 120 /// lib.dart. | |
| 121 sourceTarget = new KernelTarget( | |
| 122 PhysicalFileSystem.instance, dillTarget, context.uriTranslator); | |
| 123 | |
| 124 await dillTarget.buildOutlines(); | |
| 125 sourceTarget.read(inputUri); | 113 sourceTarget.read(inputUri); |
| 126 var contents = new File.fromUri(inputUri).readAsStringSync(); | 114 var contents = new File.fromUri(inputUri).readAsStringSync(); |
| 127 var showCoreLibraries = contents.contains("@@SHOW_CORE_LIBRARIES@@"); | 115 var showCoreLibraries = contents.contains("@@SHOW_CORE_LIBRARIES@@"); |
| 128 | |
| 129 await sourceTarget.buildOutlines(); | 116 await sourceTarget.buildOutlines(); |
| 130 // Note: We run the tree-shaker as a separate step on this suite to be | 117 var program = await sourceTarget.buildProgram(); |
| 131 // able to specify what libraries to tree shake (by default only the code | 118 bool isIncluded(Uri uri) => !_isTreeShaken(uri); |
| 132 // in the dillTarget gets tree-shaken). We could apply the tree-shaker | 119 trimProgram(program, isIncluded); |
| 133 // twice, but that seems unnecessary. | |
| 134 var program = await sourceTarget.buildProgram(trimDependencies: true); | |
| 135 return pass(new _IntermediateData(inputUri, program, showCoreLibraries)); | 120 return pass(new _IntermediateData(inputUri, program, showCoreLibraries)); |
| 136 } on InputError catch (e, s) { | 121 } on InputError catch (e, s) { |
| 137 return fail(null, e.error, s); | 122 return fail(null, e.error, s); |
| 138 } | 123 } |
| 139 } | 124 } |
| 140 } | 125 } |
| 141 | 126 |
| 142 /// Intermediate result from the testing chain. | 127 /// Intermediate result from the testing chain. |
| 143 class _IntermediateData { | 128 class _IntermediateData { |
| 144 /// The input URI provided to the test. | 129 /// The input URI provided to the test. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 156 } | 141 } |
| 157 | 142 |
| 158 /// A step that runs the tree-shaker and checks againt an expectation file for | 143 /// A step that runs the tree-shaker and checks againt an expectation file for |
| 159 /// the list of members and classes that should be preserved by the tree-shaker. | 144 /// the list of members and classes that should be preserved by the tree-shaker. |
| 160 class CheckShaker extends Step<_IntermediateData, String, ChainContext> { | 145 class CheckShaker extends Step<_IntermediateData, String, ChainContext> { |
| 161 final bool updateExpectations; | 146 final bool updateExpectations; |
| 162 const CheckShaker({this.updateExpectations: false}); | 147 const CheckShaker({this.updateExpectations: false}); |
| 163 | 148 |
| 164 String get name => "match shaker expectation"; | 149 String get name => "match shaker expectation"; |
| 165 | 150 |
| 166 /// Tree-shake dart:* libraries and the library under [_specialLibraryPath]. | |
| 167 bool _isTreeShaken(Uri uri) => | |
| 168 uri.isScheme('dart') || | |
| 169 Uri.base.resolveUri(uri).path.endsWith(_specialLibraryPath); | |
| 170 | |
| 171 Future<Result<String>> run( | 151 Future<Result<String>> run( |
| 172 _IntermediateData data, ChainContext context) async { | 152 _IntermediateData data, ChainContext context) async { |
| 173 String actualResult; | 153 String actualResult; |
| 174 var entryUri = data.program.libraries | 154 var entryUri = data.uri; |
| 175 .firstWhere((l) => !l.importUri.isScheme('dart')) | |
| 176 .importUri; | |
| 177 | |
| 178 var program = data.program; | 155 var program = data.program; |
| 179 | 156 |
| 180 var errors = verifyProgram(program, isOutline: false); | 157 var errors = verifyProgram(program, isOutline: false); |
| 181 if (!errors.isEmpty) { | 158 if (!errors.isEmpty) { |
| 182 return new Result<String>( | 159 return new Result<String>( |
| 183 null, context.expectationSet["VerificationError"], errors, null); | 160 null, context.expectationSet["VerificationError"], errors, null); |
| 184 } | 161 } |
| 185 | 162 |
| 186 // Build a text representation of what we expect to be retained. | 163 // Build a text representation of what we expect to be retained. |
| 187 var buffer = new StringBuffer(); | 164 var buffer = new StringBuffer(); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 """ | 214 """ |
| 238 Please create file ${expectedFile.path} with this content: | 215 Please create file ${expectedFile.path} with this content: |
| 239 $buffer"""); | 216 $buffer"""); |
| 240 } | 217 } |
| 241 } | 218 } |
| 242 } | 219 } |
| 243 | 220 |
| 244 /// A special library used only to test the shaker. The suite above will | 221 /// A special library used only to test the shaker. The suite above will |
| 245 /// tree-shake the contents of this library. | 222 /// tree-shake the contents of this library. |
| 246 const _specialLibraryPath = 'pkg/front_end/testcases/shaker/lib/lib.dart'; | 223 const _specialLibraryPath = 'pkg/front_end/testcases/shaker/lib/lib.dart'; |
| 224 |
| 225 /// Tree-shake dart:* libraries and the library under [_specialLibraryPath]. |
| 226 bool _isTreeShaken(Uri uri) => |
| 227 uri.isScheme('dart') || |
| 228 Uri.base.resolveUri(uri).path.endsWith(_specialLibraryPath); |
| OLD | NEW |