analyze_test.dart 3.46 KB
Newer Older
1 2 3 4 5 6 7 8
// 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';
import 'dart:io';

import 'package:flutter_tools/src/base/os.dart';
9
import 'package:flutter_tools/src/cache.dart';
10 11 12
import 'package:flutter_tools/src/commands/analyze.dart';
import 'package:flutter_tools/src/dart/pub.dart';
import 'package:flutter_tools/src/dart/sdk.dart';
13
import 'package:flutter_tools/src/runner/flutter_command_runner.dart';
14 15 16 17 18
import 'package:path/path.dart' as path;
import 'package:test/test.dart';

import 'src/context.dart';

19
void main() {
20 21 22 23
  AnalysisServer server;
  Directory tempDir;

  setUp(() {
24
    FlutterCommandRunner.initFlutterRoot();
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    tempDir = Directory.systemTemp.createTempSync('analysis_test');
  });

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

  group('analyze', () {
    testUsingContext('AnalysisServer success', () async {
      _createSampleProject(tempDir);

      await pubGet(directory: tempDir.path);

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

      int errorCount = 0;
Ian Hickson's avatar
Ian Hickson committed
42
      Future<bool> onDone = server.onAnalyzing.where((bool analyzing) => analyzing == false).first;
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
      server.onErrors.listen((FileAnalysisErrors errors) => errorCount += errors.errors.length);

      await server.start();
      await onDone;

      expect(errorCount, 0);
    }, overrides: <Type, dynamic>{
      OperatingSystemUtils: os
    });

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

      await pubGet(directory: tempDir.path);

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

      int errorCount = 0;
Ian Hickson's avatar
Ian Hickson committed
61
      Future<bool> onDone = server.onAnalyzing.where((bool analyzing) => analyzing == false).first;
62 63 64 65 66 67 68 69 70
      server.onErrors.listen((FileAnalysisErrors errors) => errorCount += errors.errors.length);

      await server.start();
      await onDone;

      expect(errorCount, 2);
    }, overrides: <Type, dynamic>{
      OperatingSystemUtils: os
    });
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

    testUsingContext('inRepo', () {
      AnalyzeCommand cmd = new AnalyzeCommand();
      // Absolute paths
      expect(cmd.inRepo(<String>[tempDir.path]), isFalse);
      expect(cmd.inRepo(<String>[path.join(tempDir.path, 'foo')]), isFalse);
      expect(cmd.inRepo(<String>[Cache.flutterRoot]), isTrue);
      expect(cmd.inRepo(<String>[path.join(Cache.flutterRoot, 'foo')]), isTrue);
      // Relative paths
      String oldWorkingDirectory = path.current;
      try {
        Directory.current = Cache.flutterRoot;
        expect(cmd.inRepo(<String>['.']), isTrue);
        expect(cmd.inRepo(<String>['foo']), isTrue);
        Directory.current = tempDir.path;
        expect(cmd.inRepo(<String>['.']), isFalse);
        expect(cmd.inRepo(<String>['foo']), isFalse);
      } finally {
        Directory.current = oldWorkingDirectory;
      }
      // Ensure no exceptions
      cmd.inRepo(null);
      cmd.inRepo(<String>[]);
    });
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  });
}

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

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