OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 library fletchc.test.run; | |
6 | |
7 import 'dart:async'; | |
8 | |
9 import 'dart:io'; | |
10 | |
11 import 'dart:io' as io; | |
12 | |
13 import 'package:fletchc/src/hub/session_manager.dart'; | |
14 | |
15 import 'package:fletchc/src/worker/developer.dart'; | |
16 | |
17 import 'package:fletchc/src/worker/developer.dart' as developer; | |
18 | |
19 import 'package:fletchc/src/verbs/infrastructure.dart' show fileUri; | |
20 | |
21 import 'package:fletchc/src/device_type.dart' show | |
22 DeviceType, | |
23 parseDeviceType; | |
24 | |
25 const String userVmAddress = const String.fromEnvironment("attachToVm"); | |
26 | |
27 const String exportTo = const String.fromEnvironment("snapshot"); | |
28 | |
29 const String userPackages = const String.fromEnvironment("packages"); | |
30 | |
31 const String userAgentAddress = const String.fromEnvironment("agent"); | |
32 | |
33 const String fletchSettingsFile = | |
34 const String.fromEnvironment("test.fletch_settings_file_name"); | |
35 | |
36 /// Enables printing of Compiler/VM protocol commands after each compilation. | |
37 const bool printCommands = const bool.fromEnvironment("printCommands"); | |
38 | |
39 /// Enables pretty printing the Fletch system (the compilation result) after | |
40 /// each compilation. | |
41 const bool printSystem = const bool.fromEnvironment("printSystem"); | |
42 | |
43 class FletchRunner { | |
44 Future<Null> attach(SessionState state) async { | |
45 if (userVmAddress == null) { | |
46 await startAndAttachDirectly(state, Uri.base); | |
47 } else { | |
48 Address address = parseAddress(userVmAddress); | |
49 await attachToVm(address.host, address.port, state); | |
50 } | |
51 } | |
52 | |
53 Future<Settings> computeSettings() async { | |
54 if (fletchSettingsFile != null) { | |
55 return await readSettings(fileUri(fletchSettingsFile, Uri.base)); | |
56 } | |
57 Address agentAddress = | |
58 userAgentAddress == null ? null : parseAddress(userAgentAddress); | |
59 return new Settings( | |
60 fileUri(userPackages == null ? ".packages" : userPackages, Uri.base), | |
61 ["--verbose"], | |
62 <String, String>{ | |
63 "foo": "1", | |
64 "bar": "baz", | |
65 }, | |
66 agentAddress, | |
67 DeviceType.mobile, | |
68 IncrementalMode.production); | |
69 } | |
70 | |
71 Future<int> run(List<String> arguments, {int expectedExitCode: 0}) async { | |
72 Settings settings = await computeSettings(); | |
73 SessionState state = createSessionState("test", settings); | |
74 for (String script in arguments) { | |
75 print("Compiling $script"); | |
76 await compile(fileUri(script, Uri.base), state, Uri.base); | |
77 if (state.compilationResults.isNotEmpty) { | |
78 // Always generate the debug string to ensure test coverage. | |
79 String debugString = | |
80 state.compilationResults.last.system.toDebugString(Uri.base); | |
81 if (printSystem) { | |
82 // But only print the debug string if requested. | |
83 print(debugString); | |
84 } | |
85 if (printCommands) { | |
86 print("Compiled $script"); | |
87 for (var delta in state.compilationResults) { | |
88 print("\nDelta:"); | |
89 for (var cmd in delta.commands) { | |
90 print(cmd); | |
91 } | |
92 } | |
93 } | |
94 } | |
95 await attach(state); | |
96 state.stdoutSink.attachCommandSender(stdout.add); | |
97 state.stderrSink.attachCommandSender(stderr.add); | |
98 | |
99 if (exportTo != null) { | |
100 await developer.export(state, fileUri(exportTo, Uri.base)); | |
101 } else { | |
102 await developer.run(state); | |
103 } | |
104 if (state.fletchVm != null) { | |
105 int exitCode = await state.fletchVm.exitCode; | |
106 print("$script: Fletch VM exit code: $exitCode"); | |
107 if (exitCode != expectedExitCode) { | |
108 return exitCode; | |
109 } | |
110 } | |
111 } | |
112 print(state.getLog()); | |
113 return 0; | |
114 } | |
115 } | |
116 | |
117 main(List<String> arguments) async { | |
118 io.exitCode = await new FletchRunner().run(arguments); | |
119 } | |
120 | |
121 void checkExitCode(int expected, int actual) { | |
122 if (expected != actual) { | |
123 throw "Unexpected exit code: $expected != $actual"; | |
124 } | |
125 } | |
126 | |
127 Future<Null> test() async { | |
128 checkExitCode( | |
129 0, await new FletchRunner().run( | |
130 <String>['tests/language/application_test.dart'])); | |
131 } | |
132 | |
133 Future<Null> testIncrementalDebugInfo() async { | |
134 checkExitCode( | |
135 0, await new FletchRunner().run( | |
136 <String>['tests/fletchc/test_incremental_debug_info.dart', | |
137 'tests/fletchc/test_incremental_debug_info.dart'], | |
138 expectedExitCode: 255)); | |
139 } | |
140 | |
141 // TODO(ahe): Move this method into FletchRunner and use computeSettings. | |
142 Future<Null> export( | |
143 String script, String snapshot, {bool binaryProgramInfo: false, | |
144 Map<String, String> constants: const <String, String> {}}) async { | |
145 Settings settings; | |
146 if (fletchSettingsFile == null) { | |
147 settings = new Settings( | |
148 fileUri(".packages", Uri.base), | |
149 <String>[], | |
150 constants, | |
151 null, | |
152 null, | |
153 IncrementalMode.none); | |
154 } else { | |
155 settings = await readSettings(fileUri(fletchSettingsFile, Uri.base)); | |
156 } | |
157 SessionState state = createSessionState("test", settings); | |
158 await compile(fileUri(script, Uri.base), state, Uri.base); | |
159 await startAndAttachDirectly(state, Uri.base); | |
160 state.stdoutSink.attachCommandSender(stdout.add); | |
161 state.stderrSink.attachCommandSender(stderr.add); | |
162 await developer.export( | |
163 state, fileUri(snapshot, Uri.base), binaryProgramInfo: binaryProgramInfo); | |
164 } | |
OLD | NEW |