| 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 | 4 |
| 5 library rasta.dart2js_test; | 5 library rasta.dart2js_test; |
| 6 | 6 |
| 7 import 'dart:async' show | 7 import 'dart:async' show |
| 8 Future, | 8 Future, |
| 9 Stream; | 9 Stream; |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 | 22 |
| 23 import 'package:rasta/testing.dart' show | 23 import 'package:rasta/testing.dart' show |
| 24 TestDescription, | 24 TestDescription, |
| 25 dartSdk, | 25 dartSdk, |
| 26 listTests, | 26 listTests, |
| 27 startDart; | 27 startDart; |
| 28 | 28 |
| 29 import 'package:rasta/src/rastask.dart' show | 29 import 'package:rasta/src/rastask.dart' show |
| 30 openWrite; | 30 openWrite; |
| 31 | 31 |
| 32 import 'package:expect/expect.dart' show | |
| 33 Expect; | |
| 34 | |
| 35 import '../../bin/rastak.dart' as rastak show main; | 32 import '../../bin/rastak.dart' as rastak show main; |
| 36 | 33 |
| 37 const bool generateExpectations = | 34 const bool generateExpectations = |
| 38 const bool.fromEnvironment("generateExpectations"); | 35 const bool.fromEnvironment("generateExpectations"); |
| 39 | 36 |
| 40 Future<Null> addRegressions(Map<Uri, Uri> tests, Uri temp) async { | 37 Future<Null> addRegressions(Map<Uri, Uri> tests, Uri temp) async { |
| 41 Stream<TestDescription> regressions = listTests( | 38 Stream<TestDescription> regressions = listTests( |
| 42 <Uri>[Uri.base.resolve("test/kernel/regression/")], pattern: ".dart"); | 39 <Uri>[Uri.base.resolve("test/kernel/regression/")], pattern: ".dart"); |
| 43 await for (TestDescription regression in regressions) { | 40 await for (TestDescription regression in regressions) { |
| 44 tests[regression.uri] = temp.resolve("${regression.shortName}.bart"); | 41 tests[regression.uri] = temp.resolve("${regression.shortName}.bart"); |
| 45 } | 42 } |
| 46 } | 43 } |
| 47 | 44 |
| 48 Future<Null> main() async { | 45 Future<Null> main() async { |
| 49 // When running via `testa.dart`, [Platform.script] is located in a temporary | 46 // When running via `testa.dart`, [Platform.script] is located in a temporary |
| 50 // directory. | 47 // directory. |
| 51 Uri temp = Platform.script; | 48 Uri temp = Platform.script; |
| 52 Map<Uri, Uri> tests = <Uri, Uri>{ | 49 Map<Uri, Uri> tests = <Uri, Uri>{ |
| 53 Uri.parse("dart:core"): temp.resolve("core.bart"), | 50 Uri.parse("dart:core"): temp.resolve("core.bart"), |
| 54 | 51 |
| 55 Uri.parse("package:compiler/src/dart2js.dart"): | 52 Uri.parse("package:compiler/src/dart2js.dart"): |
| 56 temp.resolve("dart2js.bart"), | 53 temp.resolve("dart2js.bart"), |
| 57 }; | 54 }; |
| 58 | 55 |
| 59 await addRegressions(tests, temp); | 56 await addRegressions(tests, temp); |
| 60 | 57 |
| 61 List<Uri> generated = <Uri>[]; | 58 List<Uri> generated = <Uri>[]; |
| 59 Map<Uri, Uri> mismatches = <Uri, Uri>{}; |
| 62 | 60 |
| 63 for (Uri source in tests.keys) { | 61 for (Uri source in tests.keys) { |
| 64 print("Rastarizing $source"); | 62 print("Rastarizing $source"); |
| 65 Uri bart = tests[source]; | 63 Uri bart = tests[source]; |
| 66 File output = new File.fromUri(bart); | 64 File output = new File.fromUri(bart); |
| 67 await output.parent.create(recursive: true); | 65 await output.parent.create(recursive: true); |
| 68 ir.TreeNode node = await rastak.main(<String>["$source", "$bart"], null); | 66 ir.TreeNode node = await rastak.main(<String>["$source", "$bart"], null); |
| 69 | 67 |
| 70 await checkAgainstExpectations(node, generated); | 68 await checkAgainstExpectations(node, generated, mismatches); |
| 71 | 69 |
| 72 Uri textOutput = bart.resolve("${bart.path}.txt"); | 70 Uri textOutput = bart.resolve("${bart.path}.txt"); |
| 73 print("Kernelizing $bart to $textOutput"); | 71 print("Kernelizing $bart to $textOutput"); |
| 74 Process process = await startDart( | 72 Process process = await startDart( |
| 75 Uri.base.resolve("third_party/kernel/bin/dartk.dart"), | 73 Uri.base.resolve("third_party/kernel/bin/dartk.dart"), |
| 76 <String>["--sdk=${dartSdk.toFilePath()}", "${bart.toFilePath()}"], | 74 <String>["--sdk=${dartSdk.toFilePath()}", "${bart.toFilePath()}"], |
| 77 const <String>[]); | 75 const <String>[]); |
| 78 process.stdin.close(); | 76 process.stdin.close(); |
| 79 Future stdoutFuture = | 77 Future stdoutFuture = |
| 80 process.stdout.pipe(new File.fromUri(textOutput).openWrite()); | 78 process.stdout.pipe(new File.fromUri(textOutput).openWrite()); |
| 81 Future stderrFuture = | 79 Future stderrFuture = |
| 82 process.stderr.listen((data) => stderr.add(data)).asFuture(); | 80 process.stderr.listen((data) => stderr.add(data)).asFuture(); |
| 83 int exitCode = await process.exitCode; | 81 int exitCode = await process.exitCode; |
| 84 await stdoutFuture; | 82 await stdoutFuture; |
| 85 await stderrFuture; | 83 await stderrFuture; |
| 86 if (exitCode != 0) { | 84 if (exitCode != 0) { |
| 87 throw "non-zero exit code ($exitCode) from rastak"; | 85 throw "non-zero exit code ($exitCode) from rastak"; |
| 88 } | 86 } |
| 89 } | 87 } |
| 90 if (generated.isNotEmpty) { | 88 if (generated.isNotEmpty) { |
| 91 throw "The following files were generated, " | 89 throw "The following files were generated, " |
| 92 "please check them and re-run this test: ${generated}"; | 90 "please check them and re-run this test: ${generated}"; |
| 93 } | 91 } |
| 92 if (mismatches.isNotEmpty) { |
| 93 StringBuffer sb = new StringBuffer(); |
| 94 mismatches.forEach((Uri test, Uri expectation) { |
| 95 sb.writeln("Output from '$test' doesn't match '$expectation'."); |
| 96 }); |
| 97 throw "$sb"; |
| 98 } |
| 94 } | 99 } |
| 95 | 100 |
| 96 Future<Null> checkAgainstExpectations(ir.Node node, List<Uri> generated) async { | 101 Future<Null> checkAgainstExpectations( |
| 102 ir.Node node, |
| 103 List<Uri> generated, |
| 104 Map<Uri, Uri> mismatches) async { |
| 97 ir.Library library = (node is ir.Program) ? node.mainMethod.parent : node; | 105 ir.Library library = (node is ir.Program) ? node.mainMethod.parent : node; |
| 98 Uri uri = library.importUri; | 106 Uri uri = library.importUri; |
| 99 | 107 |
| 100 // We only validate output of the small tests fully under our control. | 108 // We only validate output of the small tests fully under our control. |
| 101 if (!uri.path.contains("/regression/")) return; | 109 if (!uri.path.contains("/regression/")) return; |
| 102 | 110 |
| 103 StringBuffer buffer = new StringBuffer(); | 111 StringBuffer buffer = new StringBuffer(); |
| 104 new Printer(buffer).writeLibraryFile(library); | 112 new Printer(buffer).writeLibraryFile(library); |
| 105 File expectedFile = new File("${uri.toFilePath()}.txt"); | 113 File expectedFile = new File("${uri.toFilePath()}.txt"); |
| 106 if (await expectedFile.exists()) { | 114 if (await expectedFile.exists()) { |
| 107 String expected = await expectedFile.readAsString(); | 115 String expected = await expectedFile.readAsString(); |
| 108 Expect.stringEquals(expected.trim(), "$buffer".trim()); | 116 if (expected.trim() != "$buffer".trim()) { |
| 117 print("$uri doesn't match ${expectedFile.uri}"); |
| 118 mismatches[uri] = expectedFile.uri; |
| 119 } |
| 109 } else if (generateExpectations) { | 120 } else if (generateExpectations) { |
| 110 await openWrite(expectedFile.uri, (IOSink sink) { | 121 await openWrite(expectedFile.uri, (IOSink sink) { |
| 111 sink.write("$buffer".trim()); | 122 sink.write("$buffer".trim()); |
| 112 }); | 123 }); |
| 113 generated.add(expectedFile.uri); | 124 generated.add(expectedFile.uri); |
| 114 } else { | 125 } else { |
| 115 throw """ | 126 throw """ |
| 116 Please create file ${expectedFile.path} with this content: | 127 Please create file ${expectedFile.path} with this content: |
| 117 $buffer"""; | 128 $buffer"""; |
| 118 } | 129 } |
| 119 } | 130 } |
| OLD | NEW |