os_test.dart 5.83 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
import 'dart:typed_data';

7 8
import 'package:file/file.dart';
import 'package:file/memory.dart';
9
import 'package:flutter_tools/src/base/file_system.dart';
10
import 'package:flutter_tools/src/base/io.dart';
11
import 'package:flutter_tools/src/base/logger.dart';
12
import 'package:flutter_tools/src/base/os.dart';
13
import 'package:flutter_tools/src/base/platform.dart';
14 15 16
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';

17 18
import '../../src/common.dart';
import '../../src/context.dart';
19 20 21 22 23 24

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

void main() {
25 26 27 28 29
  MockProcessManager mockProcessManager;

  setUp(() {
    mockProcessManager = MockProcessManager();
  });
30

31 32 33
  OperatingSystemUtils createOSUtils(Platform platform) {
    return OperatingSystemUtils(
      fileSystem: MemoryFileSystem(),
34
      logger: BufferLogger.test(),
35
      platform: platform,
36
      processManager: mockProcessManager,
37 38
    );
  }
39

40 41
  group('which on POSIX', () {
    testWithoutContext('returns null when executable does not exist', () async {
42 43 44
      when(mockProcessManager.runSync(<String>['which', kExecutable]))
          .thenReturn(ProcessResult(0, 1, null, null));
      final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'linux'));
45 46 47
      expect(utils.which(kExecutable), isNull);
    });

48
    testWithoutContext('returns exactly one result', () async {
49 50 51
      when(mockProcessManager.runSync(<String>['which', 'foo']))
          .thenReturn(ProcessResult(0, 0, kPath1, null));
      final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'linux'));
52 53 54
      expect(utils.which(kExecutable).path, kPath1);
    });

55
    testWithoutContext('returns all results for whichAll', () async {
56 57 58
      when(mockProcessManager.runSync(<String>['which', '-a', kExecutable]))
          .thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null));
      final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'linux'));
59 60 61 62 63 64 65 66
      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', () {
67
    testWithoutContext('returns null when executable does not exist', () async {
68 69 70
      when(mockProcessManager.runSync(<String>['where', kExecutable]))
          .thenReturn(ProcessResult(0, 1, null, null));
      final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
71 72 73
      expect(utils.which(kExecutable), isNull);
    });

74
    testWithoutContext('returns exactly one result', () async {
75 76 77
      when(mockProcessManager.runSync(<String>['where', 'foo']))
          .thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null));
      final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
78 79 80
      expect(utils.which(kExecutable).path, kPath1);
    });

81
    testWithoutContext('returns all results for whichAll', () async {
82 83 84
      when(mockProcessManager.runSync(<String>['where', kExecutable]))
          .thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null));
      final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
85 86 87 88 89 90
      final List<File> result = utils.whichAll(kExecutable);
      expect(result, hasLength(2));
      expect(result[0].path, kPath1);
      expect(result[1].path, kPath2);
    });
  });
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  group('gzip on Windows:', () {
    testWithoutContext('verifyGzip returns false on a FileSystemException', () {
      final MockFileSystem fileSystem = MockFileSystem();
      final MockFile mockFile = MockFile();
      when(fileSystem.file(any)).thenReturn(mockFile);
      when(mockFile.readAsBytesSync()).thenThrow(
        const FileSystemException('error'),
      );
      final OperatingSystemUtils osUtils = OperatingSystemUtils(
        fileSystem: fileSystem,
        logger: BufferLogger.test(),
        platform: FakePlatform(operatingSystem: 'windows'),
        processManager: mockProcessManager,
      );

      expect(osUtils.verifyGzip(mockFile), isFalse);
    });

    testWithoutContext('verifyGzip returns false on an ArchiveException', () {
      final MockFileSystem fileSystem = MockFileSystem();
      final MockFile mockFile = MockFile();
      when(fileSystem.file(any)).thenReturn(mockFile);
      when(mockFile.readAsBytesSync()).thenReturn(Uint8List.fromList(<int>[
        // Anything other than the magic header: 0x1f, 0x8b.
        0x01,
        0x02,
      ]));
      final OperatingSystemUtils osUtils = OperatingSystemUtils(
        fileSystem: fileSystem,
        logger: BufferLogger.test(),
        platform: FakePlatform(operatingSystem: 'windows'),
        processManager: mockProcessManager,
      );

      expect(osUtils.verifyGzip(mockFile), isFalse);
    });

    testWithoutContext('verifyGzip returns false on an empty file', () {
      final MockFileSystem fileSystem = MockFileSystem();
      final MockFile mockFile = MockFile();
      when(fileSystem.file(any)).thenReturn(mockFile);
      when(mockFile.readAsBytesSync()).thenReturn(Uint8List(0));
      final OperatingSystemUtils osUtils = OperatingSystemUtils(
        fileSystem: fileSystem,
        logger: BufferLogger.test(),
        platform: FakePlatform(operatingSystem: 'windows'),
        processManager: mockProcessManager,
      );

      expect(osUtils.verifyGzip(mockFile), isFalse);
    });
  });

145 146 147
  testWithoutContext('stream compression level', () {
    expect(OperatingSystemUtils.gzipLevel1.level, equals(1));
  });
148 149
}

150
class MockProcessManager extends Mock implements ProcessManager {}
151 152
class MockFileSystem extends Mock implements FileSystem {}
class MockFile extends Mock implements File {}