2019-11-27 23:04:02 +00:00
|
|
|
// Copyright 2014 The Flutter Authors. All rights reserved.
|
2017-03-21 17:23:44 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2020-01-30 01:51:31 +00:00
|
|
|
import 'package:file/file.dart';
|
|
|
|
import 'package:file/memory.dart';
|
2020-02-15 00:10:47 +00:00
|
|
|
import 'package:flutter_tools/src/base/io.dart';
|
2017-03-21 17:23:44 +00:00
|
|
|
import 'package:flutter_tools/src/base/file_system.dart';
|
2020-01-30 01:51:31 +00:00
|
|
|
import 'package:flutter_tools/src/base/logger.dart';
|
2017-03-21 17:23:44 +00:00
|
|
|
import 'package:flutter_tools/src/base/os.dart';
|
|
|
|
import 'package:mockito/mockito.dart';
|
|
|
|
import 'package:process/process.dart';
|
|
|
|
import 'package:platform/platform.dart';
|
|
|
|
|
2019-07-13 18:51:44 +00:00
|
|
|
import '../../src/common.dart';
|
|
|
|
import '../../src/context.dart';
|
2017-03-21 17:23:44 +00:00
|
|
|
|
|
|
|
const String kExecutable = 'foo';
|
|
|
|
const String kPath1 = '/bar/bin/$kExecutable';
|
|
|
|
const String kPath2 = '/another/bin/$kExecutable';
|
|
|
|
|
2020-01-30 01:51:31 +00:00
|
|
|
class MockLogger extends Mock implements Logger {}
|
|
|
|
|
2017-03-21 17:23:44 +00:00
|
|
|
void main() {
|
2020-02-15 00:10:47 +00:00
|
|
|
MockProcessManager mockProcessManager;
|
|
|
|
|
|
|
|
setUp(() {
|
|
|
|
mockProcessManager = MockProcessManager();
|
|
|
|
});
|
2017-03-21 17:23:44 +00:00
|
|
|
|
2020-01-30 01:51:31 +00:00
|
|
|
OperatingSystemUtils createOSUtils(Platform platform) {
|
|
|
|
return OperatingSystemUtils(
|
|
|
|
fileSystem: MemoryFileSystem(),
|
|
|
|
logger: MockLogger(),
|
|
|
|
platform: platform,
|
2020-02-15 00:10:47 +00:00
|
|
|
processManager: mockProcessManager,
|
2020-01-30 01:51:31 +00:00
|
|
|
);
|
|
|
|
}
|
2017-03-21 17:23:44 +00:00
|
|
|
|
2020-01-30 01:51:31 +00:00
|
|
|
group('which on POSIX', () {
|
|
|
|
testWithoutContext('returns null when executable does not exist', () async {
|
2020-02-15 00:10:47 +00:00
|
|
|
when(mockProcessManager.runSync(<String>['which', kExecutable]))
|
|
|
|
.thenReturn(ProcessResult(0, 1, null, null));
|
|
|
|
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'linux'));
|
2017-03-21 17:23:44 +00:00
|
|
|
expect(utils.which(kExecutable), isNull);
|
|
|
|
});
|
|
|
|
|
2020-01-30 01:51:31 +00:00
|
|
|
testWithoutContext('returns exactly one result', () async {
|
2020-02-15 00:10:47 +00:00
|
|
|
when(mockProcessManager.runSync(<String>['which', 'foo']))
|
|
|
|
.thenReturn(ProcessResult(0, 0, kPath1, null));
|
|
|
|
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'linux'));
|
2017-03-21 17:23:44 +00:00
|
|
|
expect(utils.which(kExecutable).path, kPath1);
|
|
|
|
});
|
|
|
|
|
2020-01-30 01:51:31 +00:00
|
|
|
testWithoutContext('returns all results for whichAll', () async {
|
2020-02-15 00:10:47 +00:00
|
|
|
when(mockProcessManager.runSync(<String>['which', '-a', kExecutable]))
|
|
|
|
.thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null));
|
|
|
|
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'linux'));
|
2017-03-21 17:23:44 +00:00
|
|
|
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', () {
|
2020-01-30 01:51:31 +00:00
|
|
|
testWithoutContext('returns null when executable does not exist', () async {
|
2020-02-15 00:10:47 +00:00
|
|
|
when(mockProcessManager.runSync(<String>['where', kExecutable]))
|
|
|
|
.thenReturn(ProcessResult(0, 1, null, null));
|
|
|
|
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
|
2017-03-21 17:23:44 +00:00
|
|
|
expect(utils.which(kExecutable), isNull);
|
|
|
|
});
|
|
|
|
|
2020-01-30 01:51:31 +00:00
|
|
|
testWithoutContext('returns exactly one result', () async {
|
2020-02-15 00:10:47 +00:00
|
|
|
when(mockProcessManager.runSync(<String>['where', 'foo']))
|
|
|
|
.thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null));
|
|
|
|
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
|
2017-03-21 17:23:44 +00:00
|
|
|
expect(utils.which(kExecutable).path, kPath1);
|
|
|
|
});
|
|
|
|
|
2020-01-30 01:51:31 +00:00
|
|
|
testWithoutContext('returns all results for whichAll', () async {
|
2020-02-15 00:10:47 +00:00
|
|
|
when(mockProcessManager.runSync(<String>['where', kExecutable]))
|
|
|
|
.thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null));
|
|
|
|
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
|
2017-03-21 17:23:44 +00:00
|
|
|
final List<File> result = utils.whichAll(kExecutable);
|
|
|
|
expect(result, hasLength(2));
|
|
|
|
expect(result[0].path, kPath1);
|
|
|
|
expect(result[1].path, kPath2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-27 15:44:39 +00:00
|
|
|
class MockProcessManager extends Mock implements ProcessManager {}
|