OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /// Common compiler options and helper functions used for testing. | |
6 library front_end.testing.compiler_options_common; | |
7 | |
8 import 'dart:async'; | |
9 | |
10 import 'package:front_end/front_end.dart'; | |
11 import 'package:front_end/memory_file_system.dart'; | |
12 import 'package:front_end/src/fasta/testing/patched_sdk_location.dart'; | |
13 import 'package:front_end/src/testing/hybrid_file_system.dart'; | |
14 import 'package:kernel/ast.dart'; | |
15 | |
16 /// Generate kernel for a script. | |
17 /// | |
18 /// [scriptOrSources] can be a String, in which case it is the script to be | |
19 /// compiled, or a Map containing source files. In which case, this function | |
20 /// compiles the entry whose name is [fileName]. | |
21 /// | |
22 /// Wraps [kernelForProgram] with some default testing options (see [setup]). | |
23 Future<Program> compileScript(dynamic scriptOrSources, | |
24 {fileName: 'main.dart', | |
25 List<String> inputSummaries: const [], | |
26 List<String> linkedDependencies: const [], | |
27 CompilerOptions options}) async { | |
28 options ??= new CompilerOptions(); | |
29 Map<String, dynamic> sources; | |
30 if (scriptOrSources is String) { | |
31 sources = {fileName: scriptOrSources}; | |
32 } else { | |
33 assert(scriptOrSources is Map); | |
34 sources = scriptOrSources; | |
35 } | |
36 await setup(options, sources, | |
37 inputSummaries: inputSummaries, linkedDependencies: linkedDependencies); | |
38 return await kernelForProgram(toTestUri(fileName), options); | |
39 } | |
40 | |
41 /// Generate a program for a modular complation unit. | |
42 /// | |
43 /// Wraps [kernelForBuildUnit] with some default testing options (see [setup]). | |
44 Future<Program> compileUnit(List<String> inputs, Map<String, dynamic> sources, | |
45 {List<String> inputSummaries: const [], | |
46 List<String> linkedDependencies: const [], | |
47 CompilerOptions options}) async { | |
48 options ??= new CompilerOptions(); | |
49 await setup(options, sources, | |
50 inputSummaries: inputSummaries, linkedDependencies: linkedDependencies); | |
51 return await kernelForBuildUnit(inputs.map(toTestUri).toList(), options); | |
52 } | |
53 | |
54 /// Generate a summary for a modular complation unit. | |
55 /// | |
56 /// Wraps [summaryFor] with some default testing options (see [setup]). | |
57 Future<List<int>> summarize(List<String> inputs, Map<String, dynamic> sources, | |
58 {List<String> inputSummaries: const [], CompilerOptions options}) async { | |
59 options ??= new CompilerOptions(); | |
60 await setup(options, sources, inputSummaries: inputSummaries); | |
61 return await summaryFor(inputs.map(toTestUri).toList(), options); | |
62 } | |
63 | |
64 /// Defines a default set of options for testing: | |
65 /// | |
66 /// * create a hybrid file system that stores [sources] in memory but allows | |
67 /// access to the physical file system to load the SDK. [sources] can | |
68 /// contain either source files (value is [String]) or .dill files (value | |
69 /// is [List<int>]). | |
70 /// | |
71 /// * define an empty .packages file | |
72 /// | |
73 /// * specify the location of the sdk and sdk summaries based on | |
74 /// the path where the `patched_sdk` is generated in the sdk-repo. | |
75 Future<Null> setup(CompilerOptions options, Map<String, dynamic> sources, | |
76 {List<String> inputSummaries: const [], | |
77 List<String> linkedDependencies: const []}) async { | |
78 var fs = new MemoryFileSystem(_defaultDir); | |
79 sources.forEach((name, data) { | |
80 var entity = fs.entityForUri(toTestUri(name)); | |
81 if (data is String) { | |
82 entity.writeAsStringSync(data); | |
83 } else { | |
84 entity.writeAsBytesSync(data); | |
85 } | |
86 }); | |
87 fs.entityForUri(toTestUri('.packages')).writeAsStringSync(''); | |
88 options | |
89 ..verify = true | |
90 ..fileSystem = new HybridFileSystem(fs) | |
91 ..inputSummaries = inputSummaries.map(toTestUri).toList() | |
92 ..linkedDependencies = linkedDependencies.map(toTestUri).toList() | |
93 ..packagesFileUri = toTestUri('.packages'); | |
94 | |
95 if (options.sdkSummary == null) { | |
96 options.sdkRoot = await computePatchedSdk(); | |
97 } | |
98 } | |
99 | |
100 /// A fake absolute directory used as the root of a memory-file system in the | |
101 /// helpers above. | |
102 Uri _defaultDir = Uri.parse('file:///a/b/c/'); | |
103 | |
104 /// Convert relative file paths into an absolute Uri as expected by the test | |
105 /// helpers above. | |
106 Uri toTestUri(String relativePath) => _defaultDir.resolve(relativePath); | |
107 | |
108 /// A map defining the location of core libraries that purposely provides | |
109 /// invalid Uris. Used by tests that want to ensure that the sdk libraries are | |
110 /// not loaded from sources, but read from a .dill file. | |
111 Map<String, Uri> invalidCoreLibs = { | |
112 'core': Uri.parse('file:///non_existing_file/core.dart'), | |
113 'async': Uri.parse('file:///non_existing_file/async.dart'), | |
114 }; | |
115 | |
116 bool isDartCoreLibrary(Library lib) => isDartCore(lib.importUri); | |
117 bool isDartCore(Uri uri) => uri.scheme == 'dart' && uri.path == 'core'; | |
118 | |
119 /// Find a library in [program] whose Uri ends with the given [suffix] | |
120 Library findLibrary(Program program, String suffix) { | |
121 return program.libraries | |
122 .firstWhere((lib) => lib.importUri.path.endsWith(suffix)); | |
123 } | |
OLD | NEW |