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/generated/source.dart' show Source; | 23 import 'package:analyzer/src/generated/source.dart' show Source; |
24 import 'package:args/args.dart' show ArgParser, ArgResults; | 24 import 'package:args/args.dart' show ArgParser, ArgResults; |
25 import 'package:dev_compiler/src/analyzer/context.dart' show AnalyzerOptions; | 25 import 'package:dev_compiler/src/analyzer/context.dart' |
| 26 show AnalyzerOptions, parseDeclaredVariables; |
26 import 'package:dev_compiler/src/compiler/compiler.dart' | 27 import 'package:dev_compiler/src/compiler/compiler.dart' |
27 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; | 28 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; |
28 import 'package:dev_compiler/src/compiler/module_builder.dart' | 29 import 'package:dev_compiler/src/compiler/module_builder.dart' |
29 show ModuleFormat, addModuleFormatOptions, parseModuleFormatOption; | 30 show ModuleFormat, addModuleFormatOptions, parseModuleFormatOption; |
30 import 'package:path/path.dart' as path; | 31 import 'package:path/path.dart' as path; |
31 import 'package:test/test.dart' show expect, isFalse, isTrue, test; | 32 import 'package:test/test.dart' show expect, isFalse, isTrue, test; |
32 | 33 |
33 import '../tool/build_sdk.dart' as build_sdk; | 34 import '../tool/build_sdk.dart' as build_sdk; |
34 import 'testing.dart' show repoDirectory, testDirectory; | 35 import 'testing.dart' show repoDirectory, testDirectory; |
35 import 'multitest.dart' show extractTestsFromMultitest, isMultiTest; | 36 import 'multitest.dart' show extractTestsFromMultitest, isMultiTest; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 var sdkDir = path.join(repoDirectory, 'gen', 'patched_sdk'); | 70 var sdkDir = path.join(repoDirectory, 'gen', 'patched_sdk'); |
70 var sdkSummaryFile = | 71 var sdkSummaryFile = |
71 path.join(testDirectory, '..', 'lib', 'sdk', 'ddc_sdk.sum'); | 72 path.join(testDirectory, '..', 'lib', 'sdk', 'ddc_sdk.sum'); |
72 | 73 |
73 var summaryPaths = new Directory(path.join(codegenOutputDir, 'pkg')) | 74 var summaryPaths = new Directory(path.join(codegenOutputDir, 'pkg')) |
74 .listSync() | 75 .listSync() |
75 .map((e) => e.path) | 76 .map((e) => e.path) |
76 .where((p) => p.endsWith('.sum')) | 77 .where((p) => p.endsWith('.sum')) |
77 .toList(); | 78 .toList(); |
78 | 79 |
79 var analyzerOptions = new AnalyzerOptions( | 80 var sharedCompiler = new ModuleCompiler(new AnalyzerOptions( |
80 dartSdkSummaryPath: sdkSummaryFile, summaryPaths: summaryPaths); | 81 dartSdkSummaryPath: sdkSummaryFile, summaryPaths: summaryPaths)); |
81 var compiler = new ModuleCompiler(analyzerOptions); | |
82 | 82 |
83 var testDirs = [ | 83 var testDirs = [ |
84 'language', | 84 'language', |
85 'corelib', | 85 'corelib', |
86 path.join('corelib', 'regexp'), | 86 path.join('corelib', 'regexp'), |
87 path.join('lib', 'convert'), | 87 path.join('lib', 'convert'), |
88 path.join('lib', 'html'), | 88 path.join('lib', 'html'), |
89 // TODO(vsm): Fix these - they import files from a different directory | 89 // TODO(vsm): Fix these - they import files from a different directory |
90 // - this triggers an invalid library root build error. | 90 // - this triggers an invalid library root build error. |
91 // path.join('lib', 'html', 'custom'), | 91 // path.join('lib', 'html', 'custom'), |
92 path.join('lib', 'math'), | 92 path.join('lib', 'math'), |
93 path.join('lib', 'mirrors'), | 93 path.join('lib', 'mirrors'), |
94 path.join('lib', 'typed_data'), | 94 path.join('lib', 'typed_data'), |
95 ]; | 95 ]; |
96 | 96 |
97 // Copy all of the test files and expanded multitest files to | 97 // Copy all of the test files and expanded multitest files to |
98 // gen/codegen_tests. We'll compile from there. | 98 // gen/codegen_tests. We'll compile from there. |
99 var testFiles = _setUpTests(testDirs); | 99 var testFiles = _setUpTests(testDirs); |
100 | 100 |
101 // Our default compiler options. Individual tests can override these. | 101 // Our default compiler options. Individual tests can override these. |
102 var defaultOptions = ['--no-source-map', '--no-summarize']; | 102 var defaultOptions = ['--no-source-map', '--no-summarize']; |
103 var compilerArgParser = new ArgParser(); | 103 var compileArgParser = new ArgParser(); |
104 CompilerOptions.addArguments(compilerArgParser); | 104 CompilerOptions.addArguments(compileArgParser); |
105 addModuleFormatOptions(compilerArgParser); | 105 addModuleFormatOptions(compileArgParser); |
| 106 |
| 107 var testFileOptionsMatcher = |
| 108 new RegExp(r'// (compile options: |SharedOptions=)(.*)', multiLine: true); |
| 109 |
| 110 // Ignore dart2js options that we don't support in DDC. |
| 111 var ignoreOptions = [ |
| 112 '--enable-enum', |
| 113 '--experimental-trust-js-interop-type-annotations', |
| 114 '--trust-type-annotations', |
| 115 '--supermixin' |
| 116 ]; |
106 | 117 |
107 // Compile each test file to JS and put the result in gen/codegen_output. | 118 // Compile each test file to JS and put the result in gen/codegen_output. |
108 for (var testFile in testFiles) { | 119 for (var testFile in testFiles) { |
109 var relativePath = path.relative(testFile, from: codegenTestDir); | 120 var relativePath = path.relative(testFile, from: codegenTestDir); |
110 | 121 |
111 // Only compile the top-level files for generating coverage. | 122 // Only compile the top-level files for generating coverage. |
112 bool isTopLevelTest = path.dirname(relativePath) == "."; | 123 bool isTopLevelTest = path.dirname(relativePath) == "."; |
113 if (codeCoverage && !isTopLevelTest) continue; | 124 if (codeCoverage && !isTopLevelTest) continue; |
114 | 125 |
115 var name = path.withoutExtension(relativePath); | 126 var name = path.withoutExtension(relativePath); |
116 test('dartdevc $name', () { | 127 test('dartdevc $name', () { |
117 // Check if we need to use special compile options. | 128 // Check if we need to use special compile options. |
118 var contents = new File(testFile).readAsStringSync(); | 129 var contents = new File(testFile).readAsStringSync(); |
119 var match = | 130 var match = testFileOptionsMatcher.firstMatch(contents); |
120 new RegExp(r'// compile options: (.*)').matchAsPrefix(contents); | |
121 | 131 |
122 var args = defaultOptions.toList(); | 132 var args = defaultOptions.toList(); |
123 if (match != null) { | 133 if (match != null) { |
124 args.addAll(match.group(1).split(' ')); | 134 var matchedArgs = match.group(2).split(' '); |
| 135 args.addAll(matchedArgs.where((s) => !ignoreOptions.contains(s))); |
125 } | 136 } |
126 | 137 |
127 var argResults = compilerArgParser.parse(args); | 138 var declaredVars = <String, String>{}; |
| 139 var argResults = |
| 140 compileArgParser.parse(parseDeclaredVariables(args, declaredVars)); |
128 var options = new CompilerOptions.fromArguments(argResults); | 141 var options = new CompilerOptions.fromArguments(argResults); |
129 var moduleFormat = parseModuleFormatOption(argResults).first; | 142 var moduleFormat = parseModuleFormatOption(argResults).first; |
130 | 143 |
131 // Collect any other files we've imported. | 144 // Collect any other files we've imported. |
132 var files = new Set<String>(); | 145 var files = new Set<String>(); |
133 _collectTransitiveImports(contents, files, from: testFile); | 146 _collectTransitiveImports(contents, files, from: testFile); |
134 var unit = new BuildUnit( | 147 var unit = new BuildUnit( |
135 name, path.dirname(testFile), files.toList(), _moduleForLibrary); | 148 name, path.dirname(testFile), files.toList(), _moduleForLibrary); |
| 149 |
| 150 var compiler = sharedCompiler; |
| 151 if (declaredVars.isNotEmpty) { |
| 152 compiler = new ModuleCompiler(new AnalyzerOptions( |
| 153 dartSdkSummaryPath: sdkSummaryFile, |
| 154 summaryPaths: summaryPaths, |
| 155 declaredVariables: declaredVars)); |
| 156 } |
136 var module = compiler.compile(unit, options); | 157 var module = compiler.compile(unit, options); |
137 | 158 |
138 bool notStrong = notYetStrongTests.contains(name); | 159 bool notStrong = notYetStrongTests.contains(name); |
139 if (module.isValid) { | 160 if (module.isValid) { |
140 _writeModule( | 161 _writeModule( |
141 path.join(codegenOutputDir, name), | 162 path.join(codegenOutputDir, name), |
142 isTopLevelTest ? path.join(codegenExpectDir, name) : null, | 163 isTopLevelTest ? path.join(codegenExpectDir, name) : null, |
143 moduleFormat, | 164 moduleFormat, |
144 module); | 165 module); |
145 | 166 |
146 expect(notStrong, isFalse, | 167 expect(notStrong, isFalse, |
147 reason: "test $name expected strong mode errors, but compiled."); | 168 reason: "test $name expected strong mode errors, but compiled."); |
148 } else { | 169 } else { |
149 expect(notStrong, isTrue, | 170 expect(notStrong, isTrue, |
150 reason: "test $name failed to compile due to strong mode errors:" | 171 reason: "test $name failed to compile due to strong mode errors:" |
151 "\n\n${module.errors.join('\n')}."); | 172 "\n\n${module.errors.join('\n')}."); |
152 } | 173 } |
153 }); | 174 }); |
154 } | 175 } |
155 | 176 |
156 if (filePattern.hasMatch('sunflower')) { | 177 if (filePattern.hasMatch('sunflower')) { |
157 test('sunflower', () { | 178 test('sunflower', () { |
158 _buildSunflower(compiler, codegenOutputDir, codegenExpectDir); | 179 _buildSunflower(sharedCompiler, codegenOutputDir, codegenExpectDir); |
159 }); | 180 }); |
160 } | 181 } |
161 | 182 |
162 if (codeCoverage) { | 183 if (codeCoverage) { |
163 test('build_sdk code coverage', () { | 184 test('build_sdk code coverage', () { |
164 return build_sdk.main(['--dart-sdk', sdkDir, '-o', codegenOutputDir]); | 185 return build_sdk.main(['--dart-sdk', sdkDir, '-o', codegenOutputDir]); |
165 }); | 186 }); |
166 } | 187 } |
167 } | 188 } |
168 | 189 |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 /// Simplified from ParseDartTask.resolveDirective. | 346 /// Simplified from ParseDartTask.resolveDirective. |
326 String _resolveDirective(UriBasedDirective directive) { | 347 String _resolveDirective(UriBasedDirective directive) { |
327 StringLiteral uriLiteral = directive.uri; | 348 StringLiteral uriLiteral = directive.uri; |
328 String uriContent = uriLiteral.stringValue; | 349 String uriContent = uriLiteral.stringValue; |
329 if (uriContent != null) { | 350 if (uriContent != null) { |
330 uriContent = uriContent.trim(); | 351 uriContent = uriContent.trim(); |
331 directive.uriContent = uriContent; | 352 directive.uriContent = uriContent; |
332 } | 353 } |
333 return directive.validate() == null ? uriContent : null; | 354 return directive.validate() == null ? uriContent : null; |
334 } | 355 } |
OLD | NEW |