analyze_continuously_test.dart 5.92 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
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
11
import 'package:flutter_tools/src/base/platform.dart';
12
import 'package:flutter_tools/src/base/terminal.dart';
13
import 'package:flutter_tools/src/dart/analysis.dart';
14 15
import 'package:flutter_tools/src/dart/pub.dart';
import 'package:flutter_tools/src/dart/sdk.dart';
16
import 'package:flutter_tools/src/globals.dart' as globals;
17
import 'package:flutter_tools/src/runner/flutter_command_runner.dart';
18
import 'package:process/process.dart';
19

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

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

  setUp(() {
33 34 35 36 37 38
    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);
39
    FlutterCommandRunner.initFlutterRoot();
40
    tempDir = fileSystem.systemTempDirectory.createTempSync('flutter_analysis_test.');
41 42 43
  });

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

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

  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");' : ''}
  }
  ''');
  }

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

69 70 71 72 73 74 75 76 77
      final Pub pub = Pub(
        fileSystem: fileSystem,
        logger: logger,
        processManager: processManager,
        platform: const LocalPlatform(),
        botDetector: globals.botDetector,
        usage: globals.flutterUsage,
      );
      await pub.get(context: PubContext.flutterTests, directory: tempDir.path);
78

79 80 81 82 83 84
      server = AnalysisServer(dartSdkPath, <String>[tempDir.path],
        fileSystem: fileSystem,
        platform: platform,
        processManager: processManager,
        logger: logger,
        terminal: terminal,
85
        experiments: <String>[],
86
      );
87 88 89 90

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

92 93 94 95
      await server.start();
      await onDone;

      expect(errorCount, 0);
96
    });
97
  });
98

99 100 101
  testUsingContext('AnalysisServer errors', () async {
    _createSampleProject(tempDir, brokenCode: true);

102 103 104 105 106 107 108 109 110
    final Pub pub = Pub(
      fileSystem: fileSystem,
      logger: logger,
      processManager: processManager,
      platform: const LocalPlatform(),
      usage: globals.flutterUsage,
      botDetector: globals.botDetector,
    );
    await pub.get(context: PubContext.flutterTests, directory: tempDir.path);
111

112 113 114 115 116 117
    server = AnalysisServer(dartSdkPath, <String>[tempDir.path],
      fileSystem: fileSystem,
      platform: platform,
      processManager: processManager,
      logger: logger,
      terminal: terminal,
118
      experiments: <String>[],
119
    );
120 121 122 123 124

    int errorCount = 0;
    final Future<bool> onDone = server.onAnalyzing.where((bool analyzing) => analyzing == false).first;
    server.onErrors.listen((FileAnalysisErrors errors) {
      errorCount += errors.errors.length;
125
    });
126 127 128 129 130

    await server.start();
    await onDone;

    expect(errorCount, greaterThan(0));
131
  });
132

133
  testUsingContext('Returns no errors when source is error-free', () async {
134
    const String contents = "StringBuffer bar = StringBuffer('baz');";
135
    tempDir.childFile('main.dart').writeAsStringSync(contents);
136 137 138 139 140 141
    server = AnalysisServer(dartSdkPath, <String>[tempDir.path],
      fileSystem: fileSystem,
      platform: platform,
      processManager: processManager,
      logger: logger,
      terminal: terminal,
142
      experiments: <String>[],
143
    );
144

145 146 147 148 149 150 151 152 153
    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);
  });
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

  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();
  });
189
}