analyze_continuously_test.dart 13.1 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:fake_async/fake_async.dart';
8
import 'package:file/memory.dart';
9
import 'package:flutter_tools/src/artifacts.dart';
10
import 'package:flutter_tools/src/base/file_system.dart';
11 12
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
13
import 'package:flutter_tools/src/base/platform.dart';
14
import 'package:flutter_tools/src/base/terminal.dart';
15
import 'package:flutter_tools/src/cache.dart';
16
import 'package:flutter_tools/src/commands/analyze.dart';
17
import 'package:flutter_tools/src/dart/analysis.dart';
18
import 'package:flutter_tools/src/dart/pub.dart';
19
import 'package:flutter_tools/src/globals.dart' as globals;
20
import 'package:flutter_tools/src/project.dart';
21
import 'package:flutter_tools/src/project_validator.dart';
22
import 'package:process/process.dart';
23

24 25
import '../../src/common.dart';
import '../../src/context.dart';
26
import '../../src/fake_process_manager.dart';
27
import '../../src/fakes.dart';
28
import '../../src/test_flutter_command_runner.dart';
29 30

void main() {
31 32 33 34
  setUpAll(() {
    Cache.flutterRoot = getFlutterRoot();
  });

35 36 37 38 39 40
  late Directory tempDir;
  late FileSystem fileSystem;
  late Platform platform;
  late ProcessManager processManager;
  late AnsiTerminal terminal;
  late Logger logger;
41
  late FakeStdio mockStdio;
42 43

  setUp(() {
44
    fileSystem = globals.localFileSystem;
45
    platform = const LocalPlatform();
46
    processManager = const LocalProcessManager();
47
    terminal = AnsiTerminal(platform: platform, stdio: Stdio());
48
    logger = BufferLogger(outputPreferences: OutputPreferences.test(), terminal: terminal);
49
    tempDir = fileSystem.systemTempDirectory.createTempSync('flutter_analysis_test.');
50
    mockStdio = FakeStdio();
51 52 53
  });

  tearDown(() {
54
    tryToDelete(tempDir);
55 56
  });

57

58
  void createSampleProject(Directory directory, { bool brokenCode = false }) {
59 60 61
    final File pubspecFile = fileSystem.file(fileSystem.path.join(directory.path, 'pubspec.yaml'));
    pubspecFile.writeAsStringSync('''
  name: foo_project
62
  environment:
63
    sdk: '>=3.0.0-0 <4.0.0'
64 65 66 67 68 69 70 71 72 73 74 75
  ''');

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

76
  group('analyze --watch', () {
77
    testUsingContext('AnalysisServer success', () async {
78
      createSampleProject(tempDir);
79

80
      final Pub pub = Pub.test(
81 82 83 84 85 86
        fileSystem: fileSystem,
        logger: logger,
        processManager: processManager,
        platform: const LocalPlatform(),
        botDetector: globals.botDetector,
        usage: globals.flutterUsage,
87
        stdio: mockStdio,
88
      );
89 90
      await pub.get(
        context: PubContext.flutterTests,
91
        project: FlutterProject.fromDirectoryTest(tempDir),
92
      );
93

94
      final AnalysisServer server = AnalysisServer(
95
        globals.artifacts!.getArtifactPath(Artifact.engineDartSdkPath),
96
        <String>[tempDir.path],
97 98 99 100 101
        fileSystem: fileSystem,
        platform: platform,
        processManager: processManager,
        logger: logger,
        terminal: terminal,
102
        suppressAnalytics: true,
103
      );
104 105

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

109 110 111 112
      await server.start();
      await onDone;

      expect(errorCount, 0);
113 114

      await server.dispose();
115
    });
116
  });
117

118
  testUsingContext('AnalysisServer errors', () async {
119
    createSampleProject(tempDir, brokenCode: true);
120

121
    final Pub pub = Pub.test(
122 123 124 125 126 127
      fileSystem: fileSystem,
      logger: logger,
      processManager: processManager,
      platform: const LocalPlatform(),
      usage: globals.flutterUsage,
      botDetector: globals.botDetector,
128
      stdio: mockStdio,
129 130 131
    );
    await pub.get(
      context: PubContext.flutterTests,
132
      project: FlutterProject.fromDirectoryTest(tempDir),
133
    );
134

135
    final AnalysisServer server = AnalysisServer(
136
      globals.artifacts!.getArtifactPath(Artifact.engineDartSdkPath),
137 138 139 140 141 142
      <String>[tempDir.path],
      fileSystem: fileSystem,
      platform: platform,
      processManager: processManager,
      logger: logger,
      terminal: terminal,
143
      suppressAnalytics: true,
144
    );
145 146

    int errorCount = 0;
147
    final Future<bool> onDone = server.onAnalyzing.where((bool analyzing) => !analyzing).first;
148 149
    server.onErrors.listen((FileAnalysisErrors errors) {
      errorCount += errors.errors.length;
150
    });
151 152 153 154 155

    await server.start();
    await onDone;

    expect(errorCount, greaterThan(0));
156 157

    await server.dispose();
158
  });
159

160
  testUsingContext('Returns no errors when source is error-free', () async {
161
    const String contents = "StringBuffer bar = StringBuffer('baz');";
162
    tempDir.childFile('main.dart').writeAsStringSync(contents);
163
    final AnalysisServer server = AnalysisServer(
164
      globals.artifacts!.getArtifactPath(Artifact.engineDartSdkPath),
165
      <String>[tempDir.path],
166 167 168 169 170
      fileSystem: fileSystem,
      platform: platform,
      processManager: processManager,
      logger: logger,
      terminal: terminal,
171
      suppressAnalytics: true,
172
    );
173

174
    int errorCount = 0;
175
    final Future<bool> onDone = server.onAnalyzing.where((bool analyzing) => !analyzing).first;
176 177 178 179 180 181
    server.onErrors.listen((FileAnalysisErrors errors) {
      errorCount += errors.errors.length;
    });
    await server.start();
    await onDone;
    expect(errorCount, 0);
182
    await server.dispose();
183
  });
184

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  testUsingContext('Can run AnalysisService without suppressing analytics', () async {
    final StreamController<List<int>> stdin = StreamController<List<int>>();
    final FakeProcessManager processManager = FakeProcessManager.list(
      <FakeCommand>[
        FakeCommand(
          command: const <String>[
            'Artifact.engineDartSdkPath/bin/dart',
            '--disable-dart-dev',
            'Artifact.engineDartSdkPath/bin/snapshots/analysis_server.dart.snapshot',
            '--disable-server-feature-completion',
            '--disable-server-feature-search',
            '--sdk',
            'Artifact.engineDartSdkPath',
          ],
          stdin: IOSink(stdin.sink),
        ),
      ]);

    final Artifacts artifacts = Artifacts.test();
    final AnalyzeCommand command = AnalyzeCommand(
      terminal: Terminal.test(),
      artifacts: artifacts,
      logger: BufferLogger.test(),
      platform: FakePlatform(),
      fileSystem: MemoryFileSystem.test(),
      processManager: processManager,
      allProjectValidators: <ProjectValidator>[],
      suppressAnalytics: false,
    );

    final TestFlutterCommandRunner commandRunner = TestFlutterCommandRunner();
    commandRunner.addCommand(command);
    unawaited(commandRunner.run(<String>['analyze', '--watch']));
    await stdin.stream.first;

    expect(processManager, hasNoRemainingExpectations);
  });

223 224 225 226 227 228
  testUsingContext('Can run AnalysisService with customized cache location', () async {
    final StreamController<List<int>> stdin = StreamController<List<int>>();
    final FakeProcessManager processManager = FakeProcessManager.list(
      <FakeCommand>[
        FakeCommand(
          command: const <String>[
229
            'Artifact.engineDartSdkPath/bin/dart',
230
            '--disable-dart-dev',
231
            'Artifact.engineDartSdkPath/bin/snapshots/analysis_server.dart.snapshot',
232 233 234
            '--disable-server-feature-completion',
            '--disable-server-feature-search',
            '--sdk',
235
            'Artifact.engineDartSdkPath',
236
            '--suppress-analytics',
237 238 239 240 241
          ],
          stdin: IOSink(stdin.sink),
        ),
      ]);

242
    final Artifacts artifacts = Artifacts.test();
243 244 245 246
    final AnalyzeCommand command = AnalyzeCommand(
      terminal: Terminal.test(),
      artifacts: artifacts,
      logger: BufferLogger.test(),
247
      platform: FakePlatform(),
248 249
      fileSystem: MemoryFileSystem.test(),
      processManager: processManager,
250
      allProjectValidators: <ProjectValidator>[],
251
      suppressAnalytics: true,
252 253 254 255 256 257 258
    );

    final TestFlutterCommandRunner commandRunner = TestFlutterCommandRunner();
    commandRunner.addCommand(command);
    unawaited(commandRunner.run(<String>['analyze', '--watch']));
    await stdin.stream.first;

259
    expect(processManager, hasNoRemainingExpectations);
260 261 262
  });

  testUsingContext('Can run AnalysisService with customized cache location --watch', () async {
263 264 265 266 267
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
    fileSystem.directory('directoryA').childFile('foo').createSync(recursive: true);

    final BufferLogger logger = BufferLogger.test();

268 269 270 271 272 273
    final Completer<void> completer = Completer<void>();
    final StreamController<List<int>> stdin = StreamController<List<int>>();
    final FakeProcessManager processManager = FakeProcessManager.list(
      <FakeCommand>[
        FakeCommand(
          command: const <String>[
274
            'Artifact.engineDartSdkPath/bin/dart',
275
            '--disable-dart-dev',
276
            'Artifact.engineDartSdkPath/bin/snapshots/analysis_server.dart.snapshot',
277 278 279
            '--disable-server-feature-completion',
            '--disable-server-feature-search',
            '--sdk',
280
            'Artifact.engineDartSdkPath',
281
            '--suppress-analytics',
282 283
          ],
          stdin: IOSink(stdin.sink),
284 285 286 287 288
          stdout: '''
{"event":"server.status","params":{"analysis":{"isAnalyzing":true}}}
{"event":"analysis.errors","params":{"file":"/directoryA/foo","errors":[{"type":"TestError","message":"It's an error.","severity":"warning","code":"500","location":{"file":"/directoryA/foo","startLine": 100,"startColumn":5,"offset":0}}]}}
{"event":"server.status","params":{"analysis":{"isAnalyzing":false}}}
'''
289 290 291
        ),
      ]);

292
    final Artifacts artifacts = Artifacts.test();
293 294 295
    final AnalyzeCommand command = AnalyzeCommand(
      terminal: Terminal.test(),
      artifacts: artifacts,
296 297 298 299
      logger: logger,
      platform: FakePlatform(),
      fileSystem: fileSystem,
      processManager: processManager,
300
      allProjectValidators: <ProjectValidator>[],
301
      suppressAnalytics: true,
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    );

    await FakeAsync().run((FakeAsync time) async {
      final TestFlutterCommandRunner commandRunner = TestFlutterCommandRunner();
      commandRunner.addCommand(command);
      unawaited(commandRunner.run(<String>['analyze', '--watch']));

      while (!logger.statusText.contains('analyzed 1 file')) {
        time.flushMicrotasks();
      }
      completer.complete();
      return completer.future;
    });
    expect(logger.statusText, contains("warning  It's an error • directoryA/foo:100:5 • 500"));
    expect(logger.statusText, contains('1 issue found. (1 new)'));
    expect(logger.errorText, isEmpty);
    expect(processManager, hasNoRemainingExpectations);
  });

  testUsingContext('AnalysisService --watch skips errors from non-files', () async {
    final BufferLogger logger = BufferLogger.test();
    final Completer<void> completer = Completer<void>();
    final StreamController<List<int>> stdin = StreamController<List<int>>();
    final FakeProcessManager processManager = FakeProcessManager.list(
        <FakeCommand>[
          FakeCommand(
              command: const <String>[
329
                'Artifact.engineDartSdkPath/bin/dart',
330
                '--disable-dart-dev',
331
                'Artifact.engineDartSdkPath/bin/snapshots/analysis_server.dart.snapshot',
332 333 334
                '--disable-server-feature-completion',
                '--disable-server-feature-search',
                '--sdk',
335
                'Artifact.engineDartSdkPath',
336
                '--suppress-analytics',
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
              ],
              stdin: IOSink(stdin.sink),
              stdout: '''
{"event":"server.status","params":{"analysis":{"isAnalyzing":true}}}
{"event":"analysis.errors","params":{"file":"/directoryA/bar","errors":[{"type":"TestError","message":"It's an error.","severity":"warning","code":"500","location":{"file":"/directoryA/bar","startLine":100,"startColumn":5,"offset":0}}]}}
{"event":"server.status","params":{"analysis":{"isAnalyzing":false}}}
'''
          ),
        ]);

    final Artifacts artifacts = Artifacts.test();
    final AnalyzeCommand command = AnalyzeCommand(
      terminal: Terminal.test(),
      artifacts: artifacts,
      logger: logger,
352
      platform: FakePlatform(),
353 354
      fileSystem: MemoryFileSystem.test(),
      processManager: processManager,
355
      allProjectValidators: <ProjectValidator>[],
356
      suppressAnalytics: true,
357 358
    );

359 360 361 362 363 364 365 366 367 368 369
    await FakeAsync().run((FakeAsync time) async {
      final TestFlutterCommandRunner commandRunner = TestFlutterCommandRunner();
      commandRunner.addCommand(command);
      unawaited(commandRunner.run(<String>['analyze', '--watch']));

      while (!logger.statusText.contains('analyzed 1 file')) {
        time.flushMicrotasks();
      }
      completer.complete();
      return completer.future;
    });
370

371 372
    expect(logger.statusText, contains('No issues found!'));
    expect(logger.errorText, isEmpty);
373
    expect(processManager, hasNoRemainingExpectations);
374
  });
375
}