os_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
import 'package:archive/archive.dart';
6 7
import 'package:file/file.dart';
import 'package:file/memory.dart';
8
import 'package:file_testing/file_testing.dart';
9
import 'package:flutter_tools/src/base/file_system.dart';
10
import 'package:flutter_tools/src/base/logger.dart';
11
import 'package:flutter_tools/src/base/os.dart';
12
import 'package:flutter_tools/src/base/platform.dart';
13

14
import '../../src/common.dart';
15
import '../../src/fake_process_manager.dart';
16 17 18 19 20 21

const String kExecutable = 'foo';
const String kPath1 = '/bar/bin/$kExecutable';
const String kPath2 = '/another/bin/$kExecutable';

void main() {
22
  late FakeProcessManager fakeProcessManager;
23 24

  setUp(() {
25
    fakeProcessManager = FakeProcessManager.empty();
26
  });
27

28 29
  OperatingSystemUtils createOSUtils(Platform platform) {
    return OperatingSystemUtils(
30
      fileSystem: MemoryFileSystem.test(),
31
      logger: BufferLogger.test(),
32
      platform: platform,
33
      processManager: fakeProcessManager,
34 35
    );
  }
36

37 38
  group('which on POSIX', () {
    testWithoutContext('returns null when executable does not exist', () async {
39 40 41 42 43 44 45 46 47
      fakeProcessManager.addCommand(
        const FakeCommand(
          command: <String>[
            'which',
            kExecutable,
          ],
          exitCode: 1,
        ),
      );
48
      final OperatingSystemUtils utils = createOSUtils(FakePlatform());
49 50 51
      expect(utils.which(kExecutable), isNull);
    });

52
    testWithoutContext('returns exactly one result', () async {
53 54 55 56 57 58 59 60 61
      fakeProcessManager.addCommand(
        const FakeCommand(
          command: <String>[
            'which',
            'foo',
          ],
          stdout: kPath1,
        ),
      );
62
      final OperatingSystemUtils utils = createOSUtils(FakePlatform());
63
      expect(utils.which(kExecutable)!.path, kPath1);
64 65
    });

66
    testWithoutContext('returns all results for whichAll', () async {
67 68 69 70 71 72 73 74 75 76
      fakeProcessManager.addCommand(
        const FakeCommand(
          command: <String>[
            'which',
            '-a',
            kExecutable,
          ],
          stdout: '$kPath1\n$kPath2',
        ),
      );
77
      final OperatingSystemUtils utils = createOSUtils(FakePlatform());
78 79 80 81 82 83 84 85
      final List<File> result = utils.whichAll(kExecutable);
      expect(result, hasLength(2));
      expect(result[0].path, kPath1);
      expect(result[1].path, kPath2);
    });
  });

  group('which on Windows', () {
86 87
    testWithoutContext('throws tool exit if where.exe cannot be run', () async {
      fakeProcessManager.excludedExecutables.add('where');
88

89 90 91 92
      final OperatingSystemUtils utils = OperatingSystemUtils(
        fileSystem: MemoryFileSystem.test(),
        logger: BufferLogger.test(),
        platform: FakePlatform(operatingSystem: 'windows'),
93
        processManager: fakeProcessManager,
94
      );
95

96
      expect(() => utils.which(kExecutable), throwsToolExit());
97
    });
98

99
    testWithoutContext('returns null when executable does not exist', () async {
100 101 102 103 104 105 106 107 108 109
      fakeProcessManager.addCommand(
        const FakeCommand(
          command: <String>[
            'where',
            kExecutable,
          ],
          exitCode: 1,
        ),
      );

110
      final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
111 112 113
      expect(utils.which(kExecutable), isNull);
    });

114
    testWithoutContext('returns exactly one result', () async {
115 116 117 118 119 120 121 122 123
      fakeProcessManager.addCommand(
        const FakeCommand(
          command: <String>[
            'where',
            'foo',
          ],
          stdout: '$kPath1\n$kPath2',
        ),
      );
124
      final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
125
      expect(utils.which(kExecutable)!.path, kPath1);
126 127
    });

128
    testWithoutContext('returns all results for whichAll', () async {
129 130 131 132 133 134 135 136 137
      fakeProcessManager.addCommand(
        const FakeCommand(
          command: <String>[
            'where',
            kExecutable,
          ],
          stdout: '$kPath1\n$kPath2',
        ),
      );
138
      final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
139 140 141 142 143 144
      final List<File> result = utils.whichAll(kExecutable);
      expect(result, hasLength(2));
      expect(result[0].path, kPath1);
      expect(result[1].path, kPath2);
    });
  });
145

146 147
  group('host platform', () {
    testWithoutContext('unknown defaults to Linux', () async {
148 149 150 151 152 153 154 155 156 157
      fakeProcessManager.addCommand(
        const FakeCommand(
          command: <String>[
            'uname',
            '-m',
          ],
          stdout: 'x86_64',
        ),
      );

158 159 160 161 162 163 164 165 166 167 168
      final OperatingSystemUtils utils =
      createOSUtils(FakePlatform(operatingSystem: 'fuchsia'));
      expect(utils.hostPlatform, HostPlatform.linux_x64);
    });

    testWithoutContext('Windows', () async {
      final OperatingSystemUtils utils =
      createOSUtils(FakePlatform(operatingSystem: 'windows'));
      expect(utils.hostPlatform, HostPlatform.windows_x64);
    });

169 170 171 172 173 174 175 176 177 178 179
    testWithoutContext('Linux x64', () async {
      fakeProcessManager.addCommand(
        const FakeCommand(
          command: <String>[
            'uname',
            '-m',
          ],
          stdout: 'x86_64',
        ),
      );

180
      final OperatingSystemUtils utils =
181
      createOSUtils(FakePlatform());
182 183 184
      expect(utils.hostPlatform, HostPlatform.linux_x64);
    });

185 186 187 188 189 190 191 192 193 194 195 196
    testWithoutContext('Linux ARM', () async {
      fakeProcessManager.addCommand(
        const FakeCommand(
          command: <String>[
            'uname',
            '-m',
          ],
          stdout: 'aarch64',
        ),
      );

      final OperatingSystemUtils utils =
197
      createOSUtils(FakePlatform());
198 199 200
      expect(utils.hostPlatform, HostPlatform.linux_arm64);
    });

201
    testWithoutContext('macOS ARM', () async {
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
      fakeProcessManager.addCommands(
        <FakeCommand>[
          const FakeCommand(
            command: <String>[
              'which',
              'sysctl',
            ],
          ),
          const FakeCommand(
            command: <String>[
              'sysctl',
              'hw.optional.arm64',
            ],
            stdout: 'hw.optional.arm64: 1',
          ),
        ],
218 219 220 221
      );

      final OperatingSystemUtils utils =
      createOSUtils(FakePlatform(operatingSystem: 'macos'));
222
      expect(utils.hostPlatform, HostPlatform.darwin_arm64);
223 224 225
    });

    testWithoutContext('macOS 11 x86', () async {
226 227 228 229 230 231 232 233
      fakeProcessManager.addCommands(
        <FakeCommand>[
          const FakeCommand(
            command: <String>[
              'which',
              'sysctl',
            ],
          ),
234 235 236 237 238 239 240
          const FakeCommand(
            command: <String>[
              'sysctl',
              'hw.optional.arm64',
            ],
            stdout: 'hw.optional.arm64: 0',
          ),
241 242
        ],
      );
243 244 245 246 247 248

      final OperatingSystemUtils utils =
      createOSUtils(FakePlatform(operatingSystem: 'macos'));
      expect(utils.hostPlatform, HostPlatform.darwin_x64);
    });

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    testWithoutContext('sysctl not found', () async {
      fakeProcessManager.addCommands(
        <FakeCommand>[
          const FakeCommand(
            command: <String>[
              'which',
              'sysctl',
            ],
            exitCode: 1,
          ),
        ],
      );

      final OperatingSystemUtils utils =
      createOSUtils(FakePlatform(operatingSystem: 'macos'));
      expect(() => utils.hostPlatform, throwsToolExit(message: 'sysctl'));
    });

267
    testWithoutContext('macOS 10 x86', () async {
268 269 270 271 272 273 274 275
      fakeProcessManager.addCommands(
        <FakeCommand>[
          const FakeCommand(
            command: <String>[
              'which',
              'sysctl',
            ],
          ),
276 277 278 279 280 281 282
          const FakeCommand(
            command: <String>[
              'sysctl',
              'hw.optional.arm64',
            ],
            exitCode: 1,
          ),
283 284
        ],
      );
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

      final OperatingSystemUtils utils =
      createOSUtils(FakePlatform(operatingSystem: 'macos'));
      expect(utils.hostPlatform, HostPlatform.darwin_x64);
    });

    testWithoutContext('macOS ARM name', () async {
      fakeProcessManager.addCommands(<FakeCommand>[
        const FakeCommand(
          command: <String>[
            'sw_vers',
            '-productName',
          ],
          stdout: 'product',
        ),
        const FakeCommand(
          command: <String>[
            'sw_vers',
            '-productVersion',
          ],
          stdout: 'version',
        ),
        const FakeCommand(
          command: <String>[
            'sw_vers',
            '-buildVersion',
          ],
          stdout: 'build',
        ),
314 315 316 317 318 319 320
        const FakeCommand(
          command: <String>[
            'uname',
            '-m',
          ],
          stdout: 'arm64',
        ),
321 322 323 324 325 326
        const FakeCommand(
          command: <String>[
            'which',
            'sysctl',
          ],
        ),
327 328 329 330 331 332 333 334 335 336 337
        const FakeCommand(
          command: <String>[
            'sysctl',
            'hw.optional.arm64',
          ],
          stdout: 'hw.optional.arm64: 1',
        ),
      ]);

      final OperatingSystemUtils utils =
          createOSUtils(FakePlatform(operatingSystem: 'macos'));
338
      expect(utils.name, 'product version build darwin-arm64');
339 340
    });

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 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
    testWithoutContext('macOS ARM on Rosetta name', () async {
      fakeProcessManager.addCommands(<FakeCommand>[
        const FakeCommand(
          command: <String>[
            'sw_vers',
            '-productName',
          ],
          stdout: 'product',
        ),
        const FakeCommand(
          command: <String>[
            'sw_vers',
            '-productVersion',
          ],
          stdout: 'version',
        ),
        const FakeCommand(
          command: <String>[
            'sw_vers',
            '-buildVersion',
          ],
          stdout: 'build',
        ),
        const FakeCommand(
          command: <String>[
            'uname',
            '-m',
          ],
          stdout: 'x86_64', // Running on Rosetta
        ),
        const FakeCommand(
          command: <String>[
            'which',
            'sysctl',
          ],
        ),
        const FakeCommand(
          command: <String>[
            'sysctl',
            'hw.optional.arm64',
          ],
          stdout: 'hw.optional.arm64: 1',
        ),
      ]);

      final OperatingSystemUtils utils =
      createOSUtils(FakePlatform(operatingSystem: 'macos'));
388
      expect(utils.name, 'product version build darwin-arm64 (Rosetta)');
389 390
    });

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    testWithoutContext('macOS x86 name', () async {
      fakeProcessManager.addCommands(<FakeCommand>[
        const FakeCommand(
          command: <String>[
            'sw_vers',
            '-productName',
          ],
          stdout: 'product',
        ),
        const FakeCommand(
          command: <String>[
            'sw_vers',
            '-productVersion',
          ],
          stdout: 'version',
        ),
        const FakeCommand(
          command: <String>[
            'sw_vers',
            '-buildVersion',
          ],
          stdout: 'build',
        ),
414 415 416 417 418 419 420
        const FakeCommand(
          command: <String>[
            'uname',
            '-m',
          ],
          stdout: 'x86_64',
        ),
421 422 423 424 425 426
        const FakeCommand(
          command: <String>[
            'which',
            'sysctl',
          ],
        ),
427 428 429 430 431 432 433 434 435 436 437 438 439
        const FakeCommand(
          command: <String>[
            'sysctl',
            'hw.optional.arm64',
          ],
          exitCode: 1,
        ),
      ]);

      final OperatingSystemUtils utils =
          createOSUtils(FakePlatform(operatingSystem: 'macos'));
      expect(utils.name, 'product version build darwin-x64');
    });
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563

    testWithoutContext('Windows name', () async {
      fakeProcessManager.addCommands(<FakeCommand>[
        const FakeCommand(
          command: <String>[
            'ver',
          ],
          stdout: 'version',
        ),
      ]);

      final OperatingSystemUtils utils =
          createOSUtils(FakePlatform(operatingSystem: 'windows'));
      expect(utils.name, 'version');
    });

    testWithoutContext('Linux name', () async {
      const String fakeOsRelease = '''
      NAME="Name"
      ID=id
      ID_LIKE=id_like
      BUILD_ID=build_id
      PRETTY_NAME="Pretty Name"
      ANSI_COLOR="ansi color"
      HOME_URL="https://home.url/"
      DOCUMENTATION_URL="https://documentation.url/"
      SUPPORT_URL="https://support.url/"
      BUG_REPORT_URL="https://bug.report.url/"
      LOGO=logo
      ''';
      final FileSystem fileSystem = MemoryFileSystem.test();
      fileSystem.directory('/etc').createSync();
      fileSystem.file('/etc/os-release').writeAsStringSync(fakeOsRelease);

      final OperatingSystemUtils utils = OperatingSystemUtils(
        fileSystem: fileSystem,
        logger: BufferLogger.test(),
        platform: FakePlatform(
          operatingSystemVersion: 'Linux 1.2.3-abcd #1 SMP PREEMPT Sat Jan 1 00:00:00 UTC 2000',
        ),
        processManager: fakeProcessManager,
      );
      expect(utils.name, 'Pretty Name 1.2.3-abcd');
    });

    testWithoutContext('Linux name reads from "/usr/lib/os-release" if "/etc/os-release" is missing', () async {
      const String fakeOsRelease = '''
      NAME="Name"
      ID=id
      ID_LIKE=id_like
      BUILD_ID=build_id
      PRETTY_NAME="Pretty Name"
      ANSI_COLOR="ansi color"
      HOME_URL="https://home.url/"
      DOCUMENTATION_URL="https://documentation.url/"
      SUPPORT_URL="https://support.url/"
      BUG_REPORT_URL="https://bug.report.url/"
      LOGO=logo
      ''';
      final FileSystem fileSystem = MemoryFileSystem.test();
      fileSystem.directory('/usr/lib').createSync(recursive: true);
      fileSystem.file('/usr/lib/os-release').writeAsStringSync(fakeOsRelease);

      expect(fileSystem.file('/etc/os-release').existsSync(), false);

      final OperatingSystemUtils utils = OperatingSystemUtils(
        fileSystem: fileSystem,
        logger: BufferLogger.test(),
        platform: FakePlatform(
          operatingSystemVersion: 'Linux 1.2.3-abcd #1 SMP PREEMPT Sat Jan 1 00:00:00 UTC 2000',
        ),
        processManager: fakeProcessManager,
      );
      expect(utils.name, 'Pretty Name 1.2.3-abcd');
    });

    testWithoutContext('Linux name when reading "/etc/os-release" fails', () async {
      final FileExceptionHandler handler = FileExceptionHandler();
      final FileSystem fileSystem = MemoryFileSystem.test(opHandle: handler.opHandle);

      fileSystem.directory('/etc').createSync();
      final File osRelease = fileSystem.file('/etc/os-release');

      handler.addError(osRelease, FileSystemOp.read, const FileSystemException());

      final OperatingSystemUtils utils = OperatingSystemUtils(
        fileSystem: fileSystem,
        logger: BufferLogger.test(),
        platform: FakePlatform(
          operatingSystemVersion: 'Linux 1.2.3-abcd #1 SMP PREEMPT Sat Jan 1 00:00:00 UTC 2000',
        ),
        processManager: fakeProcessManager,
      );
      expect(utils.name, 'Linux 1.2.3-abcd');
    });

    testWithoutContext('Linux name omits kernel release if undefined', () async {
      const String fakeOsRelease = '''
      NAME="Name"
      ID=id
      ID_LIKE=id_like
      BUILD_ID=build_id
      PRETTY_NAME="Pretty Name"
      ANSI_COLOR="ansi color"
      HOME_URL="https://home.url/"
      DOCUMENTATION_URL="https://documentation.url/"
      SUPPORT_URL="https://support.url/"
      BUG_REPORT_URL="https://bug.report.url/"
      LOGO=logo
      ''';
      final FileSystem fileSystem = MemoryFileSystem.test();
      fileSystem.directory('/etc').createSync();
      fileSystem.file('/etc/os-release').writeAsStringSync(fakeOsRelease);

      final OperatingSystemUtils utils = OperatingSystemUtils(
        fileSystem: fileSystem,
        logger: BufferLogger.test(),
        platform: FakePlatform(
          operatingSystemVersion: 'undefinedOperatingSystemVersion',
        ),
        processManager: fakeProcessManager,
      );
      expect(utils.name, 'Pretty Name');
    });
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

    // See https://snyk.io/research/zip-slip-vulnerability for more context
    testWithoutContext('Windows validates paths when unzipping', () {
      // on POSIX systems we use the `unzip` binary, which will fail to extract
      // files with paths outside the target directory
      final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
      final MemoryFileSystem fs = MemoryFileSystem.test();
      final File fakeZipFile = fs.file('archive.zip');
      final Directory targetDirectory = fs.directory('output')..createSync(recursive: true);
      const String content = 'hello, world!';
      final Archive archive = Archive()..addFile(
        // This file would be extracted outside of the target extraction dir
        ArchiveFile(r'..\..\..\Target File.txt', content.length, content.codeUnits),
      );
      final List<int> zipData = ZipEncoder().encode(archive)!;
      fakeZipFile.writeAsBytesSync(zipData);
      expect(
        () => utils.unzip(fakeZipFile, targetDirectory),
        throwsA(
          isA<StateError>().having(
            (StateError error) => error.message,
            'correct error message',
            contains('Tried to extract the file '),
          ),
        ),
      );
    });
591 592
  });

593 594
  testWithoutContext('If unzip fails, include stderr in exception text', () {
    const String exceptionMessage = 'Something really bad happened.';
595 596
    final FileExceptionHandler handler = FileExceptionHandler();
    final FileSystem fileSystem = MemoryFileSystem.test(opHandle: handler.opHandle);
597 598 599 600 601 602

    fakeProcessManager.addCommand(
      const FakeCommand(command: <String>[
        'unzip',
        '-o',
        '-q',
603
        'bar.zip',
604
        '-d',
605
        'foo',
606 607 608
      ], exitCode: 1, stderr: exceptionMessage),
    );

609 610 611 612 613 614
    final Directory foo = fileSystem.directory('foo')
      ..createSync();
    final File bar = fileSystem.file('bar.zip')
      ..createSync();
    handler.addError(bar, FileSystemOp.read, const FileSystemException(exceptionMessage));

615 616 617
    final OperatingSystemUtils osUtils = OperatingSystemUtils(
      fileSystem: fileSystem,
      logger: BufferLogger.test(),
618
      platform: FakePlatform(),
619
      processManager: fakeProcessManager,
620 621 622
    );

    expect(
623
      () => osUtils.unzip(bar, foo),
624 625 626 627
      throwsProcessException(message: exceptionMessage),
    );
  });

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
  group('unzip on macOS', () {
    testWithoutContext('falls back to unzip when rsync cannot run', () {
      final FileSystem fileSystem = MemoryFileSystem.test();
      fakeProcessManager.excludedExecutables.add('rsync');

      final BufferLogger logger = BufferLogger.test();
      final OperatingSystemUtils macOSUtils = OperatingSystemUtils(
        fileSystem: fileSystem,
        logger: logger,
        platform: FakePlatform(operatingSystem: 'macos'),
        processManager: fakeProcessManager,
      );

      final Directory targetDirectory = fileSystem.currentDirectory;
      fakeProcessManager.addCommand(FakeCommand(
        command: <String>['unzip', '-o', '-q', 'foo.zip', '-d', targetDirectory.path],
      ));

      macOSUtils.unzip(fileSystem.file('foo.zip'), targetDirectory);
      expect(fakeProcessManager, hasNoRemainingExpectations);
      expect(logger.traceText, contains('Unable to find rsync'));
    });

    testWithoutContext('unzip and rsyncs', () {
      final FileSystem fileSystem = MemoryFileSystem.test();

      final OperatingSystemUtils macOSUtils = OperatingSystemUtils(
        fileSystem: fileSystem,
        logger: BufferLogger.test(),
        platform: FakePlatform(operatingSystem: 'macos'),
        processManager: fakeProcessManager,
      );

      final Directory targetDirectory = fileSystem.currentDirectory;
      final Directory tempDirectory = fileSystem.systemTempDirectory.childDirectory('flutter_foo.zip.rand0');
      fakeProcessManager.addCommands(<FakeCommand>[
        FakeCommand(
          command: <String>[
            'unzip',
            '-o',
            '-q',
            'foo.zip',
            '-d',
            tempDirectory.path,
          ],
          onRun: () {
            expect(tempDirectory, exists);
            tempDirectory.childDirectory('dirA').childFile('fileA').createSync(recursive: true);
            tempDirectory.childDirectory('dirB').childFile('fileB').createSync(recursive: true);
          },
        ),
        FakeCommand(command: <String>[
          'rsync',
681
          '-8',
682 683 684 685 686 687 688
          '-av',
          '--delete',
          tempDirectory.childDirectory('dirA').path,
          targetDirectory.path,
        ]),
        FakeCommand(command: <String>[
          'rsync',
689
          '-8',
690 691 692 693 694 695 696 697 698 699 700 701 702
          '-av',
          '--delete',
          tempDirectory.childDirectory('dirB').path,
          targetDirectory.path,
        ]),
      ]);

      macOSUtils.unzip(fileSystem.file('foo.zip'), fileSystem.currentDirectory);
      expect(fakeProcessManager, hasNoRemainingExpectations);
      expect(tempDirectory, isNot(exists));
    });
  });

703
  group('display an install message when unzip cannot be run', () {
704 705
    testWithoutContext('Linux', () {
      final FileSystem fileSystem = MemoryFileSystem.test();
706
      fakeProcessManager.excludedExecutables.add('unzip');
707

708 709 710
      final OperatingSystemUtils linuxOsUtils = OperatingSystemUtils(
        fileSystem: fileSystem,
        logger: BufferLogger.test(),
711
        platform: FakePlatform(),
712 713
        processManager: fakeProcessManager,
      );
714

715 716 717 718 719 720 721
      expect(
        () => linuxOsUtils.unzip(fileSystem.file('foo.zip'), fileSystem.currentDirectory),
        throwsToolExit(
          message: 'Missing "unzip" tool. Unable to extract foo.zip.\n'
          'Consider running "sudo apt-get install unzip".'),
      );
    });
722

723 724
    testWithoutContext('macOS', () {
      final FileSystem fileSystem = MemoryFileSystem.test();
725
      fakeProcessManager.excludedExecutables.add('unzip');
726

727 728 729 730 731 732
      final OperatingSystemUtils macOSUtils = OperatingSystemUtils(
        fileSystem: fileSystem,
        logger: BufferLogger.test(),
        platform: FakePlatform(operatingSystem: 'macos'),
        processManager: fakeProcessManager,
      );
733

734
      expect(
735
        () => macOSUtils.unzip(fileSystem.file('foo.zip'), fileSystem.currentDirectory),
736 737 738 739 740
        throwsToolExit
          (message: 'Missing "unzip" tool. Unable to extract foo.zip.\n'
            'Consider running "brew install unzip".'),
      );
    });
741

742 743
    testWithoutContext('unknown OS', () {
      final FileSystem fileSystem = MemoryFileSystem.test();
744
      fakeProcessManager.excludedExecutables.add('unzip');
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

      final OperatingSystemUtils unknownOsUtils = OperatingSystemUtils(
        fileSystem: fileSystem,
        logger: BufferLogger.test(),
        platform: FakePlatform(operatingSystem: 'fuchsia'),
        processManager: fakeProcessManager,
      );

      expect(
            () => unknownOsUtils.unzip(fileSystem.file('foo.zip'), fileSystem.currentDirectory),
        throwsToolExit
          (message: 'Missing "unzip" tool. Unable to extract foo.zip.\n'
            'Please install unzip.'),
      );
    });
760 761
  });

762 763 764
  testWithoutContext('stream compression level', () {
    expect(OperatingSystemUtils.gzipLevel1.level, equals(1));
  });
765
}