build_linux_test.dart 23 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 6
// @dart = 2.8

7
import 'package:args/command_runner.dart';
8
import 'package:file/memory.dart';
9
import 'package:file_testing/file_testing.dart';
10
import 'package:flutter_tools/src/base/file_system.dart';
11
import 'package:flutter_tools/src/base/os.dart';
12
import 'package:flutter_tools/src/base/platform.dart';
13
import 'package:flutter_tools/src/base/utils.dart';
14
import 'package:flutter_tools/src/cache.dart';
15
import 'package:flutter_tools/src/cmake.dart';
16
import 'package:flutter_tools/src/commands/build.dart';
17
import 'package:flutter_tools/src/commands/build_linux.dart';
18
import 'package:flutter_tools/src/features.dart';
19
import 'package:flutter_tools/src/project.dart';
20
import 'package:flutter_tools/src/reporting/reporting.dart';
21
import 'package:test/fake.dart';
22

23 24
import '../../src/common.dart';
import '../../src/context.dart';
25
import '../../src/fakes.dart';
26
import '../../src/test_flutter_command_runner.dart';
27

28 29
const String _kTestFlutterRoot = '/flutter';

30 31
final Platform linuxPlatform = FakePlatform(
  environment: <String, String>{
32 33
    'FLUTTER_ROOT': _kTestFlutterRoot,
    'HOME': '/',
34 35 36 37 38
  }
);
final Platform notLinuxPlatform = FakePlatform(
  operatingSystem: 'macos',
  environment: <String, String>{
39
    'FLUTTER_ROOT': _kTestFlutterRoot,
40 41 42 43
  }
);

void main() {
44 45
  setUpAll(() {
    Cache.disableLocking();
46
  });
47

48 49
  FileSystem fileSystem;
  ProcessManager processManager;
50
  TestUsage usage;
51

52
  setUp(() {
53
    fileSystem = MemoryFileSystem.test();
54
    Cache.flutterRoot = _kTestFlutterRoot;
55
    usage = TestUsage();
56 57
  });

58 59
  // Creates the mock files necessary to look like a Flutter project.
  void setUpMockCoreProjectFiles() {
60 61 62
    fileSystem.file('pubspec.yaml').createSync();
    fileSystem.file('.packages').createSync();
    fileSystem.file(fileSystem.path.join('lib', 'main.dart')).createSync(recursive: true);
63 64
  }

65
  // Creates the mock files necessary to run a build.
66
  void setUpMockProjectFilesForBuild() {
67
    setUpMockCoreProjectFiles();
68
    fileSystem.file(fileSystem.path.join('linux', 'CMakeLists.txt')).createSync(recursive: true);
69 70
  }

71
  // Returns the command matching the build_linux call to cmake.
72 73 74 75
  FakeCommand cmakeCommand(String buildMode, {
    String target = 'x64',
    void Function() onRun,
  }) {
76 77 78 79 80
    return FakeCommand(
      command: <String>[
        'cmake',
        '-G',
        'Ninja',
81
        '-DCMAKE_BUILD_TYPE=${sentenceCase(buildMode)}',
82
        '-DFLUTTER_TARGET_PLATFORM=linux-$target',
83 84
        '/linux',
      ],
85
      workingDirectory: 'build/linux/$target/$buildMode',
86 87
      onRun: onRun,
    );
88 89
  }

90 91 92
  // Returns the command matching the build_linux call to ninja.
  FakeCommand ninjaCommand(String buildMode, {
    Map<String, String> environment,
93
    String target = 'x64',
94 95 96 97 98 99 100
    void Function() onRun,
    String stdout = '',
  }) {
    return FakeCommand(
      command: <String>[
        'ninja',
        '-C',
101
        'build/linux/$target/$buildMode',
102 103 104 105 106 107
        'install',
      ],
      environment: environment,
      onRun: onRun,
      stdout: stdout,
    );
108 109
  }

110 111
  testUsingContext('Linux build fails when there is no linux project', () async {
    final BuildCommand command = BuildCommand();
112
    setUpMockCoreProjectFiles();
113

114
    expect(createTestCommandRunner(command).run(
115
      const <String>['build', 'linux', '--no-pub']
116
    ), throwsToolExit(message: 'No Linux desktop project configured. See '
117
      'https://docs.flutter.dev/desktop#add-desktop-support-to-an-existing-flutter-app '
118
      'to learn about adding Linux support to a project.'));
119 120
  }, overrides: <Type, Generator>{
    Platform: () => linuxPlatform,
121
    FileSystem: () => fileSystem,
122
    ProcessManager: () => FakeProcessManager.any(),
123
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
124 125 126 127
  });

  testUsingContext('Linux build fails on non-linux platform', () async {
    final BuildCommand command = BuildCommand();
128
    setUpMockProjectFilesForBuild();
129 130

    expect(createTestCommandRunner(command).run(
131
      const <String>['build', 'linux', '--no-pub']
132
    ), throwsToolExit(message: '"build linux" only supported on Linux hosts.'));
133 134
  }, overrides: <Type, Generator>{
    Platform: () => notLinuxPlatform,
135
    FileSystem: () => fileSystem,
136
    ProcessManager: () => FakeProcessManager.any(),
137
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
138 139
  });

140 141 142 143 144 145 146 147 148 149 150
  testUsingContext('Linux build fails when feature is disabled', () async {
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();

    expect(createTestCommandRunner(command).run(
        const <String>['build', 'linux', '--no-pub']
    ), throwsToolExit(message: '"build linux" is not currently supported. To enable, run "flutter config --enable-linux-desktop".'));
  }, overrides: <Type, Generator>{
    Platform: () => linuxPlatform,
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
151
    FeatureFlags: () => TestFeatureFlags(),
152 153
  });

154
  testUsingContext('Linux build invokes CMake and ninja, and writes temporary files', () async {
155
    final BuildCommand command = BuildCommand();
156
    processManager = FakeProcessManager.list(<FakeCommand>[
157 158
      cmakeCommand('release'),
      ninjaCommand('release'),
159 160
    ]);

161
    setUpMockProjectFilesForBuild();
162 163

    await createTestCommandRunner(command).run(
164
      const <String>['build', 'linux', '--no-pub']
165
    );
166
    expect(fileSystem.file('linux/flutter/ephemeral/generated_config.cmake'), exists);
167
  }, overrides: <Type, Generator>{
168 169
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
170
    Platform: () => linuxPlatform,
171
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
172
    OperatingSystemUtils: () => FakeOperatingSystemUtils(),
173
  });
174

175
  testUsingContext('Handles missing cmake', () async {
176 177
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();
178 179
    processManager = FakeProcessManager.empty()
        ..excludedExecutables.add('cmake');
180 181

    expect(createTestCommandRunner(command).run(
182
      const <String>['build', 'linux', '--no-pub']
183
    ), throwsToolExit(message: 'CMake is required for Linux development.'));
184 185 186 187 188
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
189
    OperatingSystemUtils: () => FakeOperatingSystemUtils(),
190 191 192 193 194 195
  });

  testUsingContext('Handles argument error from missing ninja', () async {
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();
    processManager = FakeProcessManager.list(<FakeCommand>[
196 197
      cmakeCommand('release'),
      ninjaCommand('release', onRun: () {
198
        throw ArgumentError();
199 200 201 202 203 204
      }),
    ]);

    expect(createTestCommandRunner(command).run(
      const <String>['build', 'linux', '--no-pub']
    ), throwsToolExit(message: "ninja not found. Run 'flutter doctor' for more information."));
205
  }, overrides: <Type, Generator>{
206 207
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
208 209
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
210
    OperatingSystemUtils: () => FakeOperatingSystemUtils(),
211 212
  });

213 214 215
  testUsingContext('Linux build does not spew stdout to status logger', () async {
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();
216
    processManager = FakeProcessManager.list(<FakeCommand>[
217 218 219 220
      cmakeCommand('debug'),
      ninjaCommand('debug',
        stdout: 'STDOUT STUFF',
      ),
221
    ]);
222 223

    await createTestCommandRunner(command).run(
224
      const <String>['build', 'linux', '--debug', '--no-pub']
225
    );
226
    expect(testLogger.statusText, isNot(contains('STDOUT STUFF')));
227 228
    expect(testLogger.warningText, isNot(contains('STDOUT STUFF')));
    expect(testLogger.errorText, isNot(contains('STDOUT STUFF')));
229
    expect(testLogger.traceText, contains('STDOUT STUFF'));
230
  }, overrides: <Type, Generator>{
231 232
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
233 234
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
235
    OperatingSystemUtils: () => FakeOperatingSystemUtils(),
236 237
  });

238 239 240 241 242 243 244
  testUsingContext('Linux build extracts errors from stdout', () async {
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();

    // This contains a mix of routine build output and various types of errors
    // (Dart error, compile error, link error), edited down for compactness.
    const String stdout = r'''
245
ninja: Entering directory `build/linux/x64/release'
246 247 248 249 250 251 252
[1/6] Generating /foo/linux/flutter/ephemeral/libflutter_linux_gtk.so, /foo/linux/flutter/ephemeral/flutter_linux/flutter_linux.h, _phony
lib/main.dart:4:3: Error: Method not found: 'foo'.
[2/6] Building CXX object CMakeFiles/foo.dir/main.cc.o
/foo/linux/main.cc:6:2: error: expected ';' after class
/foo/linux/main.cc:9:7: warning: unused variable 'unused_variable' [-Wunused-variable]
/foo/linux/main.cc:10:3: error: unknown type name 'UnknownType'
/foo/linux/main.cc:12:7: error: 'bar' is a private member of 'Foo'
253
/foo/linux/my_application.h:4:10: fatal error: 'gtk/gtk.h' file not found
254 255 256 257 258 259
[3/6] Building CXX object CMakeFiles/foo_bar.dir/flutter/generated_plugin_registrant.cc.o
[4/6] Building CXX object CMakeFiles/foo_bar.dir/my_application.cc.o
[5/6] Linking CXX executable intermediates_do_not_run/foo_bar
main.cc:(.text+0x13): undefined reference to `Foo::bar()'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
260
ERROR: No file or variants found for asset: images/a_dot_burr.jpeg
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
''';

    processManager = FakeProcessManager.list(<FakeCommand>[
      cmakeCommand('release'),
      ninjaCommand('release',
        stdout: stdout,
      ),
    ]);

    await createTestCommandRunner(command).run(
      const <String>['build', 'linux', '--no-pub']
    );
    // Just the warnings and errors should be surfaced.
    expect(testLogger.errorText, r'''
lib/main.dart:4:3: Error: Method not found: 'foo'.
/foo/linux/main.cc:6:2: error: expected ';' after class
/foo/linux/main.cc:9:7: warning: unused variable 'unused_variable' [-Wunused-variable]
/foo/linux/main.cc:10:3: error: unknown type name 'UnknownType'
/foo/linux/main.cc:12:7: error: 'bar' is a private member of 'Foo'
280
/foo/linux/my_application.h:4:10: fatal error: 'gtk/gtk.h' file not found
281
clang: error: linker command failed with exit code 1 (use -v to see invocation)
282
ERROR: No file or variants found for asset: images/a_dot_burr.jpeg
283 284 285 286 287 288
''');
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
289
    OperatingSystemUtils: () => FakeOperatingSystemUtils(),
290 291
  });

292 293 294 295
  testUsingContext('Linux verbose build sets VERBOSE_SCRIPT_LOGGING', () async {
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();
    processManager = FakeProcessManager.list(<FakeCommand>[
296 297
      cmakeCommand('debug'),
      ninjaCommand('debug',
298
        environment: const <String, String>{
299
          'VERBOSE_SCRIPT_LOGGING': 'true',
300 301 302 303 304 305 306 307 308 309
        },
        stdout: 'STDOUT STUFF',
      ),
    ]);

    await createTestCommandRunner(command).run(
      const <String>['build', 'linux', '--debug', '-v', '--no-pub']
    );
    expect(testLogger.statusText, contains('STDOUT STUFF'));
    expect(testLogger.traceText, isNot(contains('STDOUT STUFF')));
310 311
    expect(testLogger.warningText, isNot(contains('STDOUT STUFF')));
    expect(testLogger.errorText, isNot(contains('STDOUT STUFF')));
312 313 314 315 316
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
317
    OperatingSystemUtils: () => FakeOperatingSystemUtils(),
318 319
  });

320
  testUsingContext('Linux on x64 build --debug passes debug mode to cmake and ninja', () async {
321 322
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();
323
    processManager = FakeProcessManager.list(<FakeCommand>[
324 325
      cmakeCommand('debug'),
      ninjaCommand('debug'),
326 327
    ]);

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    await createTestCommandRunner(command).run(
      const <String>['build', 'linux', '--debug', '--no-pub']
    );
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
    OperatingSystemUtils: () => FakeOperatingSystemUtils(),
  });

  testUsingContext('Linux on ARM64 build --debug passes debug mode to cmake and ninja', () async {
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();
    processManager = FakeProcessManager.list(<FakeCommand>[
      cmakeCommand('debug', target: 'arm64'),
      ninjaCommand('debug', target: 'arm64'),
    ]);
346 347

    await createTestCommandRunner(command).run(
348
      const <String>['build', 'linux', '--debug', '--no-pub']
349 350
    );
  }, overrides: <Type, Generator>{
351 352
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
353 354
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
355
    OperatingSystemUtils: () => CustomFakeOperatingSystemUtils(hostPlatform: HostPlatform.linux_arm64),
356 357
  });

358
  testUsingContext('Linux on x64 build --profile passes profile mode to make', () async {
359 360
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();
361
    processManager = FakeProcessManager.list(<FakeCommand>[
362 363
      cmakeCommand('profile'),
      ninjaCommand('profile'),
364
    ]);
365 366

    await createTestCommandRunner(command).run(
367
      const <String>['build', 'linux', '--profile', '--no-pub']
368 369
    );
  }, overrides: <Type, Generator>{
370 371
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
372 373
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    OperatingSystemUtils: () => FakeOperatingSystemUtils(),
  });

  testUsingContext('Linux on ARM64 build --profile passes profile mode to make', () async {
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();
    processManager = FakeProcessManager.list(<FakeCommand>[
      cmakeCommand('profile', target: 'arm64'),
      ninjaCommand('profile', target: 'arm64'),
    ]);

    await createTestCommandRunner(command).run(
      const <String>['build', 'linux', '--profile', '--no-pub']
    );
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
    OperatingSystemUtils: () => CustomFakeOperatingSystemUtils(hostPlatform: HostPlatform.linux_arm64),
  });

  testUsingContext('Not support Linux cross-build for x64 on arm64', () async {
    final BuildCommand command = BuildCommand();

    expect(createTestCommandRunner(command).run(
      const <String>['build', 'linux', '--no-pub', '--target-platform=linux-x64']
    ), throwsToolExit());
  }, overrides: <Type, Generator>{
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
    OperatingSystemUtils: () => CustomFakeOperatingSystemUtils(hostPlatform: HostPlatform.linux_arm64),
406 407
  });

408
  testUsingContext('Linux build configures CMake exports', () async {
409 410 411
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();
    processManager = FakeProcessManager.list(<FakeCommand>[
412 413
      cmakeCommand('release'),
      ninjaCommand('release'),
414 415 416
    ]);
    fileSystem.file('lib/other.dart')
      .createSync(recursive: true);
417 418
    fileSystem.file('foo/bar.sksl.json')
      .createSync(recursive: true);
419 420 421 422 423 424 425 426 427 428 429 430 431 432

    await createTestCommandRunner(command).run(
      const <String>[
        'build',
        'linux',
        '--target=lib/other.dart',
        '--no-pub',
        '--track-widget-creation',
        '--split-debug-info=foo/',
        '--enable-experiment=non-nullable',
        '--obfuscate',
        '--dart-define=foo.bar=2',
        '--dart-define=fizz.far=3',
        '--tree-shake-icons',
433
        '--bundle-sksl-path=foo/bar.sksl.json',
434 435 436
      ]
    );

437
    final File cmakeConfig = fileSystem.currentDirectory
438 439 440
      .childDirectory('linux')
      .childDirectory('flutter')
      .childDirectory('ephemeral')
441
      .childFile('generated_config.cmake');
442

443
    expect(cmakeConfig, exists);
444

445
    final List<String> configLines = cmakeConfig.readAsLinesSync();
446 447

    expect(configLines, containsAll(<String>[
448 449
      'file(TO_CMAKE_PATH "$_kTestFlutterRoot" FLUTTER_ROOT)',
      'file(TO_CMAKE_PATH "${fileSystem.currentDirectory.path}" PROJECT_DIR)',
450 451 452 453 454
      'set(FLUTTER_VERSION "1.0.0" PARENT_SCOPE)',
      'set(FLUTTER_VERSION_MAJOR 1 PARENT_SCOPE)',
      'set(FLUTTER_VERSION_MINOR 0 PARENT_SCOPE)',
      'set(FLUTTER_VERSION_PATCH 0 PARENT_SCOPE)',
      'set(FLUTTER_VERSION_BUILD 0 PARENT_SCOPE)',
455
      '  "DART_DEFINES=Zm9vLmJhcj0y,Zml6ei5mYXI9Mw=="',
456
      '  "DART_OBFUSCATION=true"',
457 458
      '  "EXTRA_FRONT_END_OPTIONS=--enable-experiment=non-nullable"',
      '  "EXTRA_GEN_SNAPSHOT_OPTIONS=--enable-experiment=non-nullable"',
459 460 461 462 463 464 465
      '  "SPLIT_DEBUG_INFO=foo/"',
      '  "TRACK_WIDGET_CREATION=true"',
      '  "TREE_SHAKE_ICONS=true"',
      '  "FLUTTER_ROOT=$_kTestFlutterRoot"',
      '  "PROJECT_DIR=${fileSystem.currentDirectory.path}"',
      '  "FLUTTER_TARGET=lib/other.dart"',
      '  "BUNDLE_SKSL_PATH=foo/bar.sksl.json"',
466 467 468 469 470 471
    ]));
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
472
    OperatingSystemUtils: () => FakeOperatingSystemUtils(),
473 474
  });

475 476
  testUsingContext('linux can extract binary name from CMake file', () async {
    fileSystem.file('linux/CMakeLists.txt')
477 478
      ..createSync(recursive: true)
      ..writeAsStringSync(r'''
479 480
cmake_minimum_required(VERSION 3.10)
project(runner LANGUAGES CXX)
481

482
set(BINARY_NAME "fizz_bar")
483
''');
484 485
    fileSystem.file('pubspec.yaml').createSync();
    fileSystem.file('.packages').createSync();
486
    final FlutterProject flutterProject = FlutterProject.fromDirectoryTest(fileSystem.currentDirectory);
487

488
    expect(getCmakeExecutableName(flutterProject.linux), 'fizz_bar');
489
  }, overrides: <Type, Generator>{
490
    FileSystem: () => fileSystem,
491
    ProcessManager: () => FakeProcessManager.any(),
492 493 494 495 496 497
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
  });

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

498
    expect(() => runner.run(<String>['build', 'linux', '--no-pub']),
499
      throwsToolExit());
500
  }, overrides: <Type, Generator>{
501
    FeatureFlags: () => TestFeatureFlags(),
502
  });
503

504
  testUsingContext('hidden when not enabled on Linux host', () {
505
    expect(BuildLinuxCommand(operatingSystemUtils: FakeOperatingSystemUtils()).hidden, true);
506
  }, overrides: <Type, Generator>{
507
    FeatureFlags: () => TestFeatureFlags(),
508
    Platform: () => notLinuxPlatform,
509 510 511
  });

  testUsingContext('Not hidden when enabled and on Linux host', () {
512
    expect(BuildLinuxCommand(operatingSystemUtils: FakeOperatingSystemUtils()).hidden, false);
513 514
  }, overrides: <Type, Generator>{
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
515
    Platform: () => linuxPlatform,
516
  });
517 518 519 520 521 522 523 524 525

  testUsingContext('Performs code size analysis and sends analytics', () async {
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();
    processManager = FakeProcessManager.list(<FakeCommand>[
      cmakeCommand('release'),
      ninjaCommand('release', onRun: () {
        fileSystem.file('build/flutter_size_01/snapshot.linux-x64.json')
          ..createSync(recursive: true)
526 527 528 529 530 531 532 533 534
          ..writeAsStringSync('''
[
  {
    "l": "dart:_internal",
    "c": "SubListIterable",
    "n": "[Optimized] skip",
    "s": 2400
  }
]''');
535 536 537 538 539 540
        fileSystem.file('build/flutter_size_01/trace.linux-x64.json')
          ..createSync(recursive: true)
          ..writeAsStringSync('{}');
      }),
    ]);

541
    fileSystem.file('build/linux/x64/release/bundle/libapp.so')
542 543 544
      ..createSync(recursive: true)
      ..writeAsBytesSync(List<int>.filled(10000, 0));

545 546
    await createTestCommandRunner(command).run(
      const <String>['build', 'linux', '--no-pub', '--analyze-size']
547
    );
548

549
    expect(testLogger.statusText, contains('A summary of your Linux bundle analysis can be found at'));
550
    expect(testLogger.statusText, contains('flutter pub global activate devtools; flutter pub global run devtools --appSizeBase='));
551 552 553
    expect(usage.events, contains(
      const TestUsageEvent('code-size-analysis', 'linux'),
    ));
554 555 556 557 558 559
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
    Usage: () => usage,
560
    OperatingSystemUtils: () => FakeOperatingSystemUtils(),
561
  });
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623

  testUsingContext('Linux on ARM64 build --release passes, and check if the LinuxBuildDirectory for arm64 can be referenced correctly by using analytics', () async {
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();
    processManager = FakeProcessManager.list(<FakeCommand>[
      cmakeCommand('release', target: 'arm64'),
      ninjaCommand('release', target: 'arm64', onRun: () {
        fileSystem.file('build/flutter_size_01/snapshot.linux-arm64.json')
          ..createSync(recursive: true)
          ..writeAsStringSync('''
[
  {
    "l": "dart:_internal",
    "c": "SubListIterable",
    "n": "[Optimized] skip",
    "s": 2400
  }
]''');
        fileSystem.file('build/flutter_size_01/trace.linux-arm64.json')
          ..createSync(recursive: true)
          ..writeAsStringSync('{}');
      }),
    ]);

    fileSystem.file('build/linux/arm64/release/bundle/libapp.so')
      ..createSync(recursive: true)
      ..writeAsBytesSync(List<int>.filled(10000, 0));

    await createTestCommandRunner(command).run(
      const <String>['build', 'linux', '--no-pub', '--analyze-size']
    );

    // check if libapp.so of "build/linux/arm64/release" directory can be referenced.
    expect(testLogger.statusText,  contains('libapp.so (Dart AOT)'));
    expect(usage.events, contains(
      const TestUsageEvent('code-size-analysis', 'linux'),
    ));
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
    Usage: () => usage,
    OperatingSystemUtils: () => CustomFakeOperatingSystemUtils(hostPlatform: HostPlatform.linux_arm64),
  });
}

class CustomFakeOperatingSystemUtils extends Fake implements OperatingSystemUtils {
  CustomFakeOperatingSystemUtils({
    HostPlatform hostPlatform = HostPlatform.linux_x64
  })  : _hostPlatform = hostPlatform;

  final HostPlatform _hostPlatform;

  @override
  String get name => 'Linux';

  @override
  HostPlatform get hostPlatform => _hostPlatform;

  @override
  List<File> whichAll(String execName) => <File>[];
624
}