1// Copyright 2017 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import 'package:flutter_tools/src/base/io.dart'; 6import 'package:flutter_tools/src/base/file_system.dart'; 7import 'package:flutter_tools/src/base/os.dart'; 8import 'package:mockito/mockito.dart'; 9import 'package:process/process.dart'; 10import 'package:platform/platform.dart'; 11 12import '../../src/common.dart'; 13import '../../src/context.dart'; 14 15const String kExecutable = 'foo'; 16const String kPath1 = '/bar/bin/$kExecutable'; 17const String kPath2 = '/another/bin/$kExecutable'; 18 19void main() { 20 ProcessManager mockProcessManager; 21 22 setUp(() { 23 mockProcessManager = MockProcessManager(); 24 }); 25 26 group('which on POSIX', () { 27 28 testUsingContext('returns null when executable does not exist', () async { 29 when(mockProcessManager.runSync(<String>['which', kExecutable])) 30 .thenReturn(ProcessResult(0, 1, null, null)); 31 final OperatingSystemUtils utils = OperatingSystemUtils(); 32 expect(utils.which(kExecutable), isNull); 33 }, overrides: <Type, Generator>{ 34 ProcessManager: () => mockProcessManager, 35 Platform: () => FakePlatform(operatingSystem: 'linux'), 36 }); 37 38 testUsingContext('returns exactly one result', () async { 39 when(mockProcessManager.runSync(<String>['which', 'foo'])) 40 .thenReturn(ProcessResult(0, 0, kPath1, null)); 41 final OperatingSystemUtils utils = OperatingSystemUtils(); 42 expect(utils.which(kExecutable).path, kPath1); 43 }, overrides: <Type, Generator>{ 44 ProcessManager: () => mockProcessManager, 45 Platform: () => FakePlatform(operatingSystem: 'linux'), 46 }); 47 48 testUsingContext('returns all results for whichAll', () async { 49 when(mockProcessManager.runSync(<String>['which', '-a', kExecutable])) 50 .thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null)); 51 final OperatingSystemUtils utils = OperatingSystemUtils(); 52 final List<File> result = utils.whichAll(kExecutable); 53 expect(result, hasLength(2)); 54 expect(result[0].path, kPath1); 55 expect(result[1].path, kPath2); 56 }, overrides: <Type, Generator>{ 57 ProcessManager: () => mockProcessManager, 58 Platform: () => FakePlatform(operatingSystem: 'linux'), 59 }); 60 }); 61 62 group('which on Windows', () { 63 64 testUsingContext('returns null when executable does not exist', () async { 65 when(mockProcessManager.runSync(<String>['where', kExecutable])) 66 .thenReturn(ProcessResult(0, 1, null, null)); 67 final OperatingSystemUtils utils = OperatingSystemUtils(); 68 expect(utils.which(kExecutable), isNull); 69 }, overrides: <Type, Generator>{ 70 ProcessManager: () => mockProcessManager, 71 Platform: () => FakePlatform(operatingSystem: 'windows'), 72 }); 73 74 testUsingContext('returns exactly one result', () async { 75 when(mockProcessManager.runSync(<String>['where', 'foo'])) 76 .thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null)); 77 final OperatingSystemUtils utils = OperatingSystemUtils(); 78 expect(utils.which(kExecutable).path, kPath1); 79 }, overrides: <Type, Generator>{ 80 ProcessManager: () => mockProcessManager, 81 Platform: () => FakePlatform(operatingSystem: 'windows'), 82 }); 83 84 testUsingContext('returns all results for whichAll', () async { 85 when(mockProcessManager.runSync(<String>['where', kExecutable])) 86 .thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null)); 87 final OperatingSystemUtils utils = OperatingSystemUtils(); 88 final List<File> result = utils.whichAll(kExecutable); 89 expect(result, hasLength(2)); 90 expect(result[0].path, kPath1); 91 expect(result[1].path, kPath2); 92 }, overrides: <Type, Generator>{ 93 ProcessManager: () => mockProcessManager, 94 Platform: () => FakePlatform(operatingSystem: 'windows'), 95 }); 96 }); 97} 98 99class MockProcessManager extends Mock implements ProcessManager {} 100