analyze_continuously_test.dart 3.25 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 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';

7
import 'package:flutter_tools/src/base/file_system.dart';
8
import 'package:flutter_tools/src/base/os.dart';
9
import 'package:flutter_tools/src/dart/analysis.dart';
10 11 12 13
import 'package:flutter_tools/src/dart/pub.dart';
import 'package:flutter_tools/src/dart/sdk.dart';
import 'package:flutter_tools/src/runner/flutter_command_runner.dart';

14
import '../src/common.dart';
15
import '../src/context.dart';
16 17 18 19 20 21 22

void main() {
  AnalysisServer server;
  Directory tempDir;

  setUp(() {
    FlutterCommandRunner.initFlutterRoot();
23
    tempDir = fs.systemTempDirectory.createTempSync('flutter_analysis_test.');
24 25 26
  });

  tearDown(() {
27
    tryToDelete(tempDir);
28 29 30
    return server?.dispose();
  });

31
  group('analyze --watch', () {
32 33
    testUsingContext('AnalysisServer success', () async {
      _createSampleProject(tempDir);
34

35
      await pubGet(context: PubContext.flutterTests, directory: tempDir.path);
36

37
      server = AnalysisServer(dartSdkPath, <String>[tempDir.path]);
38 39 40 41

      int errorCount = 0;
      final Future<bool> onDone = server.onAnalyzing.where((bool analyzing) => analyzing == false).first;
      server.onErrors.listen((FileAnalysisErrors errors) => errorCount += errors.errors.length);
42

43 44 45 46
      await server.start();
      await onDone;

      expect(errorCount, 0);
47
    }, overrides: <Type, Generator>{
48
      OperatingSystemUtils: () => os,
49
    });
50
  });
51

52 53 54
  testUsingContext('AnalysisServer errors', () async {
    _createSampleProject(tempDir, brokenCode: true);

55
    await pubGet(context: PubContext.flutterTests, directory: tempDir.path);
56

57
    server = AnalysisServer(dartSdkPath, <String>[tempDir.path]);
58 59 60 61 62

    int errorCount = 0;
    final Future<bool> onDone = server.onAnalyzing.where((bool analyzing) => analyzing == false).first;
    server.onErrors.listen((FileAnalysisErrors errors) {
      errorCount += errors.errors.length;
63
    });
64 65 66 67 68 69

    await server.start();
    await onDone;

    expect(errorCount, greaterThan(0));
  }, overrides: <Type, Generator>{
70
    OperatingSystemUtils: () => os,
71
  });
72

73
  testUsingContext('Returns no errors when source is error-free', () async {
74
    const String contents = "StringBuffer bar = StringBuffer('baz');";
75
    tempDir.childFile('main.dart').writeAsStringSync(contents);
76
    server = AnalysisServer(dartSdkPath, <String>[tempDir.path]);
77

78 79 80 81 82 83 84 85 86
    int errorCount = 0;
    final Future<bool> onDone = server.onAnalyzing.where((bool analyzing) => analyzing == false).first;
    server.onErrors.listen((FileAnalysisErrors errors) {
      errorCount += errors.errors.length;
    });
    await server.start();
    await onDone;
    expect(errorCount, 0);
  }, overrides: <Type, Generator>{
87
    OperatingSystemUtils: () => os,
88
  });
89 90
}

91
void _createSampleProject(Directory directory, { bool brokenCode = false }) {
92
  final File pubspecFile = fs.file(fs.path.join(directory.path, 'pubspec.yaml'));
93 94 95 96
  pubspecFile.writeAsStringSync('''
name: foo_project
''');

97
  final File dartFile = fs.file(fs.path.join(directory.path, 'lib', 'main.dart'));
98 99 100 101 102 103 104 105
  dartFile.parent.createSync();
  dartFile.writeAsStringSync('''
void main() {
  print('hello world');
  ${brokenCode ? 'prints("hello world");' : ''}
}
''');
}