Skip to content

test: cleanup access to analysis context #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 27 additions & 30 deletions json_serializable/test/analysis_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,34 @@ import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/generated/source_io.dart';
import 'package:path/path.dart' as p;

Future<AnalysisContext> getAnalysisContextForProjectPath(
String projectPath) async {
// TODO: fail more clearly if this...fails
var sdkPath = _getSdkPath();
import 'test_utils.dart';

Future<AnalysisContext> analysisContextForProject() async {
var sdkPath = p.dirname(p.dirname(Platform.resolvedExecutable));

var resourceProvider = PhysicalResourceProvider.INSTANCE;
var sdk = new FolderBasedDartSdk(
resourceProvider, resourceProvider.getFolder(sdkPath));

var packageResolver = _getPackageResolver(projectPath, sdk);
var dotPackagesPath = p.join(getPackagePath(), '.packages');

if (!FileSystemEntity.isFileSync(dotPackagesPath)) {
throw new StateError('A package configuration file was not found at the '
'expectetd location. $dotPackagesPath');
}

var pubPackageMapProvider =
new PubPackageMapProvider(PhysicalResourceProvider.INSTANCE, sdk);
var packageMapInfo = pubPackageMapProvider.computePackageMap(
PhysicalResourceProvider.INSTANCE.getResource(getPackagePath())
as Folder);
var packageMap = packageMapInfo.packageMap;
if (packageMap == null) {
throw new StateError('An error occurred getting the package map.');
}

var packageResolver =
new PackageMapUriResolver(PhysicalResourceProvider.INSTANCE, packageMap);

var resolvers = [
new DartUriResolver(sdk),
Expand All @@ -34,35 +52,14 @@ Future<AnalysisContext> getAnalysisContextForProjectPath(

AnalysisEngine.instance.processRequiredPlugins();

var options = new AnalysisOptionsImpl()..analyzeFunctionBodies = false;
var options = new AnalysisOptionsImpl()
..analyzeFunctionBodies = false
..strongMode = true
..previewDart2 = true;

var context = AnalysisEngine.instance.createAnalysisContext()
..analysisOptions = options
..sourceFactory = new SourceFactory(resolvers);

return context;
}

UriResolver _getPackageResolver(String projectPath, FolderBasedDartSdk sdk) {
var dotPackagesPath = p.join(projectPath, '.packages');

if (!FileSystemEntity.isFileSync(dotPackagesPath)) {
throw new StateError('A package configuration file was not found at the '
'expectetd location. $dotPackagesPath');
}

var pubPackageMapProvider =
new PubPackageMapProvider(PhysicalResourceProvider.INSTANCE, sdk);
var packageMapInfo = pubPackageMapProvider.computePackageMap(
PhysicalResourceProvider.INSTANCE.getResource(projectPath) as Folder);
var packageMap = packageMapInfo.packageMap;
if (packageMap == null) {
throw new StateError('An error occurred getting the package map.');
}

return new PackageMapUriResolver(
PhysicalResourceProvider.INSTANCE, packageMap);
}

/// Return the path to the current Dart SDK.
String _getSdkPath() => p.dirname(p.dirname(Platform.resolvedExecutable));
28 changes: 12 additions & 16 deletions json_serializable/test/json_serializable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,19 @@ Matcher _throwsInvalidGenerationSourceError(messageMatcher, todoMatcher) =>
new FeatureMatcher<InvalidGenerationSourceError>(
'element', (e) => e.element, isNotNull)));

final _formatter = new dart_style.DartFormatter();

CompilationUnit _compilationUnit;

void main() {
setUpAll(() async {
_compUnit = await _getCompilationUnitForString();
var context = await analysisContextForProject();

var fileUri = p.toUri(p.join(
getPackagePath(), 'test', 'src', 'json_serializable_test_input.dart'));
var source = context.sourceFactory.forUri2(fileUri);
var libElement = context.computeLibraryElement(source);
_compilationUnit = context.resolveCompilationUnit(source, libElement);
});

group('without wrappers',
Expand All @@ -43,7 +53,7 @@ void main() {

void _registerTests(JsonSerializableGenerator generator) {
Future<String> runForElementNamed(String name) async {
var library = new LibraryReader(_compUnit.element.library);
var library = new LibraryReader(_compilationUnit.element.library);
var element = library.allElements.singleWhere((e) => e.name == name);
var annotation = generator.typeChecker.firstAnnotationOf(element);
var generated = await generator.generateForAnnotatedElement(
Expand Down Expand Up @@ -507,17 +517,3 @@ abstract class _$SubTypeSerializerMixin {
expect(output, expected);
});
}

final _formatter = new dart_style.DartFormatter();

Future<CompilationUnit> _getCompilationUnitForString() async {
var context = await getAnalysisContextForProjectPath(getPackagePath());

var fileUri = p.toUri(p.join(
getPackagePath(), 'test', 'src', 'json_serializable_test_input.dart'));
var source = context.sourceFactory.forUri2(fileUri);
var libElement = context.computeLibraryElement(source);
return context.resolveCompilationUnit(source, libElement);
}

CompilationUnit _compUnit;