analyze_continuously_test.dart 2.56 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/commands/analyze_continuously.dart';
10 11 12 13 14
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';
import 'package:test/test.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('analysis_test');
24 25 26 27 28 29 30
  });

  tearDown(() {
    tempDir?.deleteSync(recursive: true);
    return server?.dispose();
  });

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

35 36 37 38 39 40 41
      await pubGet(directory: tempDir.path);

      server = new AnalysisServer(dartSdkPath, <String>[tempDir.path]);

      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 48
    }, overrides: <Type, Generator>{
      OperatingSystemUtils: () => os
49
    });
50
  });
51

52 53 54 55 56 57 58 59 60 61 62
  testUsingContext('AnalysisServer errors', () async {
    _createSampleProject(tempDir, brokenCode: true);

    await pubGet(directory: tempDir.path);

    server = new AnalysisServer(dartSdkPath, <String>[tempDir.path]);

    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 70

    await server.start();
    await onDone;

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

void _createSampleProject(Directory directory, { bool brokenCode: false }) {
75
  final File pubspecFile = fs.file(fs.path.join(directory.path, 'pubspec.yaml'));
76 77 78 79
  pubspecFile.writeAsStringSync('''
name: foo_project
''');

80
  final File dartFile = fs.file(fs.path.join(directory.path, 'lib', 'main.dart'));
81 82 83 84 85 86 87 88
  dartFile.parent.createSync();
  dartFile.writeAsStringSync('''
void main() {
  print('hello world');
  ${brokenCode ? 'prints("hello world");' : ''}
}
''');
}