sdk_validation_test.dart 2.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/dart/analysis.dart';
import 'package:flutter_tools/src/dart/pub.dart';
import 'package:flutter_tools/src/dart/sdk.dart';

import '../../src/common.dart';
import '../../src/context.dart';

void main() {
17 18 19 20
  testSampleProject('ui', 'Window');
  testSampleProject('html', 'HttpStatus');
  testSampleProject('js', 'allowInterop');
  testSampleProject('js_util', 'jsify');
21 22
}

23 24 25 26
void testSampleProject(String lib, String member) {
  testUsingContext('contains dart:$lib', () async {
    Cache.disableLocking();
    final Directory projectDirectory = fs.systemTempDirectory.createTempSync('flutter_sdk_validation_${lib}_test.').absolute;
27

28 29 30 31 32 33 34
    try {
      final File pubspecFile = fs.file(fs.path.join(projectDirectory.path, 'pubspec.yaml'));
      pubspecFile.writeAsStringSync('''
name: ${lib}_project
dependencies:
  flutter:
    sdk: flutter
35 36
''');

37 38 39 40
      final File dartFile = fs.file(fs.path.join(projectDirectory.path, 'lib', 'main.dart'));
      dartFile.parent.createSync();
      dartFile.writeAsStringSync('''
import 'dart:$lib' as $lib;
41
void main() {
42 43
  // ignore: unnecessary_statements
  $lib.$member;
44 45 46
}
''');

47
      await pub.get(context: PubContext.flutterTests, directory: projectDirectory.path);
48 49 50 51 52 53 54 55 56 57 58
      final AnalysisServer server = AnalysisServer(dartSdkPath, <String>[projectDirectory.path]);
      try {
        final int errorCount = await analyze(server);
        expect(errorCount, 0);
      } finally {
        await server.dispose();
      }
    } finally {
      tryToDelete(projectDirectory);
      Cache.enableLocking();
    }
59
  }, skip: true);
60 61
}

62
Future<int> analyze(AnalysisServer server) async {
63
  int errorCount = 0;
64 65 66 67
  final Future<bool> onDone = server.onAnalyzing.where((bool analyzing) => analyzing == false).first;
  server.onErrors.listen((FileAnalysisErrors result) {
    for (AnalysisError error in result.errors) {
      print(error.toString().trim());
68
    }
69
    errorCount += result.errors.length;
70 71 72 73 74 75 76
  });

  await server.start();
  await onDone;

  return errorCount;
}