analyze_continuously_test.dart 5.38 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// 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:file/memory.dart';
8
import 'package:flutter_tools/src/base/file_system.dart';
9 10 11
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/terminal.dart';
12
import 'package:flutter_tools/src/dart/analysis.dart';
13 14 15
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';
16 17
import 'package:platform/platform.dart';
import 'package:process/process.dart';
18

19 20
import '../../src/common.dart';
import '../../src/context.dart';
21 22 23 24

void main() {
  AnalysisServer server;
  Directory tempDir;
25 26 27 28 29
  FileSystem fileSystem;
  Platform platform;
  ProcessManager processManager;
  AnsiTerminal terminal;
  Logger logger;
30 31

  setUp(() {
32 33 34 35 36 37
    platform = const LocalPlatform();
    fileSystem = const LocalFileSystem();
    platform = const LocalPlatform();
    processManager = const LocalProcessManager();
    terminal = AnsiTerminal(platform: platform, stdio: Stdio());
    logger = BufferLogger(outputPreferences: OutputPreferences.test(), terminal: terminal);
38
    FlutterCommandRunner.initFlutterRoot();
39
    tempDir = fileSystem.systemTempDirectory.createTempSync('flutter_analysis_test.');
40 41 42
  });

  tearDown(() {
43
    tryToDelete(tempDir);
44 45 46
    return server?.dispose();
  });

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

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

    final File dartFile = fileSystem.file(fileSystem.path.join(directory.path, 'lib', 'main.dart'));
    dartFile.parent.createSync();
    dartFile.writeAsStringSync('''
  void main() {
    print('hello world');
    ${brokenCode ? 'prints("hello world");' : ''}
  }
  ''');
  }

64
  group('analyze --watch', () {
65 66
    testUsingContext('AnalysisServer success', () async {
      _createSampleProject(tempDir);
67

68
      await const Pub().get(context: PubContext.flutterTests, directory: tempDir.path);
69

70 71 72 73 74 75
      server = AnalysisServer(dartSdkPath, <String>[tempDir.path],
        fileSystem: fileSystem,
        platform: platform,
        processManager: processManager,
        logger: logger,
        terminal: terminal,
76
        experiments: <String>[],
77
      );
78 79 80 81

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

83 84 85 86
      await server.start();
      await onDone;

      expect(errorCount, 0);
87
    });
88
  });
89

90 91 92
  testUsingContext('AnalysisServer errors', () async {
    _createSampleProject(tempDir, brokenCode: true);

93
    await const Pub().get(context: PubContext.flutterTests, directory: tempDir.path);
94

95 96 97 98 99 100
    server = AnalysisServer(dartSdkPath, <String>[tempDir.path],
      fileSystem: fileSystem,
      platform: platform,
      processManager: processManager,
      logger: logger,
      terminal: terminal,
101
      experiments: <String>[],
102
    );
103 104 105 106 107

    int errorCount = 0;
    final Future<bool> onDone = server.onAnalyzing.where((bool analyzing) => analyzing == false).first;
    server.onErrors.listen((FileAnalysisErrors errors) {
      errorCount += errors.errors.length;
108
    });
109 110 111 112 113

    await server.start();
    await onDone;

    expect(errorCount, greaterThan(0));
114
  });
115

116
  testUsingContext('Returns no errors when source is error-free', () async {
117
    const String contents = "StringBuffer bar = StringBuffer('baz');";
118
    tempDir.childFile('main.dart').writeAsStringSync(contents);
119 120 121 122 123 124
    server = AnalysisServer(dartSdkPath, <String>[tempDir.path],
      fileSystem: fileSystem,
      platform: platform,
      processManager: processManager,
      logger: logger,
      terminal: terminal,
125
      experiments: <String>[],
126
    );
127

128 129 130 131 132 133 134 135 136
    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);
  });
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

  testWithoutContext('Can forward null-safety experiments to the AnalysisServer', () async {
    final Completer<void> completer = Completer<void>();
    final StreamController<List<int>> stdin = StreamController<List<int>>();
    const String fakeSdkPath = 'dart-sdk';
    final FakeCommand fakeCommand = FakeCommand(
      command: const <String>[
        'dart-sdk/bin/dart',
        'dart-sdk/bin/snapshots/analysis_server.dart.snapshot',
        '--enable-experiment',
        'non-nullable',
        '--disable-server-feature-completion',
        '--disable-server-feature-search',
        '--sdk',
        'dart-sdk',
      ],
      completer: completer,
      stdin: IOSink(stdin.sink),
    );

    server = AnalysisServer(fakeSdkPath, <String>[''],
      fileSystem: MemoryFileSystem.test(),
      platform: FakePlatform(),
      processManager: FakeProcessManager.list(<FakeCommand>[
        fakeCommand,
      ]),
      logger: BufferLogger.test(),
      terminal: Terminal.test(),
      experiments: <String>[
        'non-nullable'
      ],
    );

    await server.start();
  });
172
}