compile_test.dart 6.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
// Copyright 2017 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:convert';

import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/context.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/compile.dart';
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';
import 'package:test/test.dart';

import 'src/context.dart';

void main() {
  group('batch compile', () {
    ProcessManager mockProcessManager;
    MockProcess mockFrontendServer;
    MockStdIn mockFrontendServerStdIn;
    MockStream mockFrontendServerStdErr;
    setUp(() {
      mockProcessManager = new MockProcessManager();
      mockFrontendServer = new MockProcess();
      mockFrontendServerStdIn = new MockStdIn();
      mockFrontendServerStdErr = new MockStream();

      when(mockFrontendServer.stderr).thenReturn(mockFrontendServerStdErr);
      final StreamController<String> stdErrStreamController = new StreamController<String>();
      when(mockFrontendServerStdErr.transform<String>(any)).thenReturn(stdErrStreamController.stream);
      when(mockFrontendServer.stdin).thenReturn(mockFrontendServerStdIn);
      when(mockProcessManager.start(any)).thenReturn(new Future<Process>.value(mockFrontendServer));
      when(mockFrontendServer.exitCode).thenReturn(0);
    });

    testUsingContext('single dart successful compilation', () async {
      final BufferLogger logger = context[Logger];
      when(mockFrontendServer.stdout).thenReturn(new Stream<List<int>>.fromFuture(
        new Future<List<int>>.value(UTF8.encode(
          'result abc\nline1\nline2\nabc /path/to/main.dart.dill'
        ))
      ));
      final String output = await compile(sdkRoot: '/path/to/sdkroot',
        mainPath: '/path/to/main.dart'
      );
      verifyNever(mockFrontendServerStdIn.writeln(any));
      expect(logger.traceText, equals('compile debug message: line1\ncompile debug message: line2\n'));
      expect(output, equals('/path/to/main.dart.dill'));
    }, overrides: <Type, Generator>{
      ProcessManager: () => mockProcessManager,
    });

    testUsingContext('single dart failed compilation', () async {
      final BufferLogger logger = context[Logger];

      when(mockFrontendServer.stdout).thenReturn(new Stream<List<int>>.fromFuture(
        new Future<List<int>>.value(UTF8.encode(
          'result abc\nline1\nline2\nabc'
        ))
      ));

      final String output = await compile(sdkRoot: '/path/to/sdkroot',
        mainPath: '/path/to/main.dart'
      );
      verifyNever(mockFrontendServerStdIn.writeln(any));
      expect(logger.traceText, equals('compile debug message: line1\ncompile debug message: line2\n'));
      expect(output, equals(null));
    }, overrides: <Type, Generator>{
      ProcessManager: () => mockProcessManager,
    });
  });

  group('incremental compile', () {
    ProcessManager mockProcessManager;
    ResidentCompiler generator;
    MockProcess mockFrontendServer;
    MockStdIn mockFrontendServerStdIn;
    MockStream mockFrontendServerStdErr;
    StreamController<String> stdErrStreamController;

    setUp(() {
      generator = new ResidentCompiler('sdkroot');
      mockProcessManager = new MockProcessManager();
      mockFrontendServer = new MockProcess();
      mockFrontendServerStdIn = new MockStdIn();
      mockFrontendServerStdErr = new MockStream();

      when(mockFrontendServer.stdin).thenReturn(mockFrontendServerStdIn);
      when(mockFrontendServer.stderr).thenReturn(mockFrontendServerStdErr);
      stdErrStreamController = new StreamController<String>();
      when(mockFrontendServerStdErr.transform<String>(any)).thenReturn(stdErrStreamController.stream);

      when(mockProcessManager.start(any)).thenReturn(
          new Future<Process>.value(mockFrontendServer)
      );
      when(mockFrontendServer.exitCode).thenReturn(0);
    });

    testUsingContext('single dart compile', () async {
      final BufferLogger logger = context[Logger];

      when(mockFrontendServer.stdout).thenReturn(new Stream<List<int>>.fromFuture(
        new Future<List<int>>.value(UTF8.encode(
          'result abc\nline1\nline2\nabc /path/to/main.dart.dill'
        ))
      ));

      final String output = await generator.recompile(
        '/path/to/main.dart', null /* invalidatedFiles */
      );
      verify(mockFrontendServerStdIn.writeln('compile /path/to/main.dart'));
      verifyNoMoreInteractions(mockFrontendServerStdIn);
      expect(logger.traceText, equals('compile debug message: line1\ncompile debug message: line2\n'));
      expect(output, equals('/path/to/main.dart.dill'));
    }, overrides: <Type, Generator>{
      ProcessManager: () => mockProcessManager,
    });

    testUsingContext('compile and recompile', () async {
      final BufferLogger logger = context[Logger];

      when(mockFrontendServer.stdout).thenReturn(new Stream<List<int>>.fromFuture(
        new Future<List<int>>.value(UTF8.encode(
          'result abc\nline1\nline2\nabc /path/to/main.dart.dill'
        ))
      ));

      await generator.recompile('/path/to/main.dart', null /* invalidatedFiles */);
      verify(mockFrontendServerStdIn.writeln('compile /path/to/main.dart'));

      final String output = await generator.recompile(
        null /* mainPath */,
        <String>['/path/to/main.dart']
      );
      final String recompileCommand = verify(mockFrontendServerStdIn.writeln(captureThat(startsWith('recompile ')))).captured[0];
      final String token = recompileCommand.split(' ')[1];
      verify(mockFrontendServerStdIn.writeln('/path/to/main.dart'));
      verify(mockFrontendServerStdIn.writeln(token));
      verifyNoMoreInteractions(mockFrontendServerStdIn);
      expect(logger.traceText, equals('compile debug message: line1\ncompile debug message: line2\n'));
      expect(output, equals('/path/to/main.dart.dill'));
    }, overrides: <Type, Generator>{
      ProcessManager: () => mockProcessManager,
    });
  });
}

class MockProcessManager extends Mock implements ProcessManager {}
class MockProcess extends Mock implements Process {}
class MockStream extends Mock implements Stream<List<int>> {}
class MockStdIn extends Mock implements IOSink {}