test_compiler_test.dart 5.35 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:file/memory.dart';
6
import 'package:file_testing/file_testing.dart';
7 8
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
9
import 'package:flutter_tools/src/base/platform.dart';
10
import 'package:flutter_tools/src/build_info.dart';
11 12 13 14 15
import 'package:flutter_tools/src/compile.dart';
import 'package:flutter_tools/src/project.dart';
import 'package:flutter_tools/src/test/test_compiler.dart';
import 'package:mockito/mockito.dart';

16
import '../src/common.dart';
17
import '../src/context.dart';
18
import '../src/testbed.dart';
19

20 21 22 23 24
final Platform linuxPlatform = FakePlatform(
  operatingSystem: 'linux',
  environment: <String, String>{},
);

25
void main() {
26 27 28 29 30 31 32 33
  MockResidentCompiler residentCompiler;
  FileSystem fileSystem;

  setUp(() {
    fileSystem = MemoryFileSystem.test();
    fileSystem.file('pubspec.yaml').createSync();
    fileSystem.file('.packages').writeAsStringSync('flutter_test:flutter_test/');
    fileSystem.file('test/foo.dart').createSync(recursive: true);
34
    fileSystem.file('.packages')
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
      ..createSync()
      ..writeAsStringSync('flutter_test:flutter_test/');
    residentCompiler = MockResidentCompiler();
  });

  testUsingContext('TestCompiler reports a dill file when compile is successful', () async {
    final FakeTestCompiler testCompiler = FakeTestCompiler(
      BuildMode.debug,
      false,
      FlutterProject.current(),
      residentCompiler,
    );
    when(residentCompiler.recompile(
      any,
      <Uri>[Uri.parse('test/foo.dart')],
      outputPath: testCompiler.outputDill.path,
      packageConfig: anyNamed('packageConfig'),
    )).thenAnswer((Invocation invocation) async {
      fileSystem.file('abc.dill').createSync();
      return const CompilerOutput('abc.dill', 0, <Uri>[]);
    });

    expect(await testCompiler.compile(Uri.parse('test/foo.dart')), 'test/foo.dart.dill');
    expect(fileSystem.file('test/foo.dart.dill'), exists);
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    Platform: () => linuxPlatform,
    ProcessManager: () => FakeProcessManager.any(),
    Logger: () => BufferLogger.test(),
  });

  testUsingContext('TestCompiler reports null when a compile fails', () async {
    final FakeTestCompiler testCompiler = FakeTestCompiler(
      BuildMode.debug,
      false,
      FlutterProject.current(),
      residentCompiler,
    );
    when(residentCompiler.recompile(
      any,
      <Uri>[Uri.parse('test/foo.dart')],
      outputPath: testCompiler.outputDill.path,
      packageConfig: anyNamed('packageConfig'),
    )).thenAnswer((Invocation invocation) async {
      fileSystem.file('abc.dill').createSync();
      return const CompilerOutput('abc.dill', 1, <Uri>[]);
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
    expect(await testCompiler.compile(Uri.parse('test/foo.dart')), null);
    expect(fileSystem.file('test/foo.dart.dill'), isNot(exists));
    verify(residentCompiler.shutdown()).called(1);
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    Platform: () => linuxPlatform,
    ProcessManager: () => FakeProcessManager.any(),
    Logger: () => BufferLogger.test(),
  });

  testUsingContext('TestCompiler disposing test compiler shuts down backing compiler', () async {
    final FakeTestCompiler testCompiler = FakeTestCompiler(
      BuildMode.debug,
      false,
      FlutterProject.current(),
      residentCompiler,
    );
    testCompiler.compiler = residentCompiler;

    expect(testCompiler.compilerController.isClosed, false);

    await testCompiler.dispose();

    expect(testCompiler.compilerController.isClosed, true);
    verify(residentCompiler.shutdown()).called(1);
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    Platform: () => linuxPlatform,
    ProcessManager: () => FakeProcessManager.any(),
    Logger: () => BufferLogger.test(),
  });

  testUsingContext('TestCompiler reports an error when there is no dependency on flutter_test', () async {
    final FakeTestCompiler testCompiler = FakeTestCompiler(
      BuildMode.debug,
      false,
      FlutterProject.current(),
      residentCompiler,
    );
    fileSystem.file('.packages').writeAsStringSync('\n');

    expect(await testCompiler.compile(Uri.parse('test/foo.dart')), null);
    expect(testLogger.errorText, contains('Error: cannot run without a dependency on "package:flutter_test"'));
    verifyNever(residentCompiler.recompile(
      any,
      <Uri>[Uri.parse('test/foo.dart')],
      outputPath: testCompiler.outputDill.path,
      packageConfig: anyNamed('packageConfig'),
    ));
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    Platform: () => linuxPlatform,
    ProcessManager: () => FakeProcessManager.any(),
    Logger: () => BufferLogger.test(),
137 138 139 140 141 142
  });
}

/// Override the creation of the Resident Compiler to simplify testing.
class FakeTestCompiler extends TestCompiler {
  FakeTestCompiler(
143
    BuildMode buildMode,
144 145 146
    bool trackWidgetCreation,
    FlutterProject flutterProject,
    this.residentCompiler,
147
  ) : super(buildMode, trackWidgetCreation, flutterProject, <String>[]);
148 149 150 151 152 153 154 155 156 157

  final MockResidentCompiler residentCompiler;

  @override
  Future<ResidentCompiler> createCompiler() async {
    return residentCompiler;
  }
}

class MockResidentCompiler extends Mock implements ResidentCompiler {}