OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014, the Dart 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 file. | |
4 | |
5 import 'dart:async'; | |
6 import 'dart:io'; | |
7 | |
8 import 'package:args/args.dart'; | |
9 import 'package:matcher/matcher.dart'; | |
10 import 'package:path/path.dart' as path; | |
11 | |
12 import 'fuzz/server_manager.dart'; | |
13 | |
14 /** | |
15 * Start analysis server as a separate process and use the stdio to communicate | |
16 * with the server. | |
17 */ | |
18 void main(List<String> args) { | |
19 new _FuzzTest().run(args); | |
20 } | |
21 | |
22 /** | |
23 * Instances of [_FuzzTest] launch and test an analysis server. | |
24 * You must specify the location of the Dart SDK and the directory | |
25 * containing sources to be analyzed. | |
26 */ | |
27 class _FuzzTest { | |
28 | |
29 //TODO (danrubel) extract common behavior for use in multiple test scenarios | |
30 //TODO (danrubel) cleanup test to use async/await for better readability | |
31 // VM flag --enable_async | |
32 | |
33 static const String DART_SDK_OPTION = 'dart-sdk'; | |
34 static const String HELP_OPTION = 'help'; | |
35 | |
36 File serverSnapshot; | |
37 Directory appDir; | |
38 | |
39 /// Parse the arguments and initialize the receiver | |
40 /// Return `true` if proper arguments were provided | |
41 bool parseArgs(List<String> args) { | |
42 ArgParser parser = new ArgParser(); | |
43 | |
44 void error(String errMsg) { | |
45 stderr.writeln(errMsg); | |
46 print(''); | |
47 _printUsage(parser); | |
48 exitCode = 11; | |
49 } | |
50 | |
51 parser.addOption(DART_SDK_OPTION, help: '[sdkPath] path to Dart SDK'); | |
52 parser.addFlag( | |
53 HELP_OPTION, | |
54 help: 'print this help message without starting analysis', | |
55 defaultsTo: false, | |
56 negatable: false); | |
57 | |
58 ArgResults results; | |
59 try { | |
60 results = parser.parse(args); | |
61 } on FormatException catch (e) { | |
62 error(e.message); | |
63 return false; | |
64 } | |
65 if (results[HELP_OPTION]) { | |
66 _printUsage(parser); | |
67 return false; | |
68 } | |
69 String sdkPath = results[DART_SDK_OPTION]; | |
70 if (sdkPath is! String) { | |
71 error('Missing path to Dart SDK'); | |
72 return false; | |
73 } | |
74 Directory sdkDir = new Directory(sdkPath); | |
75 if (!sdkDir.existsSync()) { | |
76 error('Specified Dart SDK does not exist: $sdkPath'); | |
77 return false; | |
78 } | |
79 if (results.rest.length == 0) { | |
80 error('Expected directory to analyze'); | |
81 return false; | |
82 } | |
83 appDir = new Directory(results.rest[0]); | |
84 if (!appDir.existsSync()) { | |
lukechurch
2014/09/21 05:36:10
What is an application directory? Do you mean that
danrubel
2014/09/22 18:27:25
This is the root directory containing the files to
| |
85 error('Specified application directory does not exist: $appDir'); | |
86 return false; | |
87 } | |
88 if (results.rest.length > 1) { | |
89 error('Unexpected arguments after $appDir'); | |
90 return false; | |
91 } | |
92 serverSnapshot = new File( | |
93 path.join(sdkDir.path, 'bin', 'snapshots', 'analysis_server.dart.snapsho t')); | |
94 if (!serverSnapshot.existsSync()) { | |
95 error('Analysis Server snapshot not found: $serverSnapshot'); | |
96 return false; | |
97 } | |
98 return true; | |
99 } | |
100 | |
101 /// Main entry point for launching, testing, and shutting down the server | |
102 void run(List<String> args) { | |
103 if (!parseArgs(args)) return; | |
104 ServerManager.start(serverSnapshot.path).then((ServerManager manager) { | |
105 runZoned(() { | |
106 test(manager).then(manager.stop).then((_) { | |
107 expect(manager.errorOccurred, isFalse); | |
108 print('Test completed successfully'); | |
lukechurch
2014/09/21 05:36:11
I don't really see these as tests as such, but mor
danrubel
2014/09/22 18:27:25
I'm happy to refactor in subsequent CLs as we/I be
| |
109 }); | |
110 }, onError: (error, stack) { | |
111 stderr.writeln(error); | |
lukechurch
2014/09/21 05:36:11
From experience of writing a couple of these thing
danrubel
2014/09/22 18:27:25
Fair enough. Will refactor in subsequent CLs.
| |
112 print(stack); | |
113 exitCode = 12; | |
lukechurch
2014/09/21 05:36:11
Why 12?
danrubel
2014/09/22 18:27:25
Just picking a random exit code that's not 1 and u
| |
114 manager.stop(); | |
115 }); | |
116 }); | |
117 } | |
118 | |
119 /// Use manager to exercise the analysis server | |
120 Future test(ServerManager manager) { | |
121 | |
122 // perform initial analysis | |
123 return manager.analyze(appDir).then((AnalysisResults analysisResults) { | |
124 print( | |
125 'Found ${analysisResults.errorCount} errors,' | |
126 ' ${analysisResults.warningCount} warnings,' | |
127 ' and ${analysisResults.hintCount} hints in ${analysisResults.elap sed}'); | |
128 | |
129 // edit a method body | |
130 return manager.openFileNamed( | |
131 'domain_completion.dart').then((Editor editor) { | |
132 return editor.moveAfter('Response processRequest(Request request) {'); | |
133 }).then((Editor editor) { | |
134 return editor.replace(0, '\nOb'); | |
135 }).then((Editor editor) { | |
136 | |
137 // request code completion and assert results | |
138 return editor.getSuggestions().then((List<CompletionResults> list) { | |
139 expect(list, isNotNull); | |
140 expect(list.length, equals(0)); | |
lukechurch
2014/09/21 05:36:11
I don't follow this. Why would the list has of len
danrubel
2014/09/22 18:27:25
I'm getting an exception after text replacement bu
| |
141 list.forEach((CompletionResults results) { | |
142 print('${results.elapsed} received ${results.suggestionCount} sugges tions'); | |
143 }); | |
144 return editor; | |
145 }); | |
146 | |
147 }).then((Editor editor) { | |
148 print('tests complete'); | |
149 }); | |
150 }); | |
151 } | |
152 | |
153 void _printAnalysisSummary(AnalysisResults results) { | |
154 print( | |
155 'Found ${results.errorCount} errors, ${results.warningCount} warnings,' | |
156 ' and ${results.hintCount} hints in $results.elapsed'); | |
157 } | |
158 | |
159 /// Print information about how to use the server. | |
160 void _printUsage(ArgParser parser) { | |
161 print('Usage: analyzer [flags] <application_directory>'); | |
162 print(''); | |
163 print('Supported flags are:'); | |
164 print(parser.getUsage()); | |
165 } | |
166 } | |
OLD | NEW |