build_linux_test.dart 9.31 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 11 12
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';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/build.dart';
13
import 'package:flutter_tools/src/commands/build_linux.dart';
14
import 'package:flutter_tools/src/convert.dart';
15
import 'package:flutter_tools/src/features.dart';
16 17
import 'package:flutter_tools/src/linux/makefile.dart';
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

void main() {
27 28 29 30
  MockProcessManager mockProcessManager;
  MockProcess mockProcess;
  MockPlatform linuxPlatform;
  MockPlatform notLinuxPlatform;
31

32 33
  setUpAll(() {
    Cache.disableLocking();
34
  });
35 36 37 38 39 40 41 42 43 44 45 46 47

  setUp(() {
    mockProcessManager = MockProcessManager();
    mockProcess = MockProcess();
    linuxPlatform = MockPlatform();
    notLinuxPlatform = MockPlatform();
    when(mockProcess.exitCode).thenAnswer((Invocation invocation) async {
      return 0;
    });
    when(mockProcess.stderr).thenAnswer((Invocation invocation) {
      return const Stream<List<int>>.empty();
    });
    when(mockProcess.stdout).thenAnswer((Invocation invocation) {
48
      return Stream<List<int>>.fromIterable(<List<int>>[utf8.encode('STDOUT STUFF')]);
49 50
    });
    when(linuxPlatform.isLinux).thenReturn(true);
51
    when(linuxPlatform.isWindows).thenReturn(false);
52
    when(notLinuxPlatform.isLinux).thenReturn(false);
53
    when(notLinuxPlatform.isWindows).thenReturn(false);
54 55
  });

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  // Creates the mock files necessary to run a build.
  void setUpMockProjectFilesForBuild() {
    fs.file('linux/build.sh').createSync(recursive: true);
    fs.file('pubspec.yaml').createSync();
    fs.file('.packages').createSync();
    fs.file(fs.path.join('lib', 'main.dart')).createSync(recursive: true);
  }

  // Sets up mock expectation for running 'make'.
  void expectMakeInvocationWithMode(String buildModeName) {
    when(mockProcessManager.start(<String>[
      'make',
      '-C',
      '/linux',
      'BUILD=$buildModeName',
    ])).thenAnswer((Invocation invocation) async {
      return mockProcess;
    });
  }

76 77 78 79 80 81 82 83
  testUsingContext('Linux build fails when there is no linux project', () async {
    final BuildCommand command = BuildCommand();
    applyMocksToCommand(command);
    expect(createTestCommandRunner(command).run(
      const <String>['build', 'linux']
    ), throwsA(isInstanceOf<ToolExit>()));
  }, overrides: <Type, Generator>{
    Platform: () => linuxPlatform,
84
    FileSystem: () => MemoryFileSystem(),
85
    ProcessManager: () => FakeProcessManager.any(),
86
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
87 88 89 90 91
  });

  testUsingContext('Linux build fails on non-linux platform', () async {
    final BuildCommand command = BuildCommand();
    applyMocksToCommand(command);
92
    setUpMockProjectFilesForBuild();
93 94 95 96 97 98

    expect(createTestCommandRunner(command).run(
      const <String>['build', 'linux']
    ), throwsA(isInstanceOf<ToolExit>()));
  }, overrides: <Type, Generator>{
    Platform: () => notLinuxPlatform,
99
    FileSystem: () => MemoryFileSystem(),
100
    ProcessManager: () => FakeProcessManager.any(),
101
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
102 103
  });

104
  testUsingContext('Linux build invokes make and writes temporary files', () async {
105 106
    final BuildCommand command = BuildCommand();
    applyMocksToCommand(command);
107 108
    setUpMockProjectFilesForBuild();
    expectMakeInvocationWithMode('release');
109 110 111 112

    await createTestCommandRunner(command).run(
      const <String>['build', 'linux']
    );
113
    expect(fs.file('linux/flutter/ephemeral/generated_config.mk').existsSync(), true);
114
  }, overrides: <Type, Generator>{
115
    FileSystem: () => MemoryFileSystem(),
116 117
    ProcessManager: () => mockProcessManager,
    Platform: () => linuxPlatform,
118
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
119
  });
120

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  testUsingContext('Handles argument error from missing make', () async {
    final BuildCommand command = BuildCommand();
    applyMocksToCommand(command);
    setUpMockProjectFilesForBuild();
    when(mockProcessManager.start(<String>[
      'make',
      '-C',
      '/linux',
      'BUILD=release',
    ])).thenThrow(ArgumentError());

    expect(createTestCommandRunner(command).run(
      const <String>['build', 'linux']
    ), throwsToolExit(message: 'make not found. Run \'flutter doctor\' for more information.'));
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem(),
    ProcessManager: () => mockProcessManager,
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
  });

142 143 144 145 146 147 148 149 150
  testUsingContext('Linux build does not spew stdout to status logger', () async {
    final BuildCommand command = BuildCommand();
    applyMocksToCommand(command);
    setUpMockProjectFilesForBuild();
    expectMakeInvocationWithMode('debug');

    await createTestCommandRunner(command).run(
      const <String>['build', 'linux', '--debug']
    );
151 152
    expect(testLogger.statusText, isNot(contains('STDOUT STUFF')));
    expect(testLogger.traceText, contains('STDOUT STUFF'));
153 154 155 156 157 158 159
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem(),
    ProcessManager: () => mockProcessManager,
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
  });

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  testUsingContext('Linux build --debug passes debug mode to make', () async {
    final BuildCommand command = BuildCommand();
    applyMocksToCommand(command);
    setUpMockProjectFilesForBuild();
    expectMakeInvocationWithMode('debug');

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

  testUsingContext('Linux build --profile passes profile mode to make', () async {
    final BuildCommand command = BuildCommand();
    applyMocksToCommand(command);
    setUpMockProjectFilesForBuild();
    expectMakeInvocationWithMode('profile');

    await createTestCommandRunner(command).run(
      const <String>['build', 'linux', '--profile']
    );
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem(),
    ProcessManager: () => mockProcessManager,
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
  });

192 193 194 195 196 197 198 199 200 201
  testUsingContext('linux can extract binary name from Makefile', () async {
    fs.file('linux/Makefile')
      ..createSync(recursive: true)
      ..writeAsStringSync(r'''
# Comment
SOMETHING_ELSE=FOO
BINARY_NAME=fizz_bar
''');
    fs.file('pubspec.yaml').createSync();
    fs.file('.packages').createSync();
202
    final FlutterProject flutterProject = FlutterProject.current();
203 204

    expect(makefileExecutableName(flutterProject.linux), 'fizz_bar');
205 206
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem(),
207
    ProcessManager: () => FakeProcessManager.any(),
208 209 210 211 212 213 214 215 216 217 218
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
  });

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

    expect(() => runner.run(<String>['build', 'linux']),
        throwsA(isInstanceOf<ToolExit>()));
  }, overrides: <Type, Generator>{
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: false),
  });
219 220 221 222

  testUsingContext('Release build prints an under-construction warning', () async {
    final BuildCommand command = BuildCommand();
    applyMocksToCommand(command);
223 224
    setUpMockProjectFilesForBuild();
    expectMakeInvocationWithMode('release');
225 226 227 228 229

    await createTestCommandRunner(command).run(
      const <String>['build', 'linux']
    );

230
    expect(testLogger.statusText, contains('🚧'));
231 232 233 234 235 236
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem(),
    ProcessManager: () => mockProcessManager,
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
  });
237 238 239 240 241 242 243

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

    expect(BuildLinuxCommand().hidden, true);
  }, overrides: <Type, Generator>{
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: false),
244
    Platform: () => MockPlatform(),
245 246 247 248 249 250 251 252 253 254
  });

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

    expect(BuildLinuxCommand().hidden, false);
  }, overrides: <Type, Generator>{
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
    Platform: () => MockPlatform(),
  });
255 256 257 258 259 260 261 262 263
}

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
264
}