build_linux_test.dart 22 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/build_info.dart';
15
import 'package:flutter_tools/src/cache.dart';
16
import 'package:flutter_tools/src/cmake.dart';
17
import 'package:flutter_tools/src/commands/build.dart';
18
import 'package:flutter_tools/src/commands/build_linux.dart';
19
import 'package:flutter_tools/src/features.dart';
20
import 'package:flutter_tools/src/project.dart';
21
import 'package:flutter_tools/src/reporting/reporting.dart';
22
import 'package:mockito/mockito.dart';
23
import 'package:process/process.dart';
24

25 26
import '../../src/common.dart';
import '../../src/context.dart';
27
import '../../src/testbed.dart';
28

29 30
const String _kTestFlutterRoot = '/flutter';

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

void main() {
46 47
  setUpAll(() {
    Cache.disableLocking();
48
  });
49

50 51
  FileSystem fileSystem;
  ProcessManager processManager;
52
  TestUsage usage;
53

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

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

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

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

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

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

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

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

    expect(createTestCommandRunner(command).run(
133
      const <String>['build', 'linux', '--no-pub']
Dan Field's avatar
Dan Field committed
134
    ), throwsToolExit());
135 136
  }, overrides: <Type, Generator>{
    Platform: () => notLinuxPlatform,
137
    FileSystem: () => fileSystem,
138
    ProcessManager: () => FakeProcessManager.any(),
139
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
140 141
  });

142
  testUsingContext('Linux build invokes CMake and ninja, and writes temporary files', () async {
143
    final BuildCommand command = BuildCommand();
144
    processManager = FakeProcessManager.list(<FakeCommand>[
145 146
      cmakeCommand('release'),
      ninjaCommand('release'),
147 148
    ]);

149
    setUpMockProjectFilesForBuild();
150 151

    await createTestCommandRunner(command).run(
152
      const <String>['build', 'linux', '--no-pub']
153
    );
154
    expect(fileSystem.file('linux/flutter/ephemeral/generated_config.cmake'), exists);
155
  }, overrides: <Type, Generator>{
156 157
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
158
    Platform: () => linuxPlatform,
159
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
160
    OperatingSystemUtils: () => FakeOperatingSystemUtils(),
161
  });
162

163
  testUsingContext('Handles argument error from missing cmake', () async {
164 165
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();
166
    processManager = FakeProcessManager.list(<FakeCommand>[
167
      cmakeCommand('release', onRun: () {
168
        throw ArgumentError();
169 170
      }),
    ]);
171 172

    expect(createTestCommandRunner(command).run(
173
      const <String>['build', 'linux', '--no-pub']
174 175 176 177 178 179
    ), throwsToolExit(message: "cmake not found. Run 'flutter doctor' for more information."));
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
180
    OperatingSystemUtils: () => FakeOperatingSystemUtils(),
181 182 183 184 185 186
  });

  testUsingContext('Handles argument error from missing ninja', () async {
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();
    processManager = FakeProcessManager.list(<FakeCommand>[
187 188
      cmakeCommand('release'),
      ninjaCommand('release', onRun: () {
189
        throw ArgumentError();
190 191 192 193 194 195
      }),
    ]);

    expect(createTestCommandRunner(command).run(
      const <String>['build', 'linux', '--no-pub']
    ), throwsToolExit(message: "ninja not found. Run 'flutter doctor' for more information."));
196
  }, overrides: <Type, Generator>{
197 198
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
199 200
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
201
    OperatingSystemUtils: () => FakeOperatingSystemUtils(),
202 203
  });

204 205 206
  testUsingContext('Linux build does not spew stdout to status logger', () async {
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();
207
    processManager = FakeProcessManager.list(<FakeCommand>[
208 209 210 211
      cmakeCommand('debug'),
      ninjaCommand('debug',
        stdout: 'STDOUT STUFF',
      ),
212
    ]);
213 214

    await createTestCommandRunner(command).run(
215
      const <String>['build', 'linux', '--debug', '--no-pub']
216
    );
217 218
    expect(testLogger.statusText, isNot(contains('STDOUT STUFF')));
    expect(testLogger.traceText, contains('STDOUT STUFF'));
219
  }, overrides: <Type, Generator>{
220 221
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
222 223
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
224
    OperatingSystemUtils: () => FakeOperatingSystemUtils(),
225 226
  });

227 228 229 230 231 232 233
  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'''
234
ninja: Entering directory `build/linux/x64/release'
235 236 237 238 239 240 241
[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'
242
/foo/linux/my_application.h:4:10: fatal error: 'gtk/gtk.h' file not found
243 244 245 246 247 248
[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.
249
ERROR: No file or variants found for asset: images/a_dot_burr.jpeg
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
''';

    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'
269
/foo/linux/my_application.h:4:10: fatal error: 'gtk/gtk.h' file not found
270
clang: error: linker command failed with exit code 1 (use -v to see invocation)
271
ERROR: No file or variants found for asset: images/a_dot_burr.jpeg
272 273 274 275 276 277
''');
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
278
    OperatingSystemUtils: () => FakeOperatingSystemUtils(),
279 280
  });

281 282 283 284
  testUsingContext('Linux verbose build sets VERBOSE_SCRIPT_LOGGING', () async {
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();
    processManager = FakeProcessManager.list(<FakeCommand>[
285 286
      cmakeCommand('debug'),
      ninjaCommand('debug',
287
        environment: const <String, String>{
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
          'VERBOSE_SCRIPT_LOGGING': 'true'
        },
        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')));
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
304
    OperatingSystemUtils: () => FakeOperatingSystemUtils(),
305 306
  });

307
  testUsingContext('Linux on x64 build --debug passes debug mode to cmake and ninja', () async {
308 309
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();
310
    processManager = FakeProcessManager.list(<FakeCommand>[
311 312
      cmakeCommand('debug'),
      ninjaCommand('debug'),
313 314
    ]);

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    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'),
    ]);
333 334

    await createTestCommandRunner(command).run(
335
      const <String>['build', 'linux', '--debug', '--no-pub']
336 337
    );
  }, overrides: <Type, Generator>{
338 339
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
340 341
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
342
    OperatingSystemUtils: () => CustomFakeOperatingSystemUtils(hostPlatform: HostPlatform.linux_arm64),
343 344
  });

345
  testUsingContext('Linux on x64 build --profile passes profile mode to make', () async {
346 347
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();
348
    processManager = FakeProcessManager.list(<FakeCommand>[
349 350
      cmakeCommand('profile'),
      ninjaCommand('profile'),
351
    ]);
352 353

    await createTestCommandRunner(command).run(
354
      const <String>['build', 'linux', '--profile', '--no-pub']
355 356
    );
  }, overrides: <Type, Generator>{
357 358
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
359 360
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    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),
393 394
  });

395
  testUsingContext('Linux build configures CMake exports', () async {
396 397 398
    final BuildCommand command = BuildCommand();
    setUpMockProjectFilesForBuild();
    processManager = FakeProcessManager.list(<FakeCommand>[
399 400
      cmakeCommand('release'),
      ninjaCommand('release'),
401 402 403
    ]);
    fileSystem.file('lib/other.dart')
      .createSync(recursive: true);
404 405
    fileSystem.file('foo/bar.sksl.json')
      .createSync(recursive: true);
406 407 408 409 410 411 412 413 414 415 416 417 418 419

    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',
420
        '--bundle-sksl-path=foo/bar.sksl.json',
421 422 423
      ]
    );

424
    final File cmakeConfig = fileSystem.currentDirectory
425 426 427
      .childDirectory('linux')
      .childDirectory('flutter')
      .childDirectory('ephemeral')
428
      .childFile('generated_config.cmake');
429

430
    expect(cmakeConfig, exists);
431

432
    final List<String> configLines = cmakeConfig.readAsLinesSync();
433 434

    expect(configLines, containsAll(<String>[
435 436
      'file(TO_CMAKE_PATH "$_kTestFlutterRoot" FLUTTER_ROOT)',
      'file(TO_CMAKE_PATH "${fileSystem.currentDirectory.path}" PROJECT_DIR)',
437
      '  "DART_DEFINES=Zm9vLmJhcj0y,Zml6ei5mYXI9Mw=="',
438
      '  "DART_OBFUSCATION=true"',
439 440
      '  "EXTRA_FRONT_END_OPTIONS=--enable-experiment=non-nullable"',
      '  "EXTRA_GEN_SNAPSHOT_OPTIONS=--enable-experiment=non-nullable"',
441 442 443 444 445 446 447
      '  "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"',
448 449 450 451 452 453
    ]));
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
454
    OperatingSystemUtils: () => FakeOperatingSystemUtils(),
455 456
  });

457 458
  testUsingContext('linux can extract binary name from CMake file', () async {
    fileSystem.file('linux/CMakeLists.txt')
459 460
      ..createSync(recursive: true)
      ..writeAsStringSync(r'''
461 462
cmake_minimum_required(VERSION 3.10)
project(runner LANGUAGES CXX)
463

464
set(BINARY_NAME "fizz_bar")
465
''');
466 467
    fileSystem.file('pubspec.yaml').createSync();
    fileSystem.file('.packages').createSync();
468
    final FlutterProject flutterProject = FlutterProject.current();
469

470
    expect(getCmakeExecutableName(flutterProject.linux), 'fizz_bar');
471
  }, overrides: <Type, Generator>{
472
    FileSystem: () => fileSystem,
473
    ProcessManager: () => FakeProcessManager.any(),
474 475 476 477 478 479
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
  });

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

480
    expect(() => runner.run(<String>['build', 'linux', '--no-pub']),
481
      throwsToolExit());
482 483 484
  }, overrides: <Type, Generator>{
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: false),
  });
485

486
  testUsingContext('hidden when not enabled on Linux host', () {
487
    expect(BuildLinuxCommand(operatingSystemUtils: FakeOperatingSystemUtils()).hidden, true);
488 489
  }, overrides: <Type, Generator>{
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: false),
490
    Platform: () => notLinuxPlatform,
491 492 493
  });

  testUsingContext('Not hidden when enabled and on Linux host', () {
494
    expect(BuildLinuxCommand(operatingSystemUtils: FakeOperatingSystemUtils()).hidden, false);
495 496
  }, overrides: <Type, Generator>{
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
497
    Platform: () => linuxPlatform,
498
  });
499 500 501 502 503 504 505 506 507

  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)
508 509 510 511 512 513 514 515 516
          ..writeAsStringSync('''
[
  {
    "l": "dart:_internal",
    "c": "SubListIterable",
    "n": "[Optimized] skip",
    "s": 2400
  }
]''');
517 518 519 520 521 522
        fileSystem.file('build/flutter_size_01/trace.linux-x64.json')
          ..createSync(recursive: true)
          ..writeAsStringSync('{}');
      }),
    ]);

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

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

531
    expect(testLogger.statusText, contains('A summary of your Linux bundle analysis can be found at'));
532
    expect(testLogger.statusText, contains('flutter pub global activate devtools; flutter pub global run devtools --appSizeBase='));
533 534 535
    expect(usage.events, contains(
      const TestUsageEvent('code-size-analysis', 'linux'),
    ));
536 537 538 539 540 541
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => linuxPlatform,
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
    Usage: () => usage,
542
    OperatingSystemUtils: () => FakeOperatingSystemUtils(),
543
  });
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 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

  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>[];
606
}