| 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 /// Runs Dart Dev Compiler on all input in the `codegen` directory and checks | 6 /// Runs Dart Dev Compiler on all input in the `codegen` directory and checks |
| 7 /// that the output is what we expected. | 7 /// that the output is what we expected. |
| 8 library dev_compiler.test.codegen_test; | 8 library dev_compiler.test.codegen_test; |
| 9 | 9 |
| 10 import 'dart:convert' show JSON; | 10 import 'dart:convert' show JSON; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 | 64 |
| 65 var allDirs = [null]; | 65 var allDirs = [null]; |
| 66 allDirs.addAll(testDirs); | 66 allDirs.addAll(testDirs); |
| 67 for (var dir in allDirs) { | 67 for (var dir in allDirs) { |
| 68 if (codeCoverage && dir != null) continue; | 68 if (codeCoverage && dir != null) continue; |
| 69 | 69 |
| 70 group('dartdevc ' + path.join('test', 'codegen', dir), () { | 70 group('dartdevc ' + path.join('test', 'codegen', dir), () { |
| 71 var outDir = new Directory(path.join(expectDir, dir)); | 71 var outDir = new Directory(path.join(expectDir, dir)); |
| 72 if (!outDir.existsSync()) outDir.createSync(recursive: true); | 72 if (!outDir.existsSync()) outDir.createSync(recursive: true); |
| 73 | 73 |
| 74 var testFiles = _findTests(path.join(inputDir, dir), filePattern); | 74 var baseDir = path.join(inputDir, dir); |
| 75 var testFiles = _findTests(baseDir, filePattern); |
| 75 for (var filePath in testFiles) { | 76 for (var filePath in testFiles) { |
| 76 if (multitests.contains(filePath)) continue; | 77 if (multitests.contains(filePath)) continue; |
| 77 | 78 |
| 78 var filename = path.basenameWithoutExtension(filePath); | 79 var filename = path.basenameWithoutExtension(filePath); |
| 79 | 80 |
| 80 test('$filename.dart', () { | 81 test('$filename.dart', () { |
| 81 // Check if we need to use special compile options. | 82 // Check if we need to use special compile options. |
| 82 var contents = new File(filePath).readAsStringSync(); | 83 var contents = new File(filePath).readAsStringSync(); |
| 83 var match = | 84 var match = |
| 84 new RegExp(r'// compile options: (.*)').matchAsPrefix(contents); | 85 new RegExp(r'// compile options: (.*)').matchAsPrefix(contents); |
| 85 | 86 |
| 86 var args = new List.from(defaultOptions); | 87 var args = new List.from(defaultOptions); |
| 87 if (match != null) { | 88 if (match != null) { |
| 88 args.addAll(match.group(1).split(' ')); | 89 args.addAll(match.group(1).split(' ')); |
| 89 } | 90 } |
| 90 var options = | 91 var options = |
| 91 new CompilerOptions.fromArguments(compilerArgParser.parse(args)); | 92 new CompilerOptions.fromArguments(compilerArgParser.parse(args)); |
| 92 | 93 |
| 93 // Collect any other files we've imported. | 94 // Collect any other files we've imported. |
| 94 var files = new Set<String>(); | 95 var files = new Set<String>(); |
| 95 _collectTransitiveImports(contents, files, from: filePath); | 96 _collectTransitiveImports(contents, files, from: filePath); |
| 96 | 97 |
| 97 var unit = new BuildUnit(filename, files.toList(), _moduleForLibrary); | 98 var unit = new BuildUnit( |
| 99 filename, baseDir, files.toList(), _moduleForLibrary); |
| 98 var module = compiler.compile(unit, options); | 100 var module = compiler.compile(unit, options); |
| 99 _writeModule(path.join(outDir.path, filename), module); | 101 _writeModule(path.join(outDir.path, filename), module); |
| 100 }); | 102 }); |
| 101 } | 103 } |
| 102 }); | 104 }); |
| 103 } | 105 } |
| 104 | 106 |
| 105 if (codeCoverage) { | 107 if (codeCoverage) { |
| 106 test('build_sdk code coverage', () { | 108 test('build_sdk code coverage', () { |
| 107 var generatedSdkDir = | 109 var generatedSdkDir = |
| (...skipping 13 matching lines...) Expand all Loading... |
| 121 new File(outPath + '.js').writeAsStringSync(result.code); | 123 new File(outPath + '.js').writeAsStringSync(result.code); |
| 122 if (result.sourceMap != null) { | 124 if (result.sourceMap != null) { |
| 123 var mapPath = outPath + '.js.map'; | 125 var mapPath = outPath + '.js.map'; |
| 124 new File(mapPath) | 126 new File(mapPath) |
| 125 .writeAsStringSync(JSON.encode(result.placeSourceMap(mapPath))); | 127 .writeAsStringSync(JSON.encode(result.placeSourceMap(mapPath))); |
| 126 } | 128 } |
| 127 } | 129 } |
| 128 } | 130 } |
| 129 | 131 |
| 130 void _buildSunflower(ModuleCompiler compiler, String expectDir) { | 132 void _buildSunflower(ModuleCompiler compiler, String expectDir) { |
| 133 var baseDir = path.join(inputDir, 'sunflower'); |
| 131 var files = ['sunflower', 'circle', 'painter'] | 134 var files = ['sunflower', 'circle', 'painter'] |
| 132 .map((f) => path.join(inputDir, 'sunflower', '$f.dart')) | 135 .map((f) => path.join(baseDir, '$f.dart')) |
| 133 .toList(); | 136 .toList(); |
| 134 var input = new BuildUnit('sunflower', files, _moduleForLibrary); | 137 var input = new BuildUnit('sunflower', baseDir, files, _moduleForLibrary); |
| 135 var options = new CompilerOptions(summarizeApi: false); | 138 var options = new CompilerOptions(summarizeApi: false); |
| 136 | 139 |
| 137 var built = compiler.compile(input, options); | 140 var built = compiler.compile(input, options); |
| 138 _writeModule(path.join(expectDir, 'sunflower', 'sunflower'), built); | 141 _writeModule(path.join(expectDir, 'sunflower', 'sunflower'), built); |
| 139 } | 142 } |
| 140 | 143 |
| 141 void _buildPackages(ModuleCompiler compiler, String expectDir) { | 144 void _buildPackages(ModuleCompiler compiler, String expectDir) { |
| 142 // Note: we don't summarize these, as we're going to rely on our in-memory | 145 // Note: we don't summarize these, as we're going to rely on our in-memory |
| 143 // shared analysis context for caching, and `_moduleForLibrary` below | 146 // shared analysis context for caching, and `_moduleForLibrary` below |
| 144 // understands these are from other modules. | 147 // understands these are from other modules. |
| 145 var options = new CompilerOptions(sourceMap: false, summarizeApi: false); | 148 var options = new CompilerOptions(sourceMap: false, summarizeApi: false); |
| 146 | 149 |
| 147 for (var uri in packageUrlMappings.keys) { | 150 for (var uri in packageUrlMappings.keys) { |
| 148 assert(uri.startsWith('package:')); | 151 assert(uri.startsWith('package:')); |
| 149 var uriPath = uri.substring('package:'.length); | 152 var uriPath = uri.substring('package:'.length); |
| 150 var name = path.basenameWithoutExtension(uriPath); | 153 var name = path.basenameWithoutExtension(uriPath); |
| 151 test(name, () { | 154 test(name, () { |
| 152 var input = new BuildUnit(name, [uri], _moduleForLibrary); | 155 var input = new BuildUnit(name, inputDir, [uri], _moduleForLibrary); |
| 153 var built = compiler.compile(input, options); | 156 var built = compiler.compile(input, options); |
| 154 | 157 |
| 155 var outPath = path.join(expectDir, path.withoutExtension(uriPath)); | 158 var outPath = path.join(expectDir, path.withoutExtension(uriPath)); |
| 156 _writeModule(outPath, built); | 159 _writeModule(outPath, built); |
| 157 }); | 160 }); |
| 158 } | 161 } |
| 159 } | 162 } |
| 160 | 163 |
| 161 void _buildMatcher(ModuleCompiler compiler, String expectDir) { | 164 void _buildMatcher(ModuleCompiler compiler, String expectDir) { |
| 162 var options = new CompilerOptions(sourceMap: false, summarizeApi: false); | 165 var options = new CompilerOptions(sourceMap: false, summarizeApi: false); |
| 163 | 166 |
| 164 var filePath = path.join(inputDir, 'packages', 'matcher', 'matcher.dart'); | 167 var packageRoot = path.join(inputDir, 'packages'); |
| 168 var filePath = path.join(packageRoot, 'matcher', 'matcher.dart'); |
| 165 var contents = new File(filePath).readAsStringSync(); | 169 var contents = new File(filePath).readAsStringSync(); |
| 166 | 170 |
| 167 // Collect any other files we've imported. | 171 // Collect any other files we've imported. |
| 168 var files = new Set<String>(); | 172 var files = new Set<String>(); |
| 169 _collectTransitiveImports(contents, files, from: filePath); | 173 _collectTransitiveImports(contents, files, |
| 174 packageRoot: packageRoot, from: filePath); |
| 170 | 175 |
| 171 var unit = new BuildUnit('matcher', files.toList(), _moduleForLibrary); | 176 var unit = |
| 177 new BuildUnit('matcher', inputDir, files.toList(), _moduleForLibrary); |
| 172 var module = compiler.compile(unit, options); | 178 var module = compiler.compile(unit, options); |
| 173 | 179 |
| 174 var outPath = path.join(expectDir, 'matcher', 'matcher'); | 180 var outPath = path.join(expectDir, 'matcher', 'matcher'); |
| 175 _writeModule(outPath, module); | 181 _writeModule(outPath, module); |
| 176 } | 182 } |
| 177 | 183 |
| 178 String _moduleForLibrary(Source source) { | 184 String _moduleForLibrary(Source source) { |
| 179 var scheme = source.uri.scheme; | 185 var scheme = source.uri.scheme; |
| 180 if (scheme == 'package') { | 186 if (scheme == 'package') { |
| 181 return source.uri.pathSegments.first; | 187 return source.uri.pathSegments.first; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 .where((p) => p.endsWith('_test.dart') || p.endsWith('_multi.dart')); | 246 .where((p) => p.endsWith('_test.dart') || p.endsWith('_multi.dart')); |
| 241 } | 247 } |
| 242 return files; | 248 return files; |
| 243 } | 249 } |
| 244 | 250 |
| 245 /// Parse directives from [contents] and find the complete set of transitive | 251 /// Parse directives from [contents] and find the complete set of transitive |
| 246 /// imports, reading files as needed. | 252 /// imports, reading files as needed. |
| 247 /// | 253 /// |
| 248 /// This will not include dart:* libraries, as those are implicitly available. | 254 /// This will not include dart:* libraries, as those are implicitly available. |
| 249 void _collectTransitiveImports(String contents, Set<String> libraries, | 255 void _collectTransitiveImports(String contents, Set<String> libraries, |
| 250 {String from}) { | 256 {String packageRoot, String from}) { |
| 251 if (!libraries.add(from)) return; | 257 var uri = from; |
| 258 if (packageRoot != null && path.isWithin(packageRoot, from)) { |
| 259 uri = 'package:${path.relative(from, from: packageRoot)}'; |
| 260 } |
| 261 if (!libraries.add(uri)) return; |
| 252 | 262 |
| 253 var unit = parseDirectives(contents, name: from, suppressErrors: true); | 263 var unit = parseDirectives(contents, name: from, suppressErrors: true); |
| 254 for (var d in unit.directives) { | 264 for (var d in unit.directives) { |
| 255 if (d is ImportDirective || d is ExportDirective) { | 265 if (d is ImportDirective || d is ExportDirective) { |
| 256 String uri = _resolveDirective(d); | 266 String uri = _resolveDirective(d); |
| 257 if (uri == null || | 267 if (uri == null || |
| 258 uri.startsWith('dart:') || | 268 uri.startsWith('dart:') || |
| 259 uri.startsWith('package:')) { | 269 uri.startsWith('package:')) { |
| 260 continue; | 270 continue; |
| 261 } | 271 } |
| 262 | 272 |
| 263 var f = new File(path.join(path.dirname(from), uri)); | 273 var f = new File(path.join(path.dirname(from), uri)); |
| 264 if (f.existsSync()) { | 274 if (f.existsSync()) { |
| 265 _collectTransitiveImports(f.readAsStringSync(), libraries, | 275 _collectTransitiveImports(f.readAsStringSync(), libraries, |
| 266 from: f.path); | 276 packageRoot: packageRoot, from: f.path); |
| 267 } | 277 } |
| 268 } | 278 } |
| 269 } | 279 } |
| 270 } | 280 } |
| 271 | 281 |
| 272 /// Simplified from ParseDartTask.resolveDirective. | 282 /// Simplified from ParseDartTask.resolveDirective. |
| 273 String _resolveDirective(UriBasedDirective directive) { | 283 String _resolveDirective(UriBasedDirective directive) { |
| 274 StringLiteral uriLiteral = directive.uri; | 284 StringLiteral uriLiteral = directive.uri; |
| 275 String uriContent = uriLiteral.stringValue; | 285 String uriContent = uriLiteral.stringValue; |
| 276 if (uriContent != null) { | 286 if (uriContent != null) { |
| 277 uriContent = uriContent.trim(); | 287 uriContent = uriContent.trim(); |
| 278 directive.uriContent = uriContent; | 288 directive.uriContent = uriContent; |
| 279 } | 289 } |
| 280 return directive.validate() == null ? uriContent : null; | 290 return directive.validate() == null ? uriContent : null; |
| 281 } | 291 } |
| OLD | NEW |