test_compiler_test.dart 3.2 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:flutter_tools/src/build_info.dart';
6 7 8
import 'package:flutter_tools/src/compile.dart';
import 'package:flutter_tools/src/project.dart';
import 'package:flutter_tools/src/test/test_compiler.dart';
9
import 'package:flutter_tools/src/globals.dart' as globals;
10 11
import 'package:mockito/mockito.dart';

12 13
import '../src/common.dart';
import '../src/testbed.dart';
14 15

void main() {
16 17 18 19 20 21 22
  group(TestCompiler, () {
    Testbed testbed;
    FakeTestCompiler testCompiler;
    MockResidentCompiler residentCompiler;

    setUp(() {
      testbed = Testbed(
23
        setup: () async {
24 25 26
          globals.fs.file('pubspec.yaml').createSync();
          globals.fs.file('.packages').createSync();
          globals.fs.file('test/foo.dart').createSync(recursive: true);
27 28
          residentCompiler = MockResidentCompiler();
          testCompiler = FakeTestCompiler(
29
            BuildMode.debug,
30 31 32 33 34
            false,
            FlutterProject.current(),
            residentCompiler,
          );
        },
35
      );
36 37 38
    });

    test('Reports a dill file when compile is successful', () => testbed.run(() async {
39 40 41 42 43
      when(residentCompiler.recompile(
        'test/foo.dart',
        <Uri>[Uri.parse('test/foo.dart')],
        outputPath: testCompiler.outputDill.path,
      )).thenAnswer((Invocation invocation) async {
44
        globals.fs.file('abc.dill').createSync();
Dan Field's avatar
Dan Field committed
45
        return const CompilerOutput('abc.dill', 0, <Uri>[]);
46 47 48
      });

      expect(await testCompiler.compile('test/foo.dart'), 'test/foo.dart.dill');
49
      expect(globals.fs.file('test/foo.dart.dill').existsSync(), true);
50
    }));
51

52
    test('Reports null when a compile fails', () => testbed.run(() async {
53 54 55 56 57
      when(residentCompiler.recompile(
        'test/foo.dart',
        <Uri>[Uri.parse('test/foo.dart')],
        outputPath: testCompiler.outputDill.path,
      )).thenAnswer((Invocation invocation) async {
58
        globals.fs.file('abc.dill').createSync();
Dan Field's avatar
Dan Field committed
59
        return const CompilerOutput('abc.dill', 1, <Uri>[]);
60 61 62
      });

      expect(await testCompiler.compile('test/foo.dart'), null);
63
      expect(globals.fs.file('test/foo.dart.dill').existsSync(), false);
64 65 66 67 68 69 70 71 72
      verify(residentCompiler.shutdown()).called(1);
    }));

    test('Disposing test compiler shuts down backing compiler', () => testbed.run(() async {
      testCompiler.compiler = residentCompiler;
      expect(testCompiler.compilerController.isClosed, false);
      await testCompiler.dispose();
      expect(testCompiler.compilerController.isClosed, true);
      verify(residentCompiler.shutdown()).called(1);
73
    }));
74 75 76 77 78 79
  });
}

/// Override the creation of the Resident Compiler to simplify testing.
class FakeTestCompiler extends TestCompiler {
  FakeTestCompiler(
80
    BuildMode buildMode,
81 82 83
    bool trackWidgetCreation,
    FlutterProject flutterProject,
    this.residentCompiler,
84
  ) : super(buildMode, trackWidgetCreation, flutterProject);
85 86 87 88 89 90 91 92 93 94

  final MockResidentCompiler residentCompiler;

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

class MockResidentCompiler extends Mock implements ResidentCompiler {}