Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: pkg/front_end/test/src/base/processed_options_test.dart

Issue 2979003002: support resolving .packages in FE (Closed)
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2017, 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 import 'dart:async'; 5 import 'dart:async';
6 6
7 import 'package:front_end/compiler_options.dart'; 7 import 'package:front_end/compiler_options.dart';
8 import 'package:front_end/memory_file_system.dart'; 8 import 'package:front_end/memory_file_system.dart';
9 import 'package:front_end/src/base/processed_options.dart'; 9 import 'package:front_end/src/base/processed_options.dart';
10 import 'package:front_end/src/fasta/fasta.dart' show ByteSink; 10 import 'package:front_end/src/fasta/fasta.dart' show ByteSink;
11 import 'package:front_end/src/fasta/fasta_codes.dart';
11 import 'package:kernel/binary/ast_to_binary.dart' show BinaryPrinter; 12 import 'package:kernel/binary/ast_to_binary.dart' show BinaryPrinter;
12 import 'package:kernel/kernel.dart' show Program, Library, CanonicalName; 13 import 'package:kernel/kernel.dart' show Program, Library, CanonicalName;
14 import 'package:package_config/packages.dart' show Packages;
13 15
14 import 'package:test/test.dart'; 16 import 'package:test/test.dart';
15 import 'package:test_reflective_loader/test_reflective_loader.dart'; 17 import 'package:test_reflective_loader/test_reflective_loader.dart';
16 18
17 main() { 19 main() {
18 defineReflectiveSuite(() { 20 defineReflectiveSuite(() {
19 defineReflectiveTests(ProcessedOptionsTest); 21 defineReflectiveTests(ProcessedOptionsTest);
20 }); 22 });
21 } 23 }
22 24
23 @reflectiveTest 25 @reflectiveTest
24 class ProcessedOptionsTest { 26 class ProcessedOptionsTest {
25 final fileSystem = new MemoryFileSystem(Uri.parse('file:///')); 27 MemoryFileSystem fileSystem = new MemoryFileSystem(Uri.parse('file:///'));
26 28
27 Program _mockOutline; 29 Program _mockOutline;
28 30
29 Program get mockSummary => _mockOutline ??= 31 Program get mockSummary => _mockOutline ??=
30 new Program(libraries: [new Library(Uri.parse('file:///a/b.dart'))]); 32 new Program(libraries: [new Library(Uri.parse('file:///a/b.dart'))]);
31 33
32 test_compileSdk_false() { 34 test_compileSdk_false() {
33 for (var value in [false, true]) { 35 for (var value in [false, true]) {
34 var raw = new CompilerOptions()..compileSdk = value; 36 var raw = new CompilerOptions()..compileSdk = value;
35 var processed = new ProcessedOptions(raw); 37 var processed = new ProcessedOptions(raw);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 fileSystem.entityForUri(uri).writeAsBytesSync(sink.builder.takeBytes()); 77 fileSystem.entityForUri(uri).writeAsBytesSync(sink.builder.takeBytes());
76 } 78 }
77 79
78 Future<Null> checkMockSummary(CompilerOptions raw) async { 80 Future<Null> checkMockSummary(CompilerOptions raw) async {
79 var processed = new ProcessedOptions(raw); 81 var processed = new ProcessedOptions(raw);
80 var sdkSummary = await processed.loadSdkSummary(new CanonicalName.root()); 82 var sdkSummary = await processed.loadSdkSummary(new CanonicalName.root());
81 expect(sdkSummary.libraries.single.importUri, 83 expect(sdkSummary.libraries.single.importUri,
82 mockSummary.libraries.single.importUri); 84 mockSummary.libraries.single.importUri);
83 } 85 }
84 86
87 checkPackageExpansion(
88 String packageName, String packageDir, Packages packages) {
89 var input = Uri.parse('package:$packageName/a.dart');
90 var expected = Uri.parse('file:///$packageDir/a.dart');
ahe 2017/07/14 13:44:58 Is this a real file or a fake URI?
Siggi Cherem (dart-lang) 2017/07/14 19:54:07 All fake. Tracked in https://github.com/dart-lang/
91 expect(packages.resolve(input), expected);
92 }
93
85 test_getUriTranslator_explicitPackagesFile() async { 94 test_getUriTranslator_explicitPackagesFile() async {
86 // This .packages file should be ignored. 95 // This .packages file should be ignored.
87 fileSystem 96 fileSystem
88 .entityForUri(Uri.parse('file:///.packages')) 97 .entityForUri(Uri.parse('file:///.packages'))
89 .writeAsStringSync('foo:bar\n'); 98 .writeAsStringSync('foo:bar\n');
90 // This one should be used. 99 // This one should be used.
91 fileSystem 100 fileSystem
92 .entityForUri(Uri.parse('file:///explicit.packages')) 101 .entityForUri(Uri.parse('file:///explicit.packages'))
93 .writeAsStringSync('foo:baz\n'); 102 .writeAsStringSync('foo:baz\n');
94 var raw = new CompilerOptions() 103 var raw = new CompilerOptions()
95 ..fileSystem = fileSystem 104 ..fileSystem = fileSystem
96 ..packagesFileUri = Uri.parse('file:///explicit.packages'); 105 ..packagesFileUri = Uri.parse('file:///explicit.packages');
97 var processed = new ProcessedOptions(raw); 106 var processed = new ProcessedOptions(raw);
98 var uriTranslator = await processed.getUriTranslator(); 107 var uriTranslator = await processed.getUriTranslator();
99 expect(uriTranslator.packages, {'foo': Uri.parse('file:///baz/')}); 108 checkPackageExpansion('foo', 'baz', uriTranslator.packages);
100 } 109 }
101 110
102 test_getUriTranslator_explicitPackagesFile_withBaseLocation() async { 111 test_getUriTranslator_explicitPackagesFile_withBaseLocation() async {
103 // This .packages file should be ignored. 112 // This .packages file should be ignored.
104 fileSystem 113 fileSystem
105 .entityForUri(Uri.parse('file:///.packages')) 114 .entityForUri(Uri.parse('file:///.packages'))
106 .writeAsStringSync('foo:bar\n'); 115 .writeAsStringSync('foo:bar\n');
107 // This one should be used. 116 // This one should be used.
108 fileSystem 117 fileSystem
109 .entityForUri(Uri.parse('file:///base/location/explicit.packages')) 118 .entityForUri(Uri.parse('file:///base/location/explicit.packages'))
110 .writeAsStringSync('foo:baz\n'); 119 .writeAsStringSync('foo:baz\n');
111 var raw = new CompilerOptions() 120 var raw = new CompilerOptions()
112 ..fileSystem = fileSystem 121 ..fileSystem = fileSystem
113 ..packagesFileUri = Uri.parse('file:///base/location/explicit.packages'); 122 ..packagesFileUri = Uri.parse('file:///base/location/explicit.packages');
114 var processed = new ProcessedOptions(raw); 123 var processed = new ProcessedOptions(raw);
115 var uriTranslator = await processed.getUriTranslator(); 124 var uriTranslator = await processed.getUriTranslator();
116 expect(uriTranslator.packages, 125 checkPackageExpansion('foo', 'base/location/baz', uriTranslator.packages);
117 {'foo': Uri.parse('file:///base/location/baz/')}); 126 }
127
128 test_getUriTranslator_implicitPackagesFile_ambiguous() async {
129 // This .packages file should be ignored.
130 fileSystem
131 .entityForUri(Uri.parse('file:///.packages'))
ahe 2017/07/14 13:44:58 Use custom URI. I don't like seeing error message
Siggi Cherem (dart-lang) 2017/07/14 19:54:06 tracking here https://github.com/dart-lang/sdk/iss
132 .writeAsStringSync('foo:bar\n');
133 // This one should be used.
134 fileSystem
135 .entityForUri(Uri.parse('file:///explicit.packages'))
136 .writeAsStringSync('foo:baz\n');
137 var raw = new CompilerOptions()
138 ..fileSystem = fileSystem
139 ..packagesFileUri = Uri.parse('file:///explicit.packages');
140 var processed = new ProcessedOptions(raw);
141 var uriTranslator = await processed.getUriTranslator();
142 checkPackageExpansion('foo', 'baz', uriTranslator.packages);
143 }
144
145 test_getUriTranslator_implicitPackagesFile_nextToScript() async {
146 // Fake the existence of the base directory.
147 fileSystem
148 .entityForUri(Uri.parse('file:///base/location/'))
149 .writeAsStringSync('');
150 // Packages directory should be ignored (.packages file takes precedence).
151 fileSystem
152 .entityForUri(Uri.parse('file:///base/location/packages/'))
153 .writeAsStringSync('');
154 // This .packages file should be ignored.
155 fileSystem
156 .entityForUri(Uri.parse('file:///.packages'))
157 .writeAsStringSync('foo:bar\n');
158 // This one should be used.
159 fileSystem
160 .entityForUri(Uri.parse('file:///base/location/.packages'))
161 .writeAsStringSync('foo:baz\n');
162 var raw = new CompilerOptions()..fileSystem = fileSystem;
163 var processed = new ProcessedOptions(
164 raw, false, [Uri.parse('file:///base/location/script.dart')]);
165 var uriTranslator = await processed.getUriTranslator();
166 checkPackageExpansion('foo', 'base/location/baz', uriTranslator.packages);
167 }
168
169 test_getUriTranslator_implicitPackagesFile_searchAbove() async {
170 // Fake the existence of the base directory.
171 fileSystem
172 .entityForUri(Uri.parse('file:///base/location/'))
173 .writeAsStringSync('');
174 // This .packages file should be ignored.
175 fileSystem
176 .entityForUri(Uri.parse('file:///.packages'))
177 .writeAsStringSync('foo:bar\n');
178 // This one should be used.
179 fileSystem
180 .entityForUri(Uri.parse('file:///base/.packages'))
181 .writeAsStringSync('foo:baz\n');
182 var raw = new CompilerOptions()..fileSystem = fileSystem;
183 var processed = new ProcessedOptions(
184 raw, false, [Uri.parse('file:///base/location/script.dart')]);
185 var uriTranslator = await processed.getUriTranslator();
186 checkPackageExpansion('foo', 'base/baz', uriTranslator.packages);
187 }
188
189 test_getUriTranslator_implicitPackagesFile_packagesDirectory() async {
190 // Fake the existence of the base directory.
191 fileSystem
192 .entityForUri(Uri.parse('file:///base/location/'))
193 .writeAsStringSync('');
194 fileSystem
195 .entityForUri(Uri.parse('file:///base/location/packages/'))
196 .writeAsStringSync('');
197
198 // Both of these .packages file should be ignored.
199 fileSystem
200 .entityForUri(Uri.parse('file:///.packages'))
201 .writeAsStringSync('foo:bar\n');
202 fileSystem
203 .entityForUri(Uri.parse('file:///base/.packages'))
204 .writeAsStringSync('foo:baz\n');
205 var raw = new CompilerOptions()..fileSystem = fileSystem;
206 var processed = new ProcessedOptions(
207 raw, false, [Uri.parse('file:///base/location/script.dart')]);
208 var uriTranslator = await processed.getUriTranslator();
209 checkPackageExpansion(
210 'foo', 'base/location/packages/foo', uriTranslator.packages);
211 }
212
213 test_getUriTranslator_implicitPackagesFile_noPackages() async {
214 // Fake the existence of the base directory.
215 fileSystem
216 .entityForUri(Uri.parse('file:///base/location/'))
217 .writeAsStringSync('');
218 var errors = [];
219 // .packages file should be ignored.
220 var raw = new CompilerOptions()
221 ..fileSystem = fileSystem
222 ..onError = (e) => errors.add(e);
223 var processed = new ProcessedOptions(
224 raw, false, [Uri.parse('file:///base/location/script.dart')]);
225 var uriTranslator = await processed.getUriTranslator();
226 expect(errors, isEmpty);
227 expect(uriTranslator.packages.asMap(), isEmpty);
118 } 228 }
119 229
120 test_getUriTranslator_noPackages() async { 230 test_getUriTranslator_noPackages() async {
231 var errors = [];
121 // .packages file should be ignored. 232 // .packages file should be ignored.
122 fileSystem 233 fileSystem
123 .entityForUri(Uri.parse('file:///.packages')) 234 .entityForUri(Uri.parse('file:///.packages'))
124 .writeAsStringSync('foo:bar\n'); 235 .writeAsStringSync('foo:bar\n');
125 var raw = new CompilerOptions() 236 var raw = new CompilerOptions()
126 ..fileSystem = fileSystem 237 ..fileSystem = fileSystem
127 ..packagesFileUri = new Uri(); 238 ..packagesFileUri = new Uri()
239 ..onError = (e) => errors.add(e);
128 var processed = new ProcessedOptions(raw); 240 var processed = new ProcessedOptions(raw);
129 var uriTranslator = await processed.getUriTranslator(); 241 var uriTranslator = await processed.getUriTranslator();
130 expect(uriTranslator.packages, isEmpty); 242 expect(uriTranslator.packages.asMap(), isEmpty);
243 expect(errors.single.message,
244 startsWith(_stringPrefixOf(templateInvalidPackagesFile)));
245 }
246
247 test_validateOptions_noInputs() async {
248 fileSystem
249 .entityForUri(Uri.parse('file:///foo.dart'))
250 .writeAsStringSync('main(){}\n');
251 var errors = [];
252 var raw = new CompilerOptions()
253 ..fileSystem = fileSystem
254 ..onError = (e) => errors.add(e);
255 var options = new ProcessedOptions(raw);
256 var result = await options.validateOptions();
257 expect(errors.single.message, messageMissingInputs.message);
258 expect(result, isFalse);
259 }
260
261 test_validateOptions_input_doesnt_exist() async {
262 var errors = [];
263 var raw = new CompilerOptions()
264 ..fileSystem = fileSystem
265 ..onError = (e) => errors.add(e);
266 var options = new ProcessedOptions(raw, false, [Uri.parse('foo.dart')]);
267 var result = await options.validateOptions();
268 expect(errors.single.message,
269 startsWith(_stringPrefixOf(templateMissingInputFile)));
270 expect(result, isFalse);
131 } 271 }
132 272
133 test_validateOptions_root_exists() async { 273 test_validateOptions_root_exists() async {
134 var sdkRoot = Uri.parse('file:///sdk/root/'); 274 var sdkRoot = Uri.parse('file:///sdk/root/');
135 fileSystem 275 fileSystem
136 // Note: this test is a bit hackish because the memory file system 276 // Note: this test is a bit hackish because the memory file system
137 // doesn't have the notion of directories. 277 // doesn't have the notion of directories.
138 .entityForUri(sdkRoot) 278 .entityForUri(sdkRoot)
139 .writeAsStringSync('\n'); 279 .writeAsStringSync('\n');
140 fileSystem 280 fileSystem
141 .entityForUri(sdkRoot.resolve('outline.dill')) 281 .entityForUri(sdkRoot.resolve('outline.dill'))
142 .writeAsStringSync('\n'); 282 .writeAsStringSync('\n');
283 fileSystem
284 .entityForUri(Uri.parse('file:///foo.dart'))
285 .writeAsStringSync('main(){}\n');
143 286
144 var errors = []; 287 var errors = [];
145 var raw = new CompilerOptions() 288 var raw = new CompilerOptions()
146 ..sdkRoot = sdkRoot 289 ..sdkRoot = sdkRoot
147 ..fileSystem = fileSystem 290 ..fileSystem = fileSystem
148 ..onError = (e) => errors.add(e); 291 ..onError = (e) => errors.add(e);
149 var options = new ProcessedOptions(raw); 292 var options = new ProcessedOptions(raw, false, [Uri.parse('foo.dart')]);
150 var result = await options.validateOptions(); 293 var result = await options.validateOptions();
151 // Note: we check this first so test failures show the cause directly. 294 // Note: we check this first so test failures show the cause directly.
152 expect(errors, isEmpty); 295 expect(errors, isEmpty);
153 expect(result, isTrue); 296 expect(result, isTrue);
154 } 297 }
155 298
156 test_validateOptions_root_doesnt_exists() async { 299 test_validateOptions_root_doesnt_exists() async {
300 fileSystem
301 .entityForUri(Uri.parse('file:///foo.dart'))
302 .writeAsStringSync('main(){}\n');
157 var sdkRoot = Uri.parse('file:///sdk/root'); 303 var sdkRoot = Uri.parse('file:///sdk/root');
158 var errors = []; 304 var errors = [];
159 var raw = new CompilerOptions() 305 var raw = new CompilerOptions()
160 ..sdkRoot = sdkRoot 306 ..sdkRoot = sdkRoot
161 ..fileSystem = fileSystem 307 ..fileSystem = fileSystem
162 ..onError = (e) => errors.add(e); 308 ..onError = (e) => errors.add(e);
163 var options = new ProcessedOptions(raw); 309 var options = new ProcessedOptions(raw, false, [Uri.parse('foo.dart')]);
164 expect(await options.validateOptions(), isFalse); 310 expect(await options.validateOptions(), isFalse);
165 expect(errors.first.message, contains("SDK root directory not found")); 311 expect(errors.first.message,
312 startsWith(_stringPrefixOf(templateMissingSdkRoot)));
166 } 313 }
167 314
168 test_validateOptions_summary_exists() async { 315 test_validateOptions_summary_exists() async {
169 var sdkSummary = Uri.parse('file:///sdk/root/outline.dill'); 316 var sdkSummary = Uri.parse('file:///sdk/root/outline.dill');
170 fileSystem.entityForUri(sdkSummary).writeAsStringSync('\n'); 317 fileSystem.entityForUri(sdkSummary).writeAsStringSync('\n');
318 fileSystem
319 .entityForUri(Uri.parse('file:///foo.dart'))
320 .writeAsStringSync('main(){}\n');
171 321
172 var errors = []; 322 var errors = [];
173 var raw = new CompilerOptions() 323 var raw = new CompilerOptions()
174 ..sdkSummary = sdkSummary 324 ..sdkSummary = sdkSummary
175 ..fileSystem = fileSystem 325 ..fileSystem = fileSystem
176 ..onError = (e) => errors.add(e); 326 ..onError = (e) => errors.add(e);
177 var options = new ProcessedOptions(raw); 327 var options = new ProcessedOptions(raw, false, [Uri.parse('foo.dart')]);
178 var result = await options.validateOptions(); 328 var result = await options.validateOptions();
179 expect(errors, isEmpty); 329 expect(errors, isEmpty);
180 expect(result, isTrue); 330 expect(result, isTrue);
181 } 331 }
182 332
183 test_validateOptions_summary_doesnt_exists() async { 333 test_validateOptions_summary_doesnt_exists() async {
334 fileSystem
335 .entityForUri(Uri.parse('file:///foo.dart'))
336 .writeAsStringSync('main(){}\n');
184 var sdkSummary = Uri.parse('file:///sdk/root/outline.dill'); 337 var sdkSummary = Uri.parse('file:///sdk/root/outline.dill');
185 var errors = []; 338 var errors = [];
186 var raw = new CompilerOptions() 339 var raw = new CompilerOptions()
187 ..sdkSummary = sdkSummary 340 ..sdkSummary = sdkSummary
188 ..fileSystem = fileSystem 341 ..fileSystem = fileSystem
189 ..onError = (e) => errors.add(e); 342 ..onError = (e) => errors.add(e);
190 var options = new ProcessedOptions(raw); 343 var options = new ProcessedOptions(raw, false, [Uri.parse('foo.dart')]);
191 expect(await options.validateOptions(), isFalse); 344 expect(await options.validateOptions(), isFalse);
192 expect(errors.first.message, contains("SDK summary not found")); 345 expect(errors.single.message,
346 startsWith(_stringPrefixOf(templateMissingSdkSummary)));
193 } 347 }
194 348
195 test_validateOptions_inferred_summary_exists() async { 349 test_validateOptions_inferred_summary_exists() async {
196 var sdkRoot = Uri.parse('file:///sdk/root/'); 350 var sdkRoot = Uri.parse('file:///sdk/root/');
197 var sdkSummary = Uri.parse('file:///sdk/root/outline.dill'); 351 var sdkSummary = Uri.parse('file:///sdk/root/outline.dill');
198 fileSystem.entityForUri(sdkRoot).writeAsStringSync('\n'); 352 fileSystem.entityForUri(sdkRoot).writeAsStringSync('\n');
199 fileSystem.entityForUri(sdkSummary).writeAsStringSync('\n'); 353 fileSystem.entityForUri(sdkSummary).writeAsStringSync('\n');
354 fileSystem
355 .entityForUri(Uri.parse('file:///foo.dart'))
356 .writeAsStringSync('main(){}\n');
200 357
201 var errors = []; 358 var errors = [];
202 var raw = new CompilerOptions() 359 var raw = new CompilerOptions()
203 ..sdkRoot = sdkRoot 360 ..sdkRoot = sdkRoot
204 ..fileSystem = fileSystem 361 ..fileSystem = fileSystem
205 ..onError = (e) => errors.add(e); 362 ..onError = (e) => errors.add(e);
206 var options = new ProcessedOptions(raw); 363 var options = new ProcessedOptions(raw, false, [Uri.parse('foo.dart')]);
207 var result = await options.validateOptions(); 364 var result = await options.validateOptions();
208 expect(errors, isEmpty); 365 expect(errors, isEmpty);
209 expect(result, isTrue); 366 expect(result, isTrue);
210 } 367 }
211 368
212 test_validateOptions_inferred_summary_doesnt_exists() async { 369 test_validateOptions_inferred_summary_doesnt_exists() async {
213 var sdkRoot = Uri.parse('file:///sdk/root/'); 370 var sdkRoot = Uri.parse('file:///sdk/root/');
214 var sdkSummary = Uri.parse('file:///sdk/root/outline.dill'); 371 var sdkSummary = Uri.parse('file:///sdk/root/outline.dill');
215 fileSystem.entityForUri(sdkRoot).writeAsStringSync('\n'); 372 fileSystem.entityForUri(sdkRoot).writeAsStringSync('\n');
373 fileSystem
374 .entityForUri(Uri.parse('file:///foo.dart'))
375 .writeAsStringSync('main(){}\n');
216 var errors = []; 376 var errors = [];
217 var raw = new CompilerOptions() 377 var raw = new CompilerOptions()
218 ..sdkSummary = sdkSummary 378 ..sdkSummary = sdkSummary
219 ..fileSystem = fileSystem 379 ..fileSystem = fileSystem
220 ..onError = (e) => errors.add(e); 380 ..onError = (e) => errors.add(e);
221 var options = new ProcessedOptions(raw); 381 var options = new ProcessedOptions(raw, false, [Uri.parse('foo.dart')]);
222 expect(await options.validateOptions(), isFalse); 382 expect(await options.validateOptions(), isFalse);
223 expect(errors.first.message, contains("SDK summary not found")); 383 expect(errors.single.message,
384 startsWith(_stringPrefixOf(templateMissingSdkSummary)));
385 }
386
387 /// Returns the longest prefix of the text in a message template that doesn't
388 /// mention a template argument.
389 _stringPrefixOf(Template template) {
390 var messageTemplate = template.messageTemplate;
391 var index = messageTemplate.indexOf('#');
392 var prefix = messageTemplate.substring(0, index - 1);
393
394 // Check that the prefix is not empty and that it contains more than one
395 // word.
396 expect(prefix.length > 0, isTrue);
397 expect(prefix.contains(' '), isTrue);
398 return prefix;
224 } 399 }
225 } 400 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698