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

14 15
import '../src/common.dart';
import '../src/testbed.dart';
16

17 18 19 20 21
final Platform linuxPlatform = FakePlatform(
  operatingSystem: 'linux',
  environment: <String, String>{},
);

22
void main() {
23 24 25 26 27 28 29
  group(TestCompiler, () {
    Testbed testbed;
    FakeTestCompiler testCompiler;
    MockResidentCompiler residentCompiler;

    setUp(() {
      testbed = Testbed(
30 31 32
        overrides: <Type, Generator>{
          Platform: () => linuxPlatform,
        },
33
        setup: () async {
34 35 36
          globals.fs.file('pubspec.yaml').createSync();
          globals.fs.file('.packages').createSync();
          globals.fs.file('test/foo.dart').createSync(recursive: true);
37 38
          residentCompiler = MockResidentCompiler();
          testCompiler = FakeTestCompiler(
39
            BuildMode.debug,
40 41 42 43 44
            false,
            FlutterProject.current(),
            residentCompiler,
          );
        },
45
      );
46 47 48
    });

    test('Reports a dill file when compile is successful', () => testbed.run(() async {
49
      when(residentCompiler.recompile(
50
        any,
51 52
        <Uri>[Uri.parse('test/foo.dart')],
        outputPath: testCompiler.outputDill.path,
53
        packageConfig: anyNamed('packageConfig'),
54
      )).thenAnswer((Invocation invocation) async {
55
        globals.fs.file('abc.dill').createSync();
Dan Field's avatar
Dan Field committed
56
        return const CompilerOutput('abc.dill', 0, <Uri>[]);
57 58
      });

59 60
      expect(await testCompiler.compile(Uri.parse('test/foo.dart')), 'test/foo.dart.dill');
      expect(globals.fs.file('test/foo.dart.dill'), exists);
61
    }));
62

63
    test('Reports null when a compile fails', () => testbed.run(() async {
64
      when(residentCompiler.recompile(
65
        any,
66 67
        <Uri>[Uri.parse('test/foo.dart')],
        outputPath: testCompiler.outputDill.path,
68
        packageConfig: anyNamed('packageConfig'),
69
      )).thenAnswer((Invocation invocation) async {
70
        globals.fs.file('abc.dill').createSync();
Dan Field's avatar
Dan Field committed
71
        return const CompilerOutput('abc.dill', 1, <Uri>[]);
72 73
      });

74 75
      expect(await testCompiler.compile(Uri.parse('test/foo.dart')), null);
      expect(globals.fs.file('test/foo.dart.dill'), isNot(exists));
76 77 78 79 80
      verify(residentCompiler.shutdown()).called(1);
    }));

    test('Disposing test compiler shuts down backing compiler', () => testbed.run(() async {
      testCompiler.compiler = residentCompiler;
81

82
      expect(testCompiler.compilerController.isClosed, false);
83

84
      await testCompiler.dispose();
85

86 87
      expect(testCompiler.compilerController.isClosed, true);
      verify(residentCompiler.shutdown()).called(1);
88
    }));
89 90 91 92 93 94
  });
}

/// Override the creation of the Resident Compiler to simplify testing.
class FakeTestCompiler extends TestCompiler {
  FakeTestCompiler(
95
    BuildMode buildMode,
96 97 98
    bool trackWidgetCreation,
    FlutterProject flutterProject,
    this.residentCompiler,
99
  ) : super(buildMode, trackWidgetCreation, flutterProject, <String>[]);
100 101 102 103 104 105 106 107 108 109

  final MockResidentCompiler residentCompiler;

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

class MockResidentCompiler extends Mock implements ResidentCompiler {}