build_macos_test.dart 7.71 KB
Newer Older
1 2 3 4
// Copyright 2019 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.

5
import 'package:args/command_runner.dart';
6 7 8 9 10
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/common.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/platform.dart';
11
import 'package:flutter_tools/src/build_info.dart';
12 13
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/build.dart';
14
import 'package:flutter_tools/src/commands/build_macos.dart';
15
import 'package:flutter_tools/src/features.dart';
16
import 'package:flutter_tools/src/ios/xcodeproj.dart';
17
import 'package:flutter_tools/src/project.dart';
18 19 20
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';

21 22 23
import '../../src/common.dart';
import '../../src/context.dart';
import '../../src/mocks.dart';
24
import '../../src/testbed.dart';
25

26 27
class FakeXcodeProjectInterpreterWithProfile extends FakeXcodeProjectInterpreter {
  @override
28
  Future<XcodeProjectInfo> getInfo(String projectPath, {String projectFilename}) async {
29 30 31 32 33 34 35 36
    return XcodeProjectInfo(
      <String>['Runner'],
      <String>['Debug', 'Profile', 'Release'],
      <String>['Runner'],
    );
  }
}

37
void main() {
38 39 40 41 42
  MockProcessManager mockProcessManager;
  MemoryFileSystem memoryFilesystem;
  MockProcess mockProcess;
  MockPlatform macosPlatform;
  MockPlatform notMacosPlatform;
43

44 45
  setUpAll(() {
    Cache.disableLocking();
46
  });
47 48 49 50 51 52 53 54

  setUp(() {
    mockProcessManager = MockProcessManager();
    memoryFilesystem = MemoryFileSystem();
    mockProcess = MockProcess();
    macosPlatform = MockPlatform();
    notMacosPlatform = MockPlatform();
    when(mockProcess.exitCode).thenAnswer((Invocation invocation) async {
55
      return 0;
56 57 58 59 60 61 62 63
    });
    when(mockProcess.stderr).thenAnswer((Invocation invocation) {
      return const Stream<List<int>>.empty();
    });
    when(mockProcess.stdout).thenAnswer((Invocation invocation) {
      return const Stream<List<int>>.empty();
    });
    when(macosPlatform.isMacOS).thenReturn(true);
64
    when(macosPlatform.isWindows).thenReturn(false);
65
    when(notMacosPlatform.isMacOS).thenReturn(false);
66
    when(notMacosPlatform.isWindows).thenReturn(false);
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
  // Sets up the minimal mock project files necessary for macOS builds to succeed.
  void createMinimalMockProjectFiles() {
    fs.directory('macos').createSync();
    fs.file('pubspec.yaml').createSync();
    fs.file('.packages').createSync();
    fs.file(fs.path.join('lib', 'main.dart')).createSync(recursive: true);
  }

  // Mocks the process manager to handle an xcodebuild call to build the app
  // in the given configuration.
  void setUpMockXcodeBuildHandler(String configuration) {
    final FlutterProject flutterProject = FlutterProject.fromDirectory(fs.currentDirectory);
    final Directory flutterBuildDir = fs.directory(getMacOSBuildDirectory());
    when(mockProcessManager.start(<String>[
      '/usr/bin/env',
      'xcrun',
      'xcodebuild',
      '-workspace', flutterProject.macos.xcodeWorkspace.path,
      '-configuration', configuration,
      '-scheme', 'Runner',
      '-derivedDataPath', flutterBuildDir.absolute.path,
      'OBJROOT=${fs.path.join(flutterBuildDir.absolute.path, 'Build', 'Intermediates.noindex')}',
      'SYMROOT=${fs.path.join(flutterBuildDir.absolute.path, 'Build', 'Products')}',
      'COMPILER_INDEX_STORE_ENABLE=NO',
    ])).thenAnswer((Invocation invocation) async {
      fs.file(fs.path.join('macos', 'Flutter', 'ephemeral', '.app_filename'))
        ..createSync(recursive: true)
        ..writeAsStringSync('example.app');
      return mockProcess;
    });
  }

101 102 103 104 105 106 107 108
  testUsingContext('macOS build fails when there is no macos project', () async {
    final BuildCommand command = BuildCommand();
    applyMocksToCommand(command);
    expect(createTestCommandRunner(command).run(
      const <String>['build', 'macos']
    ), throwsA(isInstanceOf<ToolExit>()));
  }, overrides: <Type, Generator>{
    Platform: () => macosPlatform,
109
    FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true),
110 111 112 113 114 115 116
  });

  testUsingContext('macOS build fails on non-macOS platform', () async {
    final BuildCommand command = BuildCommand();
    applyMocksToCommand(command);
    fs.file('pubspec.yaml').createSync();
    fs.file('.packages').createSync();
117
    fs.file(fs.path.join('lib', 'main.dart')).createSync(recursive: true);
118 119 120 121 122 123 124

    expect(createTestCommandRunner(command).run(
      const <String>['build', 'macos']
    ), throwsA(isInstanceOf<ToolExit>()));
  }, overrides: <Type, Generator>{
    Platform: () => notMacosPlatform,
    FileSystem: () => memoryFilesystem,
125
    FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true),
126 127
  });

128
  testUsingContext('macOS build invokes xcode build (debug)', () async {
129 130
    final BuildCommand command = BuildCommand();
    applyMocksToCommand(command);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    createMinimalMockProjectFiles();
    setUpMockXcodeBuildHandler('Debug');

    await createTestCommandRunner(command).run(
      const <String>['build', 'macos', '--debug']
    );
  }, overrides: <Type, Generator>{
    FileSystem: () => memoryFilesystem,
    ProcessManager: () => mockProcessManager,
    Platform: () => macosPlatform,
    FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true),
  });

  testUsingContext('macOS build invokes xcode build (profile)', () async {
    final BuildCommand command = BuildCommand();
    applyMocksToCommand(command);
    createMinimalMockProjectFiles();
    setUpMockXcodeBuildHandler('Profile');

    await createTestCommandRunner(command).run(
      const <String>['build', 'macos', '--profile']
    );
  }, overrides: <Type, Generator>{
    FileSystem: () => memoryFilesystem,
    ProcessManager: () => mockProcessManager,
    Platform: () => macosPlatform,
    XcodeProjectInterpreter: () => FakeXcodeProjectInterpreterWithProfile(),
    FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true),
  });

  testUsingContext('macOS build invokes xcode build (release)', () async {
    final BuildCommand command = BuildCommand();
    applyMocksToCommand(command);
    createMinimalMockProjectFiles();
    setUpMockXcodeBuildHandler('Release');
166

167
    await createTestCommandRunner(command).run(
168
      const <String>['build', 'macos', '--release']
169
    );
170 171 172 173
  }, overrides: <Type, Generator>{
    FileSystem: () => memoryFilesystem,
    ProcessManager: () => mockProcessManager,
    Platform: () => macosPlatform,
174 175 176 177 178 179 180 181 182 183
    FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true),
  });

  testUsingContext('Refuses to build for macOS when feature is disabled', () {
    final CommandRunner<void> runner = createTestCommandRunner(BuildCommand());

    expect(() => runner.run(<String>['build', 'macos']),
        throwsA(isInstanceOf<ToolExit>()));
  }, overrides: <Type, Generator>{
    FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: false),
184
  });
185 186 187 188 189 190 191

  testUsingContext('hidden when not enabled on macOS host', () {
    when(platform.isMacOS).thenReturn(true);

    expect(BuildMacosCommand().hidden, true);
  }, overrides: <Type, Generator>{
    FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: false),
192
    Platform: () => MockPlatform(),
193 194 195 196 197 198 199 200 201 202
  });

  testUsingContext('Not hidden when enabled and on macOS host', () {
    when(platform.isMacOS).thenReturn(true);

    expect(BuildMacosCommand().hidden, false);
  }, overrides: <Type, Generator>{
    FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true),
    Platform: () => MockPlatform(),
  });
203 204 205 206 207 208 209 210 211
}

class MockProcessManager extends Mock implements ProcessManager {}
class MockProcess extends Mock implements Process {}
class MockPlatform extends Mock implements Platform {
  @override
  Map<String, String> environment = <String, String>{
    'FLUTTER_ROOT': '/',
  };
Dan Field's avatar
Dan Field committed
212
}