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/compiler_options.dart'; |
24 import 'package:front_end/src/fasta/compiler_command_line.dart'; | 24 import 'package:front_end/src/base/processed_options.dart'; |
| 25 import 'package:front_end/src/fasta/compiler_context.dart'; |
25 import 'package:front_end/src/fasta/dill/dill_target.dart' show DillTarget; | 26 import 'package:front_end/src/fasta/dill/dill_target.dart' show DillTarget; |
26 import 'package:front_end/src/fasta/deprecated_problems.dart' | 27 import 'package:front_end/src/fasta/deprecated_problems.dart' |
27 show deprecated_InputError; | 28 show deprecated_InputError; |
28 import 'package:front_end/src/fasta/kernel/kernel_outline_shaker.dart'; | 29 import 'package:front_end/src/fasta/kernel/kernel_outline_shaker.dart'; |
29 import 'package:front_end/src/fasta/kernel/kernel_target.dart' | 30 import 'package:front_end/src/fasta/kernel/kernel_target.dart' |
30 show KernelTarget; | 31 show KernelTarget; |
31 import 'package:front_end/src/fasta/kernel/verifier.dart' show verifyProgram; | 32 import 'package:front_end/src/fasta/kernel/verifier.dart' show verifyProgram; |
32 import 'package:front_end/src/fasta/testing/kernel_chain.dart' show runDiff; | 33 import 'package:front_end/src/fasta/testing/kernel_chain.dart' show runDiff; |
33 import 'package:front_end/src/fasta/testing/patched_sdk_location.dart'; | 34 import 'package:front_end/src/fasta/testing/patched_sdk_location.dart'; |
34 import 'package:front_end/src/fasta/ticker.dart' show Ticker; | |
35 import 'package:front_end/src/fasta/uri_translator.dart' show UriTranslator; | |
36 import 'package:front_end/src/fasta/uri_translator_impl.dart'; | |
37 import 'package:front_end/src/fasta/util/relativize.dart' show relativizeUri; | 35 import 'package:front_end/src/fasta/util/relativize.dart' show relativizeUri; |
38 import 'package:kernel/ast.dart' show Program; | 36 import 'package:kernel/ast.dart' show Program; |
39 import 'package:kernel/kernel.dart' show loadProgramFromBytes; | 37 import 'package:kernel/kernel.dart' show loadProgramFromBytes; |
40 import 'package:kernel/target/targets.dart' show TargetFlags; | 38 import 'package:kernel/target/targets.dart' show TargetFlags; |
41 import 'package:kernel/target/vm_fasta.dart' show VmFastaTarget; | 39 import 'package:kernel/target/vm_fasta.dart' show VmFastaTarget; |
42 import 'package:testing/testing.dart' | 40 import 'package:testing/testing.dart' |
43 show Chain, ChainContext, ExpectationSet, Result, Step, TestDescription; | 41 show Chain, ChainContext, ExpectationSet, Result, Step, TestDescription; |
44 import 'testing/suite.dart'; | 42 import 'testing/suite.dart'; |
45 | 43 |
46 main(List<String> arguments) => | 44 main(List<String> arguments) => |
47 runMe(arguments, createContext, "../../testing.json"); | 45 runMe(arguments, createContext, "../../testing.json"); |
48 | 46 |
49 Future<TreeShakerContext> createContext( | 47 Future<TreeShakerContext> createContext( |
50 Chain suite, Map<String, String> environment) { | 48 Chain suite, Map<String, String> environment) { |
51 return TreeShakerContext.create(environment); | 49 return TreeShakerContext.create(environment); |
52 } | 50 } |
53 | 51 |
54 /// Context used to run the tree-shaking test suite. | 52 /// Context used to run the tree-shaking test suite. |
55 class TreeShakerContext extends ChainContext { | 53 class TreeShakerContext extends ChainContext { |
56 final UriTranslator uriTranslator; | 54 final ProcessedOptions options; |
57 final Uri outlineUri; | 55 final Uri outlineUri; |
58 final List<Step> steps; | 56 final List<Step> steps; |
59 final List<int> outlineBytes; | 57 final List<int> outlineBytes; |
60 | 58 |
61 final ExpectationSet expectationSet = | 59 final ExpectationSet expectationSet = |
62 new ExpectationSet.fromJsonList(JSON.decode(EXPECTATIONS)); | 60 new ExpectationSet.fromJsonList(JSON.decode(EXPECTATIONS)); |
63 | 61 |
64 TreeShakerContext(this.outlineUri, this.uriTranslator, this.outlineBytes, | 62 TreeShakerContext( |
65 bool updateExpectations) | 63 this.outlineUri, this.options, this.outlineBytes, bool updateExpectations) |
66 : steps = <Step>[ | 64 : steps = <Step>[ |
67 const BuildProgram(), | 65 const BuildProgram(), |
68 new CheckShaker(updateExpectations: updateExpectations), | 66 new CheckShaker(updateExpectations: updateExpectations), |
69 ]; | 67 ]; |
70 | 68 |
71 Program loadPlatformOutline() { | 69 Program loadPlatformOutline() { |
72 // Note: we rebuild the platform outline on every test because the | 70 // Note: we rebuild the platform outline on every test because the |
73 // tree-shaker mutates the in-memory representation of the program without | 71 // tree-shaker mutates the in-memory representation of the program without |
74 // cloning it. | 72 // cloning it. |
75 return loadProgramFromBytes(outlineBytes); | 73 return loadProgramFromBytes(outlineBytes); |
76 } | 74 } |
77 | 75 |
78 static create(Map<String, String> environment) async { | 76 static create(Map<String, String> environment) async { |
79 environment[ENABLE_FULL_COMPILE] = ""; | 77 environment[ENABLE_FULL_COMPILE] = ""; |
80 environment[AST_KIND_INDEX] = "${AstKind.Kernel.index}"; | 78 environment[AST_KIND_INDEX] = "${AstKind.Kernel.index}"; |
81 bool updateExpectations = environment["updateExpectations"] == "true"; | 79 bool updateExpectations = environment["updateExpectations"] == "true"; |
82 Uri sdk = await computePatchedSdk(); | 80 Uri sdk = await computePatchedSdk(); |
83 Uri outlineUri = sdk.resolve('outline.dill'); | 81 Uri outlineUri = sdk.resolve('outline.dill'); |
84 Uri packages = Uri.base.resolve(".packages"); | 82 var options = new CompilerOptions() |
85 UriTranslator uriTranslator = await UriTranslatorImpl | 83 ..packagesFileUri = Uri.base.resolve(".packages"); |
86 .parse(PhysicalFileSystem.instance, sdk, packages: packages); | |
87 List<int> outlineBytes = new File.fromUri(outlineUri).readAsBytesSync(); | 84 List<int> outlineBytes = new File.fromUri(outlineUri).readAsBytesSync(); |
88 return new TreeShakerContext( | 85 return new TreeShakerContext(outlineUri, new ProcessedOptions(options), |
89 outlineUri, uriTranslator, outlineBytes, updateExpectations); | 86 outlineBytes, updateExpectations); |
90 } | 87 } |
91 } | 88 } |
92 | 89 |
93 /// Step that extracts the test-specific options and builds the program without | 90 /// Step that extracts the test-specific options and builds the program without |
94 /// applying tree-shaking. | 91 /// applying tree-shaking. |
95 class BuildProgram | 92 class BuildProgram |
96 extends Step<TestDescription, _IntermediateData, TreeShakerContext> { | 93 extends Step<TestDescription, _IntermediateData, TreeShakerContext> { |
97 const BuildProgram(); | 94 const BuildProgram(); |
98 String get name => "build program"; | 95 String get name => "build program"; |
99 Future<Result<_IntermediateData>> run( | 96 Future<Result<_IntermediateData>> run( |
100 TestDescription description, TreeShakerContext context) async { | 97 TestDescription description, TreeShakerContext context) async { |
101 return await CompilerCommandLine.withGlobalOptions("", [""], (_) async { | 98 return await CompilerContext.runWithOptions(context.options, (_) async { |
102 try { | 99 try { |
103 var platformOutline = context.loadPlatformOutline(); | 100 var platformOutline = context.loadPlatformOutline(); |
104 platformOutline.unbindCanonicalNames(); | 101 platformOutline.unbindCanonicalNames(); |
105 var dillTarget = new DillTarget( | 102 var uriTranslator = await context.options.getUriTranslator(); |
106 new Ticker(isVerbose: false), | 103 var dillTarget = new DillTarget(context.options.ticker, uriTranslator, |
107 context.uriTranslator, | |
108 new VmFastaTarget(new TargetFlags(strongMode: false))); | 104 new VmFastaTarget(new TargetFlags(strongMode: false))); |
109 dillTarget.loader.appendLibraries(platformOutline); | 105 dillTarget.loader.appendLibraries(platformOutline); |
110 var sourceTarget = new KernelTarget(PhysicalFileSystem.instance, false, | 106 var sourceTarget = new KernelTarget( |
111 dillTarget, context.uriTranslator); | 107 context.options.fileSystem, false, dillTarget, uriTranslator); |
112 await dillTarget.buildOutlines(); | 108 await dillTarget.buildOutlines(); |
113 | 109 |
114 var inputUri = description.uri; | 110 var inputUri = description.uri; |
115 var libUri = inputUri.resolve('lib/lib.dart'); | 111 var libUri = inputUri.resolve('lib/lib.dart'); |
116 sourceTarget.read(libUri); | 112 sourceTarget.read(libUri); |
117 sourceTarget.read(inputUri); | 113 sourceTarget.read(inputUri); |
118 var contents = new File.fromUri(inputUri).readAsStringSync(); | 114 var contents = new File.fromUri(inputUri).readAsStringSync(); |
119 var showCoreLibraries = contents.contains("@@SHOW_CORE_LIBRARIES@@"); | 115 var showCoreLibraries = contents.contains("@@SHOW_CORE_LIBRARIES@@"); |
120 await sourceTarget.buildOutlines(); | 116 await sourceTarget.buildOutlines(); |
121 var program = await sourceTarget.buildProgram(); | 117 var program = await sourceTarget.buildProgram(); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 } | 219 } |
224 | 220 |
225 /// 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 |
226 /// tree-shake the contents of this library. | 222 /// tree-shake the contents of this library. |
227 const _specialLibraryPath = 'pkg/front_end/testcases/shaker/lib/lib.dart'; | 223 const _specialLibraryPath = 'pkg/front_end/testcases/shaker/lib/lib.dart'; |
228 | 224 |
229 /// Tree-shake dart:* libraries and the library under [_specialLibraryPath]. | 225 /// Tree-shake dart:* libraries and the library under [_specialLibraryPath]. |
230 bool _isTreeShaken(Uri uri) => | 226 bool _isTreeShaken(Uri uri) => |
231 uri.isScheme('dart') || | 227 uri.isScheme('dart') || |
232 Uri.base.resolveUri(uri).path.endsWith(_specialLibraryPath); | 228 Uri.base.resolveUri(uri).path.endsWith(_specialLibraryPath); |
OLD | NEW |