os_test.dart 3.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/os.dart';
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';
import 'package:platform/platform.dart';

12 13
import '../../src/common.dart';
import '../../src/context.dart';
14 15 16 17 18 19 20 21 22

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

void main() {
  ProcessManager mockProcessManager;

  setUp(() {
23
    mockProcessManager = MockProcessManager();
24 25 26 27 28 29
  });

  group('which on POSIX', () {

    testUsingContext('returns null when executable does not exist', () async {
      when(mockProcessManager.runSync(<String>['which', kExecutable]))
30 31
          .thenReturn(ProcessResult(0, 1, null, null));
      final OperatingSystemUtils utils = OperatingSystemUtils();
32 33 34
      expect(utils.which(kExecutable), isNull);
    }, overrides: <Type, Generator>{
      ProcessManager: () => mockProcessManager,
35
      Platform: () => FakePlatform(operatingSystem: 'linux'),
36 37 38 39
    });

    testUsingContext('returns exactly one result', () async {
      when(mockProcessManager.runSync(<String>['which', 'foo']))
40 41
          .thenReturn(ProcessResult(0, 0, kPath1, null));
      final OperatingSystemUtils utils = OperatingSystemUtils();
42 43 44
      expect(utils.which(kExecutable).path, kPath1);
    }, overrides: <Type, Generator>{
      ProcessManager: () => mockProcessManager,
45
      Platform: () => FakePlatform(operatingSystem: 'linux'),
46 47 48 49
    });

    testUsingContext('returns all results for whichAll', () async {
      when(mockProcessManager.runSync(<String>['which', '-a', kExecutable]))
50 51
          .thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null));
      final OperatingSystemUtils utils = OperatingSystemUtils();
52 53 54 55 56 57
      final List<File> result = utils.whichAll(kExecutable);
      expect(result, hasLength(2));
      expect(result[0].path, kPath1);
      expect(result[1].path, kPath2);
    }, overrides: <Type, Generator>{
      ProcessManager: () => mockProcessManager,
58
      Platform: () => FakePlatform(operatingSystem: 'linux'),
59 60 61 62 63 64 65
    });
  });

  group('which on Windows', () {

    testUsingContext('returns null when executable does not exist', () async {
      when(mockProcessManager.runSync(<String>['where', kExecutable]))
66 67
          .thenReturn(ProcessResult(0, 1, null, null));
      final OperatingSystemUtils utils = OperatingSystemUtils();
68 69 70
      expect(utils.which(kExecutable), isNull);
    }, overrides: <Type, Generator>{
      ProcessManager: () => mockProcessManager,
71
      Platform: () => FakePlatform(operatingSystem: 'windows'),
72 73 74 75
    });

    testUsingContext('returns exactly one result', () async {
      when(mockProcessManager.runSync(<String>['where', 'foo']))
76 77
          .thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null));
      final OperatingSystemUtils utils = OperatingSystemUtils();
78 79 80
      expect(utils.which(kExecutable).path, kPath1);
    }, overrides: <Type, Generator>{
      ProcessManager: () => mockProcessManager,
81
      Platform: () => FakePlatform(operatingSystem: 'windows'),
82 83 84 85
    });

    testUsingContext('returns all results for whichAll', () async {
      when(mockProcessManager.runSync(<String>['where', kExecutable]))
86 87
          .thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null));
      final OperatingSystemUtils utils = OperatingSystemUtils();
88 89 90 91 92 93
      final List<File> result = utils.whichAll(kExecutable);
      expect(result, hasLength(2));
      expect(result[0].path, kPath1);
      expect(result[1].path, kPath2);
    }, overrides: <Type, Generator>{
      ProcessManager: () => mockProcessManager,
94
      Platform: () => FakePlatform(operatingSystem: 'windows'),
95 96 97 98
    });
  });
}

99
class MockProcessManager extends Mock implements ProcessManager {}