build_test.dart 5.94 KB
Newer Older
1 2 3 4 5
// Copyright 2014 The Flutter 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 'package:args/command_runner.dart';
6 7
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
8
import 'package:flutter_tools/src/base/logger.dart';
9
import 'package:flutter_tools/src/build_system/build_system.dart';
10 11
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/build.dart';
12 13
import 'package:flutter_tools/src/runner/flutter_command.dart';

14
import '../../src/common.dart';
15
import '../../src/context.dart';
16 17
import '../../src/fakes.dart';
import '../../src/test_build_system.dart';
18
import '../../src/test_flutter_command_runner.dart';
19 20 21

void main() {
  testUsingContext('obfuscate requires split-debug-info', () {
22
    final FakeBuildInfoCommand command = FakeBuildInfoCommand();
23 24 25
    final CommandRunner<void> commandRunner = createTestCommandRunner(command);

    expect(() => commandRunner.run(<String>[
26
      'fake',
27
      '--obfuscate',
28 29 30 31
    ]), throwsToolExit(message: '"--${FlutterOptions.kDartObfuscationOption}" can only be used in '
        'combination with "--${FlutterOptions.kSplitDebugInfoOption}"'));
  });
  group('Fatal Logs', () {
32 33
    late FakeBuildCommand command;
    late MemoryFileSystem fs;
34 35 36 37 38 39 40 41 42

    setUp(() {
      fs = MemoryFileSystem.test();
      fs.file('/package/pubspec.yaml').createSync(recursive: true);
      fs.currentDirectory = '/package';
      Cache.disableLocking();
    });

    testUsingContext("doesn't fail if --fatal-warnings specified and no warnings occur", () async {
43 44 45 46
      command = FakeBuildCommand(
        androidSdk: FakeAndroidSdk(),
        buildSystem: TestBuildSystem.all(BuildResult(success: true)),
        fileSystem: MemoryFileSystem.test(),
47
        logger: BufferLogger.test(),
48 49
        osUtils: FakeOperatingSystemUtils(),
      );
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
      try {
        await createTestCommandRunner(command).run(<String>[
          'build',
          'test',
          '--${FlutterOptions.kFatalWarnings}',
        ]);
      } on Exception {
        fail('Unexpected exception thrown');
      }
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
      ProcessManager: () => FakeProcessManager.any(),
    });

    testUsingContext("doesn't fail if --fatal-warnings not specified", () async {
65 66 67 68
      command = FakeBuildCommand(
        androidSdk: FakeAndroidSdk(),
        buildSystem: TestBuildSystem.all(BuildResult(success: true)),
        fileSystem: MemoryFileSystem.test(),
69
        logger: BufferLogger.test(),
70 71
        osUtils: FakeOperatingSystemUtils(),
      );
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
      testLogger.printWarning('Warning: Mild annoyance Will Robinson!');
      try {
        await createTestCommandRunner(command).run(<String>[
          'build',
          'test',
        ]);
      } on Exception {
        fail('Unexpected exception thrown');
      }
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
      ProcessManager: () => FakeProcessManager.any(),
    });

    testUsingContext('fails if --fatal-warnings specified and warnings emitted', () async {
87 88 89 90
      command = FakeBuildCommand(
        androidSdk: FakeAndroidSdk(),
        buildSystem: TestBuildSystem.all(BuildResult(success: true)),
        fileSystem: MemoryFileSystem.test(),
91
        logger: BufferLogger.test(),
92 93
        osUtils: FakeOperatingSystemUtils(),
      );
94 95 96 97 98 99 100 101 102 103 104 105
      testLogger.printWarning('Warning: Mild annoyance Will Robinson!');
      await expectLater(createTestCommandRunner(command).run(<String>[
        'build',
        'test',
        '--${FlutterOptions.kFatalWarnings}',
      ]), throwsToolExit(message: 'Logger received warning output during the run, and "--${FlutterOptions.kFatalWarnings}" is enabled.'));
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
      ProcessManager: () => FakeProcessManager.any(),
    });

    testUsingContext('fails if --fatal-warnings specified and errors emitted', () async {
106 107 108 109
      command = FakeBuildCommand(
        androidSdk: FakeAndroidSdk(),
        buildSystem: TestBuildSystem.all(BuildResult(success: true)),
        fileSystem: MemoryFileSystem.test(),
110
        logger: BufferLogger.test(),
111 112
        osUtils: FakeOperatingSystemUtils(),
      );
113 114 115 116 117 118 119 120 121 122
      testLogger.printError('Error: Danger Will Robinson!');
      await expectLater(createTestCommandRunner(command).run(<String>[
        'build',
        'test',
        '--${FlutterOptions.kFatalWarnings}',
      ]), throwsToolExit(message: 'Logger received error output during the run, and "--${FlutterOptions.kFatalWarnings}" is enabled.'));
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
      ProcessManager: () => FakeProcessManager.any(),
    });
123 124 125
  });
}

126 127
class FakeBuildInfoCommand extends FlutterCommand {
  FakeBuildInfoCommand() : super() {
128 129 130 131 132
    addSplitDebugInfoOption();
    addDartObfuscationOption();
  }

  @override
133
  String get description => '';
134 135

  @override
136
  String get name => 'fake';
137 138 139

  @override
  Future<FlutterCommandResult> runCommand() async {
140
    await getBuildInfo();
141 142 143
    return FlutterCommandResult.success();
  }
}
144 145

class FakeBuildCommand extends BuildCommand {
146 147 148 149
  FakeBuildCommand({
    required super.fileSystem,
    required super.buildSystem,
    required super.osUtils,
150
    required Logger logger,
151 152
    required super.androidSdk,
    bool verboseHelp = false,
153 154
  }) : super(logger: logger, verboseHelp: verboseHelp,) {
    addSubcommand(FakeBuildSubcommand(logger: logger, verboseHelp: verboseHelp));
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  }

  @override
  String get description => '';

  @override
  String get name => 'build';

  @override
  Future<FlutterCommandResult> runCommand() async {
    return FlutterCommandResult.success();
  }
}

class FakeBuildSubcommand extends BuildSubCommand {
170
  FakeBuildSubcommand({required super.logger, required super.verboseHelp});
171 172 173 174 175 176 177 178 179 180 181 182

  @override
  String get description => '';

  @override
  String get name => 'test';

  @override
  Future<FlutterCommandResult> runCommand() async {
    return FlutterCommandResult.success();
  }
}