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 library kernel.batch_consistency; | 4 library kernel.batch_consistency; |
5 | 5 |
6 import 'dart:async'; | 6 import 'dart:async'; |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 import '../bin/dartk.dart' as dartk; | 8 import '../bin/dartk.dart' as dartk; |
9 import '../bin/batch_util.dart'; | 9 import '../bin/batch_util.dart'; |
10 import 'package:path/path.dart' as pathlib; | 10 import 'package:path/path.dart' as pathlib; |
(...skipping 17 matching lines...) Expand all Loading... |
28 | 28 |
29 List<String> options = args.sublist(0, separator); | 29 List<String> options = args.sublist(0, separator); |
30 List<String> files = args.sublist(separator + 1); | 30 List<String> files = args.sublist(separator + 1); |
31 | 31 |
32 await new Directory(outputDir).create(recursive: true); | 32 await new Directory(outputDir).create(recursive: true); |
33 | 33 |
34 testBatchModeConsistency(options, files); | 34 testBatchModeConsistency(options, files); |
35 } | 35 } |
36 | 36 |
37 Future<bool> areFilesEqual(String first, String second) async { | 37 Future<bool> areFilesEqual(String first, String second) async { |
38 List<List<int>> bytes = await Future.wait([ | 38 List<List<int>> bytes = await Future |
39 new File(first).readAsBytes(), | 39 .wait([new File(first).readAsBytes(), new File(second).readAsBytes()]); |
40 new File(second).readAsBytes() | |
41 ]); | |
42 if (bytes[0].length != bytes[1].length) return false; | 40 if (bytes[0].length != bytes[1].length) return false; |
43 for (int i = 0; i < bytes[0].length; ++i) { | 41 for (int i = 0; i < bytes[0].length; ++i) { |
44 if (bytes[0][i] != bytes[1][i]) return false; | 42 if (bytes[0][i] != bytes[1][i]) return false; |
45 } | 43 } |
46 return true; | 44 return true; |
47 } | 45 } |
48 | 46 |
49 testBatchModeConsistency(List<String> options, List<String> files) { | 47 testBatchModeConsistency(List<String> options, List<String> files) { |
50 var sharedState = new dartk.BatchModeState(); | 48 var sharedState = new dartk.BatchModeState(); |
51 for (String file in files) { | 49 for (String file in files) { |
52 test(file, () async { | 50 test(file, () async { |
53 var name = pathlib.basename(file); | 51 var name = pathlib.basename(file); |
54 List<String> outputFiles = <String>[ | 52 List<String> outputFiles = <String>[ |
55 '$outputDir/$name.batch.dill', | 53 '$outputDir/$name.batch.dill', |
56 '$outputDir/$name.unbatch.dill' | 54 '$outputDir/$name.unbatch.dill' |
57 ]; | 55 ]; |
58 List results = [null, null]; | 56 List results = [null, null]; |
59 bool failed = false; | 57 bool failed = false; |
60 for (int i = 0; i < 2; ++i) { | 58 for (int i = 0; i < 2; ++i) { |
61 var args = []..addAll(options)..addAll(['--out', outputFiles[i], file]); | 59 var args = <String>[] |
| 60 ..addAll(options) |
| 61 ..addAll(['--out', outputFiles[i], file]); |
62 var state = (i == 0) ? sharedState : new dartk.BatchModeState(); | 62 var state = (i == 0) ? sharedState : new dartk.BatchModeState(); |
63 try { | 63 try { |
64 // We run the two executions in a loop to ensure any stack traces | 64 // We run the two executions in a loop to ensure any stack traces |
65 // are identical in case they both crash at the same place. | 65 // are identical in case they both crash at the same place. |
66 // Crashing at the same place is acceptable for the purpose of | 66 // Crashing at the same place is acceptable for the purpose of |
67 // this test, there are other tests that check for crashes. | 67 // this test, there are other tests that check for crashes. |
68 results[i] = await dartk.batchMain(args, state); | 68 results[i] = await dartk.batchMain(args, state); |
69 } catch (e) { | 69 } catch (e) { |
70 results[i] = '$e'; | 70 results[i] = '$e'; |
71 failed = true; | 71 failed = true; |
72 } | 72 } |
73 } | 73 } |
74 if (results[0] != results[1]) { | 74 if (results[0] != results[1]) { |
75 fail('Batch mode returned ${results[0]}, expected ${results[1]}'); | 75 fail('Batch mode returned ${results[0]}, expected ${results[1]}'); |
76 return; | 76 return; |
77 } | 77 } |
78 if (results[0] == CompilerOutcome.Fail) { | 78 if (results[0] == CompilerOutcome.Fail) { |
79 failed = true; | 79 failed = true; |
80 } | 80 } |
81 if (!failed && !await areFilesEqual(outputFiles[0], outputFiles[1])) { | 81 if (!failed && !await areFilesEqual(outputFiles[0], outputFiles[1])) { |
82 fail('Batch mode output differs for $file'); | 82 fail('Batch mode output differs for $file'); |
83 } | 83 } |
84 }); | 84 }); |
85 } | 85 } |
86 } | 86 } |
OLD | NEW |