51046368c5
This gives us more flexibility for how we want to publish and deploy the tool. We now have the option, for example, of making a command line app that invokes the tool and does not depend on analysis_server. Note that some testing infrastructure had to be duplicated. I plan to consolidate this infrastructure in follow-up CLs. Change-Id: I046506bc2bb5c3e467e15885f198ee0632351ee9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/105463 Commit-Queue: Paul Berry <paulberry@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
141 lines
4.6 KiB
Dart
141 lines
4.6 KiB
Dart
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
import 'package:analyzer/dart/analysis/analysis_context.dart';
|
|
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
|
|
import 'package:analyzer/dart/analysis/session.dart';
|
|
import 'package:analyzer/file_system/file_system.dart';
|
|
import 'package:analyzer/file_system/overlay_file_system.dart';
|
|
import 'package:analyzer/src/dart/analysis/analysis_context_collection.dart';
|
|
import 'package:analyzer/src/dart/analysis/driver.dart';
|
|
import 'package:analyzer/src/dart/analysis/driver_based_analysis_context.dart';
|
|
import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine;
|
|
import 'package:analyzer/src/generated/source.dart';
|
|
import 'package:analyzer/src/test_utilities/mock_sdk.dart';
|
|
import 'package:analyzer/src/test_utilities/resource_provider_mixin.dart';
|
|
|
|
/// TODO(paulberry): this logic is duplicated from other packages. Find a way
|
|
/// share it, or avoid relying on it.
|
|
class AbstractContextTest with ResourceProviderMixin {
|
|
OverlayResourceProvider overlayResourceProvider;
|
|
|
|
AnalysisContextCollection _analysisContextCollection;
|
|
AnalysisDriver _driver;
|
|
|
|
AnalysisDriver get driver => _driver;
|
|
|
|
AnalysisSession get session => driver.currentSession;
|
|
|
|
void addMetaPackage() {
|
|
addPackageFile('meta', 'meta.dart', r'''
|
|
library meta;
|
|
|
|
const Required required = const Required();
|
|
|
|
class Required {
|
|
final String reason;
|
|
const Required([this.reason]);
|
|
}
|
|
''');
|
|
}
|
|
|
|
/// Add a new file with the given [pathInLib] to the package with the
|
|
/// given [packageName]. Then ensure that the test package depends on the
|
|
/// [packageName].
|
|
File addPackageFile(String packageName, String pathInLib, String content) {
|
|
var packagePath = '/.pub-cache/$packageName';
|
|
_addTestPackageDependency(packageName, packagePath);
|
|
return newFile('$packagePath/lib/$pathInLib', content: content);
|
|
}
|
|
|
|
Source addSource(String path, String content, [Uri uri]) {
|
|
File file = newFile(path, content: content);
|
|
Source source = file.createSource(uri);
|
|
driver.addFile(file.path);
|
|
driver.changeFile(file.path);
|
|
return source;
|
|
}
|
|
|
|
/// Create all analysis contexts in `/home`.
|
|
void createAnalysisContexts() {
|
|
_analysisContextCollection = AnalysisContextCollectionImpl(
|
|
includedPaths: [convertPath('/home')],
|
|
enableIndex: true,
|
|
resourceProvider: overlayResourceProvider,
|
|
sdkPath: convertPath('/sdk'),
|
|
);
|
|
|
|
var testPath = convertPath('/home/test');
|
|
_driver = getDriver(testPath);
|
|
}
|
|
|
|
/// Return the existing analysis context that should be used to analyze the
|
|
/// given [path], or throw [StateError] if the [path] is not analyzed in any
|
|
/// of the created analysis contexts.
|
|
AnalysisContext getContext(String path) {
|
|
path = convertPath(path);
|
|
return _analysisContextCollection.contextFor(path);
|
|
}
|
|
|
|
/// Return the existing analysis driver that should be used to analyze the
|
|
/// given [path], or throw [StateError] if the [path] is not analyzed in any
|
|
/// of the created analysis contexts.
|
|
AnalysisDriver getDriver(String path) {
|
|
DriverBasedAnalysisContext context =
|
|
getContext(path) as DriverBasedAnalysisContext;
|
|
return context.driver;
|
|
}
|
|
|
|
void setUp() {
|
|
setupResourceProvider();
|
|
overlayResourceProvider = OverlayResourceProvider(resourceProvider);
|
|
|
|
new MockSdk(resourceProvider: resourceProvider);
|
|
|
|
newFolder('/home/test');
|
|
newFile('/home/test/.packages', content: r'''
|
|
test:file:///home/test/lib
|
|
''');
|
|
|
|
createAnalysisContexts();
|
|
}
|
|
|
|
void setupResourceProvider() {}
|
|
|
|
void tearDown() {
|
|
AnalysisEngine.instance.clearCaches();
|
|
AnalysisEngine.instance.logger = null;
|
|
}
|
|
|
|
void _addTestPackageDependency(String name, String rootPath) {
|
|
var packagesFile = getFile('/home/test/.packages');
|
|
var packagesContent = packagesFile.readAsStringSync();
|
|
|
|
// Ignore if there is already the same package dependency.
|
|
if (packagesContent.contains('$name:file://')) {
|
|
return;
|
|
}
|
|
|
|
packagesContent += '$name:${toUri('$rootPath/lib')}\n';
|
|
|
|
packagesFile.writeAsStringSync(packagesContent);
|
|
|
|
_createDriver();
|
|
}
|
|
|
|
void _createDriver() {
|
|
var collection = AnalysisContextCollectionImpl(
|
|
includedPaths: [convertPath('/home')],
|
|
enableIndex: true,
|
|
resourceProvider: resourceProvider,
|
|
sdkPath: convertPath('/sdk'),
|
|
);
|
|
|
|
var testPath = convertPath('/home/test');
|
|
var context = collection.contextFor(testPath) as DriverBasedAnalysisContext;
|
|
|
|
_driver = context.driver;
|
|
}
|
|
}
|