Unverified Commit 682e6383 authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Refactor sdk_validation_test (#42064)

parent 1196f91f
...@@ -9,125 +9,64 @@ import 'package:flutter_tools/src/cache.dart'; ...@@ -9,125 +9,64 @@ import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/dart/analysis.dart'; import 'package:flutter_tools/src/dart/analysis.dart';
import 'package:flutter_tools/src/dart/pub.dart'; import 'package:flutter_tools/src/dart/pub.dart';
import 'package:flutter_tools/src/dart/sdk.dart'; import 'package:flutter_tools/src/dart/sdk.dart';
import 'package:meta/meta.dart';
import '../../src/common.dart'; import '../../src/common.dart';
import '../../src/context.dart'; import '../../src/context.dart';
void main() { void main() {
group('sdk validation', () { testSampleProject('ui', 'Window');
AnalysisServer server; testSampleProject('html', 'HttpStatus');
Directory tempDir; testSampleProject('js', 'allowInterop');
testSampleProject('js_util', 'jsify');
}
setUpAll(() { void testSampleProject(String lib, String member) {
testUsingContext('contains dart:$lib', () async {
Cache.disableLocking(); Cache.disableLocking();
tempDir = final Directory projectDirectory = fs.systemTempDirectory.createTempSync('flutter_sdk_validation_${lib}_test.').absolute;
fs.systemTempDirectory.createTempSync('sdk_validation_test').absolute;
});
tearDownAll(() { try {
Cache.enableLocking(); final File pubspecFile = fs.file(fs.path.join(projectDirectory.path, 'pubspec.yaml'));
tryToDelete(tempDir); pubspecFile.writeAsStringSync('''
return server?.dispose(); name: ${lib}_project
}); dependencies:
flutter:
testUsingContext('contains dart:ui', () async { sdk: flutter
createSampleProject(tempDir, dartSource: '''
import 'dart:ui' as ui;
void main() {
// ignore: unnecessary_statements
ui.Window;
}
'''); ''');
await pubGet(context: PubContext.flutterTests, directory: tempDir.path); final File dartFile = fs.file(fs.path.join(projectDirectory.path, 'lib', 'main.dart'));
dartFile.parent.createSync();
server = AnalysisServer(dartSdkPath, <String>[tempDir.path]); dartFile.writeAsStringSync('''
import 'dart:$lib' as $lib;
final int errorCount = await analyze(server);
expect(errorCount, 0);
});
testUsingContext('contains dart:html', () async {
createSampleProject(tempDir, dartSource: '''
import 'dart:html' as html;
void main() { void main() {
// ignore: unnecessary_statements // ignore: unnecessary_statements
html.HttpStatus; $lib.$member;
}
''');
await pubGet(context: PubContext.flutterTests, directory: tempDir.path);
server = AnalysisServer(dartSdkPath, <String>[tempDir.path]);
final int errorCount = await analyze(server);
expect(errorCount, 0);
});
testUsingContext('contains dart:js', () async {
createSampleProject(tempDir, dartSource: '''
import 'dart:js' as js;
void main() {
// ignore: unused_local_variable
var foo = js.allowInterop(null);
}
''');
await pubGet(context: PubContext.flutterTests, directory: tempDir.path);
server = AnalysisServer(dartSdkPath, <String>[tempDir.path]);
final int errorCount = await analyze(server);
expect(errorCount, 0);
});
testUsingContext('contains dart:js_util', () async {
createSampleProject(tempDir, dartSource: '''
import 'dart:js_util' as js_util;
void main() {
// ignore: unused_local_variable
var bar = js_util.jsify(null);
} }
'''); ''');
await pubGet(context: PubContext.flutterTests, directory: tempDir.path); await pubGet(context: PubContext.flutterTests, directory: projectDirectory.path);
final AnalysisServer server = AnalysisServer(dartSdkPath, <String>[projectDirectory.path]);
server = AnalysisServer(dartSdkPath, <String>[tempDir.path]); try {
final int errorCount = await analyze(server); final int errorCount = await analyze(server);
expect(errorCount, 0); expect(errorCount, 0);
} finally {
await server.dispose();
}
} finally {
tryToDelete(projectDirectory);
Cache.enableLocking();
}
}); });
}, skip: true);
} }
void createSampleProject(Directory directory, {@required String dartSource}) { Future<int> analyze(AnalysisServer server) async {
final File pubspecFile =
fs.file(fs.path.join(directory.path, 'pubspec.yaml'));
pubspecFile.writeAsStringSync('''
name: foo_project
dependencies:
flutter:
sdk: flutter
''');
final File dartFile =
fs.file(fs.path.join(directory.path, 'lib', 'main.dart'));
dartFile.parent.createSync();
dartFile.writeAsStringSync(dartSource);
}
Future<int> analyze(AnalysisServer server, {bool printErrors = true}) async {
int errorCount = 0; int errorCount = 0;
final Future<bool> onDone = final Future<bool> onDone = server.onAnalyzing.where((bool analyzing) => analyzing == false).first;
server.onAnalyzing.where((bool analyzing) => analyzing == false).first; server.onErrors.listen((FileAnalysisErrors result) {
server.onErrors.listen((FileAnalysisErrors errors) { for (AnalysisError error in result.errors) {
if (printErrors) {
for (AnalysisError error in errors.errors) {
print(error.toString().trim()); print(error.toString().trim());
} }
} errorCount += result.errors.length;
errorCount += errors.errors.length;
}); });
await server.start(); await server.start();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment