os_test.dart 11.6 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 'package:file/file.dart';
import 'package:file/memory.dart';
7
import 'package:flutter_tools/src/base/common.dart';
8
import 'package:flutter_tools/src/base/file_system.dart';
9
import 'package:flutter_tools/src/base/logger.dart';
10
import 'package:flutter_tools/src/base/os.dart';
11
import 'package:flutter_tools/src/base/platform.dart';
12
import 'package:flutter_tools/src/build_info.dart';
13 14 15
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';

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

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

void main() {
24
  MockProcessManager mockProcessManager;
25
  FakeProcessManager fakeProcessManager;
26 27 28

  setUp(() {
    mockProcessManager = MockProcessManager();
29
    fakeProcessManager = FakeProcessManager.list(<FakeCommand>[]);
30
  });
31

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

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

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

70
    testWithoutContext('returns all results for whichAll', () async {
71 72 73 74 75 76 77 78 79 80
      fakeProcessManager.addCommand(
        const FakeCommand(
          command: <String>[
            'which',
            '-a',
            kExecutable,
          ],
          stdout: '$kPath1\n$kPath2',
        ),
      );
81
      final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'linux'));
82 83 84 85 86 87 88 89
      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', () {
90
    testWithoutContext('throws tool exit if where throws an argument error', () async {
91
      when(mockProcessManager.runSync(<String>['where', kExecutable]))
92
          .thenThrow(ArgumentError('Cannot find executable for where'));
93 94 95 96 97 98
      final OperatingSystemUtils utils = OperatingSystemUtils(
        fileSystem: MemoryFileSystem.test(),
        logger: BufferLogger.test(),
        platform: FakePlatform(operatingSystem: 'windows'),
        processManager: mockProcessManager,
      );
99 100 101

      expect(() => utils.which(kExecutable), throwsA(isA<ToolExit>()));
    });
102

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

114
      final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
115 116 117
      expect(utils.which(kExecutable), isNull);
    });

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

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

150 151 152 153 154 155 156 157 158 159 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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
  group('host platform', () {
    testWithoutContext('unknown defaults to Linux', () async {
      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);
    });

    testWithoutContext('Linux', () async {
      final OperatingSystemUtils utils =
      createOSUtils(FakePlatform(operatingSystem: 'linux'));
      expect(utils.hostPlatform, HostPlatform.linux_x64);
    });

    testWithoutContext('macOS ARM', () async {
      fakeProcessManager.addCommand(
        const FakeCommand(
          command: <String>[
            'sysctl',
            'hw.optional.arm64',
          ],
          stdout: 'hw.optional.arm64: 1',
        ),
      );

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

    testWithoutContext('macOS 11 x86', () async {
      fakeProcessManager.addCommand(
          const FakeCommand(
            command: <String>[
              'sysctl',
              'hw.optional.arm64',
            ],
            stdout: 'hw.optional.arm64: 0',
          ),
          );

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

    testWithoutContext('macOS 10 x86', () async {
      fakeProcessManager.addCommand(
          const FakeCommand(
            command: <String>[
              'sysctl',
              'hw.optional.arm64',
            ],
            exitCode: 1,
          ),
          );

      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',
        ),
        const FakeCommand(
          command: <String>[
            'sysctl',
            'hw.optional.arm64',
          ],
          stdout: 'hw.optional.arm64: 1',
        ),
      ]);

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

    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',
        ),
        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');
    });
290 291
  });

292 293
  testWithoutContext('If unzip fails, include stderr in exception text', () {
    const String exceptionMessage = 'Something really bad happened.';
294 295 296 297 298 299 300 301 302 303 304 305

    fakeProcessManager.addCommand(
      const FakeCommand(command: <String>[
        'unzip',
        '-o',
        '-q',
        null,
        '-d',
        null,
      ], exitCode: 1, stderr: exceptionMessage),
    );

306 307 308 309 310 311 312 313 314 315 316
    final MockFileSystem fileSystem = MockFileSystem();
    final MockFile mockFile = MockFile();
    final MockDirectory mockDirectory = MockDirectory();
    when(fileSystem.file(any)).thenReturn(mockFile);
    when(mockFile.readAsBytesSync()).thenThrow(
      const FileSystemException(exceptionMessage),
    );
    final OperatingSystemUtils osUtils = OperatingSystemUtils(
      fileSystem: fileSystem,
      logger: BufferLogger.test(),
      platform: FakePlatform(operatingSystem: 'linux'),
317
      processManager: fakeProcessManager,
318 319 320 321 322 323 324 325
    );

    expect(
      () => osUtils.unzip(mockFile, mockDirectory),
      throwsProcessException(message: exceptionMessage),
    );
  });

326
  testWithoutContext('If unzip throws an ArgumentError, display an install message', () {
327 328 329
    final FileSystem fileSystem = MemoryFileSystem.test();
    when(mockProcessManager.runSync(
      <String>['unzip', '-o', '-q', 'foo.zip', '-d', fileSystem.currentDirectory.path],
330
    )).thenThrow(ArgumentError());
331 332 333 334 335 336 337 338 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

    final OperatingSystemUtils linuxOsUtils = OperatingSystemUtils(
      fileSystem: fileSystem,
      logger: BufferLogger.test(),
      platform: FakePlatform(operatingSystem: 'linux'),
      processManager: mockProcessManager,
    );

    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".'),
    );

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

    expect(
      () => macOSUtils.unzip(fileSystem.file('foo.zip'), fileSystem.currentDirectory),
      throwsToolExit
      (message: 'Missing "unzip" tool. Unable to extract foo.zip.\n'
        'Consider running "brew install unzip".'),
    );

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

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

375 376 377
  testWithoutContext('stream compression level', () {
    expect(OperatingSystemUtils.gzipLevel1.level, equals(1));
  });
378 379
}

380
class MockProcessManager extends Mock implements ProcessManager {}
381
class MockDirectory extends Mock implements Directory {}
382 383
class MockFileSystem extends Mock implements FileSystem {}
class MockFile extends Mock implements File {}