| 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 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 // Our default compiler options. Individual tests can override these. | 95 // Our default compiler options. Individual tests can override these. |
| 96 var defaultOptions = ['--no-source-map', '--no-summarize']; | 96 var defaultOptions = ['--no-source-map', '--no-summarize']; |
| 97 var compilerArgParser = new ArgParser(); | 97 var compilerArgParser = new ArgParser(); |
| 98 CompilerOptions.addArguments(compilerArgParser); | 98 CompilerOptions.addArguments(compilerArgParser); |
| 99 | 99 |
| 100 // Compile each test file to JS and put the result in gen/codegen_output. | 100 // Compile each test file to JS and put the result in gen/codegen_output. |
| 101 for (var testFile in testFiles) { | 101 for (var testFile in testFiles) { |
| 102 var relativePath = path.relative(testFile, from: codegenTestDir); | 102 var relativePath = path.relative(testFile, from: codegenTestDir); |
| 103 | 103 |
| 104 // Only compile the top-level files for generating coverage. | 104 // Only compile the top-level files for generating coverage. |
| 105 if (codeCoverage && path.dirname(relativePath) != ".") continue; | 105 bool isTopLevelTest = path.dirname(relativePath) == "."; |
| 106 if (codeCoverage && !isTopLevelTest) continue; |
| 106 | 107 |
| 107 var name = path.withoutExtension(relativePath); | 108 var name = path.withoutExtension(relativePath); |
| 108 test('dartdevc $name', () { | 109 test('dartdevc $name', () { |
| 109 var relativeDir = path.dirname(relativePath); | |
| 110 var outDir = path.join(codegenOutputDir, relativeDir); | |
| 111 var expectDir = path.join(codegenExpectDir, relativeDir); | |
| 112 | |
| 113 // Check if we need to use special compile options. | 110 // Check if we need to use special compile options. |
| 114 var contents = new File(testFile).readAsStringSync(); | 111 var contents = new File(testFile).readAsStringSync(); |
| 115 var match = | 112 var match = |
| 116 new RegExp(r'// compile options: (.*)').matchAsPrefix(contents); | 113 new RegExp(r'// compile options: (.*)').matchAsPrefix(contents); |
| 117 | 114 |
| 118 var args = defaultOptions.toList(); | 115 var args = defaultOptions.toList(); |
| 119 if (match != null) { | 116 if (match != null) { |
| 120 args.addAll(match.group(1).split(' ')); | 117 args.addAll(match.group(1).split(' ')); |
| 121 } | 118 } |
| 122 var options = | 119 var options = |
| 123 new CompilerOptions.fromArguments(compilerArgParser.parse(args)); | 120 new CompilerOptions.fromArguments(compilerArgParser.parse(args)); |
| 124 | 121 |
| 125 // Collect any other files we've imported. | 122 // Collect any other files we've imported. |
| 126 var files = new Set<String>(); | 123 var files = new Set<String>(); |
| 127 _collectTransitiveImports(contents, files, from: testFile); | 124 _collectTransitiveImports(contents, files, from: testFile); |
| 128 var moduleName = | 125 var unit = new BuildUnit( |
| 129 path.withoutExtension(path.relative(testFile, from: codegenTestDir)); | 126 name, path.dirname(testFile), files.toList(), _moduleForLibrary); |
| 130 var unit = new BuildUnit(moduleName, path.dirname(testFile), | |
| 131 files.toList(), _moduleForLibrary); | |
| 132 var module = compiler.compile(unit, options); | 127 var module = compiler.compile(unit, options); |
| 133 _writeModule( | 128 _writeModule(path.join(codegenOutputDir, name), |
| 134 path.join(outDir, path.basenameWithoutExtension(testFile)), | 129 isTopLevelTest ? path.join(codegenExpectDir, name) : null, module); |
| 135 path.join(expectDir, path.basenameWithoutExtension(testFile)), | |
| 136 module); | |
| 137 }); | 130 }); |
| 138 } | 131 } |
| 139 | 132 |
| 140 if (filePattern.hasMatch('sunflower')) { | 133 if (filePattern.hasMatch('sunflower')) { |
| 141 _buildSunflower(compiler, codegenOutputDir, codegenExpectDir); | 134 _buildSunflower(compiler, codegenOutputDir, codegenExpectDir); |
| 142 } | 135 } |
| 143 | 136 |
| 144 if (codeCoverage) { | 137 if (codeCoverage) { |
| 145 test('build_sdk code coverage', () { | 138 test('build_sdk code coverage', () { |
| 146 return build_sdk.main(['--dart-sdk', sdkDir, '-o', codegenOutputDir]); | 139 return build_sdk.main(['--dart-sdk', sdkDir, '-o', codegenOutputDir]); |
| 147 }); | 140 }); |
| 148 } | 141 } |
| 149 } | 142 } |
| 150 | 143 |
| 151 void _writeModule(String outPath, String expectPath, JSModuleFile result) { | 144 void _writeModule(String outPath, String expectPath, JSModuleFile result) { |
| 152 _ensureDirectory(path.dirname(outPath)); | 145 _ensureDirectory(path.dirname(outPath)); |
| 153 _ensureDirectory(path.dirname(expectPath)); | |
| 154 | 146 |
| 155 String errors = result.errors.join('\n'); | 147 String errors = result.errors.join('\n'); |
| 156 if (errors.isNotEmpty && !errors.endsWith('\n')) errors += '\n'; | 148 if (errors.isNotEmpty && !errors.endsWith('\n')) errors += '\n'; |
| 157 new File(outPath + '.txt').writeAsStringSync(errors); | 149 new File(outPath + '.txt').writeAsStringSync(errors); |
| 158 | 150 |
| 159 var jsFile = new File(outPath + '.js'); | 151 var jsFile = new File(outPath + '.js'); |
| 160 var summaryFile = new File(outPath + '.sum'); | 152 var summaryFile = new File(outPath + '.sum'); |
| 161 var expectFile = new File(expectPath + '.js'); | |
| 162 var errorFile = new File(outPath + '.err'); | 153 var errorFile = new File(outPath + '.err'); |
| 163 | 154 |
| 164 if (result.isValid) { | 155 if (result.isValid) { |
| 165 jsFile.writeAsStringSync(result.code); | 156 jsFile.writeAsStringSync(result.code); |
| 166 if (result.summaryBytes != null) { | 157 if (result.summaryBytes != null) { |
| 167 summaryFile.writeAsBytesSync(result.summaryBytes); | 158 summaryFile.writeAsBytesSync(result.summaryBytes); |
| 168 } | 159 } |
| 169 if (result.sourceMap != null) { | 160 if (result.sourceMap != null) { |
| 170 var mapPath = outPath + '.js.map'; | 161 var mapPath = outPath + '.js.map'; |
| 171 new File(mapPath) | 162 new File(mapPath) |
| 172 .writeAsStringSync(JSON.encode(result.placeSourceMap(mapPath))); | 163 .writeAsStringSync(JSON.encode(result.placeSourceMap(mapPath))); |
| 173 } | 164 } |
| 174 | 165 |
| 175 expectFile.writeAsStringSync(result.code); | |
| 176 | |
| 177 // There are no errors, so delete any stale ".err" file. | 166 // There are no errors, so delete any stale ".err" file. |
| 178 if (errorFile.existsSync()) { | 167 if (errorFile.existsSync()) { |
| 179 errorFile.deleteSync(); | 168 errorFile.deleteSync(); |
| 180 } | 169 } |
| 181 } else { | 170 } else { |
| 182 // Also write the errors to a '.err' file for easy counting. | 171 // Also write the errors to a '.err' file for easy counting. |
| 183 var moduleName = result.name; | 172 var moduleName = result.name; |
| 184 var libraryName = path.split(moduleName).last; | 173 var libraryName = path.split(moduleName).last; |
| 185 var count = "[error]".allMatches(errors).length; | 174 var count = "[error]".allMatches(errors).length; |
| 186 var text = ''' | 175 var text = ''' |
| 187 dart_library.library('$moduleName', null, [ | 176 dart_library.library('$moduleName', null, [ |
| 188 'dart_sdk', | 177 'dart_sdk', |
| 189 'expect' | 178 'expect' |
| 190 ], function(exports, dart_sdk, expect) { | 179 ], function(exports, dart_sdk, expect) { |
| 191 const message = `DDC Compilation Error: $moduleName has $count errors`; | 180 const message = `DDC Compilation Error: $moduleName has $count errors`; |
| 192 const error = new Error(message); | 181 const error = new Error(message); |
| 193 exports.$libraryName = Object.create(null); | 182 exports.$libraryName = Object.create(null); |
| 194 exports.$libraryName.main = function() { | 183 exports.$libraryName.main = function() { |
| 195 throw error; | 184 throw error; |
| 196 } | 185 } |
| 197 }); | 186 }); |
| 198 '''; | 187 '''; |
| 199 errorFile.writeAsStringSync(text); | 188 errorFile.writeAsStringSync(text); |
| 200 | 189 |
| 201 // There are errors, so delete any stale ".js" file. | 190 // There are errors, so delete any stale ".js" file. |
| 202 if (jsFile.existsSync()) { | 191 if (jsFile.existsSync()) { |
| 203 jsFile.deleteSync(); | 192 jsFile.deleteSync(); |
| 204 } | 193 } |
| 194 } |
| 205 | 195 |
| 206 // There are errors, so delete any stale expect ".js" file. | 196 // Write the expectation file if needed. |
| 207 if (expectFile.existsSync()) { | 197 // Generally speaking we try to avoid these tests, but they are occasionally |
| 208 expectFile.deleteSync(); | 198 // useful. |
| 199 if (expectPath != null) { |
| 200 _ensureDirectory(path.dirname(expectPath)); |
| 201 |
| 202 var expectFile = new File(expectPath + '.js'); |
| 203 if (result.isValid) { |
| 204 expectFile.writeAsStringSync(result.code); |
| 205 } else { |
| 206 // There are errors, so delete any stale expect ".js" file. |
| 207 if (expectFile.existsSync()) { |
| 208 expectFile.deleteSync(); |
| 209 } |
| 210 expectFile.writeAsStringSync("//FAILED TO COMPILE"); |
| 209 } | 211 } |
| 210 expectFile.writeAsStringSync("//FAILED TO COMPILE"); | |
| 211 } | 212 } |
| 212 } | 213 } |
| 213 | 214 |
| 214 void _buildSunflower( | 215 void _buildSunflower( |
| 215 ModuleCompiler compiler, String outputDir, String expectDir) { | 216 ModuleCompiler compiler, String outputDir, String expectDir) { |
| 216 var baseDir = path.join(codegenDir, 'sunflower'); | 217 var baseDir = path.join(codegenDir, 'sunflower'); |
| 217 var files = ['sunflower', 'circle', 'painter'] | 218 var files = ['sunflower', 'circle', 'painter'] |
| 218 .map((f) => path.join(baseDir, '$f.dart')) | 219 .map((f) => path.join(baseDir, '$f.dart')) |
| 219 .toList(); | 220 .toList(); |
| 220 var input = new BuildUnit('sunflower', baseDir, files, _moduleForLibrary); | 221 var input = new BuildUnit('sunflower', baseDir, files, _moduleForLibrary); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 /// Simplified from ParseDartTask.resolveDirective. | 342 /// Simplified from ParseDartTask.resolveDirective. |
| 342 String _resolveDirective(UriBasedDirective directive) { | 343 String _resolveDirective(UriBasedDirective directive) { |
| 343 StringLiteral uriLiteral = directive.uri; | 344 StringLiteral uriLiteral = directive.uri; |
| 344 String uriContent = uriLiteral.stringValue; | 345 String uriContent = uriLiteral.stringValue; |
| 345 if (uriContent != null) { | 346 if (uriContent != null) { |
| 346 uriContent = uriContent.trim(); | 347 uriContent = uriContent.trim(); |
| 347 directive.uriContent = uriContent; | 348 directive.uriContent = uriContent; |
| 348 } | 349 } |
| 349 return directive.validate() == null ? uriContent : null; | 350 return directive.validate() == null ? uriContent : null; |
| 350 } | 351 } |
| OLD | NEW |