OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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 file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Tests code generation. | 5 /// Tests code generation. |
6 /// | 6 /// |
7 /// Runs Dart Dev Compiler on all input in the `codegen` directory and checks | 7 /// Runs Dart Dev Compiler on all input in the `codegen` directory and checks |
8 /// that the output is what we expected. | 8 /// that the output is what we expected. |
9 library dev_compiler.test.codegen_test; | 9 library dev_compiler.test.codegen_test; |
10 | 10 |
11 // TODO(rnystrom): This doesn't actually run any tests any more. It just | 11 // TODO(rnystrom): This doesn't actually run any tests any more. It just |
12 // compiles stuff. This should be changed to not use unittest and just be a | 12 // compiles stuff. This should be changed to not use unittest and just be a |
13 // regular program that outputs files. | 13 // regular program that outputs files. |
14 | 14 |
15 import 'dart:io' show Directory, File, Platform; | 15 import 'dart:io' show Directory, File, Platform; |
16 import 'package:analyzer/analyzer.dart' | 16 import 'package:analyzer/analyzer.dart' |
17 show | 17 show |
18 ExportDirective, | 18 ExportDirective, |
19 ImportDirective, | 19 ImportDirective, |
20 StringLiteral, | 20 StringLiteral, |
21 UriBasedDirective, | 21 UriBasedDirective, |
22 parseDirectives; | 22 parseDirectives; |
23 import 'package:analyzer/src/command_line/arguments.dart' | 23 import 'package:analyzer/src/command_line/arguments.dart' |
24 show extractDefinedVariables; | 24 show defineAnalysisArguments; |
25 import 'package:analyzer/src/dart/ast/ast.dart'; | 25 import 'package:analyzer/src/dart/ast/ast.dart'; |
26 import 'package:analyzer/src/generated/source.dart' show Source; | 26 import 'package:analyzer/src/generated/source.dart' show Source; |
27 import 'package:args/args.dart' show ArgParser, ArgResults; | 27 import 'package:args/args.dart' show ArgParser, ArgResults; |
28 import 'package:dev_compiler/src/analyzer/context.dart'; | 28 import 'package:dev_compiler/src/analyzer/context.dart'; |
29 import 'package:dev_compiler/src/compiler/compiler.dart' | 29 import 'package:dev_compiler/src/compiler/compiler.dart' |
30 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; | 30 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; |
31 import 'package:dev_compiler/src/compiler/module_builder.dart' | 31 import 'package:dev_compiler/src/compiler/module_builder.dart' |
32 show ModuleFormat, addModuleFormatOptions, parseModuleFormatOption; | 32 show ModuleFormat, addModuleFormatOptions, parseModuleFormatOption; |
33 import 'package:path/path.dart' as path; | 33 import 'package:path/path.dart' as path; |
34 import 'package:test/test.dart' show expect, isFalse, isTrue, test; | 34 import 'package:test/test.dart' show expect, isFalse, isTrue, test; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 var sdkDir = path.join(repoDirectory, 'gen', 'patched_sdk'); | 72 var sdkDir = path.join(repoDirectory, 'gen', 'patched_sdk'); |
73 var sdkSummaryFile = | 73 var sdkSummaryFile = |
74 path.join(testDirectory, '..', 'lib', 'sdk', 'ddc_sdk.sum'); | 74 path.join(testDirectory, '..', 'lib', 'sdk', 'ddc_sdk.sum'); |
75 | 75 |
76 var summaryPaths = new Directory(path.join(codegenOutputDir, 'pkg')) | 76 var summaryPaths = new Directory(path.join(codegenOutputDir, 'pkg')) |
77 .listSync() | 77 .listSync() |
78 .map((e) => e.path) | 78 .map((e) => e.path) |
79 .where((p) => p.endsWith('.sum')) | 79 .where((p) => p.endsWith('.sum')) |
80 .toList(); | 80 .toList(); |
81 | 81 |
82 var sharedCompiler = new ModuleCompiler(new AnalyzerOptions( | 82 var sharedCompiler = new ModuleCompiler(new AnalyzerOptions.basic( |
83 dartSdkSummaryPath: sdkSummaryFile, summaryPaths: summaryPaths)); | 83 dartSdkSummaryPath: sdkSummaryFile, summaryPaths: summaryPaths)); |
84 | 84 |
85 var testDirs = [ | 85 var testDirs = [ |
86 'language', | 86 'language', |
87 'corelib', | 87 'corelib', |
88 path.join('corelib', 'regexp'), | 88 path.join('corelib', 'regexp'), |
89 path.join('lib', 'collection'), | 89 path.join('lib', 'collection'), |
90 path.join('lib', 'convert'), | 90 path.join('lib', 'convert'), |
91 path.join('lib', 'html'), | 91 path.join('lib', 'html'), |
92 // TODO(vsm): Fix these - they import files from a different directory | 92 // TODO(vsm): Fix these - they import files from a different directory |
93 // - this triggers an invalid library root build error. | 93 // - this triggers an invalid library root build error. |
94 // path.join('lib', 'html', 'custom'), | 94 // path.join('lib', 'html', 'custom'), |
95 path.join('lib', 'math'), | 95 path.join('lib', 'math'), |
96 path.join('lib', 'mirrors'), | 96 path.join('lib', 'mirrors'), |
97 path.join('lib', 'typed_data'), | 97 path.join('lib', 'typed_data'), |
98 ]; | 98 ]; |
99 | 99 |
100 // Copy all of the test files and expanded multitest files to | 100 // Copy all of the test files and expanded multitest files to |
101 // gen/codegen_tests. We'll compile from there. | 101 // gen/codegen_tests. We'll compile from there. |
102 var testFiles = _setUpTests(testDirs); | 102 var testFiles = _setUpTests(testDirs); |
103 | 103 |
104 // Our default compiler options. Individual tests can override these. | 104 // Our default compiler options. Individual tests can override these. |
105 var defaultOptions = ['--no-source-map', '--no-summarize']; | 105 var defaultOptions = ['--no-source-map', '--no-summarize']; |
106 var compileArgParser = new ArgParser(); | 106 var compileArgParser = new ArgParser(); |
| 107 defineAnalysisArguments(compileArgParser, ddc: true); |
| 108 AnalyzerOptions.addArguments(compileArgParser); |
107 CompilerOptions.addArguments(compileArgParser); | 109 CompilerOptions.addArguments(compileArgParser); |
108 addModuleFormatOptions(compileArgParser); | 110 addModuleFormatOptions(compileArgParser); |
109 | 111 |
110 var testFileOptionsMatcher = | 112 var testFileOptionsMatcher = |
111 new RegExp(r'// (compile options: |SharedOptions=)(.*)', multiLine: true); | 113 new RegExp(r'// (compile options: |SharedOptions=)(.*)', multiLine: true); |
112 | 114 |
113 // Ignore dart2js options that we don't support in DDC. | 115 // Ignore dart2js options that we don't support in DDC. |
114 var ignoreOptions = [ | 116 var ignoreOptions = [ |
115 '--enable-enum', | 117 '--enable-enum', |
116 '--experimental-trust-js-interop-type-annotations', | 118 '--experimental-trust-js-interop-type-annotations', |
(...skipping 14 matching lines...) Expand all Loading... |
131 // Check if we need to use special compile options. | 133 // Check if we need to use special compile options. |
132 var contents = new File(testFile).readAsStringSync(); | 134 var contents = new File(testFile).readAsStringSync(); |
133 var match = testFileOptionsMatcher.firstMatch(contents); | 135 var match = testFileOptionsMatcher.firstMatch(contents); |
134 | 136 |
135 var args = defaultOptions.toList(); | 137 var args = defaultOptions.toList(); |
136 if (match != null) { | 138 if (match != null) { |
137 var matchedArgs = match.group(2).split(' '); | 139 var matchedArgs = match.group(2).split(' '); |
138 args.addAll(matchedArgs.where((s) => !ignoreOptions.contains(s))); | 140 args.addAll(matchedArgs.where((s) => !ignoreOptions.contains(s))); |
139 } | 141 } |
140 | 142 |
141 var declaredVars = <String, String>{}; | 143 ArgResults argResults = compileArgParser.parse(args); |
142 args = extractDefinedVariables(args, declaredVars); | 144 var analyzerOptions = new AnalyzerOptions.fromArguments(argResults, |
143 ArgResults argResults; | 145 dartSdkSummaryPath: sdkSummaryFile, summaryPaths: summaryPaths); |
144 try { | 146 |
145 argResults = compileArgParser.parse(args); | |
146 } catch (e) { | |
147 print('Failed to parse $args'); | |
148 rethrow; | |
149 } | |
150 var options = new CompilerOptions.fromArguments(argResults); | 147 var options = new CompilerOptions.fromArguments(argResults); |
| 148 |
151 var moduleFormat = parseModuleFormatOption(argResults).first; | 149 var moduleFormat = parseModuleFormatOption(argResults).first; |
152 | 150 |
153 // Collect any other files we've imported. | 151 // Collect any other files we've imported. |
154 var files = new Set<String>(); | 152 var files = new Set<String>(); |
155 _collectTransitiveImports(contents, files, from: testFile); | 153 _collectTransitiveImports(contents, files, from: testFile); |
156 var unit = new BuildUnit( | 154 var unit = new BuildUnit( |
157 name, path.dirname(testFile), files.toList(), _moduleForLibrary); | 155 name, path.dirname(testFile), files.toList(), _moduleForLibrary); |
158 | 156 |
159 var compiler = sharedCompiler; | 157 var compiler = sharedCompiler; |
160 if (declaredVars.isNotEmpty) { | 158 if (analyzerOptions.declaredVariables.isNotEmpty) { |
161 compiler = new ModuleCompiler(new AnalyzerOptions( | 159 compiler = new ModuleCompiler(analyzerOptions); |
162 dartSdkSummaryPath: sdkSummaryFile, | |
163 summaryPaths: summaryPaths, | |
164 declaredVariables: declaredVars)); | |
165 } | 160 } |
166 var module = compiler.compile(unit, options); | 161 var module = compiler.compile(unit, options); |
167 | 162 |
168 bool notStrong = notYetStrongTests.contains(name); | 163 bool notStrong = notYetStrongTests.contains(name); |
169 if (module.isValid) { | 164 if (module.isValid) { |
170 _writeModule( | 165 _writeModule( |
171 path.join(codegenOutputDir, name), | 166 path.join(codegenOutputDir, name), |
172 isTopLevelTest ? path.join(codegenExpectDir, name) : null, | 167 isTopLevelTest ? path.join(codegenExpectDir, name) : null, |
173 moduleFormat, | 168 moduleFormat, |
174 module); | 169 module); |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 StringLiteral uriLiteral = directive.uri; | 358 StringLiteral uriLiteral = directive.uri; |
364 String uriContent = uriLiteral.stringValue; | 359 String uriContent = uriLiteral.stringValue; |
365 if (uriContent != null) { | 360 if (uriContent != null) { |
366 uriContent = uriContent.trim(); | 361 uriContent = uriContent.trim(); |
367 directive.uriContent = uriContent; | 362 directive.uriContent = uriContent; |
368 } | 363 } |
369 return (directive as UriBasedDirectiveImpl).validate() == null | 364 return (directive as UriBasedDirectiveImpl).validate() == null |
370 ? uriContent | 365 ? uriContent |
371 : null; | 366 : null; |
372 } | 367 } |
OLD | NEW |