Skip to content

Commit a69c81a

Browse files
jensjohacommit-bot@chromium.org
authored andcommitted
Id testing for directories with .packages etc
We should also be able to test for larger examples, so this CL adds support for directories where all files are included. If the directory contains a .packages file that is used so we can test packages too. Change-Id: Iceda03505575bd9e5604a6e374b7f5fa7185a82e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/111421 Commit-Queue: Jens Johansen <[email protected]> Reviewed-by: Johnni Winther <[email protected]>
1 parent d49de8b commit a69c81a

File tree

14 files changed

+152
-38
lines changed

14 files changed

+152
-38
lines changed

pkg/front_end/analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ analyzer:
1010
- test/flow_analysis/nullability_and_reachability/data/**
1111
- test/flow_analysis/type_promotion/data/**
1212
- testcases/**
13+
- test/id_testing/data/**
1314
errors:
1415
# Allow having TODOs in the code
1516
todo: ignore

pkg/front_end/lib/src/testing/compiler_common.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import '../api_prototype/front_end.dart'
1616
kernelForProgramInternal,
1717
summaryFor;
1818

19-
import '../api_prototype/memory_file_system.dart' show MemoryFileSystem;
19+
import '../api_prototype/memory_file_system.dart'
20+
show MemoryFileSystem, MemoryFileSystemEntity;
2021

2122
import '../compute_platform_binaries_location.dart'
2223
show computePlatformBinariesLocation;
@@ -85,7 +86,7 @@ Future<List<int>> summarize(List<String> inputs, Map<String, dynamic> sources,
8586
/// contain either source files (value is [String]) or .dill files (value
8687
/// is [List<int>]).
8788
///
88-
/// * define an empty .packages file
89+
/// * define an empty .packages file (if one isn't defined in sources)
8990
///
9091
/// * specify the location of the sdk summaries.
9192
Future<Null> setup(CompilerOptions options, Map<String, dynamic> sources,
@@ -100,7 +101,9 @@ Future<Null> setup(CompilerOptions options, Map<String, dynamic> sources,
100101
entity.writeAsBytesSync(data);
101102
}
102103
});
103-
fs.entityForUri(toTestUri('.packages')).writeAsStringSync('');
104+
MemoryFileSystemEntity dotPackagesFile =
105+
fs.entityForUri(toTestUri('.packages'));
106+
if (!await dotPackagesFile.exists()) dotPackagesFile.writeAsStringSync('');
104107
fs
105108
.entityForUri(invalidCoreLibsSpecUri)
106109
.writeAsStringSync(_invalidLibrariesSpec);

pkg/front_end/lib/src/testing/id_extractor.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ abstract class DataExtractor<T> extends Visitor with DataRegistry<T> {
5656

5757
DataExtractor(this.actualMap);
5858

59-
void computeForLibrary(Library library) {
60-
LibraryId id = new LibraryId(library.importUri);
59+
void computeForLibrary(Library library, {bool useFileUri: false}) {
60+
LibraryId id =
61+
new LibraryId(useFileUri ? library.fileUri : library.importUri);
6162
T value = computeLibraryValue(id, library);
6263
registerValue(library.fileUri, null, id, value, library);
6364
}

pkg/front_end/lib/src/testing/id_testing.dart

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,35 @@ void computeExpectedMap(Uri sourceUri, String filename, AnnotatedCode code,
184184

185185
/// Creates a [TestData] object for the annotated test in [testFile].
186186
///
187+
/// If [testFile] is a file, use that directly. If it's a directory include
188+
/// everything in that directory.
189+
///
187190
/// If [testLibDirectory] is not `null`, files in [testLibDirectory] with the
188191
/// [testFile] name as a prefix are included.
189-
TestData computeTestData(File testFile, Directory testLibDirectory,
192+
TestData computeTestData(FileSystemEntity testFile, Directory testLibDirectory,
190193
{Iterable<String> supportedMarkers,
191194
Uri createUriForFileName(String fileName, {bool isLib}),
192195
void onFailure(String message)}) {
193196
Uri entryPoint = createUriForFileName('main.dart', isLib: false);
194-
String annotatedCode = new File.fromUri(testFile.uri).readAsStringSync();
197+
198+
File mainTestFile;
199+
Map<String, File> additionalFiles;
200+
if (testFile is File) {
201+
mainTestFile = testFile;
202+
} else if (testFile is Directory) {
203+
additionalFiles = new Map<String, File>();
204+
for (FileSystemEntity entry in testFile.listSync(recursive: true)) {
205+
if (entry is! File) continue;
206+
if (entry.uri.pathSegments.last == "main.dart") {
207+
mainTestFile = entry;
208+
} else {
209+
additionalFiles[entry.uri.path.substring(testFile.uri.path.length)] =
210+
entry;
211+
}
212+
}
213+
}
214+
215+
String annotatedCode = new File.fromUri(mainTestFile.uri).readAsStringSync();
195216
Map<Uri, AnnotatedCode> code = {
196217
entryPoint:
197218
new AnnotatedCode.fromText(annotatedCode, commentStart, commentEnd)
@@ -208,26 +229,36 @@ TestData computeTestData(File testFile, Directory testLibDirectory,
208229
};
209230

210231
List<String> libFileNames;
232+
233+
addSupportingFile(String libFileName, File libEntity, bool isLib) {
234+
libFileNames ??= [];
235+
libFileNames.add(libFileName);
236+
Uri libFileUri = createUriForFileName(libFileName, isLib: isLib);
237+
String libCode = libEntity.readAsStringSync();
238+
AnnotatedCode annotatedLibCode =
239+
new AnnotatedCode.fromText(libCode, commentStart, commentEnd);
240+
memorySourceFiles[libFileUri.path] = annotatedLibCode.sourceCode;
241+
code[libFileUri] = annotatedLibCode;
242+
computeExpectedMap(libFileUri, libFileName, annotatedLibCode, expectedMaps,
243+
onFailure: onFailure);
244+
}
245+
211246
if (testLibDirectory != null) {
212247
String name = testFile.uri.pathSegments.last;
213248
String filePrefix = name.substring(0, name.lastIndexOf('.'));
214249
for (FileSystemEntity libEntity in testLibDirectory.listSync()) {
215250
String libFileName = libEntity.uri.pathSegments.last;
216251
if (libFileName.startsWith(filePrefix)) {
217-
libFileNames ??= [];
218-
libFileNames.add(libFileName);
219-
Uri libFileUri = createUriForFileName(libFileName, isLib: true);
220-
String libCode = new File.fromUri(libEntity.uri).readAsStringSync();
221-
AnnotatedCode annotatedLibCode =
222-
new AnnotatedCode.fromText(libCode, commentStart, commentEnd);
223-
memorySourceFiles[libFileUri.path] = annotatedLibCode.sourceCode;
224-
code[libFileUri] = annotatedLibCode;
225-
computeExpectedMap(
226-
libFileUri, libFileName, annotatedLibCode, expectedMaps,
227-
onFailure: onFailure);
252+
addSupportingFile(libFileName, libEntity, true);
228253
}
229254
}
230255
}
256+
if (additionalFiles != null) {
257+
for (MapEntry<String, File> additionalFileData in additionalFiles.entries) {
258+
addSupportingFile(
259+
additionalFileData.key, additionalFileData.value, false);
260+
}
261+
}
231262

232263
return new TestData(testFile.uri, entryPoint, memorySourceFiles, code,
233264
expectedMaps, libFileNames);
@@ -613,12 +644,13 @@ Future runTests(Directory dataDir,
613644
}
614645
print('----------------------------------------------------------------');
615646

616-
TestData testData = computeTestData(entity, libDirectory,
647+
TestData testData = computeTestData(
648+
entity, entity is File ? libDirectory : null,
617649
supportedMarkers: supportedMarkers,
618650
createUriForFileName: createUriForFileName,
619651
onFailure: onFailure);
620652
print('Test file: ${testData.testFileUri}');
621-
if (testData.libFileNames != null) {
653+
if (!succinct && testData.libFileNames != null) {
622654
print('Supporting libraries:');
623655
for (String libFileName in testData.libFileNames) {
624656
print(' - libs/$libFileName');

pkg/front_end/lib/src/testing/id_testing_helper.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,10 @@ Future<bool> runTestForConfig<T>(
306306
}
307307

308308
for (Library library in component.libraries) {
309-
if (excludeLibrary(library)) continue;
309+
if (excludeLibrary(library) &&
310+
!testData.memorySourceFiles.containsKey(library.fileUri.path)) {
311+
continue;
312+
}
310313
dataComputer.computeLibraryData(
311314
compilerResult, library, actualMapFor(library));
312315
for (Class cls in library.classes) {
@@ -379,7 +382,6 @@ Future<bool> runTestForConfig<T>(
379382

380383
CfeCompiledData compiledData = new CfeCompiledData<T>(
381384
compilerResult, testData.testFileUri, actualMaps, globalData);
382-
383385
return checkCode(config.name, testData.testFileUri, testData.code,
384386
memberAnnotations, compiledData, dataComputer.dataValidator,
385387
fatalErrors: fatalErrors, succinct: succinct, onFailure: onFailure);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo:lib/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) 2019, 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+
/*library: file=foo.dart*/
6+
7+
/*member: fooMethod:fooMethod*/
8+
fooMethod() {
9+
print("Hello from foo!");
10+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2019, 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+
/*library: file=library.dart*/
6+
7+
import 'package:foo/foo.dart';
8+
9+
/*member: libraryInteger:libraryInteger*/
10+
int libraryInteger = 42;
11+
12+
/*member: methodFromLibrary:methodFromLibrary*/
13+
methodFromLibrary() {
14+
print(libraryInteger);
15+
fooMethod();
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) 2019, 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+
/*library: file=main.dart*/
6+
7+
import 'library.dart';
8+
9+
/*member: main:main*/
10+
main() {
11+
methodFromLibrary();
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright (c) 2019, 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+
# Analyzer workaround, see https://github.com/dart-lang/sdk/issues/37513

0 commit comments

Comments
 (0)